Skip to content

Commit 141a4b4

Browse files
stduhpfleejet
andauthored
feat: add flow shift parameter (for SD3 and Wan) (#780)
* Add flow shift parameter (for SD3 and Wan) * unify code style and fix some issues --------- Co-authored-by: leejet <[email protected]>
1 parent 21ce9fe commit 141a4b4

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

denoiser.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ struct DiscreteFlowDenoiser : public Denoiser {
382382

383383
float sigma_data = 1.0f;
384384

385-
DiscreteFlowDenoiser() {
385+
DiscreteFlowDenoiser(float shift = 3.0f)
386+
: shift(shift) {
386387
set_parameters();
387388
}
388389

examples/cli/main.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ struct SDParams {
115115
bool chroma_use_dit_mask = true;
116116
bool chroma_use_t5_mask = false;
117117
int chroma_t5_mask_pad = 1;
118+
float flow_shift = INFINITY;
118119

119120
SDParams() {
120121
sd_sample_params_init(&sample_params);
@@ -171,6 +172,7 @@ void print_params(SDParams params) {
171172
printf(" sample_params: %s\n", SAFE_STR(sample_params_str));
172173
printf(" high_noise_sample_params: %s\n", SAFE_STR(high_noise_sample_params_str));
173174
printf(" moe_boundary: %.3f\n", params.moe_boundary);
175+
printf(" flow_shift: %.2f\n", params.flow_shift);
174176
printf(" strength(img2img): %.2f\n", params.strength);
175177
printf(" rng: %s\n", sd_rng_type_name(params.rng_type));
176178
printf(" seed: %ld\n", params.seed);
@@ -278,8 +280,9 @@ void print_usage(int argc, const char* argv[]) {
278280
printf(" --chroma-t5-mask-pad PAD_SIZE t5 mask pad size of chroma\n");
279281
printf(" --video-frames video frames (default: 1)\n");
280282
printf(" --fps fps (default: 24)\n");
281-
printf(" --moe-boundary BOUNDARY Timestep boundary for Wan2.2 MoE model. (default: 0.875)\n");
282-
printf(" Only enabled if `--high-noise-steps` is set to -1\n");
283+
printf(" --moe-boundary BOUNDARY timestep boundary for Wan2.2 MoE model. (default: 0.875)\n");
284+
printf(" only enabled if `--high-noise-steps` is set to -1\n");
285+
printf(" --flow-shift SHIFT shift value for Flow models like SD3.x or WAN (default: auto)\n");
283286
printf(" -v, --verbose print extra info\n");
284287
}
285288

@@ -514,6 +517,7 @@ void parse_args(int argc, const char** argv, SDParams& params) {
514517
{"", "--style-ratio", "", &params.style_ratio},
515518
{"", "--control-strength", "", &params.control_strength},
516519
{"", "--moe-boundary", "", &params.moe_boundary},
520+
{"", "--flow-shift", "", &params.flow_shift},
517521
};
518522

519523
options.bool_options = {
@@ -1181,6 +1185,7 @@ int main(int argc, const char* argv[]) {
11811185
params.chroma_use_dit_mask,
11821186
params.chroma_use_t5_mask,
11831187
params.chroma_t5_mask_pad,
1188+
params.flow_shift,
11841189
};
11851190

11861191
sd_ctx_t* sd_ctx = new_sd_ctx(&sd_ctx_params);

stable-diffusion.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,11 @@ class StableDiffusionGGML {
681681

682682
if (sd_version_is_sd3(version)) {
683683
LOG_INFO("running in FLOW mode");
684-
denoiser = std::make_shared<DiscreteFlowDenoiser>();
684+
float shift = sd_ctx_params->flow_shift;
685+
if (shift == INFINITY) {
686+
shift = 3.0;
687+
}
688+
denoiser = std::make_shared<DiscreteFlowDenoiser>(shift);
685689
} else if (sd_version_is_flux(version)) {
686690
LOG_INFO("running in Flux FLOW mode");
687691
float shift = 1.0f; // TODO: validate
@@ -694,7 +698,11 @@ class StableDiffusionGGML {
694698
denoiser = std::make_shared<FluxFlowDenoiser>(shift);
695699
} else if (sd_version_is_wan(version)) {
696700
LOG_INFO("running in FLOW mode");
697-
denoiser = std::make_shared<DiscreteFlowDenoiser>();
701+
float shift = sd_ctx_params->flow_shift;
702+
if (shift == INFINITY) {
703+
shift = 5.0;
704+
}
705+
denoiser = std::make_shared<DiscreteFlowDenoiser>(shift);
698706
} else if (is_using_v_parameterization) {
699707
LOG_INFO("running in v-prediction mode");
700708
denoiser = std::make_shared<CompVisVDenoiser>();
@@ -1553,6 +1561,7 @@ void sd_ctx_params_init(sd_ctx_params_t* sd_ctx_params) {
15531561
sd_ctx_params->chroma_use_dit_mask = true;
15541562
sd_ctx_params->chroma_use_t5_mask = false;
15551563
sd_ctx_params->chroma_t5_mask_pad = 1;
1564+
sd_ctx_params->flow_shift = INFINITY;
15561565
}
15571566

15581567
char* sd_ctx_params_to_str(const sd_ctx_params_t* sd_ctx_params) {

stable-diffusion.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ typedef struct {
142142
bool chroma_use_dit_mask;
143143
bool chroma_use_t5_mask;
144144
int chroma_t5_mask_pad;
145+
float flow_shift;
145146
} sd_ctx_params_t;
146147

147148
typedef struct {

0 commit comments

Comments
 (0)