Repository navigation
fix(awq): keep the MoE block signature visible in bypass replay (#5095) - #5142
Merged
Merged
Conversation
`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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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**kwargsblock.Root cause
AWQProcessor._all_experts_replay_forwardinstalls an instance-levelall_experts_forward(hidden_states, **kwargs)on the MoE block. #5019 made_forward_accepts_var_kwargstreat anyVAR_KEYWORDforward as accepting every layer kwarg (needed forHymbaBlock).inspect.signature(moe_block.forward)therefore saw the wrapper, not the block:_sanitize_kwargskeptposition_ids,attention_mask,position_embeddings, ..._prepare_module_forwardsetsupports_position_ids = True, promoted the 2-D replay to 3-D and synthesizedposition_ids.SparseMoeBlock.forward(hidden_states), which raisedTypeError: ... 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:#842029file:lineevidence on e2f9676: wrapper atgptqmodel/looper/awq_processor.py:3377, direct forward at:3410,_forward_accepts_var_kwargsat: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:#0f5132New
file:linepointers:functools.wrapsatgptqmodel/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.signaturefollows__wrapped__, so the decorated wrapper reports the block's real(hidden_states). Hymba'sHymbaBlock.forward(self, hidden_states, past_key_value=None, **kwargs)still declaresVAR_KEYWORDin its own function and keeps receiving the captured keys, so #4891 is not regressed.safe_kwargs_calladds 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
hidden_statesreaches the original forward**kwargsblock, bypass replayfunctools.wraps**kwargsblock; the regression test failsinspect.signatureraises_forward_accepts_var_kwargsis false;safe_kwargs_callforwards kwargs unchangedBefore / after
OlmoeSparseMoeBlock/LinearQwen3MoeSparseMoeBlockwith AWQ bypassTypeErrorin layer 0HymbaBlockwith AWQ (#4891)**kwargskeys forwardedEvidence
OLMoE-1B-7B-0924 truncated to 2 layers, the issue's 16 C4 samples,
CUDA_VISIBLE_DEVICES=1:TypeError: OlmoeSparseMoeBlock.forward() got an unexpected keyword argument 'position_ids'at_iter_prepared_forward_outputs→all_experts_forwardactivation_raw_tokensover 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
tests/test_awq_moe_bypass_4223.py::test_bypass_wrapper_keeps_the_block_signature_and_filters_kwargsTypeErrorat:3410tests/test_awq_moe_bypass_4223.py::test_bypass_wrapper_is_not_a_var_keyword_block_for_kwarg_sanitizingassert sanitized == {}fails with both keys kepttests/test_awq_moe_bypass_4223.py::test_awq_bypass_replay_scales_every_expertTypeErrorin layer 0**kwargsreplay unchanged (#4891)tests/test_hymba_awq_4891.pyCommands (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_bypasshit 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.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.shitself cannotpip installin this host's externally-managed Python (PEP 668), so the same ruff invocation was run from the venv.MoEConfig) on the tiny Qwen3-MoE fixture → 28 modules, 24 expert modules, completed.Scope / unverified
**kwargsblock and fold the norm the layer owns (#4891) #5019/[QUALITY] #4772 (fix of #4741) incomplete: Cohere2-MoE AWQ still sends q/k/v + every routed/shared expert gate/up to plain quantization in every MoE layer (router consumer outside the self_attn replay boundary) #4898; realnvidia/Hymba-1.5B-Baseweights and CUDA mamba kernels are not installed on this host.afmoe/flint/bailingfailures above are pre-existing environment failures, not caused by this change; they reproduce on the base commit.Docs:
docs/moe-routing-override.md§8 (same two diagrams plus the failure contract), linked from the AWQ/MoE routing bullet inREADME.md.