Skip to content

fix(exl3): map the scalar bf16 conversion in the HIP shim (#5208) - #5227

Merged
Qubitium merged 1 commit into
mainfrom
fix/5208-hip-bf16-scalar
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5208-hip-bf16-scalar

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #5208.

Problem

#5184 added gptqmodel_ext/exllamav3/bf16_scale.cu to the exllamav3 torch.ops extension. That extension is also hipified and built on ROCm. The file's HIP shim aliased only the type:

using __nv_bfloat16 = __hip_bfloat16;

but the store at bf16_scale.cu:173 uses the CUDA intrinsic __float2bfloat16_rn(value), which hipcc does not provide under that name:

bf16_scale.hip:175:35: error: use of undeclared identifier '__float2bfloat16_rn'; did you mean '__float22bfloat162_rn'?
bf16_scale.hip:175:55: error: no viable conversion from 'float' to 'float2' (aka 'HIP_vector_type<float, 2>')
ninja: build stopped: subcommand failed.

So gptqmodel_exllamav3_ops no longer built on ROCm at all, and every EXL3 quantize there failed at layer 0 (exllamav3_processor.py:3374 → RuntimeError: EXL3 quantization failed: ... failed to build torch.ops JIT extension).

flowchart TB
  subgraph before["Before: hipcc rejects the file"]
    A1["hipify bf16_scale.cu"] --> B1["HIP shim aliases only<br/>the __nv_bfloat16 TYPE"]
    B1 --> C1["kernel body still calls<br/>__float2bfloat16_rn"]
    C1 --> D1["hipcc: undeclared identifier"]
    D1 --> E1["exllamav3_ops build fails<br/>→ every EXL3 quantize on ROCm dies at layer 0"]
  end
  subgraph after["After: one shim function"]
    A2["hipify bf16_scale.cu"] --> B2["HIP shim aliases the type<br/>AND maps the scalar conversion"]
    B2 --> C2["kernel body unchanged<br/>(same CUDA name)"]
    C2 --> D2["hipcc compiles"]
    D2 --> E2["exllamav3_ops builds,<br/>EXL3 quantize runs"]
  end
Loading

Fix

Ten added lines, in the HIP branch only:

// HIP has no scalar ``__float2bfloat16_rn``: the only scalar spelling is
// ``__float2bfloat16``, which converts through clang's ``__bf16`` and therefore
// rounds to nearest-even, the same rounding as CUDA's ``__float2bfloat16_rn``.
// Map the CUDA name onto it so the kernel bodies below stay identical on both
// platforms, exactly as rotation.cuh maps ``__floats2bfloat162_rn``.
static __device__ __forceinline__ __nv_bfloat16 __float2bfloat16_rn(float value)
{
    return __float2bfloat16(value);
}

This is literally the gptqmodel_ext/paro/rotation.cuh pattern (a static __device__ __forceinline__ shim function carrying the CUDA name, built from HIP's scalar conversion), which #5184 claimed to mirror.

The CUDA branch and both kernel bodies are byte-for-byte untouched, so the CUDA token stream — and therefore the #5148 fused-equals-eager bitwise arithmetic — is unchanged.

Why the HIP mapping rounds identically

  • hip/amd_detail/amd_hip_bf16.h:448 → __float2bfloat16(float f) { __hip_bfloat16 ret{f}; return ret; }
  • :196 → __hip_bfloat16(const float val) : __x_bf16(static_cast<__bf16>(val))
  • Compiling the shim for gfx1201 emits LLVM IR %42 = fptrunc contract float %41 to bfloat — fptrunc without fpexcept.strict is round-to-nearest-even per the LLVM LangRef.
  • The gfx1201 asm is the canonical RTNE sequence: v_bfe_u32 v1, v0, 16, 1 / v_add3_u32 v1, v1, v0, 0x7fff / v_lshrrev_b32 v0, v0, 16 (lsb-based ties-to-even), not truncation.
  • CUDA's __float2bfloat16_rn is round-to-nearest-even, so the two agree bit-for-bit.
  • Confirmed absent on HIP: grep -r "__float2bfloat16_rn" /opt/rocm/core-10.1/include/hip/ → no hits (only __float22bfloat162_rn).

New guard (the issue's other complaint)

The issue notes tests/test_rocm_kernel_contracts.py passed even though the extension could not build. This PR closes that gap with a CPU-only contract that walks the preprocessor regions of every source under gptqmodel_ext/ declaring a HIP branch, and requires each __-prefixed symbol hipcc will actually see to be HIP-native, hipify-rewritten, or declared by that file's HIP shim (#define / using / typedef / __device__ shim function, before first use). Comments are stripped; .hip sources are out of scope (hipcc-only, so this failure mode cannot hide there).

Cost: 12 hipified files, 26 ms, no GPU or ROCm needed.

Two controls keep it honest:

  • one feeds the pre-fix shim as a string and asserts exactly that violation;
  • one feeds the corrected shim (CUDA intrinsic left in the #else branch) and asserts clean.

A fourth node pins torch's hipify table (__nv_bfloat16→__hip_bfloat16, no conversion-intrinsic entry), since the allowlist relies on it.

Red on the pre-fix tree (real node, not a string fixture):

FAILED tests/test_rocm_kernel_contracts.py::test_hip_shims_provide_every_cuda_only_intrinsic_used_under_hip
  gptqmodel_ext/exllamav3/bf16_scale.cu:173: __float2bfloat16_rn is
  CUDA-only and not declared by the file's HIP shim

Green after restoring the fix: 1 passed.

Validation

check result
uvx [email protected] check gptqmodel docs/eora tests setup.py All checks passed
tests/test_rocm_kernel_contracts.py 464 passed
tests/test_exl3_bf16_fused_prescale_5148.py 23 passed (fused == eager bitwise, incl. adversarial rows)
tests/test_exl3*.py tests/test_exllamav3*.py (RTX 4090, sm_89) 559 passed, 12 skipped, 0 failed

Skips are all environment gates: 3× free-threaded-build subprocess (#4768), 2× two-CUDA-device MoE, 1× cuda:1 workspace, 1× RNG-isolation CUDA-device gate, 2× "ROCm GPU required", 2× "BF16 uses native EXL3 path on CUDA". The CUDA extension .so was rebuilt from the edited source, so those runs exercised the new file.

Audit of the rest of gptqmodel_ext/

No other in-scope break. Only bf16_scale.cu used a CUDA-only intrinsic in a hipified TU; bf16_scale.h and bindings.cu are plain at::Tensor registrations. Every other HIP-targeting source already uses the HIP spellings (paro/rotation.cuh, allspark_hip/*, nf12_hip/*.hip, tt_bfloat_hip/*.hip). The __float2bfloat16_rn users (nf12/, marlin/, swordfish/, tt_bfloat_cuda/) are NVIDIA-only extensions with no HIP build. Separately, __nv_bfloat16/__nv_bfloat162 need no shim because torch's hipify maps them (table lines 284-285) — that is why exllamav3/util.cuh has no alias and still builds.

Not verified on ROCm

This host does have ROCm — /opt/rocm/core-10.1/bin/hipcc (HIP 7.16.26385, AMD clang 24) and /opt/venvs/rocm101t with torch 2.16.0.dev+rocm10.1, the same versions as the issue box — but that wheel's torch headers are not hipified (ATen/cuda/CUDAContextLight.h still includes <cuda_runtime_api.h>; cusparse.h/cublas_v2.h are absent). Therefore:

  • ❌ the full gptqmodel_exllamav3_ops JIT build/link was not run — no TU beyond the probe was compiled by hipcc;
  • ❌ no EXL3 quantize or forward ran on an AMD device, and no ROCm numerics were compared;
  • ✅ verified: the failing construct and every intrinsic these kernels use compile with hipcc for gfx1201, and the RTNE lowering is visible in the IR/asm.

One forward-compat note: if ROCm ever ships __float2bfloat16_rn itself, the static shim must be removed (redefinition error) — the same class of risk rotation.cuh already carries.

#5184 added gptqmodel_ext/exllamav3/bf16_scale.cu. Its HIP shim aliased only
the type (__nv_bfloat16 = __hip_bfloat16), but the store uses the CUDA
intrinsic __float2bfloat16_rn, which hipcc does not provide. So
gptqmodel_exllamav3_ops no longer built on ROCm at all and every EXL3 quantize
there failed at layer 0.

HIP's only scalar spelling is __float2bfloat16, which converts through clang's
__bf16 and therefore rounds to nearest-even - the same rounding as CUDA's
__float2bfloat16_rn. Declare a static __device__ __forceinline__ shim carrying
the CUDA name over it, so the kernel bodies stay identical on both platforms.
This is the gptqmodel_ext/paro/rotation.cuh pattern, which #5184 claimed to
mirror. The CUDA branch and both kernel bodies are byte-for-byte untouched, so
the #5148 fused==eager bitwise arithmetic is unchanged.

Also extend tests/test_rocm_kernel_contracts.py with a CPU-only contract that
walks the preprocessor regions of every hipified source under gptqmodel_ext/
and requires each __-prefixed symbol hipcc will see to be HIP-native,
hipify-rewritten, or declared by that file's HIP shim. The issue noted that
test_rocm_kernel_contracts.py passed while the extension could not build; this
closes that gap without ROCm hardware. 12 hipified files, 26 ms.

Red before the shim (real node): "gptqmodel_ext/exllamav3/bf16_scale.cu:173:
__float2bfloat16_rn is CUDA-only and not declared by the file's HIP shim".
Green after: test_rocm_kernel_contracts.py 464 passed;
tests/test_exl3_bf16_fused_prescale_5148.py 23 passed.
@Qubitium
Qubitium marked this pull request as ready for review November 18, 2026 11:52
@Qubitium
Qubitium merged commit 0249e9b into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/5208-hip-bf16-scalar branch November 18, 2026 11:53
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