Skip to content

fix(quantization): score the unshrunk SmoothMSE range as a candidate - #5221

Merged
Qubitium merged 1 commit into
mainfrom
fix/smoothmse-endpoint-candidates
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/smoothmse-endpoint-candidates

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

Summary

mse_optimal_quant (the SmoothMSE smoother used by the RTN, GPTQ and QQQ
fallback paths) never evaluated the unshrunk range as a candidate. It started
with best_p = 1 but best_err = inf, and the ternary probes are interior
thirds of [maxshrink, 1], so p = 1 was never scored. The first interior
probe 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:

mode before after independent p = 1 baseline MSE
asymmetric 0.9979189634, MSE 4.330713e-06 1.0, MSE 0 0
symmetric 0.9313910604, MSE 4.707187e-03 0.9333333969, MSE 4.444436e-03 4.444444e-03

The symmetric row is the best grid that a fixed zero point of (maxq+1)/2
allows; the remaining 8e-12 is 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 = maxshrink and p = 1) with the same _eval_mse used by the search,
    seed the incumbent from the lower-error one, and adopt a refined candidate
    only when its measured error is strictly lower.
  • No API, config or serialization change; the search cost grows by one
    two-point _eval_mse call per group.
  • Out of scope: replacing the ternary search with a uniform grid. This change
    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 "SmoothMSE
endpoint 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:#c33
Loading

New 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:#0f5132
Loading
Mechanism Before After
p = 1 scoring Never evaluated; best_err started at inf Scored with p = maxshrink before the search
First interior probe Always replaced the unscored incumbent Must beat a measured endpoint
Refinement Argmin over the refinement set only Compared with the incumbent error
Result vs the unshrunk grid Could be worse (constant row returned 0.9979189634) Rejected unless strictly lower than the p = 1 score

Tests

New file tests/test_smooth_mse_endpoint_baseline.py (48 cases): an
independent 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:1958 and qqq.py:458 fallback consumers. A
control case asserts an interior shrink still wins where it measures a lower
error.

Red evidence (new tests against the unmodified main implementation):

cd tests && PYTHONPATH=.. python -m pytest test_smooth_mse_endpoint_baseline.py -q
# 18 failed, 18 passed
# failed nodes include
#   test_smooth_mse_is_never_worse_than_the_unshrunk_baseline[negative_constant-asymmetric]
#   test_smooth_mse_is_never_worse_than_the_unshrunk_baseline[normal-symmetric]
#   test_smooth_mse_consumers_return_a_grid_no_worse_than_the_baseline[rtn|gptq|qqq-...]

After the patch, on CPU and on both accelerator backends used here:

cd tests && PYTHONPATH=.. python -m pytest test_smooth_mse_endpoint_baseline.py -q
# 48 passed

cd tests && PYTHONPATH=.. python -m pytest \
  test_smooth_mse_endpoint_baseline.py test_smooth_mse_affine_range.py \
  test_find_params_mse_grid.py test_quant_scale_overflow.py test_fallback.py \
  test_hessian_fallback_contracts.py test_gptq_fallback_offset.py \
  test_qqq_fallback_threshold_4566.py test_docs_contract_refs.py -q
# 493 passed, 1 skipped
  • The single skip is test_gptq_fallback_offset.py:113 ("Marlin torch.ops
    kernels 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.sh equivalent (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_MAP entry, module plan or
fusion 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

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

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 dequant
values are the existing quantizer outputs).

Notes

  • Behavioural risk is one-directional: the returned grid can only move toward
    a strictly lower measured error, so no path can regress relative to the
    p = 1 baseline. Rows that already selected an interior shrink keep it.
  • Reviewers comparing against a float64 recomputation may see ~1e-5 relative
    differences on selected rows; that is float32 scoring/final-reconstruction
    rounding in the same expression the search uses, documented in the new docs
    section.

`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
ZX-ModelCloud marked this pull request as ready for review November 18, 2026 11:38
@Qubitium
Qubitium merged commit 099955f into main Nov 18, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/smoothmse-endpoint-candidates branch November 18, 2026 11:42
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).
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