prithivMLmods
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -15,3 +15,85 @@ base_model:
|
|
15 |
- stabilityai/stable-diffusion-3.5-large-turbo
|
16 |
base_model_relation: merge
|
17 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
- stabilityai/stable-diffusion-3.5-large-turbo
|
16 |
base_model_relation: merge
|
17 |
---
|
18 |
+
# **SD3.5-Merged**
|
19 |
+
|
20 |
+
This repository contains the merged version of **Stable Diffusion 3.5**, combining the best features from both the [**Large**](https://huggingface.co/stabilityai/stable-diffusion-3.5-large) and [**Turbo**](https://huggingface.co/stabilityai/stable-diffusion-3.5-large-turbo) variants.
|
21 |
+
|
22 |
+
# **Merge & Upload**
|
23 |
+
|
24 |
+
To access the Stable Diffusion 3.5 models, one needs to fill the forms in the corresponding repositories, and then `huggingface_cli login` to let your system know
|
25 |
+
who you are and whether you have access to the models!
|
26 |
+
|
27 |
+
```python
|
28 |
+
from diffusers import SD3Transformer2DModel
|
29 |
+
from huggingface_hub import snapshot_download
|
30 |
+
from accelerate import init_empty_weights
|
31 |
+
from diffusers.models.model_loading_utils import load_model_dict_into_meta
|
32 |
+
import safetensors.torch
|
33 |
+
from huggingface_hub import upload_folder
|
34 |
+
import glob
|
35 |
+
import torch
|
36 |
+
|
37 |
+
large_model_id = "stabilityai/stable-diffusion-3.5-large"
|
38 |
+
turbo_model_id = "stabilityai/stable-diffusion-3.5-large-turbo"
|
39 |
+
|
40 |
+
with init_empty_weights():
|
41 |
+
config = SD3Transformer2DModel.load_config(large_model_id, subfolder="transformer")
|
42 |
+
model = SD3Transformer2DModel.from_config(config)
|
43 |
+
|
44 |
+
large_ckpt = snapshot_download(repo_id=large_model_id, allow_patterns="transformer/*")
|
45 |
+
turbo_ckpt = snapshot_download(repo_id=turbo_model_id, allow_patterns="transformer/*")
|
46 |
+
|
47 |
+
large_shards = sorted(glob.glob(f"{large_ckpt}/transformer/*.safetensors"))
|
48 |
+
turbo_shards = sorted(glob.glob(f"{turbo_ckpt}/transformer/*.safetensors"))
|
49 |
+
|
50 |
+
merged_state_dict = {}
|
51 |
+
guidance_state_dict = {}
|
52 |
+
|
53 |
+
for i in range(len((large_shards))):
|
54 |
+
state_dict_large_temp = safetensors.torch.load_file(large_shards[i])
|
55 |
+
state_dict_turbo_temp = safetensors.torch.load_file(turbo_shards[i])
|
56 |
+
|
57 |
+
keys = list(state_dict_large_temp.keys())
|
58 |
+
for k in keys:
|
59 |
+
if "guidance" not in k:
|
60 |
+
merged_state_dict[k] = (state_dict_large_temp.pop(k) + state_dict_turbo_temp.pop(k)) / 2
|
61 |
+
else:
|
62 |
+
guidance_state_dict[k] = state_dict_large_temp.pop(k)
|
63 |
+
|
64 |
+
if len(state_dict_large_temp) > 0:
|
65 |
+
raise ValueError(f"There should not be any residue but got: {list(state_dict_large_temp.keys())}.")
|
66 |
+
if len(state_dict_turbo_temp) > 0:
|
67 |
+
raise ValueError(f"There should not be any residue but got: {list(state_dict_turbo_temp.keys())}.")
|
68 |
+
|
69 |
+
merged_state_dict.update(guidance_state_dict)
|
70 |
+
load_model_dict_into_meta(model, merged_state_dict)
|
71 |
+
|
72 |
+
model.to(torch.bfloat16).save_pretrained("transformer")
|
73 |
+
|
74 |
+
upload_folder(
|
75 |
+
repo_id="prithivMLmods/Sd3.5-Merged",
|
76 |
+
folder_path="transformer",
|
77 |
+
path_in_repo="transformer",
|
78 |
+
)
|
79 |
+
```
|
80 |
+
|
81 |
+
# **Inference**
|
82 |
+
|
83 |
+
```python
|
84 |
+
from diffusers import StableDiffusion3Pipeline
|
85 |
+
import torch
|
86 |
+
|
87 |
+
pipeline = StableDiffusion3Pipeline.from_pretrained(
|
88 |
+
"prithivMLmods/Sd3.5-Merged", torch_dtype=torch.bfloat16
|
89 |
+
).to("cuda")
|
90 |
+
|
91 |
+
prompt = "a tiny astronaut hatching from an egg on the moon"
|
92 |
+
image = pipeline(
|
93 |
+
prompt=prompt,
|
94 |
+
guidance_scale=1.0,
|
95 |
+
num_inference_steps=6, # Run faster ⚡️
|
96 |
+
generator=torch.manual_seed(0),
|
97 |
+
).images[0]
|
98 |
+
image.save("sd-3.5-merged.png")
|
99 |
+
```
|