Repository navigation
Studio: a denoiser-only LoRA change keeps the cached text embeddings - #17105
Conversation
The prompt cache keyed every entry on the whole attached LoRA selection, so a strength sweep of a transformer-only adapter re-ran the text encoder for an unchanged prompt. Leave an adapter out of the key when every component it patches is a denoiser and the pipeline's encode provably runs no denoiser (static read of the class body and the self methods it calls). Text-encoder, mixed and unclassified adapters, and encodes that cannot be proven, keep the full key. UNSLOTH_DIFFUSION_TE_SCOPED_LORA_KEY=0 restores it everywhere.
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. |
[skip ci]
|
Codex Review: Didn't find any major issues. Swish! 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". |
…ce overrides The static check behind the denoiser-only prompt-cache key followed class methods only. A helper replaced on the instance, or a property or other descriptor reading the denoiser, escaped it and the scoped key reused stale embeddings. Properties are now examined (the base pipeline's own device/dtype properties are known inert), other descriptors and instance overrides of followed names keep the full LoRA key. [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
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". |
|
|
…ds self to [skip ci]
…the no-K/V wrapper, and key callable helpers fully (unslothai#17192) ## What happened unslothai#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.** unslothai#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 unslothai#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.
Summary
Studio's text-embedding cache (
diffusion_prompt_cache) included the whole attached LoRA selection (name, path, strength) in every cache key. As a result, a strength sweep with an adapter that only patches the transformer re-ran the text encoder for the same prompt at every new strength, even though the embeddings cannot change. On an offloaded Qwen-Image text encoder, that is one full encoder pass per strength step.Changes
An adapter is now left out of the key only when both of these hold:
transformer,transformer_2,unet, ...). The components come from:get_list_adapters()diffusion_lora_quant_merge), which by construction live only in the denoisers.encode_promptbody and everyselfmethod it calls. The check fails (and the encode keeps the full key) if any of these appear:.dtype/.device/.configselfhanded to another callablegetattr(self, ...)super()Everything else keeps today's key:
Textual-inversion embeddings stay in the key as before.
In diffusers 0.42.1, the check proves these encodes, among others:
It keeps the full key for:
Kill switch:
UNSLOTH_DIFFUSION_TE_SCOPED_LORA_KEY=0.Evidence
Real tiny
QwenImagePipeline(CPU), a transformer LoRA, strengths 0.25 / 0.5 / 0.75 / 1.0 / 0.5:New tests in
tests/test_diffusion_prompt_cache.pycover:.dtyperead allowedsuper()/ dynamicgetattr/self-to-helper / indirect denoiser calls being unprovableResult: 40 passed, 1 skipped. On main, 5 of the new tests fail.
Follow-up: helpers the check cannot see
The check used to follow class methods only, so two cases slipped through and reused stale embeddings after a denoiser change:
pipe._embed = ...) that reads the denoiserNow the check:
_execution_device/device/dtypeproperties are known not to run a layer)Repro (custom pipe, transformer-only adapter, strength 0.25 then 0.75, denoiser weight changed to match):
[0.25, 0.25], 1 encode[0.25, 0.25], 1 encode[0.25, 0.75], 2 encodes[0.25, 0.75], 2 encodesStock pipelines keep the optimization: over every
diffusers0.42.1 pipeline class with anencode_prompt(225), the check returns the same answer before and after (144 skip the denoiser adapters, 0 moved), and no pipeline's__init__sets an attribute that shadows a followed name. 3 new tests fail before the follow-up and pass after (44 passed, 1 skipped; same on diffusers 0.41.0).