Skip to content

Studio diffusion: do not let functools.wraps copy the prompt-cache transparent-wrapper marker - #17348

Merged
danielhanchen merged 2 commits into
mainfrom
studio-prompt-cache-marker-no-wraps
Nov 10, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-prompt-cache-marker-no-wraps

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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 #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-Studio diffusion: keep Qwen-Image-2.1 scoped LoRA prompt reuse after the no-K/V wrapper, and key callable helpers fully #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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 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-10T04:20:33.263982Z c5b56e8 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: c5b56e8da1

ℹ️ 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 cef96a9 into main Nov 10, 2026
@danielhanchen
danielhanchen deleted the studio-prompt-cache-marker-no-wraps branch November 10, 2026 04:58
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