Repository navigation
fix(quantization): native calibration batch split, 3-bit pack contract, fallback group offset (#3978) - #3995
Merged
Merged
Conversation
…tails, keep fallback group offset (#3978)
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 #3978
Three independent defects in calibration capture, 3-bit packing, and the GPTQ fallback grid.
1. GPTAQ/FOEM crash at calibration
batch_size > 1Root cause.
NativeProcessorstores one native entry per forward batch (native_processor.py:101on main, masked and flattened to[N_kept, H]).GPTQProcessorsetspreserve_batch_keep_maskand callsadd_batchonce per sample (gptq_processor.py:594-623).GPTAQ.process_batch/FOEM.process_batchthen didnative_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.
NativeProcessornow setspreserve_batch_keep_maskand captures one entry per kept sample via_split_batch_samples, applying the same[B, S]keep-mask and skipping empty samples exactly likeGPTQProcessor. 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"]native_processor.py:101NativeProcessor._split_batch_samplespreserve_batch_keep_masklets the hook apply the same[B, S]mask per samplegptaq.py:79/foem.py:160popped per sample from a per-batch listadd_batchmatches one captured sampleFailure path before the fix: sample 0
RuntimeError: The size of tensor a (6) must match the size of tensor b (3), sample 1IndexError: pop from empty list.2.
pack_gpusilently truncated 3-bitqzerosRoot cause. The 3-bit layout packs 32 zero columns into three words.
pack_gpucomputedgroups = zeros_int.shape[1] // word_bitsand dropped the tail (__init__.py:1834-1841);pack_originalwalked the same tail and raisedIndexError(__init__.py:1989).pack_blockand thepack_block_cpuextension already requiredout_features % 32 == 0.Fix. Both packers raise
ValueErrorforbits == 3 and out_features % 32 != 0.hf_pack_layer(pack_impl="auto")catches the GPUValueError, falls back topack_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"]pack_gpuqzeros (1, 3)instead of the registered(1, 5)ValueErrorpack_originalIndexError: index 48 is out of bounds for dimension 1 with size 48ValueErrorpack_block/pack_block_cpuValueError3. Fallback MIDPOINT/MEAN/MEDIAN lost the group offset
Root cause.
_fallback_quantize(gptq.py:1597-1648on 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;qandzerowere also rounded independently.Fix. A shared
affine_range_with_zerohelper (also used bymse_optimal_quant) extends the fitted range to include zero; the zero point is derived from the final range andqis 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:tests/test_fallback.pyhad 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.mddocs/3bit-zero-point-packing.mdBoth are linked from
README.md.Tests
New tests fail on unmodified
origin/mainand pass after:tests/test_gptaq_foem_batch.py— 4 passed after; 4 failed on main with the reportedRuntimeError(batched) andIndexError(empty pop).tests/test_pack_3bit_out_features.py— 5 passed after; 3 failed on main (IndexError, andDID NOT RAISE ValueErrorforpack_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):
ruff check gptqmodel docs/eora tests setup.pyreports one pre-existingF401ingptqmodel/utils/audio.py:16(present onorigin/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-modelGPTQModel.quantizerepro is blocked in this checkout by an unrelatedNamedModuleStopIterationon modules without parameters; the regression tests drive the realNativeProcessor/GPTAQ/FOEMsymbols 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.