Skip to content

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

Merged
leejet merged 1 commit into
masterfrom
fix/qwen-image-2-1-prefix-cache
Nov 23, 2026
Merged

leejet merged 1 commit into
masterfrom
fix/qwen-image-2-1-prefix-cache

Conversation

@leejet

@leejet leejet commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Restore Qwen Image 2.1 prefix caching after the DGL migration by passing a stable, nonzero prefix_id from each fixed predictor to the runner. Addresses the edit-sampling regression reported in #2401.
  • Give positive, negative, and distinct reference predictors separate IDs within each sampling run. Step-dependent conditioning keeps ID 0. The existing runner handles cache cleanup and low-memory fallback.

Related Issue / Discussion

N/A

Additional Information

N/A

Checklist

Summary by CodeRabbit

  • Bug Fixes
    • Improved image generation with Qwen Image 2.1 models in configurations without controls, IP-Adapters, or PuLID bindings.
    • Generation now better distinguishes eligible model configurations, helping produce more consistent results across image-generation steps.

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Qwen Image 2.1 prediction steps now carry a prefix ID. Predictor collection assigns IDs to eligible leaves, and the DiT code passes each ID in QwenImage21DiffusionExtra.

Changes: Qwen Image 2.1 prefix ID flow

Layer / File(s) Summary
Assign and forward prefix IDs
src/dgl/models/diffusion_prediction.h, src/dgl/ops/predictor.cpp, src/dgl/models/dit.cpp
DiffusionPredictionStep gains a uint64_t prefix ID. Predictor collection stores sequential IDs for Qwen Image 2.1 leaves without control, IP-Adapter, or PuLID bindings. Prediction passes the stored ID, or 0 when none is stored, to QwenImage21DiffusionExtra.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • leejet/stable-diffusion.cpp#2401: The prefix ID changes target the reported lack of prefix-cache use during Qwen Image 2.1 edits and the associated slowdown. The reported VRAM retention between jobs is not addressed.

Suggested reviewers: wbruna

Merge Risk: Minimal · up to a56b3

This change restores prefix caching for Qwen Image 2.1 edit sampling by passing a stable prefix ID per predictor. No actionable merge-blocking risk was found, and unsupported cases fall back to the existing uncached behavior.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: restoring Qwen Image 2.1 prefix caching, which matches the raw summary's changes to pass a stable nonzero qwen_prefix_id from predictors to the step. The 'fix:' prefix…
Description check Passed The description has the Summary, Related Issue / Discussion, Additional Information, and Checklist sections from the template. The Summary explains the change and links issue #2401. Related Issue is N…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: restoring Qwen Image 2.1 prefix caching, which matches the raw summary's changes to pass a stable nonzero qwen_prefix_id from predictors to the step. The 'fix:' prefix is conventional and the title is specific enough for a teammate scanning history.

Full details: Description check

Explanation

The description has the Summary, Related Issue / Discussion, Additional Information, and Checklist sections from the template. The Summary explains the change and links issue #2401. Related Issue is N/A, which is acceptable here since the issue is linked in the Summary. Additional Information is N/A, with no verification notes. The checklist is ticked, but the model-documentation item does not clearly apply to this DGL C++ change, which touches no model support docs.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Cppcheck (2.22.0)

Cppcheck (2.22.0) exceeded its 180-second time budget. Findings from completed work are retained; remaining checks are incomplete.

src/dgl/ops/predictor.cpp

Cppcheck interrupted; analysis of this file is incomplete

src/dgl/models/dit.cpp

Cppcheck interrupted; analysis of this file is incomplete

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a3e02706-6414-478a-958b-2aa67f6774f8
Commits

Reviewing files that changed from the base of the PR and between 5b80e1f and a56b3bc.

Files selected for processing: 3

  • src/dgl/models/diffusion_prediction.h
  • src/dgl/models/dit.cpp
  • src/dgl/ops/predictor.cpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.

@leejet
leejet merged commit 91ed009 into master Nov 23, 2026
11 checks passed
@leejet
leejet deleted the fix/qwen-image-2-1-prefix-cache branch November 23, 2026 17:19
filve pushed a commit to filve/diffusione-in-tempo-reale that referenced this pull request Nov 24, 2026
…art 2)

Brings in the Diffusion Graph Language runtime (leejet#2396) and the commits
after it: MOSS-Image edit LoRAs (leejet#2203), runtime LoRA counts (leejet#2400),
immediate LoRA merges for INT8 weights (leejet#2403), one server context with
on-demand model loading (leejet#2404), Qwen Image 2.1 prefix caching (leejet#2405)
and idle TTLs for model weights (leejet#2406). The server changes of leejet#2404
and leejet#2406 come in the same merge so that sd-server is ported once.

src/pipeline and src/stable-diffusion.cpp are gone; the fork's
additions move into the graph runtime:
- CPU fallback: ModelRuntime::configure_backends() falls back to the cpu
  backend, with a warning, when a module's backend cannot be
  initialized; disable_backend_fallback (also a DGL context option)
  makes it fail with SD_ERROR_BACKEND instead.
- memory_guard is a DGL context option for the context's ModelManager.
  ModelManager::ResourceHandle, the residency manager the models use,
  forwards the guard hooks and honours the prefetch suspension.
- layer_split_fill is applied when the context is created; multi_gpu is
  checked there. The multi-GPU planner, which upstream no longer calls,
  picks the device of each module when auto-fit is on, more than one
  GPU is present and no backend was given (planned_backend_spec() in
  src/api/generation_program.cpp); the run plan places the weights.
- The SD2 v-parameterization probe reports a failed compute instead of
  aborting.
- sd_ctx_create, sd_ctx_set_model_options*, sd_generate_image and
  sd_generate_video record the error code again; a load failure with no
  deeper cause is model_file.

sd-server keeps the ContextKeeper as the owner of the context:
- requests pick their model as upstream does (recipe and models in the
  body); the switch goes through ContextKeeper::load(), so the profile
  ladder and the recovery cover it too;
- sd_ctx_mutex serializes model switches, generations and DGL scripts;
  a DGL script runs on the keeper's context, or on a temporary one while
  no model is loaded (ContextKeeper::with_context()); the idle TTL
  release does not wait for a running generation (try_with_context());
- every context the keeper creates gets the server's file-access policy
  (create_context_from_params() + apply_server_file_access());
- /sdcpp/v1/models/load and /unload stay, rebuilt on
  resolve_model_selection(), as do /sdcpp/v1/health and /resources;
- /sdcpp/v1/upscale and the server-side ADetailer go, as upstream did:
  both are DGL scripts now.

Tests: memory_guard_engine_test follows the new capacity hook;
server_recovery_test checks with_context() and try_with_context()
(red with both behaviours broken); api_robustness expects a missing
model file at the first generation, where the weights load now;
server_cancel_e2e.py uses 100 steps, the server's maximum.

docs: the C API section of docs/integrazione_progetti.md uses the
opaque options; backend.md says what the multi-GPU planner decides
with DGL; api.md documents the kept load/unload routes.

Verified with a CPU build: the library, sd-cli and sd-server build; all
nine unit-test programs pass, server_recovery_test eight times in a row
and twice under TSan with no warnings; with a synthetic SD 1.5 model
(random weights) sd-cli generates, falls back to the cpu backend for
--backend vulkan0 and fails as backend with --disable-backend-fallback,
and test/server_cancel_e2e.py passes its 35 checks against a real
sd-server. A scratch smoke test of the server (20 checks: no model, DGL
jobs with and without a model, models/load and unload, a job that picks
its model, the idle release) passes too.

Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant