prithivMLmods commited on
Commit
602a6a4
·
verified ·
1 Parent(s): d834c97

Upload 2 files

Browse files
Files changed (2) hide show
  1. Flux-Merge/flux-merge.py +42 -0
  2. SD3_5-Merge/sd3_5.py +51 -0
Flux-Merge/flux-merge.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import FluxTransformer2DModel
2
+ from huggingface_hub import snapshot_download
3
+ from accelerate import init_empty_weights
4
+ from diffusers.models.model_loading_utils import load_model_dict_into_meta
5
+ import safetensors.torch
6
+ import glob
7
+ import torch
8
+
9
+
10
+ with init_empty_weights():
11
+ config = FluxTransformer2DModel.load_config("black-forest-labs/FLUX.1-dev", subfolder="transformer")
12
+ model = FluxTransformer2DModel.from_config(config)
13
+
14
+ dev_ckpt = snapshot_download(repo_id="black-forest-labs/FLUX.1-dev", allow_patterns="transformer/*")
15
+ schnell_ckpt = snapshot_download(repo_id="black-forest-labs/FLUX.1-schnell", allow_patterns="transformer/*")
16
+
17
+ dev_shards = sorted(glob.glob(f"{dev_ckpt}/transformer/*.safetensors"))
18
+ schnell_shards = sorted(glob.glob(f"{schnell_ckpt}/transformer/*.safetensors"))
19
+
20
+ merged_state_dict = {}
21
+ guidance_state_dict = {}
22
+
23
+ for i in range(len((dev_shards))):
24
+ state_dict_dev_temp = safetensors.torch.load_file(dev_shards[i])
25
+ state_dict_schnell_temp = safetensors.torch.load_file(schnell_shards[i])
26
+
27
+ keys = list(state_dict_dev_temp.keys())
28
+ for k in keys:
29
+ if "guidance" not in k:
30
+ merged_state_dict[k] = (state_dict_dev_temp.pop(k) + state_dict_schnell_temp.pop(k)) / 2
31
+ else:
32
+ guidance_state_dict[k] = state_dict_dev_temp.pop(k)
33
+
34
+ if len(state_dict_dev_temp) > 0:
35
+ raise ValueError(f"There should not be any residue but got: {list(state_dict_dev_temp.keys())}.")
36
+ if len(state_dict_schnell_temp) > 0:
37
+ raise ValueError(f"There should not be any residue but got: {list(state_dict_dev_temp.keys())}.")
38
+
39
+ merged_state_dict.update(guidance_state_dict)
40
+ load_model_dict_into_meta(model, merged_state_dict)
41
+
42
+ model.to(torch.bfloat16).save_pretrained("merged-flux")
SD3_5-Merge/sd3_5.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from diffusers import SD3Transformer2DModel
2
+ from huggingface_hub import snapshot_download
3
+ from accelerate import init_empty_weights
4
+ from diffusers.models.model_loading_utils import load_model_dict_into_meta
5
+ import safetensors.torch
6
+ from huggingface_hub import upload_folder
7
+ import glob
8
+ import torch
9
+
10
+ large_model_id = "stabilityai/stable-diffusion-3.5-large"
11
+ turbo_model_id = "stabilityai/stable-diffusion-3.5-large-turbo"
12
+
13
+ with init_empty_weights():
14
+ config = SD3Transformer2DModel.load_config(large_model_id, subfolder="transformer")
15
+ model = SD3Transformer2DModel.from_config(config)
16
+
17
+ large_ckpt = snapshot_download(repo_id=large_model_id, allow_patterns="transformer/*")
18
+ turbo_ckpt = snapshot_download(repo_id=turbo_model_id, allow_patterns="transformer/*")
19
+
20
+ large_shards = sorted(glob.glob(f"{large_ckpt}/transformer/*.safetensors"))
21
+ turbo_shards = sorted(glob.glob(f"{turbo_ckpt}/transformer/*.safetensors"))
22
+
23
+ merged_state_dict = {}
24
+ guidance_state_dict = {}
25
+
26
+ for i in range(len((large_shards))):
27
+ state_dict_large_temp = safetensors.torch.load_file(large_shards[i])
28
+ state_dict_turbo_temp = safetensors.torch.load_file(turbo_shards[i])
29
+
30
+ keys = list(state_dict_large_temp.keys())
31
+ for k in keys:
32
+ if "guidance" not in k:
33
+ merged_state_dict[k] = (state_dict_large_temp.pop(k) + state_dict_turbo_temp.pop(k)) / 2
34
+ else:
35
+ guidance_state_dict[k] = state_dict_large_temp.pop(k)
36
+
37
+ if len(state_dict_large_temp) > 0:
38
+ raise ValueError(f"There should not be any residue but got: {list(state_dict_large_temp.keys())}.")
39
+ if len(state_dict_turbo_temp) > 0:
40
+ raise ValueError(f"There should not be any residue but got: {list(state_dict_turbo_temp.keys())}.")
41
+
42
+ merged_state_dict.update(guidance_state_dict)
43
+ load_model_dict_into_meta(model, merged_state_dict)
44
+
45
+ model.to(torch.bfloat16).save_pretrained("transformer")
46
+
47
+ upload_folder(
48
+ repo_id="prithivMLmods/sd-3.5-merged",
49
+ folder_path="transformer",
50
+ path_in_repo="transformer",
51
+ )