Skip to content

Studio diffusion LoRA: restore requires_grad when adapters are unloaded (HiDream renders change after a detach) - #18081

Merged
danielhanchen merged 1 commit into
mainfrom
studio-hidream-lora-detach-restore
Nov 17, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
studio-hidream-lora-detach-restore

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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=False on every base weight it injects into, and unload_lora_weights() never sets it back. Weight values, dtypes, strides, module types, hooks and attention processors all came back exactly; requires_grad was the only state that differed.

Why that changes the output: torch.matmul reads requires_grad even under no_grad.

  • If the weight requires grad, a non-contiguous 3-D input to a Linear is copied and run as one mm.
  • If the weight is frozen, the same input runs as 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_adapter records requires_grad for every LoRA-loadable component, and unload_adapters restores 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.py builds a tiny HiDream transformer with a batch of 2, the shape that triggers the bug. It checks that the requires_grad flags and the exact mm / bmm kernel sequence match before attach and after detach, including for two adapters.

  • Main: those 2 new tests fail.
  • This branch: the LoRA suites (884 tests) pass.

GPU: RTX PRO 6000 (sm120), HiDream-I1-Full bf16, 1024x1024, 10 steps, pixel md5:

render main this branch
plain, then plain again 70eeabca, 70eeabca 70eeabca, 70eeabca
LoRA at weight 1 054cffda 054cffda
plain after detach b2fe6ee3 70eeabca
weight 0 b2fe6ee3 70eeabca

Controls with no LoRA at all confirm the cause:

  • setting requires_grad=False by hand reproduces b2fe6ee3;
  • setting it back gives 70eeabca again.

Not covered: other families and the video LoRA path got the same fix but no GPU run.

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.
@danielhanchen danielhanchen self-assigned this Nov 17, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 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-17T05:37:49.734392Z f148b26 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. Hooray!

Reviewed commit: f148b26e2f

ℹ️ 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 c3ad511 into main Nov 17, 2026
@danielhanchen
danielhanchen deleted the studio-hidream-lora-detach-restore branch November 17, 2026 06:47
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.
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