Skip to content

Dequantize FP8 weights in the activation dtype for fp16 LoRA - #14495

Merged
danielhanchen merged 2 commits into
mainfrom
fp8-fp16-lora-dtype
Oct 21, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fp8-fp16-lora-dtype

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

fp16 LoRA training on FP8 checkpoints crashes at the first backward:

fast_lora.py:486  dX = torch.matmul(df, upW.t(), ...)
RuntimeError: expected mat1 and mat2 to have the same dtype, but got: c10::Half != c10::BFloat16

All three fast_dequantize variants in unsloth/kernels/utils.py return weight_dequant(W, quant_state) for float8_e4m3fn weights with no dtype, so the weight always comes back in bf16. The fp16 forward is unaffected, since it goes through fp8_linear, which uses the input dtype. The backward passes of LoRA_MLP, LoRA_QKV and LoRA_W, and the dequantize fallbacks, then mix fp16 grads with a bf16 weight.

Change

  • fast_dequantize (all variants) takes an optional dtype. 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.
  • Call sites:
    • fast_linear_forward, matmul_lora and the fast_gemv fallbacks pass X.dtype.
    • The 7 backward sites in fast_lora.py pass the grad's dtype.
    • bnb_override.py passes 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_dtype from unsloth_save_model, else bf16). Non-FP8 merges are unchanged.

Validation

New tests/test_fp8_fp16_lora_dtype.py (10 GPU tests):

  • fast_dequantize returns fp16 / bf16 / fp32 in one rounding, including transposed views, and the bf16 default is unchanged.
  • fp16 forward + backward through LoRA_W (against fp32), LoRA_MLP and LoRA_QKV.
  • bf16 outputs and grads of all three kernels are bit-identical to the old path.
run main this PR
new test file, B200 7 failed, 3 passed (the bf16 identity guards) 10 passed
new file + test_fp8_pre_sm89_* + test_fp8_down_proj_bf16_backward, Colab T4 (pre-sm89 byte decoder) 8 failed, 135 passed 1 failed, 142 passed
FP8 / fused LoRA / test_lora_* / grouped / QAT / modelopt suites, B200 6 failed, 617 passed 5 failed, 619 passed
  • The one T4 failure on both sides is test_fp8_pre_sm89_byte_dequant::test_under_torch_compile (T4 cannot compile bf16).
  • The 5 B200 failures are the same on both sides (test_grouped_linear_lora::test_dense_lora_fails_on_the_grouped_linear, 4 in test_modelopt_fp8_loading).
  • torch.compile of LoRA_W on 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:

  • fp16: main crashes at the first backward; this PR trains, loss 3.385 -> 0.672, and generates normally afterwards.
  • bf16: the 20 logged losses are identical between main and this PR (3.3712 -> 0.6895).

Not covered:

  • sm89 (L4) end to end: Colab had no L4 capacity; B200 (sm100) stands in.
  • Pre-sm89 end to end: the loader refuses FP8 checkpoints there (verify_fp8_support_if_applicable), so only the kernel path is tested, through the byte decoder on T4.
  • Paths not run: AMD, XPU (same change, not run), MoE grouped FP8, a full unsloth_save_model merge (only _merge_lora on one layer).

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T11:58:58.730629Z c9d995b Manual request
🔒 Security Review 🔄 Running since 2026-10-21T11:16:16.303661Z 3067472 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: c9d995bc64

ℹ️ 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".

@danielhanchen
danielhanchen merged commit b2b9852 into main Oct 21, 2026
@danielhanchen
danielhanchen deleted the fp8-fp16-lora-dtype branch October 21, 2026 12:39
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant