Skip to content

LoRA: decide the fused order once per call - #16282

Merged
danielhanchen merged 2 commits into
mainfrom
lora-fused-host-overhead
Oct 31, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
lora-fused-host-overhead

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

The fused LoRA order from #16071 (the rank-r term goes first and the base GEMM accumulates onto it) costs extra host time on every call. That cost shows up as +4% to +7% step time on NVIDIA bf16 runs that are host bound: small models, under about 1k tokens per step. UNSLOTH_LORA_FUSED_PASSES=0 restores the old speed.

The cost is Python work, not GPU work. A CPU profile of one QKV forward and backward shows:

  • about 220 more Python calls than the kill switch;
  • _lora_fused_ok run 9 times;
  • extra bookkeeping in _shared_XA / _shared_lora_backward.

Gradient checkpointing repeats all of this in the forward, the recompute and the backward.

This PR keeps the same arithmetic and removes that host work:

  • One decision per call. LoRA_W, LoRA_QKV and LoRA_MLP decide eligibility once in the forward and record it on ctx. The backward reuses the answer when the dtype and compile state match, instead of re-running _lora_fused_ok per matmul. matmul_lora takes the caller's answer through fused_checked.
  • Fewer calls in the hot path:
    • the functorch check runs only when a torch.func transform is active;
    • the DTensor test is a plain isinstance chain;
    • the adapter casts are skipped when the dtype already matches;
    • fast_dequantize is skipped for unquantized weights, where it returned W unchanged;
    • _shared_XA / _shared_lora_backward do less per call.

The kill switch and every path that is not eligible for the fused order are unchanged.

Results

Colab A100 40GB, bf16, LoRA r16, FineTome 64 rows, seq 2048. Each value is the median steady step time (steps 3 onward) in ms. Every row is three fresh-process pairs, main then this PR, on one VM. "Kill switch" is UNSLOTH_LORA_FUSED_PASSES=0, run on both trees.

model order main this PR change
Qwen3-0.6B 16-bit (host bound) fused (default) 272.1 / 266.6 / 276.8 258.9 / 262.9 / 267.6 -3.4% (median)
Qwen3-0.6B 16-bit kill switch 252.1 / 262.8 / 259.8 263.3 / 259.1 / 254.3 same
Llama-3.2-3B 4-bit (host bound) fused (default) 320.0 / 315.2 / 317.7 310.6 / 311.7 / 306.0 -2.2% (median)
Llama-3.2-3B 4-bit kill switch 304.4 / 307.5 / 300.7 304.9 / 302.2 / 303.4 same
Qwen3-8B 4-bit (GPU bound) fused (default) 507.8 / 508.6 507.5 / 507.8 same
Qwen3-8B 4-bit kill switch 522.7 521.3 same

Fused order against the kill switch (pooled kill-switch median, with that pool's spread):

model main this PR kill-switch spread
Qwen3-0.6B 16-bit +4.9% +1.3% 4.3%
Llama-3.2-3B 4-bit +4.5% +2.2% 2.2%
Qwen3-8B 4-bit -2.8% -2.8%
  • Host-bound cases: the fused order's remaining cost is inside the kill switch's own run-to-run spread.
  • GPU-bound case: the fused order keeps its gain.

CPU, one core, one QKV forward + backward, us per iteration over three repeats:

tree fused kill switch
main 423 / 391 / 377 361 / 294 / 301
this PR 381 / 357 / 310 359 / 345 / 312

Correctness

  • Bitwise on A100. 18 cases with the fused order on and off, 132 output and gradient tensors: this PR equals main, and repeated runs of each tree are identical.
  • End-to-end losses. These vary from run to run on main itself (1.21739 to 1.21777 across three main runs of the same seed). This PR falls inside that range.
  • Compile. 0 graph breaks and 4 unique graphs on both trees in a compiled LoRA step. Recompiles go from 34 to 20.

Tests

  • A100. The 15 LoRA / FP8 test files: 835 passed, 78 skipped, 0 failed.
  • CPU. The same files on torch 2.13, 2.11 and 2.6: 403, 391 and 382 passed, 0 failed.

Not covered

  • L4, H100 and B200 timing. The earlier L4 measurement of the regression was not repeated here.
  • AMD and T4 timing. Neither showed the regression: T4 runs fp16, which the fused order does not take, and the AMD step is GPU bound.
  • DPO and GRPO timing. They use the same LoRA autograd functions.
  • The MLP path on CPU, since SwiGLU is Triton only. It is covered by the GPU runs above.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 31, 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-10-31T11:50:30.277044Z 5286b62 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 added a commit to Etherll/unsloth that referenced this pull request Oct 31, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 5286b62648

ℹ️ 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 d6ab4f6 into main Oct 31, 2026
@danielhanchen
danielhanchen deleted the lora-fused-host-overhead branch October 31, 2026 12:10
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