Repository navigation
Studio diffusion LoRA: restore requires_grad when adapters are unloaded (HiDream renders change after a detach) - #18081
Merged
Conversation
PEFT freezes every base weight it injects into and unload_lora_weights leaves them frozen. torch.matmul reads requires_grad even under no_grad: a Linear on a non-contiguous 3-D input folds into one mm while its weight requires grad, else it runs bmm, and on CUDA the two differ in their low bits. HiDream-I1 splits its joint attention output into image and text tokens before to_out / to_out_t, so every plain render after a LoRA was attached and removed (or set to weight 0) differed from the render before it. The requires_grad of every LoRA-loadable component is recorded (weakly, so a load-time bake's quantize still frees the dense weights) before the first adapter of a set and restored on unload.
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. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! 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". |
danielhanchen
added a commit
that referenced
this pull request
Nov 17, 2026
…s since #18081) (#18100) The Studio `diffusion` CPU shard has 16 failures in `tests/test_diffusion_backend.py` on main since #18081 (seen on the staging run of #18087, a frontend-only PR): ``` core/inference/diffusion_lora_files.py:532: in _lora_components return list({id(m): m for m in roots if isinstance(m, torch.nn.Module)}.values()) E AttributeError: module 'torch' has no attribute 'nn' E ValueError: Failed to apply LoRA: module 'torch' has no attribute 'nn' ``` The other 15 (`test_dense_build_cancellation_boundaries[*]`, `test_dense_quant_lora_bake_attaches_before_quantize`, `test_a_pipeline_pick_bakes_its_adapters_before_quantising`) are the same error, surfacing as "The requested LoRA adapters could not be applied". ## Cause #18081 made `load_adapter` snapshot the `requires_grad` flags of the pipe's `torch.nn.Module` components so unloading can restore them. That is correct with real torch. The `fake_runtime` fixture installs a minimal fake `torch` module with no `nn`, so every adapter load in these tests now raises. ## Fix The fake torch gets `nn.Module` (an empty class). The fake pipe components are not modules, so the snapshot is empty and the tests exercise the same paths as before. No production change. ## Verification `tests/test_diffusion_backend.py`, `-n 4`: - main: 21 failed (the 16 CI failures, plus 5 `test_download_plan_for_minimax_h3_image_*` cases that fail on this host only, from its local diffusers build, and pass in CI). - this branch: the 16 pass; only the same 5 host-only cases remain.
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.
Summary
On HiDream-I1, a plain render after a LoRA was attached and then removed (or set to weight 0) did not match the plain render from before the LoRA was attached.
Cause: PEFT sets
requires_grad=Falseon every base weight it injects into, andunload_lora_weights()never sets it back. Weight values, dtypes, strides, module types, hooks and attention processors all came back exactly;requires_gradwas the only state that differed.Why that changes the output:
torch.matmulreadsrequires_gradeven underno_grad.mm.bmm.On CUDA those two kernels differ in their low bits. HiDream splits its joint attention output into image and text tokens before
to_out/to_out_t. With the batch of 2 used for guidance, those halves are non-contiguous, so every later layer drifts.Fix: before the first adapter of a set loads,
load_adapterrecordsrequires_gradfor every LoRA-loadable component, andunload_adaptersrestores it. The record holds weak references, so a quantize after a load-time bake still frees the dense weights it replaces. Weight 0 goes through the same unload path, so it now matches no LoRA too.Testing
CPU:
test_diffusion_lora_detach_state.pybuilds a tiny HiDream transformer with a batch of 2, the shape that triggers the bug. It checks that therequires_gradflags and the exact mm / bmm kernel sequence match before attach and after detach, including for two adapters.GPU: RTX PRO 6000 (sm120), HiDream-I1-Full bf16, 1024x1024, 10 steps, pixel md5:
Controls with no LoRA at all confirm the cause:
requires_grad=Falseby hand reproducesb2fe6ee3;70eeabcaagain.Not covered: other families and the video LoRA path got the same fix but no GPU run.