Skip to content

[BUG] MoE: AWQ fold leaves router/shared_expert_gate unscaled (routing changes), AWQ crashes on DeepSeek-V2/V3 MLA, Ernie-4.5-MoE crash (_update_causal_mask), pad rows in expert Hessians at batch_size>1 #4017

Closed

Description

@Qubitium

Audit of main @ 5f5eaad, using tiny random MoE configs from transformers 5.22 (E = 4..8, top-k 2, 2 layers), RTX 5090.

Checked and working:

  • Router bypass keeps real routing for the layer output (replay at native k).
  • "all" / k overrides give the expected per-expert rows.
  • A 0-token expert falls back to RTN.
  • Router weights, e_score_correction_bias and gpt_oss expert biases survive save/reload.
  • Per-expert dynamic rules work.
  • The defuser split is fp-equivalent (< 5e-7) for 20 families.
  • The AWQ shared scale is applied consistently to all experts.

1. AWQ: the MoE gate/up scale is folded into post_attention_layernorm, but the router and shared_expert_gate are left unscaled, so routing changes (high)

awq_get_modules_for_scaling (models/base.py ~4400-4560) builds the norm → experts' gate/up group. apply_scale / scale_ln_fcs (quantization/awq/quantize/scale.py:139, 192) divides the norm by s and multiplies every expert's gate/up by s. But the router (mlp.gate, router) and qwen2's shared_expert_gate also read the normed hidden state and are not rescaled. Router logits become W_r · (x/s), so different experts get picked and the shared-expert sigmoid shifts. The scale search can't see this, because it scores the block with the router still reading the unscaled input.

Repro (qwen2_moe):

expert gate_proj column ratio matches s (median dev 0.014);  mlp.gate.weight / shared_expert_gate: ratio 1 (dev vs s: 0.31)
same input, layer 0: 6 of 40 tokens get a different expert set   (qwen3_moe 4/40, mixtral 1/40)
fold only, no quantisation — logits rel err: 0.011 (mixtral 0.052);  with router/shared_expert_gate columns × s: 0.0005 (0.0006)

On these tiny random models the scales are mild and end-to-end AWQ error is dominated by quantisation noise (0.160 vs 0.160 after compensation). Real models pick much wider scales, so the routing shift should matter more there.

This affects every AWQ MoE family whose router reads the post-attention norm: qwen2_moe, qwen3_moe, mixtral, llama4, glm/deepseek-style. Fix: when a norm is scaled, also multiply the input columns of every non-quantised linear consumer of that norm's output (router, shared_expert_gate) by s, or include them in the search's module2inspect path. Add an output-preservation check for MoE folds (the existing MoE AWQ test only checks that it runs). Related to #4005 (non-output-preserving AWQ groups in dense families).

2. AWQ crashes on DeepSeek-V2 / V3 (MLA attention); GPTQ works (high for these families)

  • With q_lora_rank set (V3/R1 style), the scale search fails: RuntimeError: mm: mat1 and mat2 shapes cannot be multiplied (20x32 and 128x32), because self_attn is re-run on the q_b input inside _search_best_scale.
  • With q_lora_rank=None, and on deepseek_v2: AssertionError at looper/awq_processor.py:1772 (assert len(layers) == 1).

Cause: the tree block [q_b_proj:in=q_a, kv_b_proj:in=kv_a] holds two layers with different inputs, and q_a_layernorm / kv_a_layernorm aren't declared as prev_ops. The same configs quantise with GPTQ (rel err 0.135).

Fix: declare q_a_layernorm:! / kv_a_layernorm:! and split q_b_proj and kv_b_proj into separate groups, or skip AWQ scaling for blocks whose layers have different inputs.

3. Ernie-4.5-MoE native checkpoints crash at the first calibration forward (high for this family)

definitions/ernie4_5_moe.py:47-109: monkey_patch replaces the native Ernie4_5_MoeModel.forward with an old remote-code forward that calls self._update_causal_mask, which transformers 5 removed:

AttributeError: 'Ernie4_5_MoeModel' object has no attribute '_update_causal_mask'
with the monkey_patch disabled: quantize OK (62 modules)

require_trust_remote_code=True doesn't help: the official baidu/ERNIE-4.5-21B-A3B-PT ships no auto_map or modeling file, so the native class is used. Fix: drop the patch, or apply it only when the loaded class is the remote one.

4. Padding rows get into routed-expert and shared-expert Hessians when batch_size > 1 (medium)

looper/gptq_processor.py:594-598 (and :625) applies the keep-mask only to inputs with dim() >= 3. Routed experts get 2-D [B*S, H] rows (from the HF expert loop and from the bypass path at moe_lifecycle.py:556), and HF qwen2_moe's shared expert is also called on a 2-D tensor. So padded rows reach add_batch, even though preserve_batch_keep_mask (gptq_processor.py:218) is meant to drop them. AWQ already handles this case in _apply_active_keep_mask_to_feature.

16 samples, 626 real tokens, layer-0 nsamples:

native  bs=2 : dense q_proj 626 | shared expert 644 | expert 0: 191 (bs=1: 173)
bypass  bs=2 : dense 626 | shared 626 | every expert 644
left-pad bs=4: shared 704 | expert 0: 251 (177 real-routed)
bs=1 vs bs=4 : dense quantised weights bit-identical; expert weights differ by 14-17% relative

Padded hidden states are nearly identical, so they pile into the same few experts.

Fix:

  • For 2-D inputs, filter rows with keep_mask.reshape(-1) when inp.shape[0] == keep_mask.numel() (bypass, 2-D shared expert).
  • For natively routed experts, zero the pad rows of the MoE-block input during calibration forwards (zero rows add nothing to XᵀX), or carry the row indices into the expert hooks.
  • Add an MoE bs=1 vs bs>1 equivalence test.

Minor: defuser>=0.0.30 (requirements.txt) isn't checked at runtime. With 0.0.28, the routed experts of afmoe / flint / granitemoe_swa stay fused 3-D tensors and are silently left unquantised; the only sign is a "0 layer matches" warning.

🤖 Generated with Claude Code

Activity

  1. added
    bug
    on Nov 14, 2026
  2. Qubitium commented on Nov 14, 2026

    @Qubitium
    CollaboratorAuthor

    Follow-up on #4035, re-checked with consumer_map passed: the router and shared_expert_gate are fixed. Routed and shared experts are still folded as two groups from the same norm (DeepSeek-V3: 0.049 / 0.080 relative logit change), see #4039.

  3. Qubitium commented on Nov 14, 2026

    @Qubitium
    CollaboratorAuthor

    Follow-up on #4035: zeroing the padded MoE rows introduced a regression. An expert that receives only padding at batch_size>1 gets an all-zero Hessian, and its whole weight is zeroed (|Wq|=0; the parent commit used the RTN fallback). See #4051 item 1.

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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions