Skip to content

fix(awq): keep the MoE block signature visible in bypass replay (#5095) - #5142

Merged
Qubitium merged 1 commit into
mainfrom
fix/5095-awq-bypass-kwargs
Nov 18, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/5095-awq-bypass-kwargs

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Fixes #5095. AWQ with MoEConfig(routing=ExpertsRoutingBypass()) crashed in layer 0 on every standard MoE block after #5019. The fix is 4 files, +244/−3: the replay wrapper keeps the block's declared signature visible and forwards only accepted keywords; a docs mechanism page and two regression tests pin the distinction from Hymba's real **kwargs block.

Root cause

AWQProcessor._all_experts_replay_forward installs an instance-level all_experts_forward(hidden_states, **kwargs) on the MoE block. #5019 made _forward_accepts_var_kwargs treat any VAR_KEYWORD forward as accepting every layer kwarg (needed for HymbaBlock). inspect.signature(moe_block.forward) therefore saw the wrapper, not the block:

  • _sanitize_kwargs kept position_ids, attention_mask, position_embeddings, ...
  • _prepare_module_forward set supports_position_ids = True, promoted the 2-D replay to 3-D and synthesized position_ids.
  • The wrapper forwarded those keys to the original SparseMoeBlock.forward(hidden_states), which raised TypeError: ... unexpected keyword argument 'position_ids'.

Before #5019 the name filter used the wrapper signature too but kept only hidden_states, so nothing extra was forwarded.

Old flow (removed mechanism)

flowchart TD
    A["AWQProcessor._all_experts_replay_forward"] --> B["moe_block.forward = all_experts_forward(hidden_states, **kwargs)"]
    B --> C["_forward_accepts_var_kwargs(moe_block) is True"]
    C --> D["_sanitize_kwargs keeps position_ids, attention_mask, position_embeddings"]
    C --> E["_prepare_module_forward sets supports_position_ids = True"]
    E --> F["2-D replay promoted to 3-D and position_ids synthesized"]
    D --> G["module(x, **module_kwargs)"]
    F --> G
    G --> H["all_experts_forward forwards every key"]
    H --> I["OlmoeSparseMoeBlock.forward(hidden_states) raises TypeError"]
    style C fill:#f8d7da,stroke:#b02a37,color:#842029
    style H fill:#f8d7da,stroke:#b02a37,color:#842029
    style I fill:#f8d7da,stroke:#b02a37,color:#842029
Loading

file:line evidence on e2f9676: wrapper at gptqmodel/looper/awq_processor.py:3377, direct forward at :3410, _forward_accepts_var_kwargs at :342, promotion at :4526, sanitizing at :5162.

New flow

flowchart TD
    A["AWQProcessor._all_experts_replay_forward"] --> B["original_forward = moe_block.forward"]
    B --> C["@functools.wraps(original_forward)<br/>all_experts_forward(hidden_states, **kwargs)"]
    C --> D{"inspect.signature(moe_block.forward)"}
    D -- "follows __wrapped__" --> E["block's declared parameters"]
    E --> F{"original declares **kwargs?"}
    F -- yes --> G["_sanitize_kwargs keeps captured keys<br/>Hymba #4891 path"]
    F -- no --> H["_sanitize_kwargs keeps only declared names<br/>standard SparseMoeBlock path"]
    H --> I["_prepare_module_forward does not promote or synthesize position_ids"]
    G --> J["replay call"]
    I --> J
    J --> K["all_experts_forward feeds every expert"]
    K --> L["safe_kwargs_call(original_forward, hidden_states, kwargs)"]
    L --> M{"signature inspection succeeds?"}
    M -- yes --> N["forward only accepted keywords"]
    M -- no --> O["forward kwargs unchanged<br/>inspection-failure fallback"]
    N --> P["routed output"]
    O --> P
    style M fill:#fff3cd,stroke:#997404,color:#664d03
    style O fill:#fff3cd,stroke:#997404,color:#664d03
    style H fill:#d1e7dd,stroke:#0f5132,color:#0f5132
    style L fill:#d1e7dd,stroke:#0f5132,color:#0f5132
Loading

New file:line pointers: functools.wraps at gptqmodel/looper/awq_processor.py:3392, wrapper at :3393, filtered call at :3429, promotion gate at :4545, sanitizer at :5181.

Why this distinguishes the two cases

The predicate is not "who installed the forward" but "what does the forward declare". inspect.signature follows __wrapped__, so the decorated wrapper reports the block's real (hidden_states). Hymba's HymbaBlock.forward(self, hidden_states, past_key_value=None, **kwargs) still declares VAR_KEYWORD in its own function and keeps receiving the captured keys, so #4891 is not regressed. safe_kwargs_call adds a second guard: even if a caller passes an unsupported key to the wrapper, only declared keywords reach the original forward.

Failure and ambiguity path

Condition Result
Standard MoE block, bypass replay Only hidden_states reaches the original forward
Real **kwargs block, bypass replay Every captured, non-private key reaches the block
Instance wrapper without functools.wraps Treated as a **kwargs block; the regression test fails
inspect.signature raises _forward_accepts_var_kwargs is false; safe_kwargs_call forwards kwargs unchanged

Before / after

Situation Before After
OlmoeSparseMoeBlock / LinearQwen3MoeSparseMoeBlock with AWQ bypass TypeError in layer 0 392 modules / 1,817,856 expert rows on the issue's 2-layer OLMoE fixture
Hymba HymbaBlock with AWQ (#4891) **kwargs keys forwarded unchanged
Unsupported key passed to the replay wrapper forwarded, raised inside the block dropped before the original forward

Evidence

OLMoE-1B-7B-0924 truncated to 2 layers, the issue's 16 C4 samples, CUDA_VISIBLE_DEVICES=1:

Tree Result
e2f9676 TypeError: OlmoeSparseMoeBlock.forward() got an unexpected keyword argument 'position_ids' at _iter_prepared_forward_outputs → all_experts_forward
this PR quantize completes: 392 modules, activation_raw_tokens over expert modules = 1,817,856 (pre-#5019 baseline in the issue: 392 / 1,817,856)

The expert-row number is sum(activation_raw_tokens) over the 384 expert modules, each 4,734 rows: 384 × 4,734 = 1,817,856.

Test plan

Invariant / trigger Test Red on e2f9676 Green here
Wrapper keeps the block signature and forwards only accepted keys tests/test_awq_moe_bypass_4223.py::test_bypass_wrapper_keeps_the_block_signature_and_filters_kwargs TypeError at :3410 pass
Sanitize/promotion see the real signature for an instance wrapper tests/test_awq_moe_bypass_4223.py::test_bypass_wrapper_is_not_a_var_keyword_block_for_kwarg_sanitizing assert sanitized == {} fails with both keys kept pass
End-to-end bypass replay scales every expert tests/test_awq_moe_bypass_4223.py::test_awq_bypass_replay_scales_every_expert TypeError in layer 0 pass
Hymba **kwargs replay unchanged (#4891) tests/test_hymba_awq_4891.py pass pass

Commands (all with CUDA_VISIBLE_DEVICES=1 PYTHONPATH=<worktree> HF_HUB_OFFLINE=1 /root/venv/bin/python -m pytest):

  • tests/test_awq_moe_bypass_4223.py → 3 passed.
  • tests/test_moe_bypass_batch_size.py tests/test_hymba_awq_4891.py tests/test_awq_gptq_lifecycle.py tests/test_moe_expert_batching.py tests/test_moe_config.py tests/test_phimoe_moe_routing_4636.py → 50 passed; test_moe_config.py::TestMoEConfig::test_moe_routing_bypass hit a transient GPU OOM from another process and passed on retry (1 passed).
  • tests/test_awq_position_kwargs.py tests/test_awq_cohere2_moe_parallel_norm_4898.py tests/test_hymba_bf16_load_4888.py → 14 passed.
  • Extended AWQ family (test_awq_scale_search_safety.py, test_awq_scaling_groups.py, test_awq_hybrid_moe_root_4903.py, test_awq_hybrid_recurrent_mask_4671.py, test_awq_moe_root_capture_4697.py, test_awq_moe_prune_uncovered_4456.py, test_awq_moe_starved_expert_group_4675.py, test_awq_moe_shared_expert_group_4760.py) → 222 passed, 2 skipped; 5 failures are identical on e2f9676 (afmoe/flint/bailing, defuser 0.0.28 < 0.0.31 fused-expert split).
  • tests/test_docs_contract_refs.py → 23 passed.
  • ruff check gptqmodel docs/eora tests setup.py (ruff 0.14.2) → all checks passed. format/check.sh itself cannot pip install in this host's externally-managed Python (PEP 668), so the same ruff invocation was run from the venv.
  • Native routing control: AWQ (no MoEConfig) on the tiny Qwen3-MoE fixture → 28 modules, 24 expert modules, completed.

Scope / unverified

Docs: docs/moe-routing-override.md §8 (same two diagrams plus the failure contract), linked from the AWQ/MoE routing bullet in README.md.

`AWQProcessor._all_experts_replay_forward` installs an instance-level
`all_experts_forward(hidden_states, **kwargs)` on the MoE block. Since #5019,
`_forward_accepts_var_kwargs` read that catch-all as a real declaration, so
`_sanitize_kwargs` kept every captured layer kwarg and
`_prepare_module_forward` promoted the 2-D replay and synthesized
`position_ids`. The wrapper then forwarded those keys to the original
`SparseMoeBlock.forward(hidden_states)`, which raised
`TypeError: ... unexpected keyword argument 'position_ids'` in layer 0.

Decorate the wrapper with `functools.wraps(original_forward)`: `inspect.signature`
follows `__wrapped__`, so the replay path sees the block's declared signature
again while Hymba's genuine `(hidden_states, past_key_value=None, **kwargs)`
block still reports `VAR_KEYWORD`. The wrapper also calls the original forward
through `safe_kwargs_call`, so a caller cannot widen that contract.

Regression coverage in `tests/test_awq_moe_bypass_4223.py` drives the real
instance wrapper through `_sanitize_kwargs` and `_prepare_module_forward` with
`position_ids`/`attention_mask` and asserts the original block receives only
the keys it declares. The existing end-to-end bypass replay test now passes.

OLMoE-1B-7B-0924 truncated to 2 layers with the issue's C4 sample:
e2f9676 raises the TypeError; this commit quantizes 392 modules and captures
1,817,856 expert rows, matching the pre-#5019 baseline.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant