Repository navigation
fix(exl3): pre-scale bf16 rows only near the FP16 limit (#4870) - #4966
Merged
Merged
Conversation
#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.
This was referenced Nov 17, 2026
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).
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
#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:
FP16's Hadamard bound (
65504 / sqrt(128) ~= 5789.8) were divided eventhough 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
amax260 (layers.15.mlp.down_proj) and 1176(
layers.1.mlp.down_proj), so they were being scaled for nothing.(0.5, 1]instead of just inside thebound. 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_AMAXis now65504 / sqrt(128) ~= 5789.8(the Hadamard boundthe issue proposed) instead of
64.torch.frexp(row_amax / _FP16_SAFE_AMAX), so ascaled row's maximum lands in
(bound / 2, bound]- the smallest power oftwo that fits - instead of
(0.5, 1]. Rows at or below the bound keepscale
1and still convert bit-exactly, andNonestill means "nothingwas scaled".
tests/test_exl3_bf16_prescale_4870.py(new): the mixed-magnitude case fromthe 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.0assertions now pin thebound.
docs/exl3-wide-activation-dispatch.md: the bound, thefrexp(row_amax / bound)range, why scaling only as far as the bound matters for the outlierrow itself, the diagram, and the new test.
Unchanged: the per-row mechanism, the
Nonefast path, the bias handling(
(out - bias) * scale + bias), the FP16 path,ExllamaV3TorchLinear, and thenon-finite -> FP32 Torch fallback.
Out of scope: the
float(amax)host read. Removing it needs either droppingthe
Nonefast path (it is what skips the rescale for ordinary inputs) ordropping the
inf/nanfallback, so it is left as a separate change.Tests
main(f160a408d): 7 failed, 4 passed of 11.main: 19 failed, 484 passed, 13 skipped.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'stubpollution, 2 in
test_exl3_auto_nf12_recovery, andtest_exl3_projection_resume). Delta: +11 passing, 0 new failures.Measured effect
A row of
N(0, 0.01)elements with one massive activation, round-trippedthrough 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):amaxThe 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 FP16path's accuracy,
2.538e-04max per-row relative error for outliers 500, 2000and 10000 - the same value #4938 already reached, kept as a regression guard.
failures above).
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.mdcarries the updated Mermaid diagram withthe real symbols (
_native_bf16_input,_rescale_native_output,_ensure_inner).amax <= 64passes throughamax <= 65504 / sqrt(128)passes through2**frexp(row_amax)[1], maximum in(0.5, 1]2**frexp(row_amax / bound)[1], maximum in(bound/2, bound](64, bound]docs page are included.
unchanged and still marked in the diagram.
Review Requirements
frexpargument, two testassertions, and the docs.
scaled, the non-finite fallback and the
Nonefast path are unchanged.Kernel Accuracy Requirements
above); the kernel itself is unchanged.
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
amaxvalues the issuemeasured on the real layers) plus the mixed-magnitude batch and the existing
tests/test_exl3_wide_input.pycases (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'smaximum a normal FP16 number; a row at the bound is treated exactly like the
unscaled rows were before.