Repository navigation
perf(exl3): fuse the bf16 pre-scale and re-scale into one launch each (#5148) - #5184
Merged
Merged
Conversation
…#5148) #4989 removed the per-forward host sync from the EXL3 BF16 path, but the mapping was still ~12 eager kernels per linear: vector_norm, .float(), div, frexp, ones_like x2, ldexp, where, divide, cast, then to(fp32), -bias, *scale, +bias, to(bf16). At decode each is a microsecond kernel behind a full host dispatch, so the 33 us GEMM starved behind ~90 us of launches and BF16 decode ran 2.7x slower than FP16. Both halves are now one CUDA launch each in the existing gptqmodel_exllamav3 extension: * bf16_prescale: one program per row; row amax, frexp(amax/bound) decoded from the exponent field, multiply by the exact power-of-two reciprocal, single RTNE FP16 cast, FP32 per-row scale. No host read. * bf16_rescale: (out - bias) * scale + bias with __fsub_rn/__fmul_rn/__fadd_rn (never an FMA) and one RTNE BF16 cast. The eager sequence stays as the reference and the fallback (missing ops in a stale build, non-contiguous/FP16/FP32/empty input, cold module), and the two paths are bit-identical: the division by a power of two is exact, so only the single FP16/BF16 cast rounds. sm_120 (RTX 5090), 8 Llama-3.1-8B-geometry layers, EXL3 W4: decode bs1 8.84 -> 5.27 ms/token (FP16 4.52), bs8 8.88 -> 5.37, prefill 2048 54.9 -> 46.6 ms. gate_proj M=1: FP16 32.8 us, BF16 88.2 -> 35.8 us. Tests: bitwise equality of pre-scale/re-scale/forward across shapes, bias dtypes, padded widths and adversarial rows; fallbacks; no retained dense weight; and an M=1 bf16 <= 1.3x fp16 bound that measures the eager chain in the same process (2.94x on 17a488e, 1.09x here). Fixes #5148
Qubitium
marked this pull request as ready for review
November 18, 2026 09:26
This was referenced Nov 18, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 18, 2026
) #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.
Summary
Fixes #5148. EXL3 BF16 decode ran ~2.7x slower than the same checkpoint in
FP16 because the per-row pre-scale (
_native_bf16_input) and the re-scale(
_rescale_native_output) were chains of ~12 eager elementwise kernels perlinear. #4989 removed the host sync, but at M=1 the 33 us GEMM starved behind
~90 us of host dispatch. Both halves are now one CUDA launch each, in the
existing
gptqmodel_exllamav3extension, and are bit-identical to the eagersequence, which stays as the reference and fallback.
Net: 9 files changed (+1040/-28), 4 of them new; BF16 decode on sm_120 goes from
8.84 ms to 5.27 ms per step on the 8-layer harness (FP16: 4.52 ms), and
mlp.gate_projM=1 from 88.2 us to 35.8 us (FP16: 32.8 us).Root cause
ExllamaV3Linear._native_bf16_input+_rescale_native_output(
gptqmodel/nn_modules/exllamav3.py:411-449at 17a488e) ranvector_norm(inf),.float(),div,frexp,ones_likex2,ldexp,where,x / scale,.to(fp16), thenresult.to(fp32),- bias,* scale,+ bias,.to(bf16). Every one is a separate kernel launch; eachlaunch is ~5-7 us of host dispatch against ~2 us of GPU work.
Before (per BF16 linear):
flowchart LR X["BF16 x"] --> A1["vector_norm inf"] A1 --> A2[".float"] A2 --> A3["div by bound"] A3 --> A4["frexp"] A4 --> A5["ones_like x2"] A5 --> A6["ldexp"] A6 --> A7["where"] A7 --> A8["x / scale"] A8 --> A9["to FP16"] A9 --> G["EXL3 GEMM"] G --> B1["to FP32"] B1 --> B2["- bias"] B2 --> B3["* scale"] B3 --> B4["+ bias"] B4 --> B5["to BF16"]After:
flowchart LR X["BF16 x"] --> P["bf16_prescale<br/>one launch: row amax,<br/>power-of-two scale, FP16 cast"] P --> G["EXL3 GEMM<br/>FP16 in, FP16 out"] G --> R["bf16_rescale<br/>one launch: FP32 bias math,<br/>BF16 cast"] R --> Y["BF16 y"]Failure path (unchanged behaviour, now explicit):
flowchart TD A["ExllamaV3Linear.forward bf16"] --> B{"trellis on CUDA?"} B -->|no| T["portable FP32 reconstruction"] B -->|yes| C{"fused op present,<br/>contiguous, warm module?"} C -->|no| E["eager reference sequence"] C -->|yes| F["bf16_prescale"] E --> G["EXL3 GEMM"] F --> G G --> H{"fused op present?"} H -->|no| I["eager re-scale, FP32"] H -->|yes| J["bf16_rescale, BF16"] I --> K["BF16 output"] J --> K A -->|non-finite first probe| TWhat changed
gptqmodel_ext/exllamav3/bf16_scale.cu(new):bf16_prescale(one programper row:
amax,frexp(amax / bound)decoded from the exponent field,multiply by the exact power-of-two reciprocal, single RTNE FP16 cast) and
bf16_rescale(__fsub_rn/__fmul_rn/__fadd_rn, never an FMA, then oneRTNE BF16 cast). A HIP shim mirrors
gptqmodel_ext/paro/rotation.cuh.gptqmodel_ext/exllamav3/bindings.cpp: registersbf16_prescale(Tensor x, float bound) -> (Tensor, Tensor)andbf16_rescale(Tensor result, Tensor scale, Tensor? bias, int n_cols) -> Tensor.gptqmodel/exllamav3/ext.py: adds the new source to the JIT build.gptqmodel/nn_modules/exllamav3_bf16_kernels.py(new): op lookup and theshape/dtype/device/stride guards that decide whether the fused path may run.
gptqmodel/nn_modules/exllamav3.py:_native_bf16_inputuses the fusedpre-scale once the module is warm; the eager sequence moves to
_prescale_from_row_amaxand remains the reference/fallback.forwardusesthe fused re-scale (which writes BF16 directly). No new host sync; the
isfinitewarmup probe is unchanged.docs/exl3-wide-activation-dispatch.md: mechanism page updated with thebefore/after and failure-path diagrams, the bitwise contract and the measured
table; linked from the EXL3 row in
README.md.tests/test_exl3_bf16_fused_prescale_5148.py(new).No expanded/dequantized weight is cached: the kernels allocate their transient
FP16/BF16 outputs per call, and a test asserts no retained tensor reaches the
dense weight size after repeated forwards.
Measurements
RTX 5090 (sm_120), torch 2.15.0+cu132, CUDA 13.2,
CUDA_DEVICE_ORDER=PCI_BUS_ID CUDA_VISIBLE_DEVICES=2. Eight Llama-3.1-8B-geometry decoder layers quantized toEXL3 W4 with
EXL3Config's quantizer (no EXL3 checkpoint was available offline,so weights are random but the projections, shapes and kernels are the real
ones). Batch 1 unless noted;
decodeis ms per generated token over 128 tokens.generate()128 tokSingle
mlp.gate_proj(4096 -> 14336), M=1, minimum CUDA-event time per call:Bitwise equality
The fused path is compared with the eager sequence by raw bits (
.view(int16)/
.view(int32)), not by tolerance, for:1e-40denormal,65504,0.99xand1.01xthe safe bound,
3e38, a 1e5 outlier row mixed with ordinary rows,negative, and
inf/nanrows;stored width, and scales 1.0, 2^17, 2^60;
Red evidence: the same test file run against 17a488e in a disposable
worktree (only the new
exllamav3_bf16_kernels.pyadded so the module imports)gives
fp16 32.84 us, bf16 96.59 us (2.94x)and the registration test failsbecause the ops do not exist — the intended assertions, not an import error.
Tests
The timing test is marked
slow. It measures FP16, BF16 and the eager chain inthe same process, asserts
bf16 <= 1.3x fp16, and additionally asserts theeager chain breaches that bound so a regression cannot pass silently. It skips
only when the FP16 baseline is under 20 us, where a fixed ~10 us of fused
launches cannot meet any ratio bound.
Unverified
.cumaps__nv_bfloat16/<cuda_fp16.h>onto HIP theway
gptqmodel_ext/paro/rotation.cuhdoes, but only the CUDA build wascompiled here.
the harness uses random weights with real Llama-3.1-8B geometry. The fused
path is bitwise identical to the shipped eager path, so accuracy is
unchanged by construction; the tok/s figures are a launch-overhead proxy.
test (
set_sync_debug_mode("error")) still passes for eight consecutivewarm forwards.
was also run on an RTX 4090 (sm_89): FP16 32.8 us, BF16 fused 35.8 us
(1.09x), BF16 eager 88.2 us (2.69x) - the same shape of improvement, but
the decode/prefill table above is sm_120.