Repository navigation
Dequantize FP8 weights in the activation dtype for fp16 LoRA - #14495
Merged
Merged
Conversation
fast_dequantize dequantized float8_e4m3fn weights with weight_dequant's bf16 default, so every fp16 LoRA backward (LoRA_MLP, LoRA_QKV, LoRA_W) and the dequant fallbacks in matmul_lora / fast_linear_forward / fast_gemv / bnb_override failed with Half != BFloat16 on block-FP8 checkpoints. fast_dequantize takes an optional dtype used only by the FP8 branch (bf16 when unset), and callers pass the activation or grad dtype, so the weight is rounded once into the compute dtype. bf16 stays bit-identical; NF4, NVFP4, int4, packed and 16-bit paths are unchanged. save.py _merge_lora: an FP8 quant_state is the fp32 scale, so the merged weight came out float32 after a bf16 round trip. Dequantize FP8 exactly in fp32 and save in the model dtype.
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. |
Member
Author
|
@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". |
danielhanchen
added a commit
that referenced
this pull request
Oct 22, 2026
…output (#14677) #14495 made fast_dequantize follow the activation dtype, so the fp16 LoRA backward now asks weight_dequant for fp16. The per-tensor and rowwise branches cast the fp32 scale to the output dtype before multiplying. In fp16 a scale below 6.1e-5 becomes subnormal (zero below about 6e-8) and one above 65504 becomes inf, and the product is rounded a second time. Representable weights and the base-weight part of dX can therefore come out as 0 or inf. Example, fp8 value 448 with scale 1e-8 (per-tensor, rowwise and transposed rowwise): - before: 0.0 - after: 4.4703e-06 (the fp64 product rounded to fp16) On a rowwise matrix with row amax 0.001, the relative error vs fp64 drops from 1.18e-2 to 2.58e-4 (bf16 gives 2.5e-3). Change: non-bf16 dtypes multiply in fp32 and round once, as the block-scaled path already does. bf16 keeps its previous expression and fp32 is unchanged bit for bit. Tests: tests/test_fp8_fp16_scale_rounding.py (CPU) checks fp16 against the fp64 oracle for per-tensor, rowwise and transposed layouts, and checks that bf16 and fp32 output is byte-identical to the previous expression. 10 of its 16 cases fail before this change. The existing FP8 tests pass on torch 2.13 and 2.14. Under torch.compile(fullgraph=True), weight_dequant still traces to one graph with no breaks. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.
fp16 LoRA training on FP8 checkpoints crashes at the first backward:
All three
fast_dequantizevariants inunsloth/kernels/utils.pyreturnweight_dequant(W, quant_state)forfloat8_e4m3fnweights with no dtype, so the weight always comes back in bf16. The fp16 forward is unaffected, since it goes throughfp8_linear, which uses the input dtype. The backward passes ofLoRA_MLP,LoRA_QKVandLoRA_W, and the dequantize fallbacks, then mix fp16 grads with a bf16 weight.Change
fast_dequantize(all variants) takes an optionaldtype. Only the FP8 branch uses it:weight_dequant(W, s, dtype or torch.bfloat16), one rounding straight into the compute dtype. NF4, NVFP4, int4, packed and 16-bit paths are unchanged.fast_linear_forward,matmul_loraand thefast_gemvfallbacks passX.dtype.fast_lora.pypass the grad's dtype.bnb_override.pypasses it too (no effect on its NF4 path).save.py_merge_lora: an FP8 quant state is the fp32 scale. Main merged through a bf16 dequant and returned a float32 weight (4.8e-4 error vs fp32). It now dequantizes exactly in fp32 and returns the model dtype (torch_dtypefromunsloth_save_model, else bf16). Non-FP8 merges are unchanged.Validation
New
tests/test_fp8_fp16_lora_dtype.py(10 GPU tests):fast_dequantizereturns fp16 / bf16 / fp32 in one rounding, including transposed views, and the bf16 default is unchanged.LoRA_W(against fp32),LoRA_MLPandLoRA_QKV.test_fp8_pre_sm89_*+test_fp8_down_proj_bf16_backward, Colab T4 (pre-sm89 byte decoder)test_lora_*/ grouped / QAT / modelopt suites, B200test_fp8_pre_sm89_byte_dequant::test_under_torch_compile(T4 cannot compile bf16).test_grouped_linear_lora::test_dense_lora_fails_on_the_grouped_linear, 4 intest_modelopt_fp8_loading).torch.compileofLoRA_Won an FP8 weight: one graph, 0 graph breaks on both sides; fp16 forward + backward crashed on main and runs here.End to end,
Qwen/Qwen3-0.6B-FP8(real FP8 layers), LoRA r 16, batch 2 x 2, 20 steps, B200:Not covered:
verify_fp8_support_if_applicable), so only the kernel path is tested, through the byte decoder on T4.unsloth_save_modelmerge (only_merge_loraon one layer).