Description
Found on main @ 4f287eb, CPU (transformers 5.22.1, defuser 0.0.31).
What is wrong
#4722 fixed the rotary KeyError from #4695, but AWQ on DeepSeek-V4 still crashes in the first decoder layer, now in the routed-expert scale search:
File "gptqmodel/looper/awq_processor.py", line 2796, in _quantize_layer
File "gptqmodel/looper/awq_processor.py", line 3294, in _search_best_scale
File "gptqmodel/looper/awq_processor.py", line 3854, in _build_chunked_reference_outputs
File "gptqmodel/looper/awq_processor.py", line 4577, in _iter_prepared_forward_outputs
File "transformers/models/deepseek_v4/modeling_deepseek_v4.py", line 1133, in forward
File "defuser/modeling/moe_experts_interface.py", line 175, in linear_loop_experts_forward
IndexError: The shape of the mask [456] at index 0 does not match the shape of the indexed tensor [590, 4096] at index 0
DeepSeek-V4 Flash/Pro begin with hash_moe layers (config.mlp_layer_types; the first 3 layers of the released checkpoints). DeepseekV4SparseMoeBlock.forward(hidden_states, input_ids) routes those layers with self.gate.tid2eid[input_ids.reshape(-1)]. So the router's expert indices must line up with the hidden-state rows, which requires one input_ids entry per token row.
The routed-expert gate/up group replays the MoE block (module2inspect = mlp) on the MoE-root feature. That feature is all captured token rows concatenated across calibration batches (590 rows here). _sanitize_kwargs keeps input_ids because it is in the block's signature, but its value comes from the cached layer kwargs and describes a different token count (456 here). _prepare_module_forward slices or rebuilds positions, masks and rotary for the replayed x, but nothing re-aligns input_ids. The router therefore produces 456 index rows for 590 hidden rows, and the expert loop raises. The IndexError is not an _AWQScaleGroupError, so the fallback does not apply and quantize() aborts.
Every DeepSeek-V4 checkpoint has hash_moe layers, so AWQ cannot complete on this family. GPTQ completes on the same model. Before #4722 the run died earlier with #4695's KeyError, so this has been hidden behind that crash.
Repro (CPU, a few minutes; 3-layer real config, random weights, 8 experts)
import gzip, json, torch
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
from gptqmodel import GPTQModel
from gptqmodel.quantization import AWQConfig
from gptqmodel.quantization.config import CalibrationConfig
SRC = "deepseek-ai/DeepSeek-V4-Flash" # any V4 config; used here: a 3-layer copy of the Flash config
cfg = AutoConfig.from_pretrained(SRC, num_hidden_layers=3, n_routed_experts=8, num_experts_per_tok=2)
print(cfg.mlp_layer_types) # ['hash_moe', 'hash_moe', 'hash_moe']
torch.manual_seed(0)
D = "dsv4_3l"
AutoModelForCausalLM.from_config(cfg).to(torch.bfloat16).save_pretrained(D)
AutoTokenizer.from_pretrained(SRC).save_pretrained(D)
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) > 1200: rows.append(t[:1200])
if len(rows) >= 16: break
q = GPTQModel.load(D, AWQConfig(bits=4, group_size=128), dtype=torch.bfloat16)
q.quantize(CalibrationConfig(dataset=rows, batch_size=1)) # IndexError aboveThe same script with GPTQConfig(bits=4, group_size=128) prints no error. The 99-family tiny-model sweep shows the same failure for deepseek_v4 (mask [160] vs [256, 32]).
Suggested fix
When the replayed module2inspect consumes per-token kwargs (input_ids for hash_moe; the same applies to any token-aligned kwarg), carry them through the same concatenation, row selection (keep mask) and chunk slicing as x. The MoE-root capture hook could record the block's input_ids next to its hidden_states. Alternatively, replay the block one captured batch at a time with that batch's own kwargs. As a guard, raise _AWQScaleGroupError when a token-aligned kwarg's length differs from x's token count, so the fallback applies instead of an unhandled IndexError.
Test that should have caught it
tests/test_awq_deepseek_v4_rotary_4695.py stubs the rotary path and never runs a real scale search through a hash_moe block. A tiny V4 AWQ quantize() with at least two calibration rows of different lengths would reproduce this.
Severity
High: AWQ for DeepSeek-V4 is impossible (hard crash in layer 0). Regression-wise, #4722 is incomplete as the fix for "AWQ crashes on DeepSeek-V4" (#4695).
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Agent claim:
agent-root-4880is working on this issue now.Qubitium commentedon Nov 17, 2026
Agent release:
agent-root-4880finished this issue. Fix: #4996, squash-merged as a62bed0.AWQ's
token_rowsreplay now packs token-aligned kwargs (input_ids) with the same(source batch, row)provenance as the replayed activation rows, and_prepare_module_forwardraises_AWQScaleGroupErrorwhen a token-aligned kwarg still does not describe the replayed rows (so the configured plain-quantization fallback applies instead of an unhandledIndexError). DeepSeek-V4hash_moelayers now quantize end-to-end: newtests/test_awq_deepseek_v4_hash_moe_4880.pyis 3 failed on unmodifiedmain(the realquantize()raisesIndexError: shape of the mask [16] ... indexed tensor [40, 64]) and 3 passed with the fix; the AWQ CPUunitCI suite runs it.fix(awq): pack token-aligned replay kwargs with the captured rows (Mo…