Skip to content

fix(quantization): native calibration batch split, 3-bit pack contract, fallback group offset (#3978) - #3995

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-3978
Nov 14, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-3978

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #3978

Three independent defects in calibration capture, 3-bit packing, and the GPTQ fallback grid.

1. GPTAQ/FOEM crash at calibration batch_size > 1

Root cause. NativeProcessor stores one native entry per forward batch (native_processor.py:101 on main, masked and flattened to [N_kept, H]). GPTQProcessor sets preserve_batch_keep_mask and calls add_batch once per sample (gptq_processor.py:594-623). GPTAQ.process_batch / FOEM.process_batch then did native_inps.pop(0) per call (gptaq.py:79, foem.py:160), so sample 0 consumed the whole batch tensor and sample 1 hit an empty list.

Fix. NativeProcessor now sets preserve_batch_keep_mask and captures one entry per kept sample via _split_batch_samples, applying the same [B, S] keep-mask and skipping empty samples exactly like GPTQProcessor. Other quantizers keep the one-entry-per-batch layout.

flowchart TD
    A["ModuleLooper._set_processor_mask(processor, keep_mask)"] --> B["_masked_hook_wrapper(processor, inner_hook)"]
    B --> C{"processor.preserve_batch_keep_mask"}
    C -- "True for GPTAQ/FOEM" --> D["inner_hook receives raw [B, S, H]"]
    C -- "False" --> E["apply_keep_mask_bt flattens to [N_kept, H]"]
    D --> F["NativeProcessor._split_batch_samples(inp, keep_mask)"]
    F --> G{"keep_mask shape matches [B, S]"}
    G -- "yes" --> H["one entry per sample, empty samples skipped"]
    G -- "no" --> I["one entry per batch"]
    H --> J["native_inp_caches[name]"]
    I --> J
    J --> K["NativeProcessor.process -> module.state[NATIVE_INPUTS_STATE_KEY]"]
    K --> L["GPTAQ/FOEM __init__ pops the list"]
    L --> M["GPTQProcessor hook calls add_batch once per kept sample"]
    M --> N["process_batch pops one entry per add_batch"]
Loading
Mechanism Before After
Capture unit one entry per forward batch, native_processor.py:101 one entry per kept sample, NativeProcessor._split_batch_samples
Mask ownership wrapper flattened the batch; no per-sample boundary preserve_batch_keep_mask lets the hook apply the same [B, S] mask per sample
Consumer alignment gptaq.py:79 / foem.py:160 popped per sample from a per-batch list one pop per add_batch matches one captured sample

Failure path before the fix: sample 0 RuntimeError: The size of tensor a (6) must match the size of tensor b (3), sample 1 IndexError: pop from empty list.

2. pack_gpu silently truncated 3-bit qzeros

Root cause. The 3-bit layout packs 32 zero columns into three words. pack_gpu computed groups = zeros_int.shape[1] // word_bits and dropped the tail (__init__.py:1834-1841); pack_original walked the same tail and raised IndexError (__init__.py:1989). pack_block and the pack_block_cpu extension already required out_features % 32 == 0.

Fix. Both packers raise ValueError for bits == 3 and out_features % 32 != 0. hf_pack_layer(pack_impl="auto") catches the GPU ValueError, falls back to pack_original, and that also fails closed instead of writing a truncated checkpoint.

flowchart TD
    A["hf_pack_layer(pack_impl='auto')"] --> B["_auto_pack_impl"]
    B -- "CUDA + pack_gpu" --> C["TorchLinear.pack_gpu"]
    B -- "otherwise" --> D["TorchLinear.pack_block"]
    C --> E{"bits == 3 and out_features % 32 != 0"}
    E -- "yes" --> F["raise ValueError"]
    E -- "no" --> G["pack qzeros and qweight words"]
    F --> H["except (ValueError, NotImplementedError)"]
    H --> I["TorchLinear.pack_original"]
    I --> J{"bits == 3 and out_features % 32 != 0"}
    J -- "yes" --> K["raise ValueError"]
    J -- "no" --> L["pack qzeros and qweight words"]
    D --> M{"out_features % 32 != 0"}
    M -- "yes" --> N["raise ValueError"]
    M -- "no" --> O["pack_block_cpu: out_features % word_bits == 0"]
    K --> P["FAIL-CLOSED: no checkpoint written"]
Loading
Packer Before After
pack_gpu K=128, N=48 produced qzeros (1, 3) instead of the registered (1, 5) ValueError
pack_original IndexError: index 48 is out of bounds for dimension 1 with size 48 ValueError
pack_block / pack_block_cpu already ValueError unchanged

3. Fallback MIDPOINT/MEAN/MEDIAN lost the group offset

Root cause. _fallback_quantize (gptq.py:1597-1648 on main) derived an affine zero point from the raw group range and clamped it to [0, maxq]. A group that does not cross zero clamped to a boundary, shifting every dequantized value by about the group minimum; q and zero were also rounded independently.

Fix. A shared affine_range_with_zero helper (also used by mse_optimal_quant) extends the fitted range to include zero; the zero point is derived from the final range and q is quantized against that integer zero. Applied to the GPTQ fallback and the QQQ copy.

Measured on the issue's [0.01, 0.05] group, K=128, 4-bit:

Path Before (max abs error / step) After
GPTQ MIDPOINT / MEAN 0.011323 / 0.002667 = 4.25 steps 0.001654 / 0.003333 = 0.50 step
GPTQ MEDIAN 0.011008 / 0.002688 = 4.10 steps 0.001654 / 0.003333 = 0.50 step
QQQ MIDPOINT / MEAN 0.011323 / 0.002667 = 4.25 steps 0.001654 / 0.003333 = 0.50 step
QQQ MEDIAN 0.011008 / 0.002688 = 4.10 steps 0.001654 / 0.003333 = 0.50 step

tests/test_fallback.py had encoded the old offset as "one-sided strategies are at least 5x worse than RTN"; that bound is updated to the zero-inclusive contract, and its FP64 near-dead-scale oracle now includes zero in the fitted range.

Reference pages

  • docs/native-calibration-batch-split.md
  • docs/3bit-zero-point-packing.md

Both are linked from README.md.

Tests

New tests fail on unmodified origin/main and pass after:

  • tests/test_gptaq_foem_batch.py — 4 passed after; 4 failed on main with the reported RuntimeError (batched) and IndexError (empty pop).
  • tests/test_pack_3bit_out_features.py — 5 passed after; 3 failed on main (IndexError, and DID NOT RAISE ValueError for pack_gpu/auto).
  • tests/test_gptq_fallback_offset.py — 24 passed after; 14 failed on main (assert 0.01134 <= 0.001344).

Commands (CUDA venv, GPU pinned by UUID):

CUDA_VISIBLE_DEVICES=GPU-d458708b-c760-bbf1-08fb-8b8803a2157d PYTHONPATH=$PWD \
  /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_gptaq_foem_batch.py tests/test_pack_3bit_out_features.py \
  tests/test_gptq_fallback_offset.py -q
# 33 passed

CUDA_VISIBLE_DEVICES=GPU-d458708b-c760-bbf1-08fb-8b8803a2157d PYTHONPATH=$PWD \
  /opt/venvs/cu130t/bin/python -m pytest \
  tests/test_fallback.py tests/test_pack_gpu_bit_exact.py \
  tests/test_packing_numeric_boundaries.py -q
# 239 passed

ruff check gptqmodel docs/eora tests setup.py reports one pre-existing F401 in gptqmodel/utils/audio.py:16 (present on origin/main); all changed files pass.

Hardware

NVIDIA RTX 4090 (GPU-d458708b-c760-bbf1-08fb-8b8803a2157d), CUDA 13.0, torch 2.16.0.dev+cu130. The full-model GPTQModel.quantize repro is blocked in this checkout by an unrelated NamedModule StopIteration on modules without parameters; the regression tests drive the real NativeProcessor/GPTAQ/FOEM symbols directly.

Notes

No dense or expanded weight cache is added. The calibration split is transient capture data; the packers now fail closed; the fallback change only adjusts grid selection.

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.

[BUG] GPTAQ/FOEM crash at batch_size>1; pack_gpu truncates 3-bit qzeros; GPTQ fallback strategies lose group offset

1 participant