Repository navigation
[QUALITY] AWQ routed-expert captures keep calibration padding rows at batch_size>1 (#4035's MoE padding fix is GPTQ-only): DeepSeek-V4 hash layers send all pad rows to 6 experts each (58-75% of their AWQ sample); OLMoE bs=4 expert rows 8.14x tokens vs GPTQ 8.00x #5337
Description
Found on main @ c03416c, RTX 4090 (CUDA 13.2, torch 2.15, transformers 5.22, defuser 0.0.31).
What is wrong
At batch_size > 1, AWQ's routed-expert captures include the calibration padding rows. GPTQ excludes them for the same models and calibration.
#4035 (fix of #4017 item 4) handles padding for natively routed experts in GPTQ only:
GPTQProcessor.register_moe_root_capture_hook(looper/gptq_processor.py:742-779) zeroes the padded rows of the MoE input.- The expert hook (
gptq_processor.py:700-735) drops all-zero rows, so padding never reaches an expert Hessian ornsamples.
AWQProcessor has neither. Its register_moe_root_capture_hook (looper/awq_processor.py:1760) only records the root input. _apply_active_keep_mask_to_feature (awq_processor.py:1712) can drop padding only from [B, S, H] or [B*S, H] tensors. A routed expert sees [routed_rows, H], so the mask never matches. Every padded position that the router sends to an expert goes into that expert's reservoir. It counts toward raw_tokens and the starvation/fallback check, and it is used in the expert's scale and clip search. Dense modules and the MoE root drop padding correctly, which hides the problem in the logs.
Padding is not spread evenly. Pad positions share one token and nearly identical hidden states, so the router sends them all to the same few experts:
- OLMoE-1B-7B (2-layer copy), 128 C4 docs,
batch_size=4, defaultsort="desc"(only 1.8% padding): 9,344 pad rows per layer land in expert captures. Layer 0experts.57gets +1,164 rows (+12.8%), and layer 1experts.34gets +1,006 rows (+17%). - DeepSeek-V4-Flash (3-layer BF16, hash routing), 32 docs,
batch_size=4(498 pad positions, right padding, pad id 1):tid2eid[pad_id]sends every pad row to the same 6 experts in each hash layer. For those 18 experts, padding is 58-75% of the AWQ sample:
| layer | experts receiving all 498 pad rows | their raw rows (pad share) |
|---|---|---|
| 0 | 239, 202, 122, 23, 115, 57 | 670 (74%), 856, 765, 774, 1037, 670 (74%) |
| 1 | 63, 126, 219, 21, 170, 150 | 664 (75%), 793, 767, 790, 735, 781 |
| 2 | 217, 221, 240, 26, 247, 39 | 857 (58%), 727, 775, 1046, 734, 1487 |
Across all routed experts, the summed rows are 94,488 = top-6 x 15,748 padded positions. The real token count is 15,250, which would give 91,500.
Repro
import torch
from gptqmodel import GPTQModel
from gptqmodel.quantization import config as C
from gptqmodel.looper import awq_processor as AP
stats = []
orig = AP.AWQProcessor._layer_input_features
def spy(self, state):
feats = orig(self, state)
s = self._feature_stats
experts = sum(v["raw_tokens"] for k, v in s.items() if ".experts." in k and k.endswith("gate_proj"))
stats.append((s["self_attn.q_proj"]["raw_tokens"], experts))
return feats
AP.AWQProcessor._layer_input_features = spy
texts = c4_docs[:128] # C4 docs >= 1500 chars, truncated to 2500 chars
m = GPTQModel.load("<OLMoE-1B-7B-0924, num_hidden_layers=2 config copy>", C.AWQConfig(bits=4, group_size=128), dtype=torch.bfloat16)
m.quantize(C.CalibrationConfig(dataset=texts, batch_size=4))
for real, experts in stats:
print(real, experts, experts / real)Output (top-k = 8):
AWQ batch_size=4: 64652 526560 8.1445 (both layers) <- 9344 padded rows routed to experts
GPTQ batch_size=4: expert nsamples sum = 8.000 x 64652 (both layers)
With the GPTQ handling mirrored into AWQ (prototype below), the AWQ sum is exactly 8.000 x 64652 in both layers. The prototype removed 56,064 pad rows (2 layers x 3 projections x 9,344).
Measured quality impact (full OLMoE-1B-7B-0924, 16 layers)
Setup: 128 C4 docs, CalibrationConfig(batch_size=8, sort=None), AWQConfig(bits=4, group_size=128), bf16. Quantize, save, reload with AwqMarlin, then wikitext-2 PPL over 40 x 512 windows. The bf16 base is 10.6925.
| run | pad rows in routed-expert captures per layer | PPL |
|---|---|---|
batch_size=1 (no padding) |
0 | 11.2870 |
batch_size=8, main |
130,976 (20% of all expert rows; layer 0 experts.6: about 42%) |
11.4213 |
batch_size=8, main + prototype fix (zero pads at the MoE input, drop all-zero expert rows) |
0 (expert rows sum to exactly 8.000 x tokens) | 11.3670 |
Excluding the padding removes 0.054 PPL, about 40% of the bs=8 vs bs=1 gap, with the same calibration data.
Suggested fix
Do what #4035 did for GPTQ:
- In
AWQProcessor.register_moe_root_capture_hook, also register the forward pre-hook that multiplies the MoE input by the padding-only mask (active_padding_mask(self)). - In
_record_input_feature, drop all-zero rows from 2-D routed-expert captures when the batch has padding.
Hash-routed layers (DeepSeek-V4 hash_moe) route by input_ids, not by hidden state, so step 1 does not change their routing. Step 2 still removes the rows. A more robust alternative is to carry the padding mask through the routed-row index (the router knows which (b, s) each row came from) and filter by it, since a real all-zero row is theoretically possible.
Test that should have caught it
tests/test_moe_padding_rows.py covers only the GPTQ hooks. test_moe_batch_equivalence compares bs=1 and bs=2 for GPTQ. The same equivalence for AWQ (routed-expert raw_tokens at bs=1 equal to bs>1, and a pad-heavy batch not changing any expert's kept rows) would catch this.
Severity
Medium. The AWQ quality loss is silent at any batch_size > 1. The effect is concentrated: a few experts per layer get a sample dominated by pad-token activations, which is 58-75% of 18 experts in DeepSeek-V4-Flash with only 3% padding. It grows with padding: sort=None/shuffled calibration, larger batches, and mixed-length chat data.
🤖 Generated with Claude Code
Activity
Qubitium commentedon Nov 19, 2026
Agent claim released:
agent-fb5145b84190finished this issue.Pull request: #5346
mirror #4035 padding handling into AWQ routed-expert captures + test
fix(awq): drop calibration padding rows from routed-expert captures (#…