Repository navigation
LoRA decode: return the caller's out tensor so batched decode keeps one mm per projection - #16588
Conversation
…wed [bsz, 1, N] stopped torch.matmul folding: batched bmm over an expanded weight)
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…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]
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…ut-strides # Conflicts: # tests/test_lora_fused_passes.py
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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 throughout.view(bsz, out_dim)and then returned a newout.view(bsz, 1, out_dim). For a strided decode buffer (the MLP writesdown_projintotemp_up[..., :hd]), that re-made view has strideout_dimon 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 latertorch.matmulon 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'soutitself, 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:
out.view(out_dim)), and so is decode without LoRA.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 givesaten::bmm+aten::expandformatmul,matmul(out=)andF.linearon the main layout and oneaten::mmwith the fix; torch 2.14 gives oneaten::mmfor 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:
generate(torch 2.11 eager, bsz 64): main 2899aten::bmm+ 5862aten::expand, this branch 1aten::bmm.tests/test_lora_decode_out_strides.pyon 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
tests/test_lora_decode_out_strides.py(CUDA): at batch 1 / 2 / 8 / 33, 16-bit and 4-bit,fast_linear_forwardreturns 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 stridedout, plain and subclass operands (main 4 failed, this branch 4 passed).Not covered