Repository navigation
Description
Summary
With MoEConfig(routing=ExpertsRoutingBypass()), _search_best_scale enters _moe_replay_routing, which installs _all_experts_replay_forward on the MoE block. That wrapper runs gate_proj/up_proj (and down_proj when targeted) of every expert in the scale group on all replay tokens, throws the results away, and then calls the native routed forward. Only the native forward's output reaches the loss. The wrapper exists so the forward pre-hooks mark the targets as "observed" (#4223). Those hooks are removed right after the reference pass, but the context also encloses the 20-candidate _compute_best_scale grid. So every candidate pays for a dense all-experts pass whose result is never used.
Impact
- Quant speed: high for bypass-mode AWQ on MoE models. Each of the
n_grid=20candidate replays of a routed gate/up group runs aboutE/top_ktimes the routed expert gate/up FLOPs on top of the native forward (Qwen3-30B-A3B: 128/8 = 16x the routed gate/up compute per candidate). In a CPU toy MoE (E=64, top-4, N=2048) the 20 candidate replays take 3.0x longer with no change in output (below). - Quant accuracy: none directly, because the outputs are discarded. But the "observed" signal the wrapper produces does not mean the objective scores those experts. A bypass-only expert still contributes nothing to the loss, so the coverage guard at 4744-4776 passes without actually checking anything in this mode.
Location (@ 85caf6c)
gptqmodel/looper/awq_processor.py
4707 with self._moe_replay_routing(module, module2inspect, replay_targets):
4708 try:
4709 for _name, target in replay_targets:
4710 handles.append(target.register_forward_pre_hook(record_target))
... # reference pass
4741 finally:
4742 for handle in handles:
4743 handle.remove() # observation ends here
...
4794 best_scales, loss = self._compute_best_scale( # 20 candidate replays, still
... # under the all-experts wrapperand the wrapper (awq_processor.py:4560-4581):
for expert, gate, up, down in experts:
...
gate_out = gate_ref(expert_input) if gate_ref is not None else None
up_out = up_ref(expert_input) if up_ref is not None else None
if down_ref is not None:
...
down_ref(intermediate)
del gate_out, up_out, expert_input
return safe_kwargs_call(original_forward, hidden_states, kwargs=kwargs)Mechanism
_moe_replay_routing yields once and stays open for the whole body, which includes _compute_best_scale. Inside the wrapper, every gate_out/up_out/down_ref(...) result is deleted unused. During candidate replays, nothing reads them: no hook is registered and the returned tensor comes only from original_forward.
Reproduction / Validation (executed, CPU. A pure-torch toy that mirrors the wrapper structure. The real processor could not be imported on this host because of the torch-safety gate.)
Script bypass_replay_waste.py: a toy MoE block, with a wrapper that runs gate/up of every targeted expert on all tokens, discards the results, and then calls the routed forward. The timing covers 20 calls, like the candidate grid.
E=64 top4 N=2048: 20 candidate replays native=1.21s with bypass wrapper=3.64s (x3.0); identical outputs: True
Suggested fix
Limit the all-experts wrapper to the reference pass that needs the observation, and run the candidate grid with native routing (plus the top-k override, which does change the scored output):
- with self._moe_replay_routing(module, module2inspect, replay_targets):
- try:
- ...reference pass...
- finally:
- ...
- ...
- best_scales, loss = self._compute_best_scale(...)
+ with self._moe_replay_routing(module, module2inspect, replay_targets):
+ try:
+ ...reference pass...
+ finally:
+ ...
+ unobserved = ...
+ with self._moe_replay_routing(module, module2inspect, replay_targets, all_experts=False):
+ best_scales, loss = self._compute_best_scale(...)(_moe_replay_routing would get an all_experts flag that skips _moe_bypass_replay.) Because the reference fp16_output comes only from original_forward, the scored loss is bit-identical.
Separately, consider whether the reference-pass "observation" should count bypass-only experts at all. Their weights never enter the objective, so treating them as scored hides the same coverage gap that allow_partial_replay already reports with a warning.
Notes
- Not executed on CUDA. The 16x figure for Qwen3-30B-A3B is the FLOP ratio for gate/up of all 128 experts vs the 8 routed ones, not a measured wall time.
down_projscale groups (prev_op=up_proj,module2inspect=down_proj) are unaffected, because_moe_bypass_replayreturnsNoneunless the boundary contains the MoE block.
Found by automated code audit (Claude Code).
Activity
Qubitium commentedon Nov 20, 2026
Agent claim released:
agent-fb5145b84190finished this issue.Pull request: #5934
Restricting the all-experts bypass replay to the reference pass; measuring CPU MoE AWQ search
fix(awq): keep bypass-mode candidate replays on native routing (#5730)
fix(awq): keep bypass-mode candidate replays on native routing (#5730) (