Repository navigation
fix(exl3): map the scalar bf16 conversion in the HIP shim (#5208) - #5227
Merged
Merged
Conversation
#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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5208.
Problem
#5184 added
gptqmodel_ext/exllamav3/bf16_scale.cuto the exllamav3torch.opsextension. 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:173uses the CUDA intrinsic__float2bfloat16_rn(value), which hipcc does not provide under that name:So
gptqmodel_exllamav3_opsno 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"] endFix
Ten added lines, in the HIP branch only:
This is literally the
gptqmodel_ext/paro/rotation.cuhpattern (astatic __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))%42 = fptrunc contract float %41 to bfloat—fptruncwithoutfpexcept.strictis round-to-nearest-even per the LLVM LangRef.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.__float2bfloat16_rnis round-to-nearest-even, so the two agree bit-for-bit.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.pypassed 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 undergptqmodel_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;.hipsources 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:
#elsebranch) 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):
Green after restoring the fix:
1 passed.Validation
uvx [email protected] check gptqmodel docs/eora tests setup.pytests/test_rocm_kernel_contracts.pytests/test_exl3_bf16_fused_prescale_5148.pytests/test_exl3*.py tests/test_exllamav3*.py(RTX 4090, sm_89)Skips are all environment gates: 3× free-threaded-build subprocess (#4768), 2× two-CUDA-device MoE, 1×
cuda:1workspace, 1× RNG-isolation CUDA-device gate, 2× "ROCm GPU required", 2× "BF16 uses native EXL3 path on CUDA". The CUDA extension.sowas 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.cuused a CUDA-only intrinsic in a hipified TU;bf16_scale.handbindings.cuare plainat::Tensorregistrations. Every other HIP-targeting source already uses the HIP spellings (paro/rotation.cuh,allspark_hip/*,nf12_hip/*.hip,tt_bfloat_hip/*.hip). The__float2bfloat16_rnusers (nf12/,marlin/,swordfish/,tt_bfloat_cuda/) are NVIDIA-only extensions with no HIP build. Separately,__nv_bfloat16/__nv_bfloat162need no shim because torch's hipify maps them (table lines 284-285) — that is whyexllamav3/util.cuhhas 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/rocm101twith 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.hstill includes<cuda_runtime_api.h>;cusparse.h/cublas_v2.hare absent). Therefore:gptqmodel_exllamav3_opsJIT build/link was not run — no TU beyond the probe was compiled by hipcc;One forward-compat note: if ROCm ever ships
__float2bfloat16_rnitself, thestaticshim must be removed (redefinition error) — the same class of riskrotation.cuhalready carries.