Repository navigation
Description
Found on main @ 4f287eb, RTX 4090 (sm_89), torch 2.15.0+cu132.
What is wrong
#4817 (fix of #4767, commit 55b267b) routes BF16 activations to the FP16-only native EXL3 kernels through ExllamaV3Linear._native_bf16_input() (gptqmodel/nn_modules/exllamav3.py). The method computes one amax over the whole activation tensor. When amax > _FP16_SAFE_AMAX = 64, it divides the whole tensor by 2**ceil(log2(amax)), so the largest value lands in (0.5, 1]. The output is then multiplied back.
The docs (docs/exl3-wide-activation-dispatch.md) say "power-of-two scaling is exact, so relative rounding is unchanged". That only holds while every value stays a normal FP16 number. FP16's smallest normal is 6.1e-5. One outlier row (a massive activation, typically the BOS token) sets the scale for every other row in the batch. All the ordinary rows then drop into FP16 subnormals, both as kernel inputs and as FP16 kernel outputs before the rescale, and lose most of their precision.
The threshold is also far below what FP16 needs. The plain FP16 path handles these same inputs (amax 1e3-1e4) with no loss at all.
This happens on real models, during prefill and PPL evaluation. In Llama-3.2-1B-Instruct (bf16, 512 wikitext tokens with BOS), layers.1.mlp.down_proj sees amax = 1176 on 2 rows, while the median row amax is 0.43. layers.15.mlp.down_proj sees amax = 260. So the whole 512-row prefill of those layers is scaled by 2^11.
Measurements
Single layer. Llama-3.2-1B-Instruct-EXL3-W4 layers.1.mlp.down_proj (K=8192), 64 rows of N(0, 0.3), and one outlier element in row 0. The table shows the max per-row relative error of rows 1..63 against the FP32 Torch reconstruction on the same BF16 input:
| outlier in row 0 | BF16 native (main) | FP16 native, same input | BF16 rounding of the reference |
|---|---|---|---|
| none | 3.97e-3 | 3.52e-3 | 1.77e-3 |
| 500 | 4.92e-3 | 3.65e-3 | 1.75e-3 |
| 2000 | 1.31e-2 | 3.62e-3 | 1.74e-3 |
| 10000 | 9.74e-2 | 3.53e-3 | 1.76e-3 |
End to end. Llama-3.2-1B-Instruct-EXL3-W4-ALL, wikitext2, 40 windows of 512 tokens (BOS + 511):
| backend / dtype | PPL |
|---|---|
EXL3 native, bf16 (main) |
18.8546 |
EXL3 native, bf16, _FP16_SAFE_AMAX patched to 4096 (no other change) |
18.7970 |
| EXL3 native, fp16 | 18.7883 |
| EXL3_TORCH, bf16 (the path bf16 used before #4817) | 18.7928 |
The whole BF16-vs-FP16 PPL gap (+0.058, about 0.3%) comes from the pre-scale. Models with larger massive activations (8B+, Qwen) will be hit harder: the error grows with the outlier, as the table shows. BF16 is the default load dtype for these checkpoints, so this is a silent accuracy loss on the default path.
Repro (single layer)
m = GPTQModel.load("Llama-3.2-1B-Instruct-EXL3-W4-ALL-d32557b6", backend=BACKEND.EXL3_EXLLAMA_V3, dtype=torch.bfloat16, device="cuda:0")
mod = m.model.model.layers[1].mlp.down_proj # ExllamaV3Linear
ref_mod = mod._ensure_torch_inner()
x = torch.randn(64, 8192, device="cuda") * 0.3; x[0, 5] = 1e4
xb = x.bfloat16()
y = mod(xb).double(); ref = ref_mod.forward(xb.float()).double()
y16 = mod(xb.half()).double()
rel = lambda a: ((a[1:] - ref[1:]).norm(dim=1) / ref[1:].norm(dim=1)).max().item()
print(rel(y), rel(y16)) # 9.74e-02 3.53e-03Suggested fix
- Scale per row, not per tensor. Compute a power-of-two scale from each row's amax so one outlier token cannot push other tokens into subnormals. Keep the result exact by rescaling each output row.
- Pre-scale only when FP16 range actually requires it. Use a threshold near FP16's limits ([PERF] EXL3 in bf16 (the default load dtype for Llama-3/Qwen checkpoints) never uses the native CUDA kernel: ExllamaV3Linear routes every non-fp16 input to the pure-Torch trellis decode, 200x slower at M=1 (8.0 vs 0.04 ms), 7.7 vs 266 tok/s #4767 proposed
amax < 65504, or 65504/sqrt(128) to cover the Hadamard bound), and scale to just below that bound, not to ≤ 1. - The per-row scale can be computed and applied on the device without the
float(amax)host read. That also removes the per-forward host sync, which today blocks CUDA graph capture and CPU/GPU overlap on the BF16 decode path.
Test that should have caught it
tests/test_exl3_wide_input.py checks the pre-scale only on uniformly wide inputs (1e4-1e30 everywhere). It needs a case with a mixed-magnitude batch: one outlier row plus ordinary rows, asserting the ordinary rows match the FP16 path or the FP32 reference. An e2e bf16-vs-fp16 PPL parity check on an EXL3 checkpoint would also catch it.
Severity
Medium. A silent accuracy regression on the default BF16 load path for every EXL3 model, in prefill and batched inference.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Perf numbers for the host-sync half of this (the
float(torch.linalg.vector_norm(...))in_native_bf16_input, which runs once perExllamaV3Linearper forward).main@ 5cded90, RTX 4090,Llama-3.2-1B-Instruct-EXL3-W4-ALL,m.generate(2 prompts, max_new_tokens=64, min_new_tokens=64)with the dynamic cache.bf16_nosyncismain's math with the scale kept on the device instead:amaxreduced on the device,scale = where(amax > 64, exp2(ceil(log2(amax))), 1),x / scale, and the output rescaled around the bias. There is no host read. It is not tuned (it adds 4-5 small elementwise launches).maingate_projper call, M=1, same conditions (ms)The per-row, on-device scale suggested above fixes both problems. One way to do it in a single pass: compute per-row
amax,exp2(ceil(log2(.)))clamped to >= 1, divide, and multiply back in the existing FP32 rescale.🤖 Generated with Claude Code
Qubitium commentedon Nov 17, 2026
Agent claim:
agent-root-4870is working on this issue now.Qubitium commentedon Nov 17, 2026
Agent claim released:
agent-11788dff99fdfinished this issue.Pull request: #4938
EXL3 bf16->fp16 pre-scale: scale per row (only rows above the FP16 threshold) so one outlier token cannot push the other rows into FP16 subnormals; keep the rescale exact
Qubitium commentedon Nov 17, 2026
Agent update (
agent-root-4870): #4938 landed the per-row part of this issue while this work was in progress, so I delivered the remaining part of the suggested fix as PR #4966 (squash-merged as0a32a1b7b).What #4966 changes:
_FP16_SAFE_AMAXis now65504 / sqrt(128) ~= 5789.8(the Hadamard bound) instead of 64, and the per-row exponent isfrexp(row_amax / bound), so a scaled row lands in(bound / 2, bound]- the smallest power of two that fits - instead of being normalized to(0.5, 1]. Rows in(64, bound](the real layers measured here: amax 260 and 1176) are now passed through untouched and convert bit-exactly.Measured on a row of
N(0, 0.01)elements with one massive activation, norm relative error of the row's elements in1e-4 .. 0.1:The ordinary rows of the mixed-magnitude batch stay at the plain FP16 path's accuracy (2.538e-04 for outliers 500/2000/10000). No EXL3 checkpoint is available offline here, so the Llama-1B PPL numbers could not be reproduced.
Still open from the issue: the
float(amax)host read. Removing it needs either dropping theNonefast path or theinf/nanTorch fallback, so I left it as a separate change.fix(exl3): scale BF16 activations per row, not per tensor (ModelCloud…
fix(exl3): pre-scale bf16 rows only near the fp16 bound (ModelCloud#4870
perf(exl3): keep the bf16 pre-scale decision on the device (ModelClou…