Skip to content

Commit 5e73e5a

Browse files
feat: support for sd3.5
1 parent 60e95c2 commit 5e73e5a

File tree

6 files changed

+78
-3
lines changed

6 files changed

+78
-3
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,33 @@ Note that:
259259
- Only the Flux-dev q8_0 will work with LoRAs.
260260
- You can download FLUX LoRA models from https://huggingface.co/XLabs-AI/flux-lora-collection/tree/main (you must use a comfy converted version!!!).
261261

262+
### SD3.5 Image Generation
263+
264+
Download the weights from the links below:
265+
266+
- Download sd3.5_large from https://huggingface.co/stabilityai/stable-diffusion-3.5-large/blob/main/sd3.5_large.safetensors
267+
- Download clip_g from https://huggingface.co/Comfy-Org/stable-diffusion-3.5-fp8/blob/main/text_encoders/clip_g.safetensors
268+
- Download clip_l from https://huggingface.co/Comfy-Org/stable-diffusion-3.5-fp8/blob/main/text_encoders/clip_l.safetensors
269+
- Download t5xxl from https://huggingface.co/Comfy-Org/stable-diffusion-3.5-fp8/blob/main/text_encoders/t5xxl_fp16.safetensors
270+
271+
```python
272+
from stable_diffusion_cpp import StableDiffusion
273+
274+
stable_diffusion = StableDiffusion(
275+
model_path="../models/sd3.5_large.safetensors",
276+
clip_l_path="../models/clip_l.safetensors",
277+
clip_g_path="../models/clip_g.safetensors",
278+
t5xxl_path="../models/t5xxl_fp16.safetensors",
279+
)
280+
output = stable_diffusion.txt_to_img(
281+
prompt="a lovely cat holding a sign says 'Stable diffusion 3.5 Large'",
282+
height=1024,
283+
width=1024,
284+
cfg_scale=4.5,
285+
sample_method="euler",
286+
)
287+
```
288+
262289
### Other High-level API Examples
263290

264291
Other examples for the high-level API (such as image to image, upscaling and model conversion) can be found in the [tests](tests) directory.
@@ -293,8 +320,6 @@ img = sd_cpp.upscale(
293320
image_bytes,
294321
upscale_factor,
295322
) # Upscale the image
296-
297-
sd_cpp.free_image(c_image)
298323
```
299324

300325
## Development

stable_diffusion_cpp/_internals.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def __init__(
2121
self,
2222
model_path: str,
2323
clip_l_path: str,
24+
clip_g_path: str,
2425
t5xxl_path: str,
2526
diffusion_model_path: str,
2627
vae_path: str,
@@ -43,6 +44,7 @@ def __init__(
4344
):
4445
self.model_path = model_path
4546
self.clip_l_path = clip_l_path
47+
self.clip_g_path = clip_g_path
4648
self.t5xxl_path = t5xxl_path
4749
self.diffusion_model_path = diffusion_model_path
4850
self.vae_path = vae_path
@@ -84,6 +86,7 @@ def __init__(
8486
self.model = sd_cpp.new_sd_ctx(
8587
self.model_path.encode("utf-8"),
8688
self.clip_l_path.encode("utf-8"),
89+
self.clip_g_path.encode("utf-8"),
8790
self.t5xxl_path.encode("utf-8"),
8891
self.diffusion_model_path.encode("utf-8"),
8992
self.vae_path.encode("utf-8"),

stable_diffusion_cpp/stable_diffusion.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(
2222
self,
2323
model_path: str = "",
2424
clip_l_path: str = "",
25+
clip_g_path: str = "",
2526
t5xxl_path: str = "",
2627
diffusion_model_path: str = "",
2728
vae_path: str = "",
@@ -88,6 +89,7 @@ def __init__(
8889
# Params
8990
self.model_path = model_path
9091
self.clip_l_path = clip_l_path
92+
self.clip_g_path = clip_g_path
9193
self.t5xxl_path = t5xxl_path
9294
self.diffusion_model_path = diffusion_model_path
9395
self.vae_path = vae_path
@@ -126,6 +128,7 @@ def __init__(
126128
_StableDiffusionModel(
127129
self.model_path,
128130
self.clip_l_path,
131+
self.clip_g_path,
129132
self.t5xxl_path,
130133
self.diffusion_model_path,
131134
self.vae_path,

stable_diffusion_cpp/stable_diffusion_cpp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ class GGMLType(IntEnum):
301301
[
302302
ctypes.c_char_p, # model_path
303303
ctypes.c_char_p, # clip_l_path
304+
ctypes.c_char_p, # clip_g_path
304305
ctypes.c_char_p, # t5xxl_path
305306
ctypes.c_char_p, # diffusion_model_path
306307
ctypes.c_char_p, # vae_path
@@ -325,6 +326,7 @@ class GGMLType(IntEnum):
325326
def new_sd_ctx(
326327
model_path: bytes,
327328
clip_l_path: bytes,
329+
clip_g_path: bytes,
328330
t5xxl_path: bytes,
329331
diffusion_model_path: bytes,
330332
vae_path: bytes,

tests/test_sd3.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import traceback
3+
from stable_diffusion_cpp import StableDiffusion
4+
5+
MODEL_PATH = "C:\\stable-diffusion\\sd3.5\\sd3.5_large-q4_k_5_0.gguf"
6+
CLIP_L_PATH = "C:\\stable-diffusion\\sd3.5\\clip_l.safetensors"
7+
CLIP_G_PATH = "C:\\stable-diffusion\\sd3.5\\clip_g.safetensors"
8+
T5XXL_PATH = "C:\\stable-diffusion\\sd3.5\\t5xxl_fp16.safetensors"
9+
10+
stable_diffusion = StableDiffusion(
11+
model_path=MODEL_PATH,
12+
clip_l_path=CLIP_L_PATH,
13+
clip_g_path=CLIP_G_PATH,
14+
t5xxl_path=T5XXL_PATH,
15+
)
16+
17+
18+
def callback(step: int, steps: int, time: float):
19+
print("Completed step: {} of {}".format(step, steps))
20+
21+
22+
try:
23+
# Generate images
24+
images = stable_diffusion.txt_to_img(
25+
prompt="a lovely cat holding a sign says 'Stable diffusion 3.5 Large'",
26+
height=832,
27+
width=832,
28+
cfg_scale=4.5,
29+
sample_method="euler",
30+
)
31+
32+
OUTPUT_DIR = "tests/outputs"
33+
if not os.path.exists(OUTPUT_DIR):
34+
os.makedirs(OUTPUT_DIR)
35+
36+
# Save images
37+
for i, image in enumerate(images):
38+
image.save(f"{OUTPUT_DIR}/sd3_{i}.png")
39+
40+
except Exception as e:
41+
traceback.print_exc()
42+
print("Test - sd3 failed: ", e)

0 commit comments

Comments
 (0)