Repository navigation
LoRA: opt-in rotated int8 frozen base for consumer Blackwell (UNSLOTH_LORA_BASE_INT8=1) - #19965
Conversation
…_LORA_BASE_INT8=1) The frozen base GEMMs of a LoRA linear (forward and input gradient) run as int8 on a block-Hadamard rotated weight with per-token int8 activations, through the fp8 LoRA plumbing (attach, master offload, merge, save) and Unsloth's fused LoRA kernels. On an RTX 5090 the int8 GEMM is 3x a bf16 GEMM and 1.5x fp8. Off by default; refused on sm75 / sm80 / sm89 / ROCm where the Triton int8 tiles were measured slower than bf16; models narrower than hidden size 2048 and calls under 2048 rows stay 16 bit.
… int8 base cannot run
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 |
|
Codex Review: Didn't find any major issues. Hooray! 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". |
…O 6000, bit-identical)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 15a466d4c4
ℹ️ 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".
| amax = tl.maximum(amax, tl.abs(x * c)) | ||
| rmax = tl.maximum(tl.max(amax, axis = 0), 1e-30) |
There was a problem hiding this comment.
Preserve NaNs in the activation quantization scales
When a row of dY contains a NaN alongside finite values, these reductions discard the NaN and produce a finite scale. The subsequent clamp and int8 cast cannot preserve NaNs either, so the input-gradient GEMM can return finite gradients where the corresponding dense GEMM would propagate NaNs. The rotated forward quantizer has the same issue. This silently masks numerical failures during training; track NaNs and mark the affected row scales as NaN, as the existing _fp8_rowwise_cast_kernel does.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in cff4eb9. Both quantizers now count NaNs while they reduce. These are the rotated forward quant, which counts them after the rotation, and the scaled dY quant. A row with a NaN gets a NaN scale, so that whole output row comes out NaN, as the 16-bit GEMM would give. New test test_nan_rows_propagate (bf16 and fp16) checks the NaN row and that every other row stays finite, for both the forward and the input gradient.
… NaN, as the 16-bit GEMM propagates it
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: cff4eb9aee
ℹ️ 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".
| if float8_training in (None, False) and not full_finetuning and int8_lora_requested(): | ||
| why = "the base is loaded quantized (load_in_4bit / 8bit / fp8)" if quantized_base else None | ||
| why = why or int8_lora_unavailable() |
There was a problem hiding this comment.
Preserve FP32 loads when enabling INT8 through the environment
On a supported GPU, setting UNSLOTH_LORA_BASE_INT8=1 and loading an unquantized model with dtype=torch.float32 now fails even when float8_training is unset. This helper checks the GPU and quantization flags but not the requested dtype, so both loaders inject "int8_lora"; _resolve_int8_lora then calls _check_precision_recipe, which rejects FP32 because the new recipe only accepts BF16/FP16. Pass the requested dtype into this preflight and ignore the environment opt-in for unsupported dtypes, preserving the documented non-failing fallback while retaining refusal for explicit INT8 requests.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in e87a04a. float8_training_request now takes the requested dtype, and both loaders pass it. With UNSLOTH_LORA_BASE_INT8=1, a float32 load gets a message and stays float32 rather than failing. An explicit float8_training = "int8_lora" still refuses fp32 with the registry's dtype reason. Tests: test_env_opt_in_on_fp32_load_is_ignored.
… int8_lora still refuses)
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e87a04a197
ℹ️ 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".
| if float8_training in (None, False) and not full_finetuning and int8_lora_requested(): | ||
| why = "the base is loaded quantized (load_in_4bit / 8bit / fp8)" if quantized_base else None | ||
| if why is None and dtype in (torch.float32, "float32", "fp32"): | ||
| why = "the int8 base needs bf16 / fp16 activations, the model loads in float32" | ||
| why = why or int8_lora_unavailable() |
There was a problem hiding this comment.
Skip the environment opt-in for QAT loads
On a supported GPU, loading a 16-bit base with qat_scheme="int8-int4" and UNSLOTH_LORA_BASE_INT8=1 fails even when float8_training is unset. Both loaders call this helper without passing qat_scheme, so it injects "int8_lora"; their subsequent resolve_fp8_lora call passes the QAT scheme to _resolve_int8_lora, which raises PrecisionRefused. Include QAT in this preflight and leave the original request unchanged for environment-only opt-ins, preserving the documented non-failing fallback while still rejecting an explicit INT8 request.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in a68680d. The env preflight now also takes qat_scheme (both loaders pass it), so a QAT load with UNSLOTH_LORA_BASE_INT8=1 prints a message and keeps its 16-bit base. I applied the same treatment to tensor / pipeline / expert parallel runs (UNSLOTH_TP_SIZE, UNSLOTH_EP_SIZE), which refuse a converted frozen base at trainer init. An explicit float8_training = "int8_lora" still refuses QAT. Tests: test_env_opt_in_on_qat_load_is_ignored, test_env_opt_in_under_tp_or_ep_is_ignored.
…pipeline / expert parallel runs
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a68680de2a
ℹ️ 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".
| _int8_request = float8_training_request( | ||
| kwargs.get("float8_training", None), | ||
| full_finetuning, | ||
| quantized_base = bool(load_in_4bit or load_in_8bit or load_in_fp8 not in (False, None)), |
There was a problem hiding this comment.
Resolve the INT8 opt-in after normalizing checkpoint precision
On a supported GPU, loading a Hub checkpoint ending in -bf16 with the default loading flags and UNSLOTH_LORA_BASE_INT8=1 silently skips INT8 conversion. This preflight sees the default load_in_4bit=True and discards the environment request, but the later bf16_repo_loads_16bit branch clears the quantization flags and loads a 16-bit base. Neither loader revisits the discarded request afterward, so an eligible 16-bit model trains without the requested optimization. Resolve the environment opt-in using the effective checkpoint precision in both loaders.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in f823346. The env opt-in is no longer pre-flighted from the load flags. Both loaders now decide it right before mark_fp8_lora, from the loaded model itself, via int8_lora_from_env. A '-bf16' checkpoint that loaded 16 bit despite the load_in_4bit default converts. A model whose Linear weights are quantized (bitsandbytes quant_state, fp8) or float32 is skipped with a message. Full finetuning, QAT and tensor / pipeline / expert parallel runs are also skipped. An explicit float8_training request is never overridden. Tests cover each case on CPU, and a B200 smoke through FastLanguageModel with the env set converted 196 / 196 Qwen3-1.7B layers.
…16' precision normalization), not on the load flags
|
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". |
…since #19965) (#20004) ## Problem `tests/test_triton_constexpr_comments.py::test_every_constexpr_line_has_a_comment` fails on main (`53a74eb64e`) in `Repo tests (CPU, rest)`: ``` tl.constexpr parameters without a comment: unsloth/kernels/int8_lora.py:213 _int8_mm_kernel.BLOCK_M unsloth/kernels/int8_lora.py:214 _int8_mm_kernel.BLOCK_N unsloth/kernels/int8_lora.py:215 _int8_mm_kernel.BLOCK_K unsloth/kernels/int8_lora.py:216 _int8_mm_kernel.GROUP_M unsloth/kernels/int8_lora.py:264 _int8_dgrad_kernel.HAS_D unsloth/kernels/int8_lora.py:265 _int8_dgrad_kernel.INV_SQRT_G unsloth/kernels/int8_lora.py:267 _int8_dgrad_kernel.BLOCK_M unsloth/kernels/int8_lora.py:269 _int8_dgrad_kernel.GROUP_M ``` #19965 added `unsloth/kernels/int8_lora.py`, and these eight parameters have no comment. It shows on any PR whose `rest` shard runs, for example the #19837 staging run. ## Fix Comments only, no code change. Each of the eight parameters gets a one-line comment saying what it is: - the tile sizes - the `K % BLOCK_K == 0` precondition that lets the K loop load unmasked - the swizzle group - the dgrad epilogue add - the `1 / sqrt(G)` normalisation ## Verification - `tests/test_triton_constexpr_comments.py`: 4 passed. Before, on main: 1 failed, listing the eight parameters above. - `ruff format` and `ruff check`: clean.
Summary
This adds an opt-in rotated INT8 frozen base for LoRA on consumer Blackwell (RTX 50, sm120). Enable it with
UNSLOTH_LORA_BASE_INT8=1(orfloat8_training = "int8_lora") on a 16-bit base. It changes nothing unless enabled.On a GeForce RTX 5090 the FP32-accumulate tensor paths run at half rate. The RTX Blackwell whitepaper lists bf16 at 209.5 TFLOPS, fp8 at 419 and INT8 at 838 TOPS. Measured on a 5090 under Windows (torch 2.14.1, triton-windows 3.8), summed over one Qwen3-8B layer's GEMMs at 8192 tokens:
_scaled_mm.How it works
W [out, in]is stored as int8 codes ofW @ H, whereHis a block-diagonal Walsh-Hadamard of order 128 along the input dim. Each output row gets one fp32 scale. That is 1 byte per weight, with the 16-bit master in host RAM as fp8 LoRA does.lora_first) in its epilogue.dY @ W = (dY * s) @ Q @ H.UNSLOTH_INT8_LORA_CACHE_T=1keeps the transposed copy instead.Int8LoRABaseStatesubclassesFp8LoRABaseState, so Unsloth's fused LoRA kernels callmatmul/lora_first/dgrad/dgrad_accumunchanged, and attach, master offload, merge and save all apply.float8_training = "int8_lora"refuses.UNSLOTH_INT8_LORA_MIN_HIDDEN,UNSLOTH_INT8_LORA_MIN_ROWS,UNSLOTH_INT8_LORA_SHARE_XQ=0.Accuracy
Setup:
FastLanguageModel, r=16 on all seven projections, alpaca-cleaned, bs 4 x 1024, 100 steps, lr 2e-4.Speed
GEMM level on an RTX PRO 6000 (sm120 workstation card, no half-rate FP32 accumulate, so int8 = fp8 = ~1.9x bf16 per GEMM). Forward twice (checkpoint recompute) plus input gradient, activation quantization included, over one decoder layer:
End to end inside Unsloth on the RTX PRO 6000 (
FastLanguageModel, bf16 base, r=16 on all projections, unsloth gradient checkpointing, bs 4 x 2048 = 8192 tokens per step, 25 steps). Each run is a fresh process with fresh compile caches, interleaved bf16, int8, fp8, fp8, int8, bf16, bf16. Values are median s/step:End to end on the RTX 5090: a device task is queued.
Tests
tests/test_int8_lora_cuda.py(GPU):F.linear.torch.compile(fullgraph = True)with 0 graph breaks and compiled == eager bit for bit.tests/test_int8_lora_recipe.py(CPU): registry entry, env opt-in resolution, refusals (4 / 8 bit / fp8 / QAT bases, sm75 / sm80 / sm89, no Triton), the 16-bit fallback of the env opt-in, eligibility, the hidden-size gate throughenable_fp8_lora.tests/test_precision_registry.py,tests/test_fp8_lora.pyandtests/test_float8_training.pypass unchanged.cuFuncSetAttribute"invalid argument" CUDA API calls from Triton's launcher, which a plain Triton int8 GEMM control with the same > 48 KB shared memory tiles also produces.Not covered