Skip to content

[PERF] #5550 (fix of #5429) incomplete: ROCm FP8 cast probe compares NaN payload bits, so e4m3fn/e5m2 -> bf16 is judged not exact on gfx1201 and bf16 never takes the lean path (still ~3x slower, ~41% misrounded) #5570

Closed

Description

@Qubitium

Found on main @ 8c71625, AMD Radeon AI PRO R9700 (gfx1201, RDNA4), torch 2.16 dev ROCm 10.1, transformers 5.23.0.

What is wrong

#5550 (fix of #5429) replaced the IS_ROCM gate on the lean FP8 epilogue with a measured cast contract: _probe_native_fp8_cast (gptqmodel/nn_modules/qlinear/fp8.py:228-243) casts all 256 codes on the device and requires the result to be bit-equal to the CPU cast. That includes the NaN codes. On gfx1201 the NaN codes cast to bf16 with a different NaN payload/sign than the CPU cast does. Every non-NaN code is bit-exact, and fp16 is bit-exact for all 256 codes:

float8_e4m3fn code 0x7f cpu 0x7fc0 gpu 0x7ff0
float8_e4m3fn code 0xff cpu 0x7fc0 gpu 0xfff0
float8_e4m3fn non-NaN codes equal: True   fp16 all equal: True
float8_e5m2   code 0x7d cpu 0x7fc0 gpu 0x7fe0
float8_e5m2   code 0x7f cpu 0x7fc0 gpu 0x7fe0
float8_e5m2   code 0xfd cpu 0x7fc0 gpu 0xffe0
float8_e5m2   code 0xfe cpu 0x7fc0 gpu 0xffc0
float8_e5m2   code 0xff cpu 0x7fc0 gpu 0xffe0
float8_e5m2   non-NaN codes equal: True   fp16 all equal: True

The CPU cast canonicalises every NaN to +0x7fc0. The GPU keeps the sign and payload. Both are NaN. So the verdict for (float8_e4m3fn | float8_e5m2) -> bfloat16 is False. bf16 activations on ROCm still never take the lean path, and #5429 is fixed only for fp16. bf16 is the default dtype for most current checkpoints.

Verdicts on gfx1201 (_native_fp8_cast_verdict(cuda:0, fp8, out)):

fp8 \ out fp16 bf16 fp32
e4m3fn True False True
e5m2 True False True
e4m3fnuz True True True
e5m2fnuz True True True

(The fnuz formats have no NaN codes apart from 0x80, which is why they pass.)

Measured (the #5429 shapes, e4m3fn, a 4096-wide random weight, 20 timed calls, error against an FP64 oracle of the un-narrowed decoded weight). "default" is HEAD. "ref" forces _native_fp8_cast_supported -> False:

N x K, scale dtype M default lean calls outputs misrounded (default / ref)
4096x4096 row fp16 1 243 us (ref 645) 24 0.24% / 42.6%
14336x4096 row fp16 1 786 us (ref 2622) 24 0.23% / 41.8%
14336x4096 tensor fp16 512 1076 us (ref 2936) 24 0.19% / 49.0%
4096x4096 row bf16 512 1226 us (ref 1162) 0 40.9% / 40.9%
14336x4096 row bf16 16 2641 us (ref 2673) 0 41.1% / 41.1%
4096x4096 tensor bf16 1 661 us (ref 663) 0 41.4% / 41.4%

fp16 now matches #5429's lean numbers. bf16 is unchanged from before #5550: about 3x slower, and about 41% of outputs misrounded.

The #5325 B3 tests that #5429 said would turn green under a real fix are still red on ROCm for bf16 only:

pytest tests/kernels/test_fp8_kernel.py::test_fp8_default_forward_is_w8a16 tests/test_fp8_bias_range.py tests/test_fp8_native_cast_probe_5429.py
FAILED tests/kernels/test_fp8_kernel.py::test_fp8_default_forward_is_w8a16[dtype1-tensor-float8_e4m3fn]
FAILED tests/kernels/test_fp8_kernel.py::test_fp8_default_forward_is_w8a16[dtype1-tensor-float8_e5m2]
FAILED tests/kernels/test_fp8_kernel.py::test_fp8_default_forward_is_w8a16[dtype1-row-float8_e4m3fn]
FAILED tests/kernels/test_fp8_kernel.py::test_fp8_default_forward_is_w8a16[dtype1-row-float8_e5m2]
FAILED tests/test_fp8_bias_range.py::test_fp8_bias_free_cuda_in_range_weights_keep_native_path[dtype1]
5 failed, 39 passed, 2 skipped

Minimal repro (ROCm GPU visible)

import torch
from gptqmodel.nn_modules.qlinear import fp8 as F
dev = torch.device("cuda:0")
for f8 in (torch.float8_e4m3fn, torch.float8_e5m2):
    print(f8, F._native_fp8_cast_verdict(dev, f8, torch.float16), F._native_fp8_cast_verdict(dev, f8, torch.bfloat16))
    codes = torch.arange(256, dtype=torch.uint8).view(f8)
    a, b = codes.to(torch.bfloat16), codes.to(dev).to(torch.bfloat16).cpu()
    nan = torch.isnan(a)
    print("  non-NaN bit-equal:", torch.equal(a[~nan].view(torch.int16), b[~nan].view(torch.int16)), "NaN agree:", torch.equal(nan, torch.isnan(b)))
# torch.float8_e4m3fn True False
#   non-NaN bit-equal: True NaN agree: True
# torch.float8_e5m2 True False
#   non-NaN bit-equal: True NaN agree: True

Why the tests did not catch it: tests/test_fp8_native_cast_probe_5429.py probes only the CPU device (_probe_native_fp8_cast(CPU, ...), CPU against CPU is trivially equal) and mocks the probe for the ROCm cases. No test runs the probe on a HIP device. The real-device guards (test_fp8_default_forward_is_w8a16[dtype1-*], test_fp8_bias_range[dtype1]) are red on ROCm, which would have shown it.

Suggested fix: in _probe_native_fp8_cast, compare NaN-ness and the non-NaN bit patterns separately, e.g. nan = torch.isnan(expected_f); ok = torch.equal(nan, torch.isnan(actual_f)) and torch.equal(expected_bits[~nan], actual_bits[~nan]). NaN payloads cannot change a matmul result, and the lean gate already rejects payloads that are not range-safe. Add a device-side probe test (requires ROCm) asserting the verdict is True for fp16 and bf16 on gfx1201.

Severity: medium. On ROCm, every bf16 FP8 row/tensor checkpoint (ModelOpt per-tensor, row-scaled) is still about 3x slower per projection and misrounds about 41% of outputs, the case #5429 reported. ROCm CI is still red for the #5325 B3 tests.

🤖 Generated with Claude Code

Activity

  1. added
    unsafe
    scanned
    safe
    and removed
    unsafe
    on Nov 20, 2026
  2. Qubitium commented on Nov 20, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-fb5145b84190 finished this issue.

    Pull request: #5649

    fix the NaN-payload comparison in _probe_native_fp8_cast; CPU-verifiable test

  3. Qubitium commented on Nov 20, 2026

    @Qubitium
    CollaboratorAuthor

    Fixed by #5649 (merged as 113780d). The cast probe now compares NaN-ness and non-NaN bits separately, so ROCm NaN payload/sign differences no longer reject an otherwise bit-exact e4m3fn/e5m2 -> bf16 cast. Verified on CPU: 215 passed / 6 skipped across the FP8 probe/lean-decode/kernel suites; the gfx1201 verdict itself was not re-measured here (no ROCm device).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    safescanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions