Repository navigation
Studio diffusion: keep Qwen-Image-2.1 scoped LoRA prompt reuse after the no-K/V wrapper, and key callable helpers fully - #17192
Merged
Conversation
…the no-K/V wrapper; key callable helpers fully The no-K/V prompt helper (#17118) is installed on the pipeline instance before the prompt cache, so the denoiser-only check (#17105) saw an instance override and kept every LoRA strength in the key. The wrapper is now marked as forwarding only to the same-named class method, which the check then follows. Callable objects held on the pipeline (an object with __call__, or a module containing a denoiser) were treated as plain data, so a helper reading the denoiser could return stale embeddings across strength changes. They now keep the full key; text encoder modules and tokenizers / processors stay exempt.
Member
Author
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
This was referenced Nov 9, 2026
dayanruben
pushed a commit
to dayanruben/unsloth
that referenced
this pull request
Nov 10, 2026
…ansparent-wrapper marker (unslothai#17348) ## Summary The prompt cache reuses text embeddings across a denoiser-only LoRA change when it can prove `encode_prompt` never runs the denoiser. Instance wrappers that only forward to the same-named class method (the Qwen-Image-2.1 no-K/V wrapper) are skipped in that proof through a marker attribute. The marker was a plain `True`, and `functools.wraps` copies a function's `__dict__` onto the outer wrapper, so any later decorator around the marked wrapper inherited "transparent" without opting in. A decorator that reads the denoiser then got stale embeddings after a LoRA strength change. ## Root cause Introduced in unslothai#17192: `_transparent_wrapper` trusted `getattr(fn, TRANSPARENT_WRAPPER_ATTR, False)` on every link of the `__wrapped__` chain. ## Fix - `mark_transparent_wrapper(fn)` stores a reference to `fn` itself; `is_transparent_wrapper(fn)` accepts only when the stored value is that same function. A copy made by `functools.wraps` names the inner function, so the outer wrapper is not trusted and keeps the full adapter key. - The no-K/V wrapper uses the helper. The final-hidden-state wrapper, which wraps it and only reads the text encoder, is marked explicitly when the function it wraps is marked, so the stock Qwen-Image-2.1 chain keeps the scoped key exactly as before. ## Flip test `studio/backend/tests/test_diffusion_prompt_cache.py`: - `test_a_wraps_copy_of_the_transparent_marker_keeps_the_full_key`: installs the no-K/V wrapper, adds a `functools.wraps` wrapper that scales by a denoiser weight, then changes LoRA strength and weight. Fails on main (scoped key accepted, one encode, stale value), passes with the fix (two encodes, fresh value). - `test_final_state_wrapper_over_the_no_kv_wrapper_keeps_the_scoped_key`: the stock no-K/V + final-hidden-state chain still passes the static check (passes on both; guards the explicit re-marking). ## Tests run - Flip test on origin/main: 1 failed, 1 passed. With the fix: 2 passed. - `tests/test_diffusion_prompt_cache.py tests/test_diffusion_qwenimage21_te_no_kv.py` with `-o addopts= -m "not server and not native_mxc"`: 55 passed, 3 skipped, 1 failed. The failure is `test_the_static_check_on_real_pipelines`, which fails identically on origin/main because of a local diffusers / huggingface_hub version mismatch (`cannot import name 'get_cached_repo_tree'`). - Standalone reproduction (no-K/V wrapper plus a denoiser-reading `functools.wraps` wrapper, strength 0.25 then 0.75): main returns `[1.25, 1.25]` with 1 encode; the fix returns `[1.25, 1.75]` with 2 encodes, matching the pre-unslothai#17192 parent. ## Not proven - No GPU run; the change is pure Python control flow on the cache key and was exercised on CPU with fake pipelines. - `test_the_static_check_on_real_pipelines` could not run here because of the local diffusers / huggingface_hub mismatch.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What happened
#17105 made the prompt cache drop denoiser-only LoRA adapters from its key when a static check proves
encode_promptnever runs a denoiser, so a strength sweep on a transformer-only LoRA reuses the text embeddings instead of re-running the text encoder.Two gaps showed up on current main:
_get_qwen_prompt_embedswrapper (it runs the text encoder withuse_cache=False) beforeprompt_cache.install. The check treats any instance-level function as unprovable, so for the realQwenImage21Pipelineit went fromTruetoFalseonce the wrapper was installed, and every strength change re-ran the encoder again. Embeddings stayed correct; only the saving was gone.__call__, or annn.Modulethat contains the denoiser, was treated as plain data. A helper like that which reads the transformer returned the first strength's embeddings for every later strength ([0.25, 0.25]with one encode, against[0.25, 0.75]with two encodes before Studio: a denoiser-only LoRA change keeps the cached text embeddings #17105).Fix
nn.Modulethat holds no denoiser (text encoders) or a tokenizer / processor / image processor / feature extractor.The check runs once when the cache is installed, so render-time cost is unchanged and nothing compiled is touched.
UNSLOTH_DIFFUSION_TE_SCOPED_LORA_KEY=0still turns the scoped key off entirely.Tests
New cases in
studio/backend/tests/test_diffusion_prompt_cache.py: the real no-K/V wrapper installed before the cache on a Qwen-Image-2.1 shaped pipeline and on the realQwenImage21Pipelineclass (one encode across a strength sweep), unmarked and mis-marked wrappers, callable-object and denoiser-holding module helpers (two encodes, fresh values), and a check that each run imports its own tree.On main 5 of them fail; on this branch the file passes (55 passed, 1 CUDA-only skip, diffusers 0.42.1, CPU).
test_diffusion_qwenimage21_te_no_kv.pyandtest_shared_memory_offload_keeps_the_prompt_cache.pyalso pass.