From 572baeeb7870d8bd0b273c346bb8491850f3de62 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Fri, 30 Oct 2026 07:16:03 -0700 Subject: [PATCH 1/3] MOSS-Image: extra sample args to window or disable the block-22 negative-branch skip moss_uncond_skip=false turns the built-in uncond skip off; moss_uncond_skip_start / moss_uncond_skip_end bound it in sigma-percent of the shifted flow schedule (ComfyUI percent_to_sigma, inclusive). Without these keys every step skips block 22 as before. Logs the number of skipped negative passes per sampling run. --- examples/common/common.cpp | 2 +- src/pipeline/diffusion_engine.cpp | 35 ++++++++++++++++++++++++++++++- src/runtime/guidance.cpp | 28 +++++++++++++++++++++++++ src/runtime/guidance.h | 11 ++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/examples/common/common.cpp b/examples/common/common.cpp index 9e7851fef1..b9fb780fdb 100644 --- a/examples/common/common.cpp +++ b/examples/common/common.cpp @@ -1180,7 +1180,7 @@ ArgOptions SDGenerationParams::get_options() { &hires_upscaler}, {"", "--extra-sample-args", - "extra sampler/scheduler/guidance args, key=value list. CFG supports guidance_schedule; APG supports apg_eta, apg_momentum, apg_norm_threshold, apg_norm_threshold_smoothing; SLG supports slg_uncond; lcm supports noise_clip_std, noise_scale_start, noise_scale_end; flux supports base_shift, max_shift; ltx2 supports max_shift, base_shift, stretch, terminal; euler_ge supports gamma; beta scheduler supports alpha, beta; logit_normal supports mu, std, logsnr_min, logsnr_max, resolution_aware; llada_image supports uniform; lms supports lms_max_order, lms_shift, lms_divisions; noise-injecting samplers support noise_sampler with value iid (default except for dpm++2m_sde_bt) or brownian_tree; brownian_tree_rng supports cpu (default), cuda, std_default or sampler_rng", + "extra sampler/scheduler/guidance args, key=value list. CFG supports guidance_schedule; APG supports apg_eta, apg_momentum, apg_norm_threshold, apg_norm_threshold_smoothing; SLG supports slg_uncond; MOSS-Image supports moss_uncond_skip, moss_uncond_skip_start, moss_uncond_skip_end (sigma-percent window of its block-22 negative-branch skip); lcm supports noise_clip_std, noise_scale_start, noise_scale_end; flux supports base_shift, max_shift; ltx2 supports max_shift, base_shift, stretch, terminal; euler_ge supports gamma; beta scheduler supports alpha, beta; logit_normal supports mu, std, logsnr_min, logsnr_max, resolution_aware; llada_image supports uniform; lms supports lms_max_order, lms_shift, lms_divisions; noise-injecting samplers support noise_sampler with value iid (default except for dpm++2m_sde_bt) or brownian_tree; brownian_tree_rng supports cpu (default), cuda, std_default or sampler_rng", (int)',', &extra_sample_args}, {"", diff --git a/src/pipeline/diffusion_engine.cpp b/src/pipeline/diffusion_engine.cpp index 1297ca0205..e8ee83e112 100644 --- a/src/pipeline/diffusion_engine.cpp +++ b/src/pipeline/diffusion_engine.cpp @@ -2472,6 +2472,32 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr(denoiser); + float shift = flow_denoiser ? flow_denoiser->shift : 5.0f; + auto percent_to_sigma = [shift](float percent) { + if (percent <= 0.0f) { + return 1.0f; + } + if (percent >= 1.0f) { + return 0.0f; + } + return time_snr_shift(shift, 1.0f - percent); + }; + moss_skip_sigma_high = percent_to_sigma(moss_uncond_skip.start); + moss_skip_sigma_low = percent_to_sigma(moss_uncond_skip.end); + LOG_INFO("MOSS uncond skip window: sigma-percent %.4f..%.4f (sigma %.4f..%.4f, shift %.3f)", + moss_uncond_skip.start, moss_uncond_skip.end, moss_skip_sigma_low, moss_skip_sigma_high, shift); + } else if (version == VERSION_MOSS_IMAGE && !moss_uncond_skip.enabled) { + LOG_INFO("MOSS uncond skip disabled"); + } + int moss_skipped_steps = 0; std::vector guidance_schedule = sd::guidance::parse_guidance_schedule(extra_sample_args); if (!guidance_schedule.empty() && guidance_schedule.size() != sigmas.size() - 1) { @@ -2907,8 +2933,12 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr= moss_skip_sigma_low && sigma <= moss_skip_sigma_high))) { + LOG_VERBOSE("MOSS uncond skip layers at step %d (sigma %.4f)\n", step, sigma); uncond_skip_layers = &moss_skip_layers; + ++moss_skipped_steps; } uncond_out = run_condition(uncond, uncond.c_concat.empty() ? nullptr : &uncond.c_concat, @@ -2999,6 +3029,9 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptrinverse_noise_scaling(sigmas[sigmas.size() - 1], x0); } diff --git a/src/runtime/guidance.cpp b/src/runtime/guidance.cpp index bfb773b0a1..0df0f7a772 100644 --- a/src/runtime/guidance.cpp +++ b/src/runtime/guidance.cpp @@ -64,6 +64,34 @@ namespace sd::guidance { return uncond; } + MossUncondSkipParams parse_moss_uncond_skip_args(const char* extra_sample_args) { + MossUncondSkipParams params; + for (const auto& [key, value] : parse_key_value_args(extra_sample_args, "extra sample arg")) { + if (key == "moss_uncond_skip") { + bool parsed = true; + if (!parse_strict_bool(value, parsed)) { + LOG_WARN("ignoring invalid MOSS extra sample arg '%s=%s'", key.c_str(), value.c_str()); + continue; + } + params.enabled = parsed; + } else if (key == "moss_uncond_skip_start" || key == "moss_uncond_skip_end") { + float parsed = 0.0f; + if (!parse_strict_float(value, parsed) || !(parsed >= 0.0f && parsed <= 1.0f)) { + LOG_WARN("ignoring invalid MOSS extra sample arg '%s=%s' (expected 0..1)", key.c_str(), value.c_str()); + continue; + } + (key == "moss_uncond_skip_start" ? params.start : params.end) = parsed; + } + } + if (params.start > params.end) { + LOG_WARN("moss_uncond_skip_start %.4f > moss_uncond_skip_end %.4f: keeping the skip on every step", + params.start, params.end); + params.start = 0.0f; + params.end = 1.0f; + } + return params; + } + std::vector parse_guidance_schedule_from_spec(std::string spec) { std::vector schedule; diff --git a/src/runtime/guidance.h b/src/runtime/guidance.h index 3de337042d..a81b9a2595 100644 --- a/src/runtime/guidance.h +++ b/src/runtime/guidance.h @@ -28,6 +28,17 @@ namespace sd::guidance { AdaptiveProjectedGuidanceParams parse_adaptive_projected_guidance_args(const char* extra_sample_args); bool is_adaptive_projected_guidance_enabled(const AdaptiveProjectedGuidanceParams& params); bool parse_skip_layer_guidance_uncond_arg(const char* extra_sample_args); + + // MOSS-Image's built-in negative-branch skip (block 22 on the uncond pass). extra_sample_args + // moss_uncond_skip=false turns it off; moss_uncond_skip_start / moss_uncond_skip_end bound it in + // sigma-percent of the shifted flow schedule (ComfyUI percent_to_sigma). Defaults keep every step. + struct MossUncondSkipParams { + bool enabled = true; + float start = 0.0f; + float end = 1.0f; + bool windowed() const { return start > 0.0f || end < 1.0f; } + }; + MossUncondSkipParams parse_moss_uncond_skip_args(const char* extra_sample_args); std::vector parse_guidance_schedule(const char* extra_sample_args); struct GuidanceInput { From 5c4248e21b9e41b15c3fccdb830ee6ca298b5f68 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Fri, 30 Oct 2026 08:01:55 -0700 Subject: [PATCH 2/3] MOSS-Image: drop the cached negative prediction when the windowed skip set changes EasyCache / UCache / CacheDIT key a condition's cached output by its pointer only, so at the moss_uncond_skip window edge a reused &uncond diff could come from the other skip configuration. Track the effective negative-branch skip per pass and invalidate &uncond in the step cache when it flips. Only runs with a window set; the default path is unchanged. Verbose logs say whether each windowed MOSS uncond pass was computed or served from cache. --- src/pipeline/diffusion_engine.cpp | 37 ++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/pipeline/diffusion_engine.cpp b/src/pipeline/diffusion_engine.cpp index e8ee83e112..f4e0875afe 100644 --- a/src/pipeline/diffusion_engine.cpp +++ b/src/pipeline/diffusion_engine.cpp @@ -2498,6 +2498,10 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr guidance_schedule = sd::guidance::parse_guidance_schedule(extra_sample_args); if (!guidance_schedule.empty() && guidance_schedule.size() != sigmas.size() - 1) { @@ -2762,9 +2766,17 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr cached_output; + const bool moss_trace_uncond = version == VERSION_MOSS_IMAGE && moss_uncond_skip.enabled && + moss_uncond_skip.windowed() && &condition == &uncond; if (step_cache.before_condition(&condition, noised_input, &cached_output)) { + if (moss_trace_uncond) { + LOG_VERBOSE("MOSS uncond served from step cache at step %d\n", step); + } return std::move(cached_output); } + if (moss_trace_uncond) { + LOG_VERBOSE("MOSS uncond computed at step %d\n", step); + } // A re-enabled condition can miss the cache even when the positive pass was reused. if (!uncond_controls_ready && !uncond.empty() && @@ -2933,12 +2945,22 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr= moss_skip_sigma_low && sigma <= moss_skip_sigma_high))) { - LOG_VERBOSE("MOSS uncond skip layers at step %d (sigma %.4f)\n", step, sigma); - uncond_skip_layers = &moss_skip_layers; - ++moss_skipped_steps; + } else if (version == VERSION_MOSS_IMAGE && moss_uncond_skip.enabled) { + const bool moss_skip_now = !moss_uncond_skip.windowed() || + (sigma >= moss_skip_sigma_low && sigma <= moss_skip_sigma_high); + if (moss_uncond_skip.windowed()) { + if (moss_prev_uncond_skip >= 0 && moss_prev_uncond_skip != static_cast(moss_skip_now)) { + step_cache.invalidate_condition(&uncond); + ++moss_cache_invalidations; + LOG_VERBOSE("MOSS uncond skip set changed at step %d: negative-branch step cache dropped\n", step); + } + moss_prev_uncond_skip = static_cast(moss_skip_now); + } + if (moss_skip_now) { + LOG_VERBOSE("MOSS uncond skip layers at step %d (sigma %.4f)\n", step, sigma); + uncond_skip_layers = &moss_skip_layers; + ++moss_skipped_steps; + } } uncond_out = run_condition(uncond, uncond.c_concat.empty() ? nullptr : &uncond.c_concat, @@ -3031,6 +3053,9 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr 0) { + LOG_INFO("MOSS uncond skip: %d window edge(s) dropped the cached negative prediction", moss_cache_invalidations); + } } if (inverse_noise_scaling) { x0 = denoiser->inverse_noise_scaling(sigmas[sigmas.size() - 1], x0); From 892e8005f2507ea4816a9ff681aeaf3395b6602c Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Fri, 30 Oct 2026 09:09:02 -0700 Subject: [PATCH 3/3] MOSS-Image: reset the Spectrum history when the windowed skip set changes --- src/pipeline/diffusion_engine.cpp | 18 ++++++++++++++++++ src/runtime/spectrum.hpp | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/src/pipeline/diffusion_engine.cpp b/src/pipeline/diffusion_engine.cpp index f4e0875afe..82b40473a5 100644 --- a/src/pipeline/diffusion_engine.cpp +++ b/src/pipeline/diffusion_engine.cpp @@ -2502,6 +2502,10 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr guidance_schedule = sd::guidance::parse_guidance_schedule(extra_sample_args); if (!guidance_schedule.empty() && guidance_schedule.size() != sigmas.size() - 1) { @@ -2638,6 +2642,17 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr(sigma >= moss_skip_sigma_low && sigma <= moss_skip_sigma_high); + if (moss_spectrum_window >= 0 && moss_spectrum_window != moss_window_now) { + cache_runtime.spectrum.reset_history(); + ++moss_spectrum_resets; + LOG_VERBOSE("MOSS uncond skip set changed at step %d: Spectrum history dropped\n", step); + } + moss_spectrum_window = moss_window_now; + } + if (cache_runtime.spectrum_enabled && cache_runtime.spectrum.should_predict()) { cache_runtime.spectrum.predict(&denoised); if (apply_denoise_mask) { @@ -3056,6 +3071,9 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr 0) { LOG_INFO("MOSS uncond skip: %d window edge(s) dropped the cached negative prediction", moss_cache_invalidations); } + if (moss_spectrum_resets > 0) { + LOG_INFO("MOSS uncond skip: %d window edge(s) reset the Spectrum history", moss_spectrum_resets); + } } if (inverse_noise_scaling) { x0 = denoiser->inverse_noise_scaling(sigmas[sigmas.size() - 1], x0); diff --git a/src/runtime/spectrum.hpp b/src/runtime/spectrum.hpp index e30f69308b..1214d53ee1 100644 --- a/src/runtime/spectrum.hpp +++ b/src/runtime/spectrum.hpp @@ -74,6 +74,13 @@ struct SpectrumState { cnt++; } + // Forget the fitted outputs but keep the step count, so predictions restart from fresh model evaluations. + void reset_history() { + H_buf.clear(); + T_buf.clear(); + num_cached = 0; + } + void predict(sd::Tensor* denoised) { GGML_ASSERT(denoised != nullptr); int64_t F = (int64_t)H_buf[0].size();