Skip to content

fix(awq): never replay a recurrent mixer with the attention grid (#4671) - #4694

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4671-awq-recurrent-mask
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4671-awq-recurrent-mask

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Root cause

At the default quantize(batch_size=1) the calibration batch is unpadded, so transformers 5's per-layer-type mask builders return None for every declared type — including create_recurrent_attention_mask and the causal builders. AWQ's token-row replay read that None as "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):

RuntimeError: The size of tensor a (768) must match the size of tensor b (366) at non-singleton dimension 4

Two gaps made this reachable:

  1. BaseQModel._resolve_layer_replay_type only looked at self_attn.layer_type / layer.layer_type / layer_idx. GraniteMoE-Hybrid exposes the recurrent mixer as a sibling (mamba) and names the slot block_type, so the type resolved to None and the per-type mask entry was never selected.
  2. Even with the type resolved, _token_row_replay_kwargs had no notion of a recurrent layer: a None override always became the additive attention grid.

Changes

  • gptqmodel/models/base.py: _resolve_layer_replay_type now also accepts block_type and a mixer sibling's layer_type/layer_idx, and only returns a value the model actually declared in config.layer_types. GPTQ/native replay benefits from the same correction.
  • gptqmodel/looper/awq_processor.py:
    • _token_row_layer_type_masks also returns the resolved layer type.
    • new _is_recurrent_attention_layer_type consults LAYER_PATTERN_TO_MASK_FUNCTION_MAPPING, so the linear_attention / conv entries 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_rows already emits rows in source order, so each document stays contiguous and the recurrent state crosses only at a document boundary — the same limitation the padded batch_size>1 path already has.
  • Docs: docs/awq-calibration-aggregation.md and docs/layer-type-replay-masks.md describe the recurrent-mixer rule.
  • CI: the new regression file is added to the AWQ CPU unit suite in .github/workflows/awq_cpu.yml.

Tests

New tests/test_awq_hybrid_recurrent_mask_4671.py (10 tests):

  • recurrent layer types (linear_attention, conv) are detected, attention types are not;
  • _token_row_replay_kwargs with recurrent=True returns a [1, retained] bool mask that broadcasts as mask[:, :, None], while an attention layer still gets the [1, 1, T, T] grid;
  • end to end: a tiny random-weight GraniteMoeHybridForCausalLM (layer_types=["mamba", "attention"]) is quantized with AWQConfig(bits=4, group_size=32) at the default batch_size=1 on CPU/AWQ_TORCH, and the recurrent mixer must be packed.

On unmodified main the file cannot import the new helper, and the same end-to-end script crashes with the reported apply_mask_to_padding_states shape error; with the fix the quantize completes (Quantize summary: 2 layer(s), 34 module(s)).

tests/test_awq_hybrid_recurrent_mask_4671.py -> 10 passed
tests/test_layer_type_replay.py tests/test_awq_weight_mean.py tests/test_awq_position_kwargs.py
tests/test_awq_scaling_groups.py tests/test_multimodal_calibration_replay.py
-> 246 passed, 39 skipped
tests/test_awq_scale_search_safety.py tests/test_awq_moe_router_scaling.py
tests/test_awq_scale_folding.py tests/test_awq_empty_groups.py tests/test_awq_mla_scaling.py
tests/test_awq_per_module_bits.py tests/test_awq_scale_search_memory.py
tests/module_tree/test_granitemoehybrid_model_tree.py tests/test_granitemoehybrid_monkeypatch.py
-> 191 passed, 32 skipped

Pre-existing failures reproduced on unmodified main in this environment (unrelated): tests/test_awq_scaling_groups.py afmoe/flint cases (5) and tests/test_gemma4_support.py local-path cases (3).

Closes #4671.

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.
@Qubitium
Qubitium merged commit 649a379 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4671-awq-recurrent-mask branch November 17, 2026 09:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant