Repository navigation
Description
Found on main @ 12c1273, AMD R9700 (ROCm 10.1).
What is wrong
With the default quantize(batch_size=1), AWQ crashes on every hybrid family whose recurrent/convolution mixer calls apply_mask_to_padding_states. That covers Granite-4.0-H (granitemoehybrid), LFM2/LFM2.5, Nemotron-H and OLMo-Hybrid (and any Bamba/Mamba2-style mixer with the same helper).
The cause is the token-row replay mask in looper/awq_processor.py:
- Since transformers 5, the recurrent layer type (
linear_attention/conv) gets its mask fromcreate_recurrent_attention_mask. That returnsNonefor an un-padded batch, which is always the case atbatch_size=1._token_row_layer_type_masks(awq_processor.py:1723) forwards thatNoneas the per-type override. _token_row_replay_kwargs(awq_processor.py:1767-1905) treats aNonemask as "the module relied on its internal causal path". It then falls through to the additive 4-D[1, 1, T, T]block-diagonal causal mask (theelsebranch at:1894).- The scale-search replay (
_search_best_scale→_build_chunked_reference_outputs→_iter_prepared_forward_outputs:4174) calls the mixer (module2inspect= themamba/convlayer) with that 4-D float mask.apply_mask_to_padding_statescomputeshidden * attention_mask[:, :, None], which needs a 2-D[B, T]keep mask, so it raises.
At batch_size>1 with padding, the recurrent mask is a 2-D tensor, so the code takes the planar branch (:1881, an all-ones [1, T]) and does not crash. In that case, though, the mixer replays rows sampled from different documents as one recurrent sequence: the SSM/conv state flows across document boundaries, and the block-diagonal structure that the attention path keeps cannot be expressed for a recurrent layer.
Repro (full checkpoint, nothing truncated)
import gzip, json
from gptqmodel import GPTQModel
from gptqmodel.quantization import config as C
rows = []
with gzip.open("c4-train.00000-of-01024.json.gz", "rt") as f:
for line in f:
t = json.loads(line)["text"]
if len(t) > 1500: rows.append(t[:1500])
if len(rows) >= 32: break
m = GPTQModel.load("ibm-granite/granite-4.0-h-350m", C.AWQConfig(bits=4, group_size=128))
m.quantize(rows) # default batch_size=1 File "gptqmodel/looper/awq_processor.py", line 3054, in _search_best_scale
fp16_output = self._build_chunked_reference_outputs(
File "gptqmodel/looper/awq_processor.py", line 3546, in _build_chunked_reference_outputs
File "gptqmodel/looper/awq_processor.py", line 4174, in _iter_prepared_forward_outputs
module_output = module(x, **module_kwargs)
File ".../transformers/models/granitemoehybrid/modeling_granitemoehybrid.py", line 590, in forward
hidden_states = apply_mask_to_padding_states(hidden_states, attention_mask)
File ".../modeling_granitemoehybrid.py", line 251, in apply_mask_to_padding_states
hidden_states = (hidden_states * attention_mask[:, :, None]).to(dtype)
RuntimeError: The size of tensor a (768) must match the size of tensor b (366) at non-singleton dimension 4
The same AWQConfig(bits=4, group_size=128) with quantize(..., batch_size=1) on layer-truncated real checkpoints (real weights, enough layers to include every layer type):
| checkpoint | result |
|---|---|
| granite-4.0-h-350m (11 layers) / granite-4.0-h-1b (6) | crash, apply_mask_to_padding_states (granite mamba) |
| LFM2.5-1.2B-Instruct (3) | crash, modeling_lfm2.py:351 ShortConv apply_mask_to_padding_states |
| NVIDIA-Nemotron-Nano-9B-v2 (15) | crash, same error (tensor a (4480) ... b (311)) |
| Olmo-Hybrid-7B (4) | crash, same error (tensor a (3840) ... b (292)) |
granite-4.0-h-350m, batch_size=4 |
completes (padded batches give a 2-D recurrent mask) |
GPTQ (GPTQConfig) on the same checkpoints and batch sizes completes, so the problem is limited to the AWQ token-row replay.
Suggested fix
In _token_row_replay_kwargs, use the mask kind the layer type actually needs. For a recurrent/conv type (create_recurrent_attention_mask in LAYER_PATTERN_TO_MASK_FUNCTION_MAPPING), a None mask must stay None, or become a 2-D keep mask. It must never become the additive 4-D attention grid. Better still, do not pack token rows across documents for recurrent mixers at all: replay each captured document contiguously (or capture module2inspect per batch). Otherwise the scale objective is computed on a state that crosses documents even when nothing crashes.
Test that should have caught it
An end-to-end AWQ quantize at the default batch_size=1 for at least one hybrid definition (granite-4.0-h-350m is 350M). Current AWQ hybrid coverage is the CPU fold invariant (tests/test_awq_scaling_groups.py), which never runs the scale-search replay.
Severity
High. AWQ cannot quantize Granite-4.0-H, LFM2/LFM2.5, Nemotron-H or OLMo-Hybrid with the default batch size. The workaround (batch_size>1) avoids the crash but scores scales on cross-document recurrent state.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Taking this. I'll make the token-row replay give each layer type the mask shape it actually consumes (a recurrent/conv mixer must never receive the additive 4-D attention grid) and stop packing rows from different documents into one recurrent sequence, with an end-to-end AWQ batch_size=1 regression on a hybrid definition.
Qubitium commentedon Nov 17, 2026
More data for this: Qwen3.5 (GatedDeltaNet) hits the same crash. Its linear-attention layers also call
apply_mask_to_padding_states, so the scope covers the Qwen3.5 dense/MoE families too, not only Mamba/conv hybrids.Qwen3.5-9B-text-onlytruncated to 4 layers (3linear_attention+ 1full_attention, real weights),AWQConfig(bits=4, group_size=128),quantize(..., batch_size=1):RuntimeError: The size of tensor a (4096) must match the size of tensor b (304) at non-singleton dimension 4qwen3_5_moe_textmodel with the same defaultbatch_size=1fails the same way:Control: GPTQ on the same truncated checkpoints completes at
batch_size=1(Olmo-Hybrid-7B, NVIDIA-Nemotron-Nano-9B-v2, LFM2.5-1.2B). AWQ atbatch_size=4completes on LFM2.5-1.2B and Nemotron-Nano-9B-v2.🤖 Generated with Claude Code
fix(awq): never replay a recurrent mixer with the attention grid
fix(awq): never replay a recurrent mixer with the attention grid (#4694)