Skip to content

fix(awq): keep bypass-mode candidate replays on native routing - #5934

Merged
Qubitium merged 1 commit into
mainfrom
fix/awq-bypass-replay-reference-only-5730
Nov 20, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/awq-bypass-replay-reference-only-5730

Conversation

@Qubitium

@Qubitium Qubitium commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5730.

AWQProcessor._search_best_scale kept _moe_replay_routing (and therefore _all_experts_replay_forward) installed around the whole 20-candidate scale grid. In bypass mode the wrapper runs every expert's gate/up on every replay token and discards the outputs; only the native routed output reaches the loss. The all-experts lifecycle is only needed by the reference pass, whose forward pre-hooks record which group members were observed (#4223).

What Changed

  • AWQProcessor._moe_replay_routing(..., all_experts: bool = True): the bypass lifecycle (all-experts wrapper) is only entered when all_experts; the top-k override is unchanged and still applies to both passes.
  • AWQProcessor._search_best_scale: [STEP 4] (_compute_best_scale and its kwargs) moved out of the reference-pass with block and now runs under a second _moe_replay_routing(..., all_experts=False) context that keeps the captured top-k override but uses native routing.
  • New test tests/test_awq_bypass_candidate_replay_5730.py (real tiny Qwen3-MoE, CPU) counts expert gate rows per MoE-block row during each _compute_best_scale call and asserts native top-1 routing (~1 row/token) instead of the all-experts wrapper (E+1 rows/token). It also asserts the reference pass still scores all 8 experts.

No API change; down_proj groups are unaffected (their replay boundary is down_proj itself, so _moe_bypass_replay already returned None).

Tests

# New regression (PR head)
PYTHONPATH=. /root/venv/bin/python -m pytest tests/test_awq_bypass_candidate_replay_5730.py -q -p no:randomly
# 1 passed

# Same test with the source change reverted (base 74a480eca; `awq_processor.py` is identical at current main e86962382)
# 1 failed: "candidate replay observed 3060 expert gate rows for 340 MoE block rows (9.00 rows/token)"

# AWQ bypass/lifecycle neighbours
PYTHONPATH=. /root/venv/bin/python -m pytest tests/test_awq_moe_bypass_4223.py tests/test_awq_gptq_lifecycle.py tests/test_tiny_moe_quant_smoke.py tests/test_subset_plan.py -q -p no:randomly
# 21 passed, 1 skipped (test_awq_weight_mean_gpu_matches_cpu_reference: CUDA-only)

# AWQ scale-group neighbours
PYTHONPATH=. /root/venv/bin/python -m pytest tests/test_awq_moe_prune_uncovered_4456.py tests/test_awq_moe_starved_expert_group_4675.py tests/test_awq_scale_search_safety.py tests/test_awq_scale_search_memory.py tests/test_awq_moe_padding_rows_5337.py tests/test_awq_moe_shared_expert_group_4760.py -q -p no:randomly
# 77 passed, 1 failed: test_tiny_qwen3_moe_awq_fallback_none_keeps_uncovered_experts_dense
#   ValueError: AWQ: failed to build torch.ops JIT extension: CUDA_HOME environment variable is not set
#   (fails identically on base 74a480eca; host has no CUDA toolkit)

# Rebased onto current main (e86962382) and rerun
PYTHONPATH=. /root/venv/bin/python -m pytest tests/test_awq_bypass_candidate_replay_5730.py tests/test_awq_moe_bypass_4223.py tests/test_awq_gptq_lifecycle.py tests/test_tiny_moe_quant_smoke.py tests/test_subset_plan.py -q -p no:randomly
# 22 passed, 1 skipped (same CUDA-only node)

PATH=/root/venv/bin:$PATH bash format/check.sh
# All checks passed! (ruff 0.14.2)

Measured Impact

Workload: real tiny Qwen3MoeForCausalLM (1 layer, hidden 64, 8 experts, top-1, moe_intermediate 32), 17 calibration tokens, AWQ 4-bit group 16, FORMAT.GEMM, MoEConfig(routing=ExpertsRoutingBypass()), CPU, torch 2.15.0+cu132, Python 3.14t. Only one scale group (all-experts gate/up) crosses the MoE block; it runs 20 candidates. Wall times are three runs each.

Metric (unit) Before After Change (after − before)
Expert gate rows in the MoE-crossing candidate call (rows) 3,060 340 −2,720 (−88.9%)
That call's wall time (s, mean of 3) 0.754 0.363 −0.391 (−51.9%)
Full quantize() wall time (s, mean of 3) 2.793 2.356 −0.437 (−15.6%)
Seeded quantized state_dict SHA-256 37e985a8… 37e985a8… identical
Seeded quant_log loss SHA-256 dc7390f1… dc7390f1… identical

Commands: /root/bench_5730.py (per-call counters + wall time) and /root/bench_5730_hash.py (torch.manual_seed(0) before the fixture, SHA-256 over the quantized state_dict and over sorted (module, loss) rows), run on 74a480eca and on the PR head. The identical hashes show the discarded all-experts pass did not affect scales, losses, or the checkpoint.

Not measured: CUDA / Qwen3-30B-A3B (host has one GPU but no CUDA toolkit, so no GPU AWQ run was possible). The issue's 16x gate/up FLOP ratio for Qwen3-30B-A3B is an estimate, not a measured wall time here.

Model Support / Module Plan

Not applicable: no MODEL_MAP entry, model type, definition, or module-plan change.

Logic Flow Documentation

  • This change does not alter a major logic flow. It only moves an existing context manager's exit above the candidate grid; no decision mechanism, resolution order, dispatch, or failure contract changes. A diagram would restate the two-line scope move, so none is included.

Kernel Accuracy Requirements

  • Not a kernel change: no kernel math, packing, or format support is touched. The seeded quantized checkpoint hash is identical to base.

Review Requirements

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Notes

  • The candidate grid now runs with native routing, exactly as the issue's suggested fix. Because the reference fp16_output is produced by the wrapper's original_forward (native) call, the scored loss is unchanged; the seeded hashes confirm it.
  • Follow-up (out of scope, as the issue suggests): the reference pass still counts bypass-only experts as "observed" even though their weights never enter the objective. That coverage gap is unchanged by this PR.

`_search_best_scale` kept the `_all_experts_replay_forward` wrapper installed
for the whole 20-candidate grid. The wrapper's gate/up outputs are discarded
and only the native routed output reaches the loss, so every candidate paid
for an all-experts pass on every replay token.

Limit the all-experts lifecycle to the reference pass that needs the
observation (#4223); run the candidate grid under the same top-k override but
with native routing.
@Qubitium
Qubitium force-pushed the fix/awq-bypass-replay-reference-only-5730 branch from 13fb831 to 400a5c8 Compare November 20, 2026 15:41
@Qubitium
Qubitium marked this pull request as ready for review November 20, 2026 15:41
@Qubitium
Qubitium merged commit db62ced into main Nov 20, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/awq-bypass-replay-reference-only-5730 branch November 20, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Audit][AWQ] Bypass-mode scale search runs every expert on all tokens for all 20 candidates and discards the outputs

1 participant