Repository navigation
Fused LoRA: fall back to PEFT for variants, disabled or merged adapters, trainable bases and autocast dtype mismatches - #14806
Merged
Conversation
… trainable bases and autocast dtype mismatches The fused LoRA Functions only compute plain LoRA over a frozen base in the input dtype. A PEFT LoRA variant trained as plain LoRA, a disabled or merged adapter crashed in backward, a trainable base weight lost its gradient, and an autocast dtype other than the input's raised a dtype mismatch. Route all of these to PEFT's own forward; eligible configurations keep the same fused call.
… needed The DPO / GRPO reference forward runs with adapters disabled under no_grad every step; the fused base matmul is correct there and matches PEFT's base_layer bitwise. Fall back to PEFT only when grad is enabled and the input or base weight requires grad. Inline the variant / trainable-base checks in the plain-LoRA shortcut, skip the autocast dtype query when no autocast is on, and name LoRA variants in the patch summary.
Member
Author
|
@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. |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. 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". |
Member
Author
|
pre-commit.ci run |
danielhanchen
added a commit
that referenced
this pull request
Oct 22, 2026
danielhanchen
added a commit
that referenced
this pull request
Oct 22, 2026
pre-commit.ci fails `ruff-format-with-kwargs` on every open PR (#14806, #14805, #14803, #14790, #14785, #14781, #14780, #14777, #14776, #14775, #14774, #14773, #14772, #14770, #14769, #14768, and others): "files were modified by this hook". pre-commit.ci runs the hooks on all files, and 20 files on main (26 by the time this lands) are not in the formatter's output, so every PR is reformatted outside its own diff. Where pre-commit.ci can push, its autofix commit drags those unrelated files into the PR (for example #14790's touches `tests/test_block_compile.py`, `tests/test_bnb_integration_compile.py` and `unsloth/models/_utils.py`). The drift came in with PRs merged today, among them #14702, #14751, #14759, #14784, #14786, #14787, #14788, #14791 and the FP8 block decode, GRPO routing replay, 4-bit decode, compiled blocks and MoE LoRA target changes. ### Change `python scripts/run_ruff_format.py` (the hook's own entry point, `ruff==0.6.9` as pinned in `.pre-commit-config.yaml`) applied to those 20 files, plus 6 more from #14739, #14779 and #14806, which merged while this was open. Formatting only. ### Verification - Every changed file parses to an identical AST before and after. - A second run of the hook over the same files changes nothing (fixed point), and over all tracked Python files on this branch it changes nothing. - `ruff check` passes on the changed files; `tests/test_formatter_fixed_point.py` passes (16). Open PRs pick this up on their next pre-commit.ci run after merging main.
danielhanchen
added a commit
that referenced
this pull request
Oct 25, 2026
… input needs no grad main (#14806) already falls back to PEFT for disabled or merged adapters when X or the base weight requires grad. What it misses: X without grad (frozen embeddings, no input-grad hook) and some projections merged while the block's other adapters train. Add that check and a regression test; drop the PR's tests that #14806's suite now covers. [skip ci]
danielhanchen
added a commit
to LeoBorcherding/unsloth
that referenced
this pull request
Oct 25, 2026
…o grad (unslothai#14748) ## Problem unslothai#14806 (merged) made the fused LoRA routing fall back to PEFT for a disabled or merged projection when a gradient is needed. It decides "gradient needed" from the input and the projection's base weight: `X.requires_grad or base_weight.requires_grad`. One case gets past that check. X does not require grad (frozen embeddings with no input-grad hook, for example layer 0 without gradient checkpointing), some projections in the block are merged, and the block's other adapters still train. The block then runs fused, autograd calls the fused backward for the trainable adapters, and the backward reads the missing LoRA weights of the merged projection: ``` AttributeError: 'NoneType' object has no attribute 'to' ``` Reproduced on current main with real PEFT layers on CPU (QKV path, `lora_dropout` 0.0 and 0.3, `k_proj` merged). ## Change The disabled / merged branch of `_lora_dropout_and_bias` also hands the block to PEFT when grad mode is on and any active adapter in the block has a trainable LoRA weight (`_active_lora_requires_grad`). The extra check only runs on that branch, after grad mode, the input and the base weight have been checked, so: - Fully disabled or fully merged blocks route exactly as before (no active adapter). - No-grad and inference-mode reference forwards (DPO / GRPO) keep the fused base matmul. - Eligible plain LoRA configs never reach the branch. This PR originally carried the whole disabled / merged fallback. unslothai#14806 landed the same fix first, so this PR was merged with main and cut down to the remaining case. Its earlier tests are covered by `tests/test_fused_lora_peft_fallbacks.py` and were dropped. ## Tests - New `test_partially_merged_block_with_frozen_input_matches_peft` in `tests/test_fused_lora_peft_fallbacks.py` (qkv on CPU, qkv and mlp on CUDA; dropout 0.0 / 0.3). It checks that no fused Function runs, and that outputs and every LoRA grad match PEFT bitwise. It fails on main and passes here. - `tests/test_fused_lora_peft_fallbacks.py` + `tests/test_fused_lora_dropout_bias.py` on CPU: 104 passed, 60 skipped (CUDA only). - `test_routing_compiles_without_graph_breaks`, plus a partially merged block under `torch.compile(fullgraph=True)` with backward: both pass on CPU (torch 2.13).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The fused LoRA kernels (apply_lora_qkv, apply_lora_o, apply_lora_mlp_*) only compute plain LoRA over a
frozen base weight in the input dtype. Routing let four other configurations through, and #13898 widened
their reach to lora_dropout > 0 and lora_bias configs:
lora_variant, so a variant adapter (KaSA, MoNTeCLoRA,VeLoRA, BD-LoRA, ...) trained as plain LoRA with no error.
'NoneType' object has no attribute 'to'whenever the input required grad.unfrozen after attaching LoRA silently got no gradient.
under fp16). The matmuls return the autocast dtype, but the in-place LoRA accumulation does not
autocast, so the forward raised
self and mat2 must have the same dtype.All four reproduce on main and, for the default dropout = 0 / no bias config, on the commit before #13898.
Change:
_lora_dropout_and_biasnow returns None (PEFT's own forward) for variants, trainable baseweights, and autocast in a dtype other than the input's. Disabled or merged adapters fall back only when
a gradient is needed (grad enabled and the input or base weight requires grad). The no-grad DPO / GRPO
reference forward stays on the fused base matmul, bitwise equal to PEFT's base_layer, including on a
4-bit NF4 base. The added checks are attribute reads and mode queries: no tensor ops, no syncs, and they
compile with fullgraph=True. The patch summary names a LoRA variant adapter. Eligible configurations take
the same branches and make the same fused calls with the same arguments as before.
Tests: tests/test_fused_lora_peft_fallbacks.py checks that no fused Function runs for each case and that
outputs, input grads and every parameter grad match PEFT bitwise (float64 on CPU, float32 on CUDA). It
covers o, qkv and mlp, dropout 0 / 0.3, and lora_bias on / off, and includes a real PEFT KaSA adapter.
On main 110 of these fail; with this change 181 pass and 10 skip (merging a lora_B bias, which PEFT
refuses). Guards confirm that eligible configs and no-grad disabled / merged adapters still fuse. The
neighbouring fused LoRA, QLoRA, FSDP and QAT suites go from 366 passed on main to 547 passed with the new
file, with no new failures.