Skip to content

Studio tests: seed the dense fast-LoRA parity input and hold its input grad to one fp32 ulp - #18739

Merged
danielhanchen merged 1 commit into
mainfrom
fix/diffusion-lora-dense-input-grad-ulp
Nov 21, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
fix/diffusion-lora-dense-input-grad-ulp

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

studio/backend/tests/test_diffusion_lora_fast.py::test_dense_base_with_fp32_inputs_is_bit_identical_to_peft failed on an unrelated PR (#18734, base 15a985f32d) in (Python 3.13, diffusion):

assert torch.equal(got[0], want[0]) and torch.equal(got[1], want[1])
E   assert (True and False)

Cause

The test drew its input with an unseeded torch.randn, so x depended on whatever the earlier tests in the same xdist worker left in the global RNG. For most inputs the fast path and PEFT agree bit for bit, but for a small fraction one element of the input gradient differs by one fp32 ulp. Reproduced locally with seed 1049 (1 of 300 seeds):

tensor bit-identical max abs diff
forward output yes 0
input grad no, 1 element 3.6e-12 (relative 3.8e-9)
all six LoRA grads yes 0

The block feeds one layer norm into both q and k, so its input gradient is a sum of the two branches, and autograd's accumulation order can change the last bit of a rare element. That is not a gradient error. diffusion_lora_fast.py (#18345) is correct; the test's exact-equality claim on the input grad is stricter than the arithmetic allows.

Fix

Test-only change:

  • The input comes from a dedicated generator, parametrized over seed 0 and seed 1049 (the failing case), so the test no longer depends on test order.
  • The forward output and every LoRA gradient still require torch.equal.
  • The input gradient is checked with torch.testing.assert_close(rtol = 1e-6, atol = 1e-9), which admits ulp-level accumulation-order noise and nothing a real bug would produce.

Verification

  • studio/backend/tests/test_diffusion_lora_fast.py: 23 passed.
  • The previous exact check fails on seed 1049, as on CI.
  • Sabotage: scaling the fast path's LoRA input gradient by 1.01 in _LoraProduct.backward fails both parametrizations (8176 of 8192 elements mismatched). Restored afterwards.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 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-21T07:15:07.749059Z 539eee3 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. What shall we delve into next?

Reviewed commit: 539eee3d4f

ℹ️ 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 ba4979c into main Nov 21, 2026
@danielhanchen
danielhanchen deleted the fix/diffusion-lora-dense-input-grad-ulp branch November 21, 2026 07:15
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