Skip to content

fix(exl3): pre-scale bf16 rows only near the FP16 limit (#4870) - #4966

Merged
Qubitium merged 1 commit into
mainfrom
root/exl3-prescale-4870
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
root/exl3-prescale-4870

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

#4938 (merged) made the BF16 pre-scale per row, which fixed the worst part
of #4870: one outlier row no longer pushes the other rows of the batch into FP16
subnormals. Two items from the issue's suggested fix were left:

  1. The trigger was still 64, far below what FP16 needs. Rows between 64 and
    FP16's Hadamard bound (65504 / sqrt(128) ~= 5789.8) were divided even
    though FP16 holds them untouched. The real Llama-1B layers quoted in [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
    see amax 260 (layers.15.mlp.down_proj) and 1176
    (layers.1.mlp.down_proj), so they were being scaled for nothing.
  2. A scaled row was normalized to (0.5, 1] instead of just inside the
    bound. The row that carries the massive activation was therefore divided by
    its whole amax, and its own small elements fell into FP16 subnormals -
    exactly the loss [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 describes, now confined to that one row.

This PR completes the issue's suggestion: trigger only near the FP16 limit and
bring a row just inside it.

What Changed

  • gptqmodel/nn_modules/exllamav3.py
    • _FP16_SAFE_AMAX is now 65504 / sqrt(128) ~= 5789.8 (the Hadamard bound
      the issue proposed) instead of 64.
    • The per-row exponent is torch.frexp(row_amax / _FP16_SAFE_AMAX), so a
      scaled row's maximum lands in (bound / 2, bound] - the smallest power of
      two that fits - instead of (0.5, 1]. Rows at or below the bound keep
      scale 1 and still convert bit-exactly, and None still means "nothing
      was scaled".
  • tests/test_exl3_bf16_prescale_4870.py (new): the mixed-magnitude case from
    the issue, the in-range pass-through, the massive-activation row's own small
    elements, the "just inside the bound" scale, and the per-row scale.
  • tests/test_exl3_wide_input.py: the two <= 1.0 assertions now pin the
    bound.
  • docs/exl3-wide-activation-dispatch.md: the bound, the frexp(row_amax / bound) range, why scaling only as far as the bound matters for the outlier
    row itself, the diagram, and the new test.

Unchanged: the per-row mechanism, the None fast path, the bias handling
((out - bias) * scale + bias), the FP16 path, ExllamaV3TorchLinear, and the
non-finite -> FP32 Torch fallback.

Out of scope: the float(amax) host read. Removing it needs either dropping
the None fast path (it is what skips the rescale for ordinary inputs) or
dropping the inf/nan fallback, so it is left as a separate change.

Tests

export CUDA_HOME=/usr/local/cuda PATH=/usr/local/cuda/bin:$PATH \
  TORCH_CUDA_ARCH_LIST=8.0 TORCH_NO_COMPILER_WRAPPER=1 GPTQ_BYPASS_SAFETY_CHECKS=1 \
  LOGBAR_ANIMATION=0 CUDA_VISIBLE_DEVICES=4 PYTHONPATH=$PWD HF_HUB_OFFLINE=1
/root/venv-kernels/bin/python /tmp/root-rt.py \
  tests/test_exl3_wide_input.py tests/test_exl3_bf16_prescale_4870.py \
  tests/test_exllamav3*.py tests/test_exl3_*.py tests/kernels/test_exllamav3*.py \
  -q -o addopts= -o log_cli=false -p no:cacheprovider
  • New test on main (f160a408d): 7 failed, 4 passed of 11.
  • New test with this PR: 11 passed.
  • Whole set on main: 19 failed, 484 passed, 13 skipped.
  • Whole set with this PR: 19 failed, 495 passed, 13 skipped - the same 19
    pre-existing node IDs (13 in tests/kernels/test_exllamav3_kernel.py,
    including the 8 GiB OOM while another process held ~89 GiB of the GPU,
    test_exl3_workspace's 'object' has no attribute 'wait_stream' stub
    pollution, 2 in test_exl3_auto_nf12_recovery, and
    test_exl3_projection_resume). Delta: +11 passing, 0 new failures.

Measured effect

A row of N(0, 0.01) elements with one massive activation, round-tripped
through the BF16 -> FP16 pre-scale and back (norm relative error of the row's
elements in 1e-4 .. 0.1, which is where a real massive-activation row lives):

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

The ordinary rows of the issue's mixed-magnitude batch (64 rows of
N(0, 0.3), one outlier in row 0, mocked FP16 GEMM) stay at the plain FP16
path's accuracy, 2.538e-04 max per-row relative error for outliers 500, 2000
and 10000 - the same value #4938 already reached, kept as a regression guard.

  • Unit tests have been executed and passed (see the pre-existing host
    failures above).
  • A new simple/fast unit test was added for this change.

Model Support / Module Plan

Not applicable: no new model, alias, or definition change.

Logic Flow Documentation

The changed flow is the BF16 bridge's scale decision;
docs/exl3-wide-activation-dispatch.md carries the updated Mermaid diagram with
the real symbols (_native_bf16_input, _rescale_native_output,
_ensure_inner).

Mechanism Before After
Trigger amax <= 64 passes through amax <= 65504 / sqrt(128) passes through
Per-row target 2**frexp(row_amax)[1], maximum in (0.5, 1] 2**frexp(row_amax / bound)[1], maximum in (bound/2, bound]
Rows in (64, bound] divided untouched, bit-exact
Outlier row's small elements FP16 subnormals normal FP16 (exact for BF16 values)
  • This change alters the pre-scale decision and the required diagram and
    docs page are included.
  • The removed/replaced mechanism is tabulated with real symbol names.
  • Failure and ambiguity paths (non-finite -> Torch, zero-row batch) are
    unchanged and still marked in the diagram.

Review Requirements

  • Reviewed this PR.
  • Matches the existing project structure and conventions.
  • No monkeypatching; the existing extension points are used.
  • Code surface minimized: a constant, one frexp argument, two test
    assertions, and the docs.
  • Regressions considered: in-range inputs are bit-exact, the bias is never
    scaled, the non-finite fallback and the None fast path are unchanged.

Kernel Accuracy Requirements

  • Accuracy measured against an independent FP32/FP64 Torch oracle (table
    above); the kernel itself is unchanged.
  • Fusion support changes: not applicable.

Notes

No EXL3 checkpoint is cached here and there is no network model download, so the
issue's end-to-end Llama-1B PPL numbers could not be reproduced. The evidence is
the synthetic massive-activation row above (using the amax values the issue
measured on the real layers) plus the mixed-magnitude batch and the existing
tests/test_exl3_wide_input.py cases (1e4..1e30, FP64-close numerics,
non-finite fallback), all green.

Reviewer guidance: raising the bound to the Hadamard limit is the issue's own
suggestion. It leaves the sqrt(128) rotation headroom and keeps a row's
maximum a normal FP16 number; a row at the bound is treated exactly like the
unscaled rows were before.

#4938 made the pre-scale per row but kept the trigger at 64 and normalized a
scaled row to (0.5, 1]. Two items from #4870 were left:

* A row whose own maximum is between 64 and 5789 - the real Llama-1B layers
  from the issue see 260 and 1176 - was divided by a power of two even though
  FP16 holds it untouched.
* The row that carries the massive activation was divided by its whole amax,
  so its own small elements fell into FP16 subnormals. On a row with amax 1176
  and elements around 1e-2 the small elements' norm error was 3.6e-3, and
  5.6e-2 at amax 20000.

`_FP16_SAFE_AMAX` is now `65504 / sqrt(128) ~= 5789.8`, the Hadamard bound the
issue proposed, 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 a normalization to 1. Rows at or below the bound keep scale 1 and
convert bit-exactly, and the rows that used to lose up to 5.6e-2 of their small
elements now round-trip exactly.

Tests: `tests/test_exl3_bf16_prescale_4870.py` (new) fails 7 of its 11 cases on
main and passes all 11 here;
`tests/test_exl3_wide_input.py`'s two `<= 1.0` assertions now pin the bound.
The required EXL3 suite is 19 failed / 484 passed / 13 skipped on main and
19 failed / 495 passed / 13 skipped here - same pre-existing host failures, 11
new passing tests. Docs updated.
@Qubitium
Qubitium merged commit 0a32a1b into main Nov 17, 2026
6 checks passed
@Qubitium
Qubitium deleted the root/exl3-prescale-4870 branch November 17, 2026 19:57
sLaffey pushed a commit to sLaffey/GPTQModel that referenced this pull request Nov 18, 2026
…d#4955) (ModelCloud#4989)

`_native_bf16_input` started every BF16 forward with
`float(torch.linalg.vector_norm(x, inf))`, a device -> host synchronization on
every native EXL3 linear: 112 syncs per decoded token on a 1B model (224 on
8B), 4-8x slower decode than the same math without it, and no CUDA-graph
capture of the BF16 path.

The read only decided "is any row above `_FP16_SAFE_AMAX`" and "is any row
non-finite". The per-row scale from ModelCloud#4870/ModelCloud#4966 already computes `row_amax`, so
the scale is now derived from it unconditionally on the device with
`frexp`/`ldexp`/`where`; a row that needs no scale gets exactly 1.0, which is
the identity, so its FP16 conversion stays bit-exact.

`isfinite` becomes a warmup probe: once per module, on its first BF16 forward,
instead of once per forward (the issue's "check isfinite once at load/warmup").
A module whose first BF16 activation is non-finite keeps the FP32 Torch
reconstruction and probes again next call.

The "nothing scaled" signal for a non-empty input is now an all-ones device
tensor rather than None; None still means an empty batch and is still what a
non-finite activation returns. None and 1.0 are the same decision, so the two
tests that pinned the literal None accept either. ExllamaV3TorchLinear, the FP16
path and the bias handling are unchanged.

Tests: tests/test_exl3_native_nosync_4955.py (new, 9 tests). On main the two
decisive CUDA tests fail with "called a synchronizing CUDA operation" /
"host read of a CUDA tensor"; here 48/48 pass for the new + prescale + wide
files. The EXL3 suite is 3 failed / 485 passed / 13 skipped on main and
3 failed / 494 passed / 13 skipped here - the same pre-existing nf12 and
projection-resume nodes.

Measured on 8x PG506 (GPU 7 shared): 20/20 forwards sync on main, 0/20 here;
interleaved A/B with a deep queue, one ExllamaV3Linear M=1: 1.040 -> 0.480 ms
per call (2.16x), second run 1.093 -> 0.442 ms (2.47x).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant