Skip to content

Commit 000653f

Browse files
feat: update submodule
1 parent e48362c commit 000653f

4 files changed

Lines changed: 78 additions & 34 deletions

File tree

‎stable_diffusion_cpp/_internals.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def __init__(
5858
chroma_use_t5_mask: bool,
5959
chroma_t5_mask_pad: int,
6060
qwen_image_zero_cond_t: bool,
61-
flow_shift: int,
6261
verbose: bool,
6362
):
6463
self._exit_stack = ExitStack()
@@ -105,7 +104,6 @@ def __init__(
105104
chroma_use_t5_mask=chroma_use_t5_mask,
106105
chroma_t5_mask_pad=chroma_t5_mask_pad,
107106
qwen_image_zero_cond_t=qwen_image_zero_cond_t,
108-
flow_shift=flow_shift,
109107
)
110108

111109
# Load the free_sd_ctx function

‎stable_diffusion_cpp/stable_diffusion.py‎

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ def __init__(
7373
chroma_use_t5_mask: bool = False,
7474
chroma_t5_mask_pad: int = 1,
7575
qwen_image_zero_cond_t: bool = False,
76-
flow_shift: float = float("inf"),
7776
image_resize_method: str = "crop",
7877
verbose: bool = True,
7978
):
@@ -132,7 +131,6 @@ def __init__(
132131
chroma_use_t5_mask: Use T5 mask for Chroma.
133132
chroma_t5_mask_pad: T5 mask padding size of Chroma.
134133
qwen_image_zero_cond_t: Enable zero_cond_t for Qwen image.
135-
flow_shift: Shift value for Flow models like SD3.x or WAN (default: auto).
136134
image_resize_method: Method to resize images for init, mask, control and reference images ("crop" or "resize").
137135
verbose: Print verbose output.
138136
@@ -187,7 +185,6 @@ def __init__(
187185
self.chroma_use_t5_mask = chroma_use_t5_mask
188186
self.chroma_t5_mask_pad = chroma_t5_mask_pad
189187
self.qwen_image_zero_cond_t = qwen_image_zero_cond_t
190-
self.flow_shift = flow_shift
191188
self.image_resize_method = image_resize_method
192189
self._stack = contextlib.ExitStack()
193190

@@ -284,7 +281,6 @@ def __init__(
284281
chroma_use_t5_mask=self.chroma_use_t5_mask,
285282
chroma_t5_mask_pad=self.chroma_t5_mask_pad,
286283
qwen_image_zero_cond_t=self.qwen_image_zero_cond_t,
287-
flow_shift=self.flow_shift,
288284
verbose=self.verbose,
289285
)
290286
)
@@ -346,6 +342,7 @@ def generate_image(
346342
eta: float = 0.0,
347343
timestep_shift: int = 0,
348344
sigmas: Optional[str] = None,
345+
flow_shift: float = float("inf"),
349346
# slg_params
350347
skip_layers: List[int] = [7, 8, 9],
351348
skip_layer_start: float = 0.01,
@@ -379,6 +376,13 @@ def generate_image(
379376
cache_max_continuous_cached_steps: int = -1,
380377
cache_taylorseer_n_derivatives: int = 1,
381378
cache_taylorseer_skip_interval: int = 1,
379+
cache_spectrum_w: float = 0.40,
380+
cache_spectrum_m: int = 3,
381+
cache_spectrum_lam: float = 1.0,
382+
cache_spectrum_window_size: int = 2,
383+
cache_spectrum_flex_window: float = 0.5,
384+
cache_spectrum_warmup_steps: int = 4,
385+
cache_spectrum_stop_percent: float = 0.9,
382386
scm_mask: str = "",
383387
scm_policy: Literal["dynamic", "static"] = "dynamic",
384388
# ---
@@ -412,6 +416,7 @@ def generate_image(
412416
eta: Eta in DDIM, only for DDIM and TCD.
413417
timestep_shift: Shift timestep for NitroFusion models, default: 0, recommended N for NitroSD-Realism around 250 and 500 for NitroSD-Vibrant.
414418
sigmas: Custom sigma values for the sampler, comma-separated (e.g. "14.61,7.8,3.5,0.0").
419+
flow_shift: Shift value for Flow models like SD3.x or WAN (default: auto).
415420
skip_layers: Layers to skip for SLG steps (SLG will be enabled at step int([STEPS]x[START]) and disabled at int([STEPS]x[END])).
416421
skip_layer_start: SLG enabling point.
417422
skip_layer_end: SLG disabling point.
@@ -552,17 +557,17 @@ def sd_preview_callback(
552557
rel_size_x, rel_size_y = self._parse_tile_size(vae_relative_tile_size, as_float=True)
553558

554559
# -------------------------------------------
555-
# Scheduler/Sample Method
560+
# Sample Method/Scheduler
556561
# -------------------------------------------
557562

558-
scheduler = self._validate_and_set_input(scheduler, SCHEDULER_MAP, "scheduler", allow_none=True)
559-
if scheduler is None:
560-
scheduler = sd_cpp.sd_get_default_scheduler(self.model)
561-
562563
sample_method = self._validate_and_set_input(sample_method, SAMPLE_METHOD_MAP, "sample_method", allow_none=True)
563564
if sample_method is None:
564565
sample_method = sd_cpp.sd_get_default_sample_method(self.model)
565566

567+
scheduler = self._validate_and_set_input(scheduler, SCHEDULER_MAP, "scheduler", allow_none=True)
568+
if scheduler is None:
569+
scheduler = sd_cpp.sd_get_default_scheduler(self.model, sample_method)
570+
566571
# -------------------------------------------
567572
# Sigmas
568573
# -------------------------------------------
@@ -590,6 +595,7 @@ def sd_preview_callback(
590595
# -------------------------------------------
591596

592597
_cache_params = sd_cpp.sd_cache_params_t(
598+
# General cache params
593599
mode=cache_mode,
594600
reuse_threshold=cache_reuse_threshold,
595601
start_percent=cache_start_percent,
@@ -602,8 +608,18 @@ def sd_preview_callback(
602608
residual_diff_threshold=cache_residual_diff_threshold,
603609
max_warmup_steps=cache_max_warmup_steps,
604610
max_continuous_cached_steps=cache_max_continuous_cached_steps,
611+
# Taylorseer cache params
605612
taylorseer_n_derivatives=cache_taylorseer_n_derivatives,
606613
taylorseer_skip_interval=cache_taylorseer_skip_interval,
614+
# Spectrum cache params
615+
spectrum_w=cache_spectrum_w,
616+
spectrum_m=cache_spectrum_m,
617+
spectrum_lam=cache_spectrum_lam,
618+
spectrum_window_size=cache_spectrum_window_size,
619+
spectrum_flex_window=cache_spectrum_flex_window,
620+
spectrum_warmup_steps=cache_spectrum_warmup_steps,
621+
spectrum_stop_percent=cache_spectrum_stop_percent,
622+
# General SCM params
607623
scm_mask=scm_mask.encode("utf-8"),
608624
scm_policy_dynamic=scm_policy,
609625
)
@@ -646,6 +662,7 @@ def sd_preview_callback(
646662
shifted_timestep=timestep_shift,
647663
custom_sigmas=_custom_sigmas,
648664
custom_sigmas_count=_custom_sigmas_count,
665+
flow_shift=flow_shift,
649666
)
650667

651668
_params = sd_cpp.sd_img_gen_params_t(
@@ -738,6 +755,7 @@ def generate_video(
738755
eta: float = 0.0,
739756
timestep_shift: int = 0,
740757
sigmas: Optional[str] = None,
758+
flow_shift: float = float("inf"),
741759
# slg_params
742760
skip_layers: List[int] = [7, 8, 9],
743761
skip_layer_start: float = 0.01,
@@ -813,6 +831,7 @@ def generate_video(
813831
eta: Eta in DDIM, only for DDIM and TCD.
814832
timestep_shift: Shift timestep for NitroFusion models, default: 0, recommended N for NitroSD-Realism around 250 and 500 for NitroSD-Vibrant.
815833
sigmas: Custom sigma values for the sampler, comma-separated (e.g. "14.61,7.8,3.5,0.0").
834+
flow_shift: Shift value for Flow models like SD3.x or WAN (default: auto).
816835
skip_layers: Layers to skip for SLG steps (SLG will be enabled at step int([STEPS]x[START]) and disabled at int([STEPS]x[END])).
817836
skip_layer_start: SLG enabling point.
818837
skip_layer_end: SLG disabling point.
@@ -965,13 +984,9 @@ def sd_preview_callback(
965984
rel_size_x, rel_size_y = self._parse_tile_size(vae_relative_tile_size, as_float=True)
966985

967986
# -------------------------------------------
968-
# Scheduler/Sample Method
987+
# Sample Method/Scheduler
969988
# -------------------------------------------
970989

971-
scheduler = self._validate_and_set_input(scheduler, SCHEDULER_MAP, "scheduler", allow_none=True)
972-
if scheduler is None:
973-
scheduler = sd_cpp.sd_get_default_scheduler(self.model)
974-
975990
# "sample_method_count" is not valid here (it will crash)
976991
sample_method = self._validate_and_set_input(
977992
sample_method,
@@ -982,19 +997,23 @@ def sd_preview_callback(
982997
if sample_method is None:
983998
sample_method = sd_cpp.sd_get_default_sample_method(self.model)
984999

985-
# High Noise
986-
high_noise_scheduler = self._validate_and_set_input(
987-
high_noise_scheduler, SCHEDULER_MAP, "high_noise_scheduler", allow_none=True
988-
)
989-
if high_noise_scheduler is None:
990-
high_noise_scheduler = scheduler
1000+
scheduler = self._validate_and_set_input(scheduler, SCHEDULER_MAP, "scheduler", allow_none=True)
1001+
if scheduler is None:
1002+
scheduler = sd_cpp.sd_get_default_scheduler(self.model, sample_method)
9911003

1004+
# High Noise
9921005
high_noise_sample_method = self._validate_and_set_input(
9931006
high_noise_sample_method, SAMPLE_METHOD_MAP, "high_noise_sample_method", allow_none=True
9941007
)
9951008
if high_noise_sample_method is None:
9961009
high_noise_sample_method = sample_method
9971010

1011+
high_noise_scheduler = self._validate_and_set_input(
1012+
high_noise_scheduler, SCHEDULER_MAP, "high_noise_scheduler", allow_none=True
1013+
)
1014+
if high_noise_scheduler is None:
1015+
high_noise_scheduler = scheduler
1016+
9981017
# -------------------------------------------
9991018
# Sigmas
10001019
# -------------------------------------------
@@ -1043,6 +1062,7 @@ def sd_preview_callback(
10431062
shifted_timestep=timestep_shift,
10441063
custom_sigmas=_custom_sigmas,
10451064
custom_sigmas_count=_custom_sigmas_count,
1065+
flow_shift=flow_shift,
10461066
)
10471067

10481068
# -------------------------------------------
@@ -1099,6 +1119,7 @@ def sd_preview_callback(
10991119
shifted_timestep=timestep_shift,
11001120
custom_sigmas=_custom_sigmas,
11011121
custom_sigmas_count=_custom_sigmas_count,
1122+
flow_shift=flow_shift,
11021123
)
11031124

11041125
_params = sd_cpp.sd_vid_gen_params_t(
@@ -1978,4 +1999,5 @@ def __del__(self) -> None:
19781999
"dbcache": SDCacheMode.SD_CACHE_DBCACHE,
19792000
"taylorseer": SDCacheMode.SD_CACHE_TAYLORSEER,
19802001
"cachedit": SDCacheMode.SD_CACHE_CACHE_DIT,
2002+
"spectrum": SDCacheMode.SD_CACHE_SPECTRUM,
19812003
}

‎stable_diffusion_cpp/stable_diffusion_cpp.py‎

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import os
4+
import re
45
import sys
56
import ctypes
67
import pathlib
@@ -40,6 +41,15 @@ def _load_shared_library(lib_base_name: str):
4041
_base_path / f"lib{lib_base_name}.dylib",
4142
]
4243
elif sys.platform == "win32":
44+
# Load the HIP runtime if present in the lib directory
45+
def extract_version(p: pathlib.Path):
46+
m = re.search(r"amdhip64[_\-]?(\d+)", p.name)
47+
return int(m.group(1)) if m else 0
48+
49+
hip_dlls = sorted(_base_path.glob("amdhip64*.dll"), key=extract_version, reverse=True)
50+
if hip_dlls:
51+
ctypes.CDLL(str(hip_dlls[0]), winmode=0x8)
52+
4353
_lib_paths += [
4454
_base_path / f"{lib_base_name}.dll",
4555
_base_path / f"lib{lib_base_name}.dll",
@@ -144,9 +154,9 @@ def byref(obj: CtypesCData, offset: Optional[int] = None) -> CtypesRef[CtypesCDa
144154
ggml_abort_callback = ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p)
145155

146156

147-
################################################
148-
# stable-diffusion.h bindings
149-
################################################
157+
# ===========================================
158+
# include/stable-diffusion.h bindings
159+
# ===========================================
150160

151161

152162
# enum rng_type_t {
@@ -369,6 +379,7 @@ class LoraApplyMode(IntEnum):
369379
# SD_CACHE_DBCACHE,
370380
# SD_CACHE_TAYLORSEER,
371381
# SD_CACHE_CACHE_DIT,
382+
# SD_CACHE_SPECTRUM,
372383
# };
373384
class SDCacheMode(IntEnum):
374385
SD_CACHE_DISABLED = 0
@@ -377,6 +388,7 @@ class SDCacheMode(IntEnum):
377388
SD_CACHE_DBCACHE = 3
378389
SD_CACHE_TAYLORSEER = 4
379390
SD_CACHE_CACHE_DIT = 5
391+
SD_CACHE_SPECTRUM = 6
380392

381393

382394
# ===========================================
@@ -402,7 +414,7 @@ class sd_embedding_t(ctypes.Structure):
402414
# -------------------------------------------
403415

404416

405-
# typedef struct { const char* model_path; const char* clip_l_path; const char* clip_g_path; const char* clip_vision_path; const char* t5xxl_path; const char* llm_path; const char* llm_vision_path; const char* diffusion_model_path; const char* high_noise_diffusion_model_path; const char* vae_path; const char* taesd_path; const char* control_net_path; const sd_embedding_t* embeddings; uint32_t embedding_count; const char* photo_maker_path; const char* tensor_type_rules; bool vae_decode_only; bool free_params_immediately; int n_threads; enum sd_type_t wtype; enum rng_type_t rng_type; enum rng_type_t sampler_rng_type; enum prediction_t prediction; enum lora_apply_mode_t lora_apply_mode; bool offload_params_to_cpu; bool enable_mmap; bool keep_clip_on_cpu; bool keep_control_net_on_cpu; bool keep_vae_on_cpu; bool flash_attn; bool diffusion_flash_attn; bool tae_preview_only; bool diffusion_conv_direct; bool vae_conv_direct; bool circular_x; bool circular_y; bool force_sdxl_vae_conv_scale; bool chroma_use_dit_mask; bool chroma_use_t5_mask; int chroma_t5_mask_pad; bool qwen_image_zero_cond_t; float flow_shift; } sd_ctx_params_t;
417+
# typedef struct { const char* model_path; const char* clip_l_path; const char* clip_g_path; const char* clip_vision_path; const char* t5xxl_path; const char* llm_path; const char* llm_vision_path; const char* diffusion_model_path; const char* high_noise_diffusion_model_path; const char* vae_path; const char* taesd_path; const char* control_net_path; const sd_embedding_t* embeddings; uint32_t embedding_count; const char* photo_maker_path; const char* tensor_type_rules; bool vae_decode_only; bool free_params_immediately; int n_threads; enum sd_type_t wtype; enum rng_type_t rng_type; enum rng_type_t sampler_rng_type; enum prediction_t prediction; enum lora_apply_mode_t lora_apply_mode; bool offload_params_to_cpu; bool enable_mmap; bool keep_clip_on_cpu; bool keep_control_net_on_cpu; bool keep_vae_on_cpu; bool flash_attn; bool diffusion_flash_attn; bool tae_preview_only; bool diffusion_conv_direct; bool vae_conv_direct; bool circular_x; bool circular_y; bool force_sdxl_vae_conv_scale; bool chroma_use_dit_mask; bool chroma_use_t5_mask; int chroma_t5_mask_pad; bool qwen_image_zero_cond_t; } sd_ctx_params_t;
406418
class sd_ctx_params_t(ctypes.Structure):
407419
_fields_ = [
408420
("model_path", ctypes.c_char_p),
@@ -446,7 +458,6 @@ class sd_ctx_params_t(ctypes.Structure):
446458
("chroma_use_t5_mask", ctypes.c_bool),
447459
("chroma_t5_mask_pad", ctypes.c_int),
448460
("qwen_image_zero_cond_t", ctypes.c_bool),
449-
("flow_shift", ctypes.c_float),
450461
]
451462

452463

@@ -586,7 +597,7 @@ class sd_guidance_params_t(ctypes.Structure):
586597
# -------------------------------------------
587598

588599

589-
# typedef struct { sd_guidance_params_t guidance; enum scheduler_t scheduler; enum sample_method_t sample_method; int sample_steps; float eta; int shifted_timestep; float* custom_sigmas; int custom_sigmas_count; } sd_sample_params_t;
600+
# typedef struct { sd_guidance_params_t guidance; enum scheduler_t scheduler; enum sample_method_t sample_method; int sample_steps; float eta; int shifted_timestep; float* custom_sigmas; int custom_sigmas_count; float flow_shift; } sd_sample_params_t;
590601
class sd_sample_params_t(ctypes.Structure):
591602
_fields_ = [
592603
("guidance", sd_guidance_params_t),
@@ -597,6 +608,7 @@ class sd_sample_params_t(ctypes.Structure):
597608
("shifted_timestep", ctypes.c_int),
598609
("custom_sigmas", ctypes.POINTER(ctypes.c_float)),
599610
("custom_sigmas_count", ctypes.c_int),
611+
("flow_shift", ctypes.c_float),
600612
]
601613

602614

@@ -605,7 +617,7 @@ class sd_sample_params_t(ctypes.Structure):
605617
# -------------------------------------------
606618

607619

608-
# typedef struct { enum sd_cache_mode_t mode; float reuse_threshold; float start_percent; float end_percent; float error_decay_rate; bool use_relative_threshold; bool reset_error_on_compute; int Fn_compute_blocks; int Bn_compute_blocks; float residual_diff_threshold; int max_warmup_steps; int max_cached_steps; int max_continuous_cached_steps; int taylorseer_n_derivatives; int taylorseer_skip_interval; const char* scm_mask; bool scm_policy_dynamic; } sd_cache_params_t;
620+
# typedef struct { enum sd_cache_mode_t mode; float reuse_threshold; float start_percent; float end_percent; float error_decay_rate; bool use_relative_threshold; bool reset_error_on_compute; int Fn_compute_blocks; int Bn_compute_blocks; float residual_diff_threshold; int max_warmup_steps; int max_cached_steps; int max_continuous_cached_steps; int taylorseer_n_derivatives; int taylorseer_skip_interval; const char* scm_mask; bool scm_policy_dynamic; float spectrum_w; int spectrum_m; float spectrum_lam; int spectrum_window_size; float spectrum_flex_window; int spectrum_warmup_steps; float spectrum_stop_percent; } sd_cache_params_t;
609621
class sd_cache_params_t(ctypes.Structure):
610622
_fields_ = [
611623
("mode", ctypes.c_int), # SDCacheMode
@@ -625,6 +637,13 @@ class sd_cache_params_t(ctypes.Structure):
625637
("taylorseer_skip_interval", ctypes.c_int),
626638
("scm_mask", ctypes.c_char_p),
627639
("scm_policy_dynamic", ctypes.c_bool),
640+
("spectrum_w", ctypes.c_float),
641+
("spectrum_m", ctypes.c_int),
642+
("spectrum_lam", ctypes.c_float),
643+
("spectrum_window_size", ctypes.c_int),
644+
("spectrum_flex_window", ctypes.c_float),
645+
("spectrum_warmup_steps", ctypes.c_int),
646+
("spectrum_stop_percent", ctypes.c_float),
628647
]
629648

630649

@@ -777,16 +796,18 @@ def sd_get_default_sample_method(
777796
# -------------------------------------------
778797

779798

780-
# SD_API enum scheduler_t sd_get_default_scheduler(const sd_ctx_t* sd_ctx);
799+
# SD_API enum scheduler_t sd_get_default_scheduler(const sd_ctx_t* sd_ctx, enum sample_method_t sample_method);
781800
@ctypes_function(
782801
"sd_get_default_scheduler",
783802
[
784803
sd_ctx_t_p_ctypes, # sd_ctx
804+
ctypes.c_int, # sample_method
785805
],
786806
ctypes.c_int, # Scheduler
787807
)
788808
def sd_get_default_scheduler(
789809
sd_ctx: sd_ctx_t_p,
810+
sample_method: SampleMethod,
790811
/,
791812
) -> Optional[Scheduler]: ...
792813

@@ -1032,7 +1053,7 @@ def sd_version() -> bytes:
10321053
@ctypes_function(
10331054
"sd_set_progress_callback",
10341055
[
1035-
ctypes.c_void_p, # sd_progress_cb_t
1056+
ctypes.c_void_p, # cb
10361057
ctypes.c_void_p, # data
10371058
],
10381059
None,
@@ -1060,7 +1081,7 @@ def sd_set_progress_callback(
10601081
@ctypes_function(
10611082
"sd_set_preview_callback",
10621083
[
1063-
ctypes.c_void_p, # sd_preview_cb_t
1084+
ctypes.c_void_p, # cb
10641085
ctypes.c_int, # mode
10651086
ctypes.c_int, # interval
10661087
ctypes.c_bool, # denoised
@@ -1092,7 +1113,10 @@ def sd_set_preview_callback(
10921113
# SD_API void sd_set_log_callback(sd_log_cb_t sd_log_cb, void* data);
10931114
@ctypes_function(
10941115
"sd_set_log_callback",
1095-
[ctypes.c_void_p, ctypes.c_void_p],
1116+
[
1117+
ctypes.c_void_p, # sd_log_cb
1118+
ctypes.c_void_p, # data
1119+
],
10961120
None,
10971121
)
10981122
def sd_set_log_callback(

0 commit comments

Comments
 (0)