Commit 91ed009 for stable-diffusion.cpp

commit 91ed00931ffc0f2fddc3d97549b43e50c05b4aaf
Author: leejet <leejet714@gmail.com>
Date:   Tue Nov 24 00:38:10 2026 +0800

    fix: restore Qwen Image 2.1 prefix caching in DGL (#2405)

diff --git a/src/dgl/models/diffusion_prediction.h b/src/dgl/models/diffusion_prediction.h
index 8535c9d..8b3330e 100644
--- a/src/dgl/models/diffusion_prediction.h
+++ b/src/dgl/models/diffusion_prediction.h
@@ -1,6 +1,7 @@
 #ifndef __SD_DGL_MODELS_DIFFUSION_PREDICTION_H__
 #define __SD_DGL_MODELS_DIFFUSION_PREDICTION_H__

+#include <cstdint>
 #include <functional>
 #include <limits>

@@ -64,6 +65,7 @@ namespace sd::dgl::models {
         const AttentionProcessor* attn2_processor           = nullptr;
         const FluxImageBlockProcessor* flux_image_processor = nullptr;
         const UNetBlockProcessor* unet_block_processor      = nullptr;
+        uint64_t qwen_prefix_id                             = 0;

         void blend_mask(Tensor<float>& latent) const {
             if (!mask.empty())
diff --git a/src/dgl/models/dit.cpp b/src/dgl/models/dit.cpp
index 73e7bba..9d541e0 100644
--- a/src/dgl/models/dit.cpp
+++ b/src/dgl/models/dit.cpp
@@ -375,7 +375,7 @@ namespace sd::dgl::models {
         } else if (version() == VERSION_FLUX3) {
             params.extra = Flux3DiffusionExtra{present(target_time)};
         } else if (version() == VERSION_QWEN_IMAGE_2_1) {
-            params.extra = QwenImage21DiffusionExtra{&condition.c_token_types};
+            params.extra = QwenImage21DiffusionExtra{&condition.c_token_types, step.qwen_prefix_id};
         } else if (sd_version_is_anima(version())) {
             params.extra = AnimaDiffusionExtra{present(condition.c_t5_ids), present(condition.c_t5_weights)};
         } else if (sd_version_is_wan(version())) {
diff --git a/src/dgl/ops/predictor.cpp b/src/dgl/ops/predictor.cpp
index 0996877..1bedd32 100644
--- a/src/dgl/ops/predictor.cpp
+++ b/src/dgl/ops/predictor.cpp
@@ -167,6 +167,7 @@ namespace sd::dgl::ops {
         const PredictionExecutionOptions options;
         std::map<models::Model*, PreparedModel> prepared;
         std::map<const ModelPredictor*, ConditioningWindows> windows;
+        std::map<const ModelPredictor*, uint64_t> qwen_prefix_ids;
         std::map<const models::DenoisingParameterization*, sd_sample::ConditionCacheRuntime> caches;
         std::map<const ModelPredictor*, ActiveConditioning> previous_conditioning;
         IPAdapterCache ip_adapter_cache;
@@ -227,6 +228,9 @@ namespace sd::dgl::ops {
                     resources.add_windows(model->controls, windows.controls);
                     resources.add_windows(model->ip_adapters, windows.ip_adapters);
                     resources.add_windows(model->pulid, windows.pulid);
+                } else if (model->dit().version() == VERSION_QWEN_IMAGE_2_1) {
+                    // This leaf's text, image slots and references are fixed for the sample.
+                    resources.qwen_prefix_ids.emplace(model, resources.qwen_prefix_ids.size() + 1);
                 }
             } else if (const auto* branches = guidance_branches(predictor)) {
                 self(self, *branches->positive);
@@ -449,6 +453,8 @@ namespace sd::dgl::ops {
         PuLIDAttention pulid(active_pulid, pulid_cache);
         if (!active_pulid.empty())
             step.flux_image_processor = &pulid;
+        const auto prefix   = qwen_prefix_ids.find(&leaf);
+        step.qwen_prefix_id = prefix == qwen_prefix_ids.end() ? 0 : prefix->second;
         sd::ParallelScope tensor_scope(engine.tensor_executor.get());
         auto prediction = engine.predict_diffusion(x, leaf.resolved_parameterization(), sigma, leaf.condition, leaf.references,
                                                    leaf.options, step);