From 8af140d58b9d5f65fb7069b72c028602d8b40e73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20du=20Hamel?= Date: Wed, 23 Sep 2026 15:30:15 +0200 Subject: [PATCH 1/3] feat: optimize cfg special cases with guidance schdeule --- src/pipeline/diffusion_engine.cpp | 33 ++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/pipeline/diffusion_engine.cpp b/src/pipeline/diffusion_engine.cpp index 022093957..0f9bf78bc 100644 --- a/src/pipeline/diffusion_engine.cpp +++ b/src/pipeline/diffusion_engine.cpp @@ -2569,12 +2569,38 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr StableDiffusionGGML::sample(const std::shared_ptr StableDiffusionGGML::sample(const std::shared_ptr Date: Wed, 23 Sep 2026 21:04:07 +0200 Subject: [PATCH 2/3] requested changes --- src/pipeline/diffusion_engine.cpp | 85 ++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 30 deletions(-) diff --git a/src/pipeline/diffusion_engine.cpp b/src/pipeline/diffusion_engine.cpp index 0f9bf78bc..f8cb9d9fb 100644 --- a/src/pipeline/diffusion_engine.cpp +++ b/src/pipeline/diffusion_engine.cpp @@ -2578,7 +2578,7 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr StableDiffusionGGML::sample(const std::shared_ptr StableDiffusionGGML::sample(const std::shared_ptr* uncond_skip_layers = nullptr; - if (is_skiplayer_step && slg_uncond) { - LOG_VERBOSE("Skipping layers at uncond step %d\n", step); - uncond_skip_layers = &skip_layer_guidance.layers(); - } - uncond_out = run_condition(uncond, - uncond.c_concat.empty() ? nullptr : &uncond.c_concat, - uncond_skip_layers, - nullptr, - true); - if (uncond_out.empty()) { - return {}; + if (!uncond.empty()) { + if (!skip_uncond) { + if (!step_cache.is_step_skipped()) { + compute_sample_controls(control_image, + noised_input, + timesteps_tensor, + uncond, + &controls); + } + const std::vector* uncond_skip_layers = nullptr; + if (is_skiplayer_step && slg_uncond) { + LOG_VERBOSE("Skipping layers at uncond step %d\n", step); + uncond_skip_layers = &skip_layer_guidance.layers(); + } + uncond_out = run_condition(uncond, + uncond.c_concat.empty() ? nullptr : &uncond.c_concat, + uncond_skip_layers, + nullptr, + true); + if (uncond_out.empty()) { + return {}; + } + } else { + if (step_cache.runtime.easycache_enabled()) + step_cache.runtime.easycache.reset_runtime(); + if (step_cache.runtime.ucache_enabled()) + step_cache.runtime.ucache.reset_runtime(); + if (step_cache.runtime.cachedit_enabled()) + step_cache.runtime.cachedit.reset_runtime(); + if (!img_uncond.empty() && !skip_img_uncond && !step_cache.is_step_skipped()) { + compute_sample_controls(control_image, + noised_input, + timesteps_tensor, + uncond, + &controls); + } } } - if (!img_uncond.empty() && !skip_img_uncond) { - img_uncond_out = run_condition(img_uncond, - img_uncond.c_concat.empty() ? nullptr : &img_uncond.c_concat, - nullptr, - uncond_without_ref_latents ? &empty_ref_latents : nullptr, - true); - if (img_uncond_out.empty()) { - return {}; + if (!img_uncond.empty()) { + if (!skip_img_uncond) { + img_uncond_out = run_condition(img_uncond, + img_uncond.c_concat.empty() ? nullptr : &img_uncond.c_concat, + nullptr, + uncond_without_ref_latents ? &empty_ref_latents : nullptr, + true); + if (img_uncond_out.empty()) { + return {}; + } + } else { + if (step_cache.runtime.easycache_enabled()) + step_cache.runtime.easycache.reset_runtime(); + if (step_cache.runtime.ucache_enabled()) + step_cache.runtime.ucache.reset_runtime(); + if (step_cache.runtime.cachedit_enabled()) + step_cache.runtime.cachedit.reset_runtime(); } } sd::guidance::GuidanceInput guidance_input; From 1fea1b9729d9ec2eecca07accf0458ed8e510d24 Mon Sep 17 00:00:00 2001 From: leejet Date: Fri, 25 Sep 2026 01:05:10 +0800 Subject: [PATCH 3/3] fix: preserve cache state when skipping CFG conditions --- src/pipeline/diffusion_engine.cpp | 48 ++++++++++++------------------- src/runtime/sample-cache.cpp | 20 +++++++++++++ src/runtime/sample-cache.h | 1 + 3 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/pipeline/diffusion_engine.cpp b/src/pipeline/diffusion_engine.cpp index f8cb9d9fb..7d380fa5d 100644 --- a/src/pipeline/diffusion_engine.cpp +++ b/src/pipeline/diffusion_engine.cpp @@ -2441,6 +2441,7 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr> empty_ref_latents; bool uncond_without_ref_latents = !img_uncond.empty() && @@ -2530,6 +2531,17 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptrbefore_diffusion(diffusion_params, step); } @@ -2569,11 +2581,11 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr StableDiffusionGGML::sample(const std::shared_ptr* uncond_skip_layers = nullptr; if (is_skiplayer_step && slg_uncond) { LOG_VERBOSE("Skipping layers at uncond step %d\n", step); @@ -2623,19 +2628,7 @@ sd::Tensor StableDiffusionGGML::sample(const std::shared_ptr StableDiffusionGGML::sample(const std::shared_ptr& input, sd::Tensor* output); void after_condition(const void* condition, const sd::Tensor& input, const sd::Tensor& output); + void invalidate_condition(const void* condition); bool is_step_skipped() const; };