Repository navigation
Run the dynamic-rank LoRA compile test on hosts without Triton - #16256
Conversation
The fresh interpreter takes the CPU path of import unsloth, which never loads the kernels, so its direct unsloth.kernels import needed a real Triton and failed on the Windows CPU runner. Define the kernels against the unsloth_zoo stub there, as the GPU path does, then drop the stub before torch.compile runs.
|
@codex review |
Codex Review SummaryThis 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. |
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
1 similar comment
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
## Summary A follow-up to #16071 (fused LoRA order: the rank-r term goes into a fresh buffer and the base GEMM accumulates onto it). **Tensor subclass types survive the fused LoRA accumulates.** The LoRA kernels update their output with in-place `addmm_` / `addmv_` / `add_` and drop the return value. For a storage-backed subclass operand (`X`, `W`, `A`, `B` or a precomputed `XA`), the in-place method returns the subclass type, but the kernels returned the plain buffer, so the result came back as a plain `torch.Tensor`. With this PR every accumulate keeps what the in-place call returns. Routing, dtypes, autocast behaviour and arithmetic are exactly as on main: the same ops run in the same order on the same buffers. - **`matmul_lora`** (`unsloth/kernels/utils.py`): the fused order (`_lora_first_addmm`, and the caller-buffer branch) and the base-then-LoRA accumulate. - **Decode** (`fast_linear_forward`): the batch-one `addmv_` and the batched `addmm_`. - **Dropout and `lora_bias`** (`_matmul_lora_ext` in `unsloth/kernels/fast_lora.py`): the LoRA accumulate and the bias accumulate. Plain tensors still return the same `out` object as before. - **Traced no-grad accumulate** (`addmm_`): the opaque `unsloth::addmm_` op returns nothing, so a subclass operand takes `out.addmm_`, which is the op's own body. Plain, fake and functional tensors still use the op. - **Not changed:** the compiled `unsloth::lora_first_addmm` op. Its functional alternative is autocast while the op is not, so swapping it would change values under autocast. **Earlier revisions of this PR** sent subclass operands to the base-then-LoRA order and then adjusted autocast and caller-buffer handling around that. Each of those changed behaviour relative to main in some dtype or buffer combination, so this revision drops them and keeps main's routing. The small-CPU shared-product gate from the first revision stays dropped: no single row cutoff was faster across dtype, width, rank and threads. ## Results **Values are main's, bit for bit.** In a CPU script over 28 subclass cases (each of `X`, `W`, `A`, `B`, `XA` with the fused order on and off, a caller buffer, the autocast cases with a precomputed `XA`, and the dropout and bias routes), every output has the same dtype and bits as main. Under bf16 / fp16 autocast with fp32 operands and a precomputed `XA`, both give fp32 10000; with half `X` / `XA` over an fp32 base, both give the autocast dtype. **Compile:** `torch.compile(fullgraph = True)` over `addmm_` (no grad) and `matmul_lora` has 0 graph breaks. **CUDA,** Colab A100 40GB, base = main at d8efd15: - **Tests:** the 15 LoRA / FP8 test files at this head give 831 passed, 78 skipped, 0 failed. - **Step time:** Llama-3.2-1B bf16 LoRA r16 SFT step (batch 2 x 512, compiled), median step time, each arm in a fresh process, head / base / base / head order twice: | arm | step ms | median | spread | |---|---|---|---| | main | 61.99 / 61.81 / 60.37 / 62.84 | 61.90 | 4.0% | | this PR | 61.23 / 62.04 / 63.76 / 60.95 | 61.63 | 4.6% | The difference is inside each arm's own spread. Both arms have 0 graph breaks and 4 graphs, and per-step losses differ between base runs as much as between base and head. ## Tests New in `tests/test_lora_fused_passes.py`. Each compares against plain operands bit for bit and checks that the result is the subclass: - `test_a_subclass_operand_keeps_its_type_through_matmul_lora[X|W|A|B|XA]`, fused order on and off; - `test_a_caller_buffer_keeps_its_storage_and_the_subclass[X|A|out]`, fused order on and off, written into the caller's storage; - `test_autocast_with_a_precomputed_xa_keeps_fp32_operands_finite`, bf16 and fp16: fp32 10000; - `test_autocast_with_a_half_xa_over_an_fp32_base_autocasts_the_base`, bf16 and fp16: 8 in the autocast dtype; - `test_the_dropout_and_bias_route_keeps_a_subclass[X_lora|A|B]`, with and without a `lora_bias`; - `test_decode_keeps_a_subclass_adapter[A|B]`, batch 1 and 3; - `test_the_traced_no_grad_accumulate_keeps_a_subclass_result[A|B]`, and `test_the_traced_no_grad_accumulate_keeps_the_op_for_plain_tensors`. With main's kernels, 35 of these cases fail, all on the result type (main's values match). The plain-operand cases pass on both. `tests/test_lora_first_addmm_op.py`: the two re-registration cases run `unsloth.kernels` in a fresh interpreter that the test stubs do not reach, so they skip when triton is not installed, as on the macOS and Windows CI runners. (Main's #16256 already makes the dynamic-rank case in `test_lora_fused_passes.py` run without triton.) **CPU,** the 15 LoRA / FP8 test files, 0 failed: | torch | passed | skipped | |---|---|---| | 2.13 | 399 | 317 | | 2.11 | 387 | 329 | | 2.6 | 378 | 338 | ## Not covered - ROCm and XPU. - Real third-party subclass libraries; the tests use a plain `torch.Tensor` subclass. - Gradients: backward accumulates into `dX` / `dA` / `dB` are unchanged.
tests/test_lora_fused_passes.py::test_dynamic_compile_with_a_rank_equal_to_the_first_length(added in #16071) fails on the Windows cross-platform leg, both parametrizations:Seen on the staging run for #16249, which only shows it because that PR touches this file; any PR that does will hit it.
Why
The rest of the file runs in the pytest process, where the conftest CUDA spoof sends
import unslothdown the GPU path, and that path defines the kernel modules against theunsloth_zootriton stub when Triton is not installed. The dynamic-rank case runs in a fresh interpreter withUNSLOTH_DISABLE_CPU_FALLBACK=0and no spoof, soimport unslothtakes the CPU path, which never loads the kernels. Its ownfrom unsloth.kernels import fast_lorathen needs a real Triton, which a Windows CPU runner does not have. The product is fine here: the CPU backend does not use these kernels. The test was relying on Triton being installed.Fix
When
find_spec("triton")is None, the case script importsfast_lorawith the stub injected, the same way_gpu_initdoes, then restoressys.meta_pathand drops the stub'striton*entries fromsys.modules, sotorch.compiledoes not see a fake Triton. With Triton installed, nothing changes.Verification
Triton hidden with a
PathFinderthat returns None fortriton*(same as the Windows runner:find_specis None,import tritonraisesModuleNotFoundError), torch 2.10 CPU:plain, no TritonModuleNotFoundError: No module named 'triton'OKparam, no TritonModuleNotFoundError: No module named 'triton'OK