Repository navigation
Run bitsandbytes NF4 Linear4bit through Unsloth's NF4 kernels - #12739
Conversation
FastModel and zoo's compiled PEFT LoRA forwards call self.base_layer(x), which lands in bnb.matmul_4bit. unsloth/kernels/bnb_override.py replaces Linear4bit.forward for NF4 weights (uint8 storage, K % blocksize == 0, CUDA, compute dtype == quant state dtype) with the Triton dequantize + F.linear, the Triton GEMV for a single decode row, and an autograd Function that recomputes the dequantized weight for dX. Everything else runs the forward installed before it (zoo's patched one), including fp32 below 8 rows. - dX is bit for bit bitsandbytes'; the forward equals bitsandbytes wherever bitsandbytes dequantizes + F.linear itself. - bitsandbytes >= 0.50 picks a fused 4-bit GEMM per GPU and shape for 2..1536 rows. The override asks that same heuristic and leaves those calls to bitsandbytes (dequantize + cuBLAS was 0.56x at 4 rows on B200), taking only the shapes where bitsandbytes would dequantize, plus the single decode row. Traced code leaves the whole 2..1536 range to bitsandbytes; eager answers are cached per layer and element count. - Traced from torch 2.11 (bf16 only when every GPU is sm80+). Below that, compiled code keeps bitsandbytes' forward when it traces (>= 0.46), else one opaque call (fewer breaks than 0.45.5's ctypes calls). - On by default for every CUDA GPU sm75+ with bitsandbytes < 0.50; with 0.50+ only when every GPU is sm100 or sm120. On A100 and T4 with 0.50, batched decode (2-8 rows, all left to bitsandbytes) still ran 2-3% slower end to end with the override installed, so those stay opt-in. Off on ROCm. UNSLOTH_BNB_NF4_LINEAR=1/0 forces it; UNSLOTH_BNB_TRITON=0 keeps the global kill switch. - Installed at the end of FastLlamaModel / FastBaseModel from_pretrained. nf4_gemv: specialize the weight sizes before the cached config lookup. Under torch.compile(dynamic=True) they arrive as SymInts, and the unhashable SymInt made Dynamo skip fast_gemv and break on its eager helpers.
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 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 |
|
Codex Review: Didn't find any major issues. 👍 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
FastModel and zoo's compiled PEFT LoRA forwards reach bitsandbytes NF4 layers through
self.base_layer(x), which goes tobnb.matmul_4bitand never uses Unsloth's NF4 kernels. This PR replacesbitsandbytes.nn.Linear4bit.forwardfor NF4 weights so those calls use the Triton dequantize, plus the Triton GEMV for a single decode row. Training losses are bit-identical to bitsandbytes on every GPU tested. With bitsandbytes 0.45.5 and per-layer compile, training is 0.58x the time and decode is 2.19x faster on B200.Why
The FastLlamaModel fast path already calls
fast_dequantize/fast_gemvitself. The generic path (FastModel, Gemma 3, vision models, zoo's compiled LoRA modules) only calls the bitsandbytes module. Before bitsandbytes 0.50, that means bitsandbytes' dequantize +F.linearon every call, and with 0.45.5 several ctypes graph breaks per layer undertorch.compile.What changed
New
unsloth/kernels/bnb_override.py, installed at the end ofFastLlamaModel.from_pretrainedandFastBaseModel.from_pretrained:K % blocksize == 0, CUDA, compute dtype equal to the quant state dtype) run the Triton dequantize +F.linear. A single decode row runsfast_gemv.F.linearanyway (same bits, faster dequantize), plus the single decode row. Eager answers are cached per layer and element count. Traced code leaves the whole 2..1536 range to bitsandbytes.nf4_gemv: the weight sizes are specialized before the cached config lookup. Undertorch.compile(dynamic=True)they arrived as SymInts, and Dynamo skippedfast_gemvand broke on its eager helpers.Default:
UNSLOTH_BNB_NF4_LINEAR=1is worth setting for batch-1 serving.UNSLOTH_BNB_NF4_LINEAR=1/0forces it on or off.UNSLOTH_BNB_TRITON=0still turns off every Unsloth NF4 kernel.Results
gemma-3-1b-it NF4, LoRA r16, FastModel defaults (compiled LoRA modules). "Train" is step time PR / base, so lower is faster. "Decode" is tok/s PR / base, so higher is faster. Every arm has an A/A base repeat. Loss is bit-identical in every row, and the teacher-forced NLL deltas (32 prompts, full prefill and token-by-token) all have a 95% CI spanning 0.
Training and batch-1 decode, override forced on:
Batched decode on bitsandbytes 0.50.2, torch 2.11, one fresh process per measurement in base, pr, pr, base, base, pr order, tok/s PR / base (base spread across its 3 processes in brackets):
That table is why the 0.50+ default is limited to sm100 / sm120.
Every NF4 layer of the model called once at M rows (no_grad), speed PR / base, A/A within 1%:
If dequantize + cuBLAS is forced on the 2..8 row calls that bitsandbytes 0.50 fuses, they run at 0.57-0.77x. That is why those calls are left to bitsandbytes.
Tests
New
tests/test_bnb_nf4_override.py:addmmintomm+ a fused bias add (unfuse_bias_add_to_pointwise, fp32 only). cuBLAS rounds that differently from the bias-epilogueaddmmon A100, T4 and RTX PRO 6000. Plain PyTorchF.linearwith no bitsandbytes at all drifts the same way, and TF32 has no effect on it. So the fp32 case is checked bit-exact against the sameF.linearcompiled.dynamic=Truedecode through a module: 0 graph breaks.Results: