Repository navigation
fix(exl3): match native half rounding in Torch codebooks - #3582
Conversation
`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.
a1de37b to
f75c7e0
Compare
Qubitium
left a comment
There was a problem hiding this comment.
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, andh * inv + biasis exact in
FP32 for all 913, so FP32-mul-add-then-half ==__hfma(no FMA contraction
difference);- exact
Fractionarithmetic 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.
ROCm / R9700 (gfx1201) validationThe PR body said "CUDA/ROCm execution remains unvalidated." Both are now validated on Method — no reimplementation of the semantics: I compiled the repository's own Three things this establishes:
The PR's own test suite also passes under the ROCm interpreter: And the device-parameterized LUT is bitwise identical between CPU and the R9700: One caveat on scopeThis validates the codebook decode path, which is what this PR changes. It does Host detail: 4x AMD Radeon AI PRO R9700 (Navi 48, gfx1201, 32 CU) alongside the |
|
Review reproduced four EXL3 range-test failures at this merge. All four pass on current main; no further fix was needed. |
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
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.