Repository navigation
fix(awq): cap packed token-row replay at the model position limit - #5120
Merged
Merged
Conversation
AWQ packs the captured rows of several calibration documents into one [1, retained, hidden] replay sequence, so with batch_size > 1 the packed length is about batch_size * seq_len. The block-diagonal mask and the retained rows' positions keep that correct for mask-driven attention, but GPTNeoSelfAttention slices a fixed [max_position_embeddings, max_position_embeddings] causal buffer. An over-long replay clips the slice and torch.where raises a plain RuntimeError, so AWQ aborted in layer 0 for every GPT-Neo checkpoint with batch_size > 1 (#5085). Bound the token-row budget (and a definition-declared max_tokens) by the model's declared sequence limit through a shared resolve_position_limit() helper, and report a residual replay tensor-geometry failure as an _AWQScaleGroupError so the configured fallback applies instead of aborting quantize(). OpenAI GPT's [n_positions, n_positions] buffer is clipped the same way without raising, so it benefits from the same bound. Fixes #5085
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.
Problem
AWQ packs the captured rows of several calibration documents into one
[1, retained_tokens, hidden]replay sequence(
AWQProcessor._pack_token_rows)._token_row_budgetismax(largest batch's kept tokens, number of batches), so withbatch_size > 1the packed sequence is aboutbatch_size x seq_lentokens.A block-diagonal mask and the retained rows' positions keep that semantically
correct for attention implementations that read their causal structure from
attention_mask.GPTNeoSelfAttentiondoes not. It slices a fixed[max_position_embeddings, max_position_embeddings]buffer:When the packed replay is longer than
max_position_embeddingsthe slice isclipped and
torch.whereraises. That is a plainRuntimeError, not an_AWQScaleGroupError, so the configured fallback never ran andquantize()aborted in layer 0. Reproduced on
mainate75181df5with the realgpt-neo-125mposition geometry (max_position_embeddings=2048,window_size=256, 2 layers x 128 hidden, four 1100-token rows):batch_size=1batch_size=2RuntimeError: The size of tensor a (2048) must match the size of tensor b (2200) at non-singleton dimension 3batch_size=2OpenAI GPT has the same fixed
[n_positions, n_positions]buffer and clips itwithout raising (
self.bias[:, :, :w.size(-2), :w.size(-1)]); rows past thelimit would be scored against a silently truncated causal mask.
Old flow
flowchart TD A["_layer_input_features"] --> B["_token_row_budget<br/>max(largest_batch, batches)"] B --> C["_pack_token_rows<br/>batch_size x seq_len rows"] C --> D["_search_best_scale reference replay"] D --> E{"torch.where against fixed<br/>causal buffer"} E -- "fits" --> F["20-candidate grid"] E -- "over limit" --> G["RuntimeError<br/>quantize aborts<br/>fallback never runs"] style G fill:#f8d7da,stroke:#842029,stroke-width:2pxNew flow
flowchart TD A["_layer_input_features"] --> B["policy max_tokens?"] B -- yes --> C["int(max_tokens)"] B -- no --> D["_token_row_budget<br/>max(largest_batch, batches)"] C --> E["_cap_token_rows<br/>min(budget, position_limit)"] D --> E E --> F["_pack_token_rows<br/>[1, retained, hidden]"] F --> G["_search_best_scale reference replay"] G --> H{"Torch tensor geometry error?"} H -- yes --> I{"configured fallback?"} I -- yes --> J["plain quantization<br/>every leaf packed"] I -- no --> K["_AWQScaleGroupError"] H -- no --> L["20-candidate grid<br/>apply_scale"] style K fill:#f8d7da,stroke:#842029,stroke-width:2px style J fill:#ffe9b3,stroke:#b38600,stroke-width:2pxRuntimeError, run abortsmax_tokensRuntimeError, run aborts_AWQScaleGroupError, group quantized plainlyfallback=NoneRuntimeError_AWQScaleGroupErrorRuntimeErrorChanges
gptqmodel/utils/model.pyresolve_position_limit()+POSITION_LIMIT_CONFIG_KEYS, reading the same config keys calibration truncation uses and recursing into nested decoder configsgptqmodel/looper/awq_processor.py_position_limit()(cached),_cap_token_rows(), clamped budget andmax_tokens,_is_replay_geometry_error()and the_AWQScaleGroupErrorconversion around the reference replaydocs/awq-calibration-aggregation.mdtests/test_awq_gpt_neo_packed_replay_5085.pyThe bound is applied only where the packed replay is built; per-batch capture
and the chunked forward are unchanged, and no mask, position or numerical
behavior changes when the packed rows already fit.
Validation
Commands run from the repository root. Environment: Python 3.14 free-threaded,
Torch 2.15, Transformers 5.22.
Red evidence on the unmodified base (
origin/mainbefore this branch), thesame test bodies against the broken code:
Green evidence on this branch:
Covered by that file: the limit resolver (all keys + nested config), the clamp
(limit present / absent / zero), native GPT-Neo AWQ with
batch_size=2under a64-position limit (all 12 leaves pack, logits finite), the
batch_size=1control (all 40 rows replayed), and the fallback contract (configured fallback
packs every leaf;
fallback=Noneraises_AWQScaleGroupError).End-to-end issue reproduction, real
max_position_embeddings=2048geometry,four 1100-token rows,
batch_size=2:Affected family suites (CPU):
Skips: none in the nodes above. One pre-existing GPT-Neo test-family gap
remains closed by this PR: no AWQ test ran GPT-Neo with
batch_size > 1.Scope limits
below the declared value (or a definition-declared
max_tokenslarger thanthe limit) now reaches the fallback instead of aborting, but the scale search
cannot run there. No such definition exists in this repository today.
how many rows are retained, it does not replay per document.
test_awq_position_kwargs.py::test_variable_length_awq_runs_on_remote_familyskips here because its local model fixtures are absent. It covers variable
length replay, not the packed-length bound, and is unaffected by this change.
Fixes #5085