Repository navigation
fix(quantization): score the unshrunk SmoothMSE range as a candidate - #5221
Merged
Merged
Conversation
`mse_optimal_quant` started with `best_p = 1` but `best_err = inf`, and the ternary probes are thirds of `[maxshrink, 1]`, so `p = 1` was never evaluated. The first interior probe replaced the unscored incumbent and the refinement only searched near that probe, which let the returned grid be worse than the plain unshrunk quantization. Score both endpoints first, start from the lower-error one, and only adopt a refined candidate whose measured error is strictly lower. Regression: `tests/test_smooth_mse_endpoint_baseline.py` uses an independent float64 endpoint oracle over normal, negative-constant, all-zero and mixed-sign rows in symmetric and asymmetric modes, through the direct search and the RTN, GPTQ and QQQ fallback consumers. For `weight = ones(1, 128)`, 4 bit, steps=32, maxshrink=0.8, the asymmetric result is now exactly `1.0` (MSE 0, previously `0.9979189634`); the symmetric result improves from `4.707187e-03` to `4.444436e-03` (the `p = 1` score is `4.444444e-03`). On 300 fixed-seed random rows per mode, the worst endpoint-relative excess fell from 10.95% to 1.8e-05 (float32 scoring rounding).
ZX-ModelCloud
marked this pull request as ready for review
November 18, 2026 11:38
This was referenced Nov 18, 2026
Qubitium
added a commit
that referenced
this pull request
Nov 18, 2026
…5294) The endpoint search minimized the uniform mean squared weight error, so clipping a row's largest weights looked free: one element's error bought a smaller step for the other 127. Those weights dominate the layer output, so #5221's exact endpoint scoring lowered weight MSE while raising PPL (Qwen 4-bit symmetric 25.394 -> 26.249). Weight each squared error by the captured Hessian diagonal for GPTQ/QQQ fallbacks and by |w| + mean(|w|) for calibration-free RTN. The unshrunk p = 1 grid remains the incumbent under the same weights. 4-bit symmetric PPL: Qwen 26.249 -> 23.852 (plain RTN 24.843), Llama 25.630 -> 24.878 (plain RTN 25.352).
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
mse_optimal_quant(theSmoothMSEsmoother used by the RTN, GPTQ and QQQfallback paths) never evaluated the unshrunk range as a candidate. It started
with
best_p = 1butbest_err = inf, and the ternary probes are interiorthirds of
[maxshrink, 1], sop = 1was never scored. The first interiorprobe replaced the unscored incumbent and the refinement only searched near
that probe, so the "no shrink" grid could lose even when it was the best.
For
weight = ones(1, 128), 4 bit (maxq=15),steps=32,maxshrink=0.8:p = 1baseline MSE0.9979189634, MSE4.330713e-061.0, MSE000.9313910604, MSE4.707187e-030.9333333969, MSE4.444436e-034.444444e-03The symmetric row is the best grid that a fixed zero point of
(maxq+1)/2allows; the remaining
8e-12is float32 scoring rounding, not a worse choice.On 300 fixed-seed random rows per mode, the worst excess over the endpoint
baseline drops from 10.95% to 1.8e-05.
What Changed
gptqmodel/quantization/fallback_smooth.py: score both endpoints(
p = maxshrinkandp = 1) with the same_eval_mseused by the search,seed the incumbent from the lower-error one, and adopt a refined candidate
only when its measured error is strictly lower.
two-point
_eval_msecall per group.only guarantees "never worse than the unshrunk baseline", not global
optimality of the shrink search.
Logic Flow Documentation
The selection mechanism changed, so the old and new flows are below and are
also committed to
docs/quantization-scale-storage.md(section "SmoothMSEendpoint candidates", already linked from
docs/README.md).Old flow: the incumbent is never measured.
flowchart TD S["mse_optimal_quant(block)"] --> I["best_err = inf, best_p = 1 (never scored)"] I --> T["Ternary midpoints over interior thirds"] T --> F["First probe always replaces the unscored incumbent"] F --> R["Refine around that probe only"] R --> O["Final quantization may be worse than p = 1"] style I fill:#fee,stroke:#c33 style O fill:#fee,stroke:#c33New flow: the endpoint is measured before any probe can win.
flowchart TD S["mse_optimal_quant(block)"] --> B["_eval_mse at p = maxshrink and p = 1"] B --> I["incumbent = lower-error endpoint"] I --> T["Ternary iterations over interior midpoints"] T --> R["Refine around the incumbent"] R --> C{"Refined error < incumbent error?"} C -- Yes --> N["Adopt refinement"] C -- No --> K["Keep incumbent"] N --> O["Final quantization at best_p"] K --> O style O fill:#d1e7dd,stroke:#0f5132,color:#0f5132p = 1scoringbest_errstarted atinfp = maxshrinkbefore the search0.9979189634)p = 1scoreTests
New file
tests/test_smooth_mse_endpoint_baseline.py(48 cases): anindependent float64 endpoint oracle for normal, negative-constant, all-zero
and mixed-sign rows, symmetric and asymmetric, through the direct search and
the
rtn.py:211,gptq.py:1958andqqq.py:458fallback consumers. Acontrol case asserts an interior shrink still wins where it measures a lower
error.
Red evidence (new tests against the unmodified
mainimplementation):After the patch, on CPU and on both accelerator backends used here:
The single skip is
test_gptq_fallback_offset.py:113("Marlin torch.opskernels are not available") on the CPU-only environment; both accelerator
runs execute it. It is a rotation/marlin packing case unrelated to the
shrink search.
format/check.shequivalent (ruff 0.14.2, whole package + tests) passes.Unit tests have been executed and passed, or the reason they are not applicable is documented.
I added a new simple/fast unit test for this change, or documented why that is not applicable.
Model Support / Module Plan
Not applicable: no model definition, alias,
MODEL_MAPentry, module plan orfusion eligibility change.
AWQ onboarding safety
Not applicable: no model onboarding and no AWQ grouping, search, replay or
folding change. The shared fallback helper is reused by RTN/GPTQ/QQQ only.
Review Requirements
Kernel Accuracy Requirements
Not applicable: no inference kernel, packer, dispatch or fusion change. The
quantizer math is compared against a float64 Torch oracle in the new tests; no
persistent dequantized weight cache is introduced (the returned
dequantvalues are the existing quantizer outputs).
Notes
a strictly lower measured error, so no path can regress relative to the
p = 1baseline. Rows that already selected an interior shrink keep it.1e-5relativedifferences on selected rows; that is float32 scoring/final-reconstruction
rounding in the same expression the search uses, documented in the new docs
section.