Skip to content

LoRA decode: return the caller's out tensor so batched decode keeps one mm per projection - #16588

Merged
danielhanchen merged 7 commits into
mainfrom
unsloth-lora-decode-out-strides
Nov 12, 2026
Merged

danielhanchen merged 7 commits into
mainfrom
unsloth-lora-decode-out-strides

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Summary

Batched LoRA / QLoRA decode was 1.6x to 6.5x slower than it should be. With a LoRA adapter, fast_linear_forward (unsloth/kernels/utils.py) applied the update through out.view(bsz, out_dim) and then returned a new out.view(bsz, 1, out_dim). For a strided decode buffer (the MLP writes down_proj into temp_up[..., :hd]), that re-made view has stride out_dim on its size-1 dim, which differs from the buffer's row stride. The next layer's norm writes into that tensor in place, so every later torch.matmul on it (the next projections, the LM head) could not fold to a single mm and ran a batched bmm over the weight expanded to the batch.

The fix applies the same in-place addmv_ / addmm_ through a temporary view and returns the caller's out itself, strides intact. The math is unchanged: same ops into the same memory.

Evidence

Decode step time at batch 64, base = main, head = main + this fix alone (every other decode lever off), same VM, interleaved, with a second base arm as the A/A check:

GPU case base ms head ms speedup
A100 Llama 1B NF4 + LoRA, eager 59.35 36.63 1.62x
A100 Llama 1B NF4 + LoRA, CUDA graphs 33.08 7.46 4.43x
A100 Llama 8B NF4 + LoRA, CUDA graphs 108.16 32.92 3.29x
RTX PRO 6000 (sm120) Llama 1B NF4 + LoRA, CUDA graphs 28.05 4.30 6.52x
RTX PRO 6000 (sm120) Llama 8B NF4 + LoRA, eager 86.40 21.02 4.11x
T4 Llama 8B NF4 + LoRA, eager 1844.98 306.30 6.02x
  • Batch 1 is unchanged (that path already wrote through out.view(out_dim)), and so is decode without LoRA.
  • With the fix, batched LoRA decode costs about what decode without LoRA costs (A100 CUDA graphs, batch 64: 7.46 vs 6.22 ms).
  • Under CUDA graphs the logits are bitwise equal to eager.

Which torch versions hit it

The slow path comes from torch.matmul's fold check on the size-1 dim stride, which torch 2.11 still applies and torch 2.14 no longer does. CPU probe, [64, 1, 2048] input re-viewed as on main vs the caller's buffer: torch 2.11 gives aten::bmm + aten::expand for matmul, matmul(out=) and F.linear on the main layout and one aten::mm with the fix; torch 2.14 gives one aten::mm for both.

Colab L4, Llama-3.2-1B shape (random init) + LoRA r=16 on all projections, 64 new tokens, ms per token, median of 5 after warmup, base and head interleaved (B H H B) on one card, fresh process each:

torch mode load bsz base head
2.11 eager 16-bit 64 164.9 31.2
2.11 eager NF4 64 169.7 36.6
2.11 eager 16-bit 8 41.9 27.0
2.11 CUDA graphs 16-bit 64 162.7 19.1
2.11 CUDA graphs NF4 64 164.3 20.0
2.11 CUDA graphs NF4 8 31.3 14.0
2.11 any any 1 equal equal
2.14 eager / CUDA graphs 16-bit / NF4 1 / 8 / 64 within 2% within 2%
  • Op census per generate (torch 2.11 eager, bsz 64): main 2899 aten::bmm + 5862 aten::expand, this branch 1 aten::bmm.
  • Peak memory is identical in every cell.
  • Greedy tokens: on torch 2.11 this branch matches the torch 2.14 output in every row; main diverges from step 1 in most rows (the expanded bmm rounds differently). On torch 2.14 both arms are token-identical.
  • tests/test_lora_decode_out_strides.py on L4, torch 2.11: main 8 failed, this branch 8 passed.
  • torch.compile(fullgraph = True) over the LoRA decode branch: no graph break at batch 1 on either side.

Tests

  • New tests/test_lora_decode_out_strides.py (CUDA): at batch 1 / 2 / 8 / 33, 16-bit and 4-bit, fast_linear_forward returns the caller's tensor with its strides, and the output matches the PEFT forward. On a Colab T4 (torch from PyPI unsloth, bitsandbytes 0.50.2, peft 0.21.2), each arm importing its own tree: main 8 failed, this branch 8 passed.
  • tests/test_lora_fused_passes.py: CPU case for the strided out, plain and subclass operands (main 4 failed, this branch 4 passed).

Not covered

  • No H100, ROCm, B200 or Windows run. The change is two lines of tensor-view bookkeeping with no arch-specific code.

@danielhanchen danielhanchen self-assigned this Nov 3, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 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-12T15:03:06.392965Z 812ee11 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

1 similar comment
@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: bb42210eb2

ℹ️ 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 added a commit that referenced this pull request Nov 3, 2026
…hecks (#16594)

## Summary

Two context parallel packing tests in `tests/test_parallel_env.py`
depend on whichever test ran before them in the same xdist worker. On
staging CI for #16588, a LoRA decode PR that does not touch this code,
`Repo tests (CPU, rest)` failed with:

```
tests/test_parallel_env.py::test_auto_keeps_packing_and_ring_turns_it_off
E   AssertionError: auto (Ulysses) must keep packing on
WARNING  unsloth.trainer:trainer.py:2030 Unsloth: packing=True ignored (UNSLOTH_RETURN_LOGITS=1).
tests/test_parallel_env.py::test_cp_x_tp_never_turns_padding_free_on_automatically
```

`UNSLOTH_RETURN_LOGITS=1` turns packing off by design, so the trainer
behaved correctly for the environment it saw. The tests never stated
that they assume the variable is unset. An earlier test in the worker
left it at `1`, and both cases then checked a run where packing had
already been switched off. The same thing reproduces on main:

| `tests/test_parallel_env.py` on main | result |
|---|---|
| clean environment | 131 passed |
| `UNSLOTH_RETURN_LOGITS=1` | 2 failed, 129 passed (the two cases above)
|

## Change

`tests/test_parallel_env.py` only:
- `_auto_packing`, the helper behind the auto-packing cases, now runs
`monkeypatch.delenv("UNSLOTH_RETURN_LOGITS")`.
- `UNSLOTH_RETURN_LOGITS` joins `_ENV`, so `clean_env` also clears it.

`monkeypatch` restores any value that was there before. No product code
changes.

## Testing

`tests/test_parallel_env.py` on this branch: 131 passed with a clean
environment, and 131 passed with `UNSLOTH_RETURN_LOGITS=1` exported.
The in-place addmv_ / addmm_ into out's 1-D / 2-D view returns the adapter's
tensor subclass (#16249), but discarding that return handed back a plain
tensor. Re-tag out itself with as_subclass, which keeps its strides.

[skip ci]
@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: eaa2e24ac7

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

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@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: 812ee11659

ℹ️ 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 562d802 into main Nov 12, 2026
@danielhanchen
danielhanchen deleted the unsloth-lora-decode-out-strides branch November 12, 2026 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant