Aryanne / flux_swap

huggingface.co
Total runs: 89
24-hour runs: 0
7-day runs: -20
30-day runs: -1
Model's Last Updated: September 10 2024

Introduction of flux_swap

Model Details of flux_swap

Aryanne/flux_swap

This model is a merge of black-forest-labs/FLUX.1-dev and black-forest-labs/FLUX.1-schnell .

But different than others methods here the values in the tensors are not changed but substitute in a checkboard pattern with the values of FLUX.1-schnell, so ~50% of each is present here.(if my code is right)

from diffusers import FluxTransformer2DModel
from huggingface_hub import snapshot_download
from accelerate import init_empty_weights
from diffusers.models.model_loading_utils import load_model_dict_into_meta
import safetensors.torch
import glob
import torch
import gc




with init_empty_weights():
    config = FluxTransformer2DModel.load_config("black-forest-labs/FLUX.1-dev", subfolder="transformer")
    model = FluxTransformer2DModel.from_config(config)

dev_ckpt = snapshot_download(repo_id="black-forest-labs/FLUX.1-dev", allow_patterns="transformer/*")
schnell_ckpt = snapshot_download(repo_id="black-forest-labs/FLUX.1-schnell", allow_patterns="transformer/*")

dev_shards = sorted(glob.glob(f"{dev_ckpt}/transformer/*.safetensors"))
schnell_shards = sorted(glob.glob(f"{schnell_ckpt}/transformer/*.safetensors"))

def swapping_method(base, x, parameters):
    def swap_values(shape, n, base, x):
        if x.dim() == 2:
           rows, cols = shape
           rows_range = torch.arange(rows).view(-1, 1)
           cols_range = torch.arange(cols).view(1, -1)
           mask = ((rows_range + cols_range) % n == 0).to(base.device.type).bool()
           x = torch.where(mask, x, base)
        else:
           rows_range = torch.arange(shape[0])
           mask = ((rows_range) % n == 0).to(base.device.type).bool()
           x = torch.where(mask, x, base)
        return x

    def rand_mask(base, x, percent, seed=None):
        oldseed = torch.seed()
        if seed is not None:
            torch.manual_seed(seed)
        random = torch.rand(base.shape)
        mask = (random <= percent).to(base.device.type).bool()
        del random
        torch.manual_seed(oldseed)
        x = torch.where(mask, x, base) 
        return x
    
   
    if x.device.type == "cpu":
         x = x.to(torch.bfloat16)
         base = base.to(torch.bfloat16)

    diagonal_offset = None
    diagonal_offset = parameters.get('diagonal_offset')
    random_mask = parameters.get('random_mask')
    random_mask_seed = parameters.get('random_mask_seed')
    random_mask_seed = int(random_mask_seed) if random_mask_seed is not None else random_mask_seed

    assert (diagonal_offset is not None) and (diagonal_offset % 1 == 0) and (diagonal_offset >= 2), "The diagonal_offset must be an integer greater than or equal to 2."
        
    if random_mask != 0.0:
       assert (random_mask is not None) and (random_mask < 1.0) and (random_mask > 0.0) , "The random_mask parameter can't be empty, 0, 1, or None, it must be a number between 0 and 1."
       assert random_mask_seed is None or (isinstance(random_mask_seed, int) and random_mask_seed % 1 == 0), "The random_mask_seed parameter must be None or an integer, None is a random seed."
       x = rand_mask(base, x, random_mask, random_mask_seed)

    else:
       if parameters.get('invert_offset') == False:
           x = swap_values(x.shape, diagonal_offset, base, x)
       else:
           x = swap_values(x.shape, diagonal_offset, x, base)

    del base
    return x

parameters = {
    'diagonal_offset': 2,      
    'random_mask': False,
    'invert_offset': False,       
   # 'random_mask_seed': "899557"
}








merged_state_dict = {}
guidance_state_dict = {}

for i in range(len((dev_shards))):
    state_dict_dev_temp = safetensors.torch.load_file(dev_shards[i])
    state_dict_schnell_temp = safetensors.torch.load_file(schnell_shards[i])

    keys = list(state_dict_dev_temp.keys())
    for k in keys:
        if "guidance" not in k:
            merged_state_dict[k] = swapping_method(state_dict_dev_temp.pop(k),state_dict_schnell_temp.pop(k), parameters)
        else:
            guidance_state_dict[k] = state_dict_dev_temp.pop(k)

    if len(state_dict_dev_temp) > 0:
        raise ValueError(f"There should not be any residue but got: {list(state_dict_dev_temp.keys())}.")
    if len(state_dict_schnell_temp) > 0:
        raise ValueError(f"There should not be any residue but got: {list(state_dict_dev_temp.keys())}.")




merged_state_dict.update(guidance_state_dict)
load_model_dict_into_meta(model, merged_state_dict)

model.to(torch.bfloat16).save_pretrained("merged-flux")

Used a piece of this code from mergekit

Thanks SayakPaul for your code which helped me do this merge.

Runs of Aryanne flux_swap on huggingface.co

89
Total runs
0
24-hour runs
-20
3-day runs
-20
7-day runs
-1
30-day runs

More Information About flux_swap huggingface.co Model

flux_swap huggingface.co

flux_swap huggingface.co is an AI model on huggingface.co that provides flux_swap's model effect (), which can be used instantly with this Aryanne flux_swap model. huggingface.co supports a free trial of the flux_swap model, and also provides paid use of the flux_swap. Support call flux_swap model through api, including Node.js, Python, http.

flux_swap huggingface.co Url

https://huggingface.co/Aryanne/flux_swap

Aryanne flux_swap online free

flux_swap huggingface.co is an online trial and call api platform, which integrates flux_swap's modeling effects, including api services, and provides a free online trial of flux_swap, you can try flux_swap online for free by clicking the link below.

Aryanne flux_swap online free url in huggingface.co:

https://huggingface.co/Aryanne/flux_swap

flux_swap install

flux_swap is an open source model from GitHub that offers a free installation service, and any user can find flux_swap on GitHub to install. At the same time, huggingface.co provides the effect of flux_swap install, users can directly use flux_swap installed effect in huggingface.co for debugging and trial. It also supports api for free installation.

flux_swap install url in huggingface.co:

https://huggingface.co/Aryanne/flux_swap

Url of flux_swap

flux_swap huggingface.co Url

Provider of flux_swap huggingface.co

Aryanne
ORGANIZATIONS

Other API from Aryanne

huggingface.co

Total runs: 1.8K
Run Growth: 739
Growth Rate: 42.08%
Updated:April 28 2024
huggingface.co

Total runs: 302
Run Growth: 47
Growth Rate: 15.56%
Updated:March 04 2024
huggingface.co

Total runs: 295
Run Growth: 55
Growth Rate: 18.64%
Updated:March 04 2024
huggingface.co

Total runs: 179
Run Growth: 1
Growth Rate: 0.56%
Updated:March 24 2024
huggingface.co

Total runs: 147
Run Growth: 69
Growth Rate: 46.94%
Updated:January 29 2024
huggingface.co

Total runs: 111
Run Growth: -26
Growth Rate: -23.42%
Updated:February 05 2025
huggingface.co

Total runs: 48
Run Growth: -6
Growth Rate: -12.50%
Updated:May 14 2024
huggingface.co

Total runs: 42
Run Growth: 6
Growth Rate: 14.29%
Updated:September 23 2024
huggingface.co

Total runs: 30
Run Growth: 7
Growth Rate: 23.33%
Updated:January 05 2025
huggingface.co

Total runs: 28
Run Growth: -5
Growth Rate: -17.86%
Updated:December 16 2023
huggingface.co

Total runs: 11
Run Growth: 2
Growth Rate: 18.18%
Updated:December 26 2023
huggingface.co

Total runs: 0
Run Growth: 0
Growth Rate: 0.00%
Updated:February 09 2024