Skip to content

[BUG] AWQ at default batch_size=1 crashes on every Mamba/conv hybrid (Granite-4.0-H, LFM2, Nemotron-H, OLMo-Hybrid): token-row replay hands the recurrent mixer a 4-D additive mask #4671

Closed

Description

@Qubitium

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:

  1. Since transformers 5, the recurrent layer type (linear_attention / conv) gets its mask from create_recurrent_attention_mask. That returns None for an un-padded batch, which is always the case at batch_size=1. _token_row_layer_type_masks (awq_processor.py:1723) forwards that None as the per-type override.
  2. _token_row_replay_kwargs (awq_processor.py:1767-1905) treats a None mask 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 (the else branch at :1894).
  3. The scale-search replay (_search_best_scale → _build_chunked_reference_outputs → _iter_prepared_forward_outputs:4174) calls the mixer (module2inspect = the mamba / conv layer) with that 4-D float mask. apply_mask_to_padding_states computes hidden * 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

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    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.

  2. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    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-only truncated to 4 layers (3 linear_attention + 1 full_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 4
    • A tiny qwen3_5_moe_text model with the same default batch_size=1 fails the same way:
      File ".../transformers/models/qwen3_5_moe/modeling_qwen3_5_moe.py", line 545, in forward
        hidden_states = apply_mask_to_padding_states(hidden_states, attention_mask)
      File ".../modeling_qwen3_5_moe.py", line 232, in apply_mask_to_padding_states
        hidden_states = (hidden_states * attention_mask[:, :, None]).to(dtype)
      RuntimeError: The size of tensor a (32) must match the size of tensor b (64) at non-singleton dimension 4
      

    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 at batch_size=4 completes on LFM2.5-1.2B and Nemotron-Nano-9B-v2.

    🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions