Repository navigation
Description
Found on main @ c2b0bf6, CPU (tiny DeepSeek-V4 config from tests/test_awq_deepseek_v4_hash_moe_4880.py; transformers 5.22.1, defuser 0.0.31).
What is wrong
#4996 (fix of #4880) packs input_ids with the replayed AWQ token rows so the hash_moe router (tid2eid[input_ids.reshape(-1)]) gets one id per hidden row. AWQProcessor._pack_token_aligned_kwargs (gptqmodel/looper/awq_processor.py:~1836) takes each provenance entry (source_batch, row_index) and reads
flat = value.detach().reshape(-1) # the batch's full [B, S] input_ids grid
rows.append(flat[int(row_index)].reshape(1))row_index is not a grid index. The capture hook strips padding (and attention-sink) rows before storing a feature (_apply_active_keep_mask_to_feature). So row_index counts kept rows: in a padded batch it indexes the compacted rows, not the [B, S] grid. _token_row_replay_kwargs already maps kept rows back to the grid for the mask and positions through _token_row_source_structure() (flat_index = nonzero(keep)). The input_ids packer skips that mapping.
The fix is correct only when the batch has no padding (batch_size=1, or equal-length rows) and no sinks are excluded. With batch_size > 1 and rows of different lengths, every token after the first padded document receives another token's id. The hash_moe router then sends those rows to the wrong experts during the scale search's reference and candidate replays. The row count still matches, so #4996's new guard (numel != token_rows) does not fire. The routed-expert scales are then searched on activations the model never produces, and nothing is logged.
Repro
# tiny DeepseekV4Config from tests/test_awq_deepseek_v4_hash_moe_4880.py (mlp_layer_types=["hash_moe"]*2)
import torch, gptqmodel.looper.awq_processor as AP
from gptqmodel.utils.attn_mask import normalize_seq_mask
stats = {"rows": 0, "mismatch": 0}
orig = AP.AWQProcessor._pack_token_aligned_kwargs
def spy(self, provenance, batch_indices):
out = orig(self, provenance, batch_indices)
if "input_ids" in out:
c = self.inputs_cache
truth = []
for s, r in provenance:
b = batch_indices[s]
keep = normalize_seq_mask(c.attention_masks[b].cpu()) # [B, S] kept grid
grid = torch.nonzero(keep.reshape(-1)).reshape(-1) # kept row -> grid index
truth.append(c.layer_input_kwargs[b]["input_ids"].reshape(-1)[grid[r]].item())
stats["rows"] += len(truth)
stats["mismatch"] += int((torch.tensor(truth) != out["input_ids"].reshape(-1).cpu()).sum())
return out
AP.AWQProcessor._pack_token_aligned_kwargs = spy
g = torch.Generator().manual_seed(0)
rows = [{"input_ids": torch.randint(5, 120, (1, L), generator=g), "attention_mask": torch.ones(1, L, dtype=torch.long)}
for L in (12, 20, 8, 16, 24, 10, 18, 14)]
q = GPTQModel.load(D, quantize_config=AWQConfig(bits=4, group_size=32, format=FORMAT.GEMM, device="cpu"), dtype=torch.float16)
q.quantize(CalibrationConfig(dataset=rows, batch_size=BS, min_length=1))
print(stats)batch_size=1: {'calls': 16, 'rows': 384, 'mismatch': 0}
batch_size=4: {'calls': 16, 'rows': 1248, 'mismatch': 528} # 42% of replayed hash_moe tokens routed with another token's id
debug, batch 0 of the bs=4 run: captured rows 78 = kept tokens 78, grid 96 (4 x 24, padded); row_index spans 0..77
Suggested fix
In _pack_token_aligned_kwargs, map each row_index through the same kept-row structure the mask path uses: _token_row_source_structure(mask, row_count, sinks, exclude_sinks) gives documents/positions, and then the id is input_ids[documents[r], positions[r]]. When the structure cannot be resolved, skip the kwarg so the _AWQScaleGroupError guard and the fallback apply, instead of guessing.
Test that should have caught it
tests/test_awq_deepseek_v4_hash_moe_4880.py runs only batch_size=1 with no padding and checks only the id count (ids == token_rows). It needs a batch_size > 1 case with rows of different lengths that checks the id values against the kept grid. A case with exclude_attention_sinks=True would also be useful.
Severity
Medium-low. The result is a silent AWQ quality loss on DeepSeek-V4 hash_moe layers (the first 3 layers of every released checkpoint) whenever calibration uses batch_size > 1 with variable-length rows, which is the common case. It does not crash.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 17, 2026
Agent claim:
agent-root-5015is working on this issue now.Qubitium commentedon Nov 17, 2026
Agent release:
agent-root-5015finished this issue. Fix: #5030, squash-merged asc060a9fc8.AWQProcessor._pack_token_aligned_kwargsnow maps each provenancerow_index(a kept row, after padding and attention-sink stripping) back onto the capture batch's[batch, seq]grid through the same_token_row_source_structurethe mask/position replay uses, via the new_kept_row_grid_positionshelper, and readsinput_ids[document * seq_len + position]. The identity mapping is used only for an unpadded capture; an unresolvable index still skips the kwarg so_AWQScaleGroupErrorand the configured fallback remain the safety net.New
tests/test_awq_hash_moe_kept_rows_5015.py(independent oracle over the kept grid): red on unmodifiedmainwith320/944 replayed hash_moe tokens were routed with another token's id(batch_size=4, variable lengths) andan excluded attention-sink token was replayed(batch_size=4, 8 BOS sinks dropped); green here (3 passed), and the batch_size=1 node passes on both trees. Neighbouring AWQ suites: 194 passed, 6 failed (the pre-existing afmoe/flint/jamba nodes, identical on main), 2 skipped. The new file runs in the AWQ CPUunitlane.fix(awq): map kept-row indices back onto the padded grid (ModelCloud#…