Skip to content

[QUALITY] #4817 (fix of #4767) EXL3 bf16->fp16 pre-scale is per-tensor with threshold 64: one outlier token pushes every other row into fp16 subnormals (layer rel err up to 9.7e-2 vs 3.5e-3; Llama-1B PPL 18.855 vs 18.797) #4870

Closed

Description

@Qubitium

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-03

Suggested fix

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

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Perf numbers for the host-sync half of this (the float(torch.linalg.vector_norm(...)) in _native_bf16_input, which runs once per ExllamaV3Linear per 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_nosync is main's math with the scale kept on the device instead: amax reduced 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).

    run fp16 (native) bf16 main bf16_nosync
    idle-ish GPU 0.50 s 1.72 s 1.04 s
    3 interleaved runs while another process kept the same GPU busy 0.51 / 0.55 / 0.65 s 2.32 / 5.94 / 4.31 s 1.04 / 1.05 / 1.17 s
    gate_proj per call, M=1, same conditions (ms) 0.027-0.039 0.66-1.61 0.087-0.096
    • The decode path syncs once per linear: 112 per token on this 1B model, 224 on an 8B.
    • On a dedicated GPU that makes bf16 decode about 3.4x slower than fp16. The same math without the sync costs about 2x.
    • On a shared GPU each sync pays a scheduling slice, so bf16 decode becomes 4-9x slower than fp16 and its timing becomes erratic. The sync-free variant stays stable.
    • The sync also blocks CUDA-graph capture of the bf16 path.

    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

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim: agent-root-4870 is working on this issue now.

  3. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-11788dff99fd finished 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

  4. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    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 as 0a32a1b7b).

    What #4966 changes: _FP16_SAFE_AMAX is now 65504 / sqrt(128) ~= 5789.8 (the Hadamard bound) instead of 64, and the per-row exponent is frexp(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 in 1e-4 .. 0.1:

    row amax before (scale / error) after (scale / error)
    260 512 / 5.29e-04 1 / 0
    1176 2048 / 3.63e-03 1 / 0
    5000 8192 / 1.43e-02 1 / 0
    20000 32768 / 5.64e-02 4 / 0

    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 the None fast path or the inf/nan Torch fallback, so I left it as a separate change.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions