Skip to content

Reuse the dY @ B.t() product in the fused LoRA backward - #12212

Merged
danielhanchen merged 1 commit into
mainfrom
perf/fused-lora-reuse-dB
Sep 29, 2026
Merged

danielhanchen merged 1 commit into
mainfrom
perf/fused-lora-reuse-dB

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Split out of #7020 so that PR carries only the dtype fix.

What

In the three fused LoRA backward passes (LoRA_MLP, LoRA_QKV, LoRA_W), each dY @ B.t() product was computed twice: once for the d_A weight gradient and once for the LoRA term of dX. This computes it once per projection and reuses it, removing 6 rank-r matmuls per layer backward (2 in MLP, 3 in QKV, 1 in O).

Verification

  • Gradients are bit-identical to main: SHA-256 over every returned gradient (dX, all A and B grads) across LoRA_W, LoRA_QKV and LoRA_MLP matches exactly (bf16, hidden 2048, intermediate 8192, r=64, 4x1024 tokens, B200).
  • Speed, backward pass only (the only code this touches), Llama-3.1-8B layer shapes (hidden 4096, intermediate 14336, 8 KV heads), 2 x 4096 tokens, B200 idle, median of 60 timed backwards after 10 warmup, base and this branch alternating in fresh processes, 3 rounds each:
Weights r main (ms) this PR (ms) change main round spread
bf16 16 3.74 / 3.73 / 3.74 3.52 / 3.53 / 3.56 -5.5% 0.4%
bf16 64 3.67 / 3.70 / 3.71 3.47 / 3.60 / 3.59 -3.1% 1.1%
bf16 128 3.69 / 3.69 / 3.72 3.60 / 3.61 / 3.65 -2.3% 0.7%
NF4 16 3.86 / 3.95 / 3.94 3.85 / 3.80 / 3.81 -3.4% 2.4%
NF4 64 3.89 / 3.99 / 3.89 3.88 / 3.80 / 3.83 -1.5% 2.7%
NF4 128 3.94 / 3.94 / 3.96 3.77 / 3.91 / 3.79 -3.7% 0.5%

Faster in every cell. Peak memory is unchanged to 10 MB. The saving is in the fused LoRA layer backward, so the end-to-end step time gain is smaller.

  • Base vs head SFT job, inference smoke and the 103-case tiny-model regression suite: no regression.
  • Extra memory: one rows x r buffer per projection stays alive until its dX term is added (512 KB per projection at the shape above).

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-29T08:51:50.103165Z b9ede13 Manual request
🔒 Security Review ✅ Completed 2026-09-29T08:52:47.795533Z b9ede13 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 danielhanchen self-assigned this Sep 28, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: b9ede135cc

ℹ️ 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".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: b9ede135cc

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

@danielhanchen
danielhanchen merged commit ae7ed40 into main Sep 29, 2026
4 of 29 checks passed
@danielhanchen
danielhanchen deleted the perf/fused-lora-reuse-dB branch September 29, 2026 11:49
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