Skip to content

fix(fp8): keep the in-session bias FP32 so it matches a reload (#4575) - #4613

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4575-fp8-bias-dtype
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4575-fp8-bias-dtype

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4575

Root cause

TorchFP8Linear.__init__ (the reload path) allocates the bias as FP32, and the loader keeps FP8 biases in keep_in_fp32_modules / fp32_bf16_buffer_names, so every reloaded model has an FP32 bias for both fp16 and bf16 loads. The pack path used by quantize() kept the source model dtype (bias = linear.bias.detach().to("cpu")). _forward_dequant_matmul then chooses its GEMM path from bias.dtype != x_flat.dtype: a model-dtype bias takes the narrow dequant + matmul_fp32_reduce path, an FP32 bias takes the full-FP32 dequant/matmul path. Same weights, different arithmetic, so in-session logits did not reproduce the checkpoint.

Fix

Both TorchFP8Linear.pack_original and pack_calibrated now cast the bias to FP32. This preserves the documented reload behaviour (FP32 bias buffers, full checkpoint bias range) and makes the in-session module the same module a reload builds. No forward-path branch was changed, so the range-safety bound from #4533/#4545 is untouched.

Before / after (tiny Qwen2, bf16, TorchFP8Linear, accelerator)

  • Before: in-session bias bf16, reload bias float32; in-session vs reloaded logits max |ΔL| = 0.00537109375 (mean 1.03e-3). Narrowing the reloaded biases to bf16 reproduces the in-session logits exactly (Δ = 0.0), confirming the dtype is the whole difference.
  • After: both sides FP32; in-session vs reloaded logits max |ΔL| = 0.0 (bitwise). The saved checkpoint now stores *.bias as float32, and narrowing only the reloaded bias to bf16 reintroduces Δ = 0.00537109375.

Tests

Thread caps on every run: OMP_NUM_THREADS=4 MKL_NUM_THREADS=4 OPENBLAS_NUM_THREADS=4 NUMEXPR_NUM_THREADS=4.

RTX 4090 (CUDA_VISIBLE_DEVICES=GPU-50a20676-a7a4-8bb3-eb3d-f191fdc9fa42), CUDA venv /opt/venvs/cu130t:

AMD R9700 (HIP_VISIBLE_DEVICES=3 only), ROCm venv /opt/venvs/rocm101t:

  • pytest tests/test_fp8_bias_dtype_4575.py tests/test_fp8_bias_range.py tests/test_portable_wide_epilogues_4471.py -q -> 29 passed, 2 skipped

The regression test includes an independent FP64 oracle for the packed module output (rtol=atol=2e-3); the in-session vs reload assertion is exact (rtol=0, atol=0).

Lint gate: ruff check gptqmodel docs/eora tests setup.py -> All checks passed!

Limitations

  • The end-to-end regression uses a synthetic 2-layer Qwen2 checkpoint (biased q/k/v projections), not Qwen2.5-0.5B; it exercises the same TorchFP8Linear pack/save/load path on an accelerator.
  • The CPU FP8 forward already takes the FP32 path regardless of bias dtype, so the logits half of the regression is only meaningful on an accelerator; the bias-dtype assertions fail on any device.

@Qubitium
Qubitium merged commit e4c558c into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/issue-4575-fp8-bias-dtype branch November 17, 2026 02:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant