Skip to content

fix(fp8): add bias before low-precision output rounding - #3737

Merged
Qubitium merged 1 commit into
mainfrom
codex/fix-fp8-bias-cancellation
Nov 9, 2026
Merged

Qubitium merged 1 commit into
mainfrom
codex/fix-fp8-bias-cancellation

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fix the FP8 cancellation error left after #3726. TorchFP8Linear.forward narrowed an FP32 checkpoint bias to FP16/BF16 before addition, and the CPU fallback rounded the matmul before the bias could cancel it.

For low precision biased fallback calls that need wide arithmetic, _forward_dequant_matmul now decodes a temporary FP32 weight, multiplies in FP32 with autocast disabled, adds the bias in FP32, and casts the final result once. The native scaled matmul continues to add its bias to an FP32 output. The compiled CPU call passes the bias into the compiled function so it follows the same order. Packed FP8 weights and scale metadata remain the persistent representation.

Reproduction and oracle

With a packed E4M3 weight from -1000, FP32 bias 70000, and CPU FP16 input 70, main produced NaN; an independent Torch FP32 decode, matmul, and bias oracle produced 0.0078125. With BF16 input 68.5, main produced 1536 and the oracle produced 1504 (2.12% relative error). The new CPU tests cover both bias signs, FP16/BF16, ordinary and compiled-call paths, and CPU autocast. All 16 combinations now meet rtol=atol=2e-3; the measured visible outputs in the two positive-bias examples equal the oracle exactly.

The tests decode stored FP8 codes and inverse scales independently of TorchFP8Linear.dequantize_weight, compare the final output to the Torch oracle, and check that repeated forwards preserve packed bytes and release temporary dense weights. GPU fallback and native scaled-matmul oracle tests are included for target hardware.

Validation

  • python -m pytest tests/test_fp8_bias_range.py tests/kernels/test_fp8_kernel.py tests/test_fp8.py tests/test_gsq_method_adapters.py tests/test_model_dequant_fp8.py -q -o log_cli=false --tb=short: 107 passed, 32 skipped.
  • Before the fix, the new CPU oracle cases failed 4/4 across FP16/BF16 and both bias signs.
  • Real optimize(backend="eager") smoke on CPU returned 0.0078125 (FP16) and 1504 (BF16) with the compiled function still active.
  • Ruff on all changed Python files: passed. git diff --check: passed.
  • One-thread CPU microbenchmark, 16×128 activations and 128×128 packed weight, 20 warmups then median of five 100-forward batches: prior CPU expression 0.165 ms versus changed forward 0.088 ms for FP16; 0.087 ms versus 0.088 ms for BF16. The prior expression used the same module's low precision dequantization, matmul, and narrowed bias. The packed weight remains 16,384 bytes with 1,024 bytes of scale/bias metadata; the changed fallback's dense FP32 weight is a 65,536-byte temporary and is released after each forward.

Scope and limitations

This is a localized numerical correction inside the existing FP8 forward flow, so the repository's logic-flow-diagram skill does not require a diagram. No public or hf_ API changes or compatibility shims were added. This host has no available CUDA/ROCm device, so the four GPU oracle cases were skipped; free-threaded concurrent execution was not measured here. The PR is intentionally left unmerged for review.

@Qubitium
Qubitium merged commit 671ba82 into main Nov 9, 2026
6 checks passed
@Qubitium
Qubitium deleted the codex/fix-fp8-bias-cancellation branch November 9, 2026 19:01
Qubitium added a commit that referenced this pull request Nov 17, 2026
Fix the test-side fallout of #4716, #4657, #4627 and #4508. Only entries
that fail on this sm_80 host (PG506) and whose stated reasoning reproduces
were changed; the rest are recorded with their outcome.

1. 8587f0b (#4716, native FP8 GEMM for a wide bias)
   - tests/kernels/test_fp8_kernel.py::test_fp8_default_forward_is_w8a16
     REPRODUCED on sm_80: 6 bf16 cases failed with 1-ulp (0.0078125) diffs
     against rtol/atol 2e-3. The oracle now narrows the decoded weight to
     the activation dtype, matching the native path; the bound is the
     half-ulp at unity (2^-9 ~= 2e-3). FIXED.
   - tests/test_fp8_bias_range.py::test_fp8_gpu_dequant_fallback_keeps_bias_wide
     REPRODUCED on sm_80 (fp16: actual 0.0 vs oracle 0.0078125). Renamed to
     test_fp8_gpu_native_gemm_keeps_bias_wide and rewritten for the #3737
     intent: finite output, FP32 bias, residual bounded by one activation-
     dtype ulp of |x.w|. FIXED.

2. bf5adeb (#4657, Marlin bias kept in checkpoint order)
   - tests/test_marlin_scratch_cuda.py 288/200 cases REPRODUCED on sm_80:
     8 failed with "b_bias.size(0) != size_n". scripts/marlin_scratch_fixture.py
     now passes layer._kernel_bias(a) (padded, tile-ordered FP32) instead of
     the checkpoint-order layer.bias. FIXED (8 failed -> 68 passed, 2 skipped).

3. 60376e9 (#4714, default placement in one capability group)
   - tests/test_tenstorrent_support.py already passes on current main: the
     monkeypatched _record accepts **kwargs (fixed upstream by 50f1752).
     ALREADY PASSES on sm_80.

4. 34672a7 (#4585, modules snapshot the kernel policy)
   - tests/kernels/test_bitsandbytes_rocm_triton.py triton cases SKIP on
     sm_80 (no tuned bitsandbytes tile for this family). DEFERRED: the
     stated failure cannot be reproduced here.

5. dfa180d (#4627, GGUF path chosen from static gates)
   - both tests REPRODUCED on sm_80 with AttributeError
     _benchmark_forward_runner. The removed timing hook is no longer patched;
     each test asserts the hook stays absent. FIXED.

6. fd8d524 (#4599, AwqGEMVTorchLinear CPU reader)
   - both kernel-registry contract tests already pass on current main
     (AwqGEMVTorchLinear is in EXCLUDED and the ROCm audit table; fixed
     upstream by ab151fe). ALREADY PASSES on sm_80.

7. d2a0508 (#4283, dense-module adapters)
   - tests/kernels/test_gptq.py::TestKernelOutput errors on sm_80 before the
     adapter path: this host's flash_attn_2_cuda has an undefined symbol, so
     the stated Lora assertion is never reached. DEFERRED: blocked by the
     pre-existing host flash_attn failure; the fix cannot be verified here.

8. f201837 (#4508, activation-quantization wording)
   - both tests REPRODUCED on sm_80. The refusal still fires; the regex now
     matches "activation quantized|activation scales". FIXED.

9. b225a43 (#4727, static-cache compile test)
   - ROCm-only failure. On sm_80 the CUDA leg runs and fails earlier on the
     host flash_attn symbol, so the ROCm gate cannot be reproduced.
     DEFERRED.

Test run (GPU 4):
  tests/kernels/test_fp8_kernel.py tests/test_fp8_bias_range.py
  tests/test_marlin_scratch_cuda.py tests/test_marlin_scratch_benchmark.py
  tests/kernels/test_gguf_cpp_activation_quantization.py
  tests/kernels/test_gguf_cpp_path_selection.py tests/test_auto_module_decoder.py
  => 254 passed, 35 skipped, 1 failed.
The single failure is the pre-existing sm_80
test_fp8_w8a8_forward_is_batch_invariant (native torch._scaled_mm needs
sm_89+); it fails identically on origin/main.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant