Repository navigation
fix(awq): never replay a recurrent mixer with the attention grid (#4671) - #4694
Merged
Merged
Conversation
With the default quantize(batch_size=1) the batch is unpadded, so transformers 5's per-layer-type mask builders return None for every type. AWQ's token-row replay read that None as "the module relied on its internal causal path" and fell back to the additive [1, 1, T, T] grid. A Mamba/conv mixer applies its mask as `hidden * attention_mask[:, :, None]`, so the grid raised on the first scale-search replay for every hybrid family (Granite-4.0-H, LFM2, Nemotron-H, OLMo-Hybrid): `tensor a (768) must match tensor b (366)`. - Resolve the replayed layer's type for hybrid layouts that expose the mixer as a sibling (`mamba`, `block_type`) instead of `self_attn`, so the per-type mask entry is actually selected. - Detect the recurrent builders through `LAYER_PATTERN_TO_MASK_FUNCTION_MAPPING` and give those layers a planar `[1, retained]` keep mask. Rows stay in source order, so each document stays contiguous and the recurrent state crosses only at document boundaries, the same limitation the padded batch_size>1 path already has. Issue #4671.
9 tasks done
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.
Root cause
At the default
quantize(batch_size=1)the calibration batch is unpadded, sotransformers5's per-layer-type mask builders returnNonefor every declared type — includingcreate_recurrent_attention_maskand the causal builders. AWQ's token-row replay read thatNoneas "the module relied on its internal causal path" and fell through to the additive[1, 1, T, T]block-diagonal grid.A Mamba/conv mixer applies its mask as
hidden * attention_mask[:, :, None], so the 4-D grid raised on the first scale-search replay for every hybrid family (Granite-4.0-H, LFM2, Nemotron-H, OLMo-Hybrid):Two gaps made this reachable:
BaseQModel._resolve_layer_replay_typeonly looked atself_attn.layer_type/layer.layer_type/layer_idx. GraniteMoE-Hybrid exposes the recurrent mixer as a sibling (mamba) and names the slotblock_type, so the type resolved toNoneand the per-type mask entry was never selected._token_row_replay_kwargshad no notion of a recurrent layer: aNoneoverride always became the additive attention grid.Changes
gptqmodel/models/base.py:_resolve_layer_replay_typenow also acceptsblock_typeand a mixer sibling'slayer_type/layer_idx, and only returns a value the model actually declared inconfig.layer_types. GPTQ/native replay benefits from the same correction.gptqmodel/looper/awq_processor.py:_token_row_layer_type_masksalso returns the resolved layer type._is_recurrent_attention_layer_typeconsultsLAYER_PATTERN_TO_MASK_FUNCTION_MAPPING, so thelinear_attention/conventries are detected as recurrent._token_row_replay_kwargs(..., recurrent=...)emits a planar[1, retained]keep mask for those layers instead of the attention grid._pack_token_rowsalready emits rows in source order, so each document stays contiguous and the recurrent state crosses only at a document boundary — the same limitation the paddedbatch_size>1path already has.docs/awq-calibration-aggregation.mdanddocs/layer-type-replay-masks.mddescribe the recurrent-mixer rule.unitsuite in.github/workflows/awq_cpu.yml.Tests
New
tests/test_awq_hybrid_recurrent_mask_4671.py(10 tests):linear_attention,conv) are detected, attention types are not;_token_row_replay_kwargswithrecurrent=Truereturns a[1, retained]bool mask that broadcasts asmask[:, :, None], while an attention layer still gets the[1, 1, T, T]grid;GraniteMoeHybridForCausalLM(layer_types=["mamba", "attention"]) is quantized withAWQConfig(bits=4, group_size=32)at the defaultbatch_size=1on CPU/AWQ_TORCH, and the recurrent mixer must be packed.On unmodified
mainthe file cannot import the new helper, and the same end-to-end script crashes with the reportedapply_mask_to_padding_statesshape error; with the fix the quantize completes (Quantize summary: 2 layer(s), 34 module(s)).Pre-existing failures reproduced on unmodified
mainin this environment (unrelated):tests/test_awq_scaling_groups.pyafmoe/flint cases (5) andtests/test_gemma4_support.pylocal-path cases (3).Closes #4671.