Repository navigation
fix(gptaq): give every calibrated processor the same shuffled batch order (#4869) - #4985
Merged
Merged
Conversation
Collaborator
Author
|
Extra data point on the residual desc↔shuffle PPL gap (same setup as the
So after the fix shuffle is within ~1.5% of desc, which is the same order of |
GPTAQ and FOEM pair the native capture with the quantized capture by calibration batch index. Every LoopProcessor prepared its own copy of the calibration data, so CalibrationConfig(sort="shuffle") gave NativeProcessor and GPTQProcessor two different batch orders: variable-length rows crashed inside layer 0 with a tensor-size mismatch, and equal-length rows silently mixed unrelated sequences into dXXT (layer 0 must be exactly zero). quantize() now prepares the batches once (main and EoRA adapter samples) and binds a passthrough preparer, so every calibrated processor in the chain replays the same list and order. sort="desc"/None produce the same prepared data as before. Adds tests/test_gptaq_shuffle_pairing_4869.py: both processors must see one batch order, layer-0 dXXT must be zero with sort="shuffle", and variable-length rows must quantize without the size mismatch. Fixes #4869
Qubitium
force-pushed
the
root/gptaq-shuffle-pairing-4869
branch
from
November 17, 2026 21:06
f11d2e7 to
110c5bd
Compare
This was referenced Nov 17, 2026
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.
Fixes #4869.
Summary
With GPTAQ (and FOEM with
alpha > 0) the run plan has two calibratedprocessors,
NativeProcessorthenGPTQProcessor(
gptqmodel/models/quantization_run_plan.py:159). Each one prepared its owncopy of the calibration data inside
LoopProcessor.__init__(
gptqmodel/looper/loop_processor.py:574), andModuleLoopercaptured inputsfor each processor from its own dataset
(
gptqmodel/looper/module_looper.py:2252). WithCalibrationConfig(sort="shuffle")each preparation called the globalrandom.shuffle(gptqmodel/utils/calibration.py:1282), so the two processorsreplayed different batch orders while GPTAQ pairs native and quantized
inputs by batch index (
gptqmodel/quantization/gptaq.py:76).Result on
origin/main:RuntimeError: The size of tensor a (88) must match the size of tensor b (80)inside layer 0 (the issue saw 367 vs 528);dXXT— layer 0 must be exactly0(native and quantized inputs are identical there) but came out at
‖dXXT‖₂ = 2562.31, and deeper modules were 10–118× the correct norm;31.09(sort="desc") →163.69(sort="shuffle").What Changed
gptqmodel/looper/loop_processor.pyprepare_calibration_dataset_once(calibration)holds the onepreparation step (it used to be inline in
LoopProcessor.__init__), keepsthe empty-after-preparation error, and normalises one-shot preparers.
LoopProcessor.__init__now calls it, so a processor built directly (unittests, adapters) still prepares exactly once.
gptqmodel/models/base.py(BaseQModel._quantize_with_calibration)CalibrationConfigwith the existing_already_prepared_calibration_datasetpassthrough ([BUG] quantize() with rotation='hadamard' rotates the model before validating calibration data: a failed call (dataset None/empty/too short) leaves it rotated and a retry rotates twice (+3.8 nll, silent) #4935), so everycalibrated processor in the chain (native, preprocessor, GPTQ/GPTAQ/FOEM,
QQQ, AWQ, Paro, EXL3, FP8 GSQ, MACAQ) replays the same list and order.
prepared list when
adapter_datasetisNone.model mutation ([BUG] quantize() with rotation='hadamard' rotates the model before validating calibration data: a failed call (dataset None/empty/too short) leaves it rotated and a retry rotates twice (+3.8 nll, silent) #4935):
prepared_dataset/prepared_adapter_datasetarereused unchanged instead of being prepared (and reshuffled) again.
sort="desc"/sort=None: preparation runs the sameprepare_datasetwith the same policy, once instead of once per processor.Follow-up intentionally out of scope:
_eora_generate()(post-quantizationadapter generation) is a separate single-pass entry point and still prepares
its own samples.
Logic Flow Documentation
The bug and the fix, with the real symbol names:
flowchart TD Q["BaseQModel._quantize_with_calibration"] --> P1["NativeProcessor.__init__"] Q --> P2["GPTQProcessor.__init__"] P1 --> A["LoopProcessor.__init__:<br/>prepare_dataset(sort='shuffle')<br/>random.shuffle → order A"] P2 --> B["LoopProcessor.__init__:<br/>prepare_dataset(sort='shuffle')<br/>random.shuffle → order B"] A --> C1["ModuleLooper.cache_inputs(order A)"] B --> C2["ModuleLooper.cache_inputs(order B)"] C1 --> G["GPTAQ.process_batch(batch_index=i)"] C2 --> G G --> R["_take_native_input(i) pairs native[i] with quantized[i]<br/>→ unrelated rows → dXXT garbage / size mismatch"] style A fill:#fff1f0,stroke:#d4380d style B fill:#fff1f0,stroke:#d4380d style R fill:#fff1f0,stroke:#d4380dflowchart TD Q["BaseQModel._quantize_with_calibration"] --> PR["prepare_calibration_dataset_once(calibration_config)<br/>one prepare_dataset + one random.shuffle"] PR --> BIND["calibration.bind(already_prepared_calibration_dataset,<br/>dataset=prepared_dataset)"] BIND --> P1["NativeProcessor.__init__"] BIND --> P2["GPTQProcessor.__init__"] P1 --> A["LoopProcessor.__init__ → passthrough<br/>order A"] P2 --> B["LoopProcessor.__init__ → passthrough<br/>order A"] A --> C1["ModuleLooper.cache_inputs(order A)"] B --> C2["ModuleLooper.cache_inputs(order A)"] C1 --> G["GPTAQ.process_batch(batch_index=i)"] C2 --> G G --> OK["native[i] and quantized[i] are the same row<br/>layer 0 → dXXT == 0"] style PR fill:#e6f4ff,stroke:#0958d9 style BIND fill:#e6f4ff,stroke:#0958d9 style OK fill:#f6ffed,stroke:#389e0dReplaced mechanism,
file:lineevidence (origin/main@68cf61f23):gptqmodel/looper/loop_processor.py:574inLoopProcessor.__init__gptqmodel/utils/calibration.py:1282(random.shuffle(sorted_dataset))gptqmodel/models/quantization_run_plan.py:159gptqmodel/looper/module_looper.py:2252gptqmodel/quantization/gptaq.py:76_take_native_inputFailure path (red) is the only one that can mix rows; the success path is drawn
separately above. No user-facing option, default, or resolution order changed,
so no
docs/reference page is added.Tests
New
tests/test_gptaq_shuffle_pairing_4869.py(CPU-only, 2-layer Qwen2truncation, 13 rows,
sort="shuffle"):test_shuffle_prepares_one_batch_order_for_every_processor— the(first token, length)of every prepared batch must be identical forNativeProcessorandGPTQProcessor(the pairing invariant).test_shuffle_keeps_layer0_gptaq_dxxt_zero— 13×128 equal-length rows;layer-0
q_projdXXTmust be exactly0.test_shuffle_does_not_crash_on_variable_length_rows— 13 rows of 64…160tokens;
quantize()must complete.Red on
origin/main@68cf61f23(test file copied into a clean worktree):Green on the PR head:
GPTAQ/FOEM + calibration + quantize regression set, same selection on both
trees (
tests/test_gptaq_shuffle_pairing_4869.py,tests/test_gptaq_foem_batch.py,tests/test_moe_gptaq_foem.py,tests/test_rehome_gptaq_foem_dxxt_4312.py,tests/test_gptaq_foem_oom_fallback_4604.py,tests/test_quantize_*.py,tests/test_calibration_config.py,tests/test_generator_calibration_4307.py,tests/test_looper_concurrency.py):origin/mainOnly delta: the 3 new tests. The remaining 3 failures are identical on both
trees and pre-existing:
test_rehome_gptaq_foem_dxxt_4312.py::test_quantize_aligns_dxxt_with_rehomed_hessian[gptaq|foem](asserts an internal
safe_move_tocall, unrelated) andtest_quantize_in_session_4381.py::test_rehome_qqq_initializes_torch_cache_after_class_switch(
QQQTorchLinear._torch_dropped_checkpointmissing).Calibration suite (
tests/test_calibration*.py+tests/test_moe_gptaq_foem.pytests/test_gptaq_foem_multigpu.py+tests/test_mlx_*gptaq|foem*):origin/mainIdentical; the 2 failures are the known
tests/test_calibration_data_device.pyones.test_gptaq.pyitself is thefull model-compat harness (whole
Qwen2.5-0.5B-Instruct+ lm_eval ARC) andcannot run CPU-only here; the new test covers the same GPTAQ path end-to-end on
a tiny model.
test_gptaq_foem_multigpu.py(4) and the MLX tests (2) skip formissing hardware.
Measurements
dXXTnorm (‖dXXT‖₂), CPU, 2-layer Qwen2.5 truncation, 13 rows × 128 tokens,GPTAQ 4-bit g32,
batch_size=1:sort="desc"origin/main+ shufflelayers.0.self_attn.q_projlayers.0.self_attn.o_projlayers.0.mlp.gate_projlayers.0.mlp.down_projlayers.1.self_attn.q_projsort="desc"/sort=Noneare unchanged bit for bit: the SHA-256 of everyrecorded module's
dXXTis identical onorigin/mainand on the PR head (8/8leader modules; shared-input followers such as
k_proj/v_proj/up_projadopt the leader's Hessian and record none), and
desc==none.PPL (CPU fp16,
HuggingFaceTB/SmolLM2-135M, GPTAQ 4-bit g128, 64 calibrationrows × 256 tokens from wikitext-2 train, 16 × 256-token wikitext-2 test
windows, in-session model):
sort="desc"sort="shuffle"origin/mainControl on the same eval (plain GPTQ 4-bit g128, no GPTAQ, so batch order
only changes float accumulation order):
sort="desc"32.388 vssort="shuffle"32.621 (+0.7%). Batch order therefore moves this eval by ~1%on its own; GPTAQ accumulates both
HanddXXT, so its residual sensitivityis a little larger. The broken run was 5.3x worse, not 1.9% worse.
Model Support / Module Plan
Not applicable — no model definitions,
MODEL_MAPentries, module plans orfusion configs change.
AWQ onboarding safety
Not applicable — no AWQ grouping/search/replay/fold code is touched.
Review Requirements
loop_processor.py(the issue asked not to touchutils/calibration.py,which another change owns).
CalibrationConfig.bind()extensionpoint.
_quantize_with_calibration.desc/Noneare bit-identical (hash checkabove); per-processor construction still prepares once, so direct
callers and unit tests are unaffected.
Kernel Accuracy Requirements
Not applicable — no kernel, fusion or packing code changes.
Notes
sort="shuffle"now produces one order per run. It is still random per run, sotwo shuffle runs are not expected to be bit-identical to each other; the
invariant is that within a run every calibrated processor sees the same
order.