Skip to content

Studio diffusion: keep Qwen-Image-2.1 scoped LoRA prompt reuse after the no-K/V wrapper, and key callable helpers fully - #17192

Merged
danielhanchen merged 3 commits into
mainfrom
dh/qwen21-prompt-cache-scoped-wrapper
Nov 9, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/qwen21-prompt-cache-scoped-wrapper

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

What happened

#17105 made the prompt cache drop denoiser-only LoRA adapters from its key when a static check proves encode_prompt never 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:

  1. Qwen-Image-2.1 lost the reuse. Studio Qwen-Image-2.1: encode prompts without the text encoder's unused K/V cache #17118 later installed an instance-level _get_qwen_prompt_embeds wrapper (it runs the text encoder with use_cache=False) before prompt_cache.install. The check treats any instance-level function as unprovable, so for the real QwenImage21Pipeline it went from True to False once the wrapper was installed, and every strength change re-ran the encoder again. Embeddings stayed correct; only the saving was gone.
  2. Callable objects were not followed. An instance attribute that is an object with __call__, or an nn.Module that 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

  • The no-K/V wrapper is marked as forwarding only to the same-named class method. The check accepts a marked instance attribute only when the unwrapped target is that instance's own bound class method, then follows the class method as before. An unmarked wrapper, or a marked one around anything else, still keeps the full key.
  • A callable instance attribute now keeps the full key unless it is an nn.Module that 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=0 still 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 real QwenImage21Pipeline class (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.py and test_shared_memory_offload_keeps_the_prompt_cache.py also pass.

…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.
@danielhanchen danielhanchen self-assigned this Nov 9, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-09T02:50:43.009667Z 63d57d6 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 63d57d624a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen
danielhanchen merged commit 864c246 into main Nov 9, 2026
@danielhanchen
danielhanchen deleted the dh/qwen21-prompt-cache-scoped-wrapper branch November 9, 2026 12:53
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.
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