Skip to content

fix(exl3): match native half rounding in Torch codebooks - #3582

Merged
Qubitium merged 2 commits into
ModelCloud:mainfrom
tpurtell:upstream/04-exl3-half-rounding-20261108
Nov 8, 2026
Merged

Qubitium merged 2 commits into
ModelCloud:mainfrom
tpurtell:upstream/04-exl3-half-rounding-20261108

Conversation

@tpurtell

@tpurtell tpurtell commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Round 3inst/MCG half sums and MUL1 fused results once to FP16 before widening to FP32. Preserve the codebook operands and Hadamard/scale arithmetic. Exhaustively compare every 16-bit state with a separate scalar native-format law, including signed-zero bits.

Stack

Layer 4/36 of a dependency-ordered fork extraction. Depends on #3581. Review only this layer.

GitHub requires this cross-fork PR to target upstream main, so its Files changed tab includes earlier stack layers. After the predecessor lands, rebase this branch and retarget the next layer; do not merge later layers out of order. No predecessor is merged automatically.

Tests

CUDA_VISIBLE_DEVICES='' HIP_VISIBLE_DEVICES='' OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1 PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python -m pytest -q tests/test_exl3_native_half_rounding.py

3 passed: all 65,536 states per codebook, zero bit mismatches, maximum absolute drift 0. Used Python 3.12, Torch 2.14.1, Transformers 5.19.0. No model quantization, GPU work, downloads, or heavyweight model validation. Unit fixtures are small and CPU execution is constrained to one math thread.

Review and remaining validation

This layer and its dependency diff were reviewed for scope, existing extension points, and compatibility with the resolved fork snapshot. New focused regression coverage is included. Kept as a draft for maintainer review; hardware paths and end-to-end model behavior have not been revalidated in this session. No speed claim. Original integration/history is preserved in tpurtell#3.

Independent native-format law: three codebooks × 65,536 states. Zero mismatches and zero maximum drift, including signed-zero bits. This is a CPU format correction; CUDA/ROCm execution remains unvalidated.

tpurtell and others added 2 commits November 8, 2026 05:39
`format/check.sh` lints the whole `tests/` tree with ruff, so the unsorted
import block (`struct` separated from third-party, `gptqmodel` in its own
group) fails CI's ruff-check job. Autofix only; no test logic change.
@Qubitium
Qubitium force-pushed the upstream/04-exl3-half-rounding-20261108 branch from a1de37b to f75c7e0 Compare November 8, 2026 05:41

@Qubitium Qubitium left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Rebased onto main after #3581 merged; PR is now MERGEABLE, changedFiles: 2
(was 7), diff is exactly this layer: exllamav3_torch.py +7/-3,
test_exl3_native_half_rounding.py +35. One CI fix pushed (f75c7e05).

The change is correct, and I validated it against real hardware

You noted "CUDA/ROCm execution remains unvalidated." I closed that gap rather than
take the scalar law on trust: I extracted the repo's own decode_3inst device
functions from gptqmodel_ext/exllamav3/quant/codebook.cuh, compiled them with
nvcc -arch=sm_90, and ran all 65,536 states per codebook on the actual GPU, so
the oracle is real __hadd / __hfma silicon output rather than a Python
reimplementation of them.

Comparing float32 LUT values against __half2float(kernel output):

codebook this PR pre-PR (main)
3inst 0 / 65536 27707 / 65536
mcg 0 / 65536 33088 / 65536
mul1 0 / 65536 62516 / 65536

So the change is necessary, sufficient, and exact on hardware. The pre-PR failure
counts are large because the unrounded FP32 sum is a value the format can never
represent — exactly the drift the PR targets.

I also checked the obvious hazard in this kind of fix, that .half().float() is
double rounding versus native's single rounding. It is safe here, and I verified
why rather than assuming:

  • 3inst/mcg: the exact half-sum of two halves is always representable in
    FP32 (0/65536 cases exceed it), so FP32-add-then-half == __hadd;
  • mul1: only 913 distinct half inputs occur, and h * inv + bias is exact in
    FP32 for all 913, so FP32-mul-add-then-half == __hfma (no FMA contraction
    difference);
  • exact Fraction arithmetic agrees with the implementation on all 196,608
    states, and the test's Python-double law is likewise free of double-rounding
    (0/65536 and 0/63665 discrepancies).

I also confirmed acc + byte_sum never overflows uint16 (max 0x67ED), so the
(uint16_t) cast is lossless, and that CPU and CUDA LUT construction are bitwise
identical.

CI failure fixed

format/check.sh runs ruff check over the whole tests/ tree, and the new test's
import block was unsorted (I001). That fails the ruff-check job. Autofixed in
f75c7e05: struct into the stdlib group, gptqmodel into its own group. No
logic change.

Test review

The exhaustive 65,536-state comparison with a separate scalar law is the right
shape for a format fix, and comparing view(torch.int32) is the right call for
representing signed zeros.

One inaccuracy to correct, since the PR body and a test comment assert it:

"Exhaustively compare every 16-bit state with a separate scalar native-format
law, including signed-zero bits."

No state in 3inst or mcg produces a negative zero — I enumerated all 65,536
and got 9 +0 for 3inst, 12 +0 for mcg, and no -0 anywhere (no state has
both halves -0). So signed-zero handling is represented by the bitwise
comparison but never actually exercised by these codebooks. The claim is
harmless but overstated; I'd drop "including signed-zero bits" or reword to
"compared as raw bits".

The mul1 comment in the implementation is slightly imprecise too. It says "These
half operands are exact in FP32; native __hfma rounds once, after the add, not
between the multiply and add." The conclusion is right, but the reason is that the
whole expression is exact in FP32 for every reachable input — not merely that the
operands are exact as halves. Minor.

Verification

tests/test_exl3_native_half_rounding.py   3 passed
ruff check tests/                          clean
nvcc sm_90 kernel vs LUT                   0 mismatches (196,608 states)

Not verified by me: ROCm/HIP .half() behaviour, and downstream EXL3 model
quality. The device functions are portable bitwise ops by design (per the file's
own comment), so I would not expect divergence, but I did not run it.

@Qubitium
Qubitium marked this pull request as ready for review November 8, 2026 05:42
@Qubitium

Qubitium commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

ROCm / R9700 (gfx1201) validation

The PR body said "CUDA/ROCm execution remains unvalidated." Both are now validated on
real hardware, and I closed the CUDA gap in my review above. Here is ROCm.

Method — no reimplementation of the semantics: I compiled the repository's own
gptqmodel_ext/exllamav3/quant/codebook.cuh with hipcc --offload-arch=gfx1201
(HIP 7.16.26385, ROCm 10.1, Torch 2.16.0.dev20261025+rocm10.1), using the real
half2_uint32 / half_uint16 unions from util.cuh copied verbatim (that header
pulls cuBLAS, which this dump does not need). Ran all 65,536 states per codebook on
an AMD Radeon AI PRO R9700, then compared against the Torch LUT at float32
precision — the same __half2float(kernel output) reference the CUDA run used.

codebook   Torch-vs-HIP   Torch-vs-CUDA   HIP-vs-CUDA
3inst                 0               0              0
mcg                   0               0              0
mul1                  0               0              0

Three things this establishes:

  1. The Torch LUT is bit-exact on ROCm, not only CUDA — 0/196,608 mismatches.
  2. HIP and CUDA kernels agree bit-for-bit on all three codebooks, so __hadd
    and __hfma behave identically across the two vendors for these inputs. The
    portable bitwise formulation in the file does what its comment claims.
  3. The fix is needed on ROCm too, with the same magnitude as CUDA:
codebook   PRE-PR vs HIP   THIS-PR vs HIP
3inst             27707                 0
mcg               33088                 0
mul1              62516                 0

The PR's own test suite also passes under the ROCm interpreter:

HIP_VISIBLE_DEVICES=0 /opt/venvs/rocm101t/bin/python -m pytest -q tests/test_exl3_native_half_rounding.py
3 passed

And the device-parameterized LUT is bitwise identical between CPU and the R9700:

3inst: device vs cpu bitwise match = True
mcg:   device vs cpu bitwise match = True
mul1:  device vs cpu bitwise match = True

One caveat on scope

This validates the codebook decode path, which is what this PR changes. It does
not re-run end-to-end EXL3 inference or quantization on ROCm, so downstream model
quality and the rest of the EXL3 kernel remain as they were. My earlier notes still
stand: float8_e4m3fnuz/e5m2fnuz are irrelevant here, and the "including
signed-zero bits" wording in the PR body remains slightly overstated (no state in
3inst/mcg produces -0).

Host detail: 4x AMD Radeon AI PRO R9700 (Navi 48, gfx1201, 32 CU) alongside the
NVIDIA cards; all measurements above used device 0.

@Qubitium
Qubitium merged commit eb433e8 into ModelCloud:main Nov 8, 2026

Copy link
Copy Markdown
Collaborator

Review reproduced four EXL3 range-test failures at this merge. All four pass on current main; no further fix was needed.

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.

2 participants