[NPUW][MoE Scaling]Support MoE expert layout variants and add FoldShapeComputeChain pass#36184
Open
intelgaoxiong wants to merge 1 commit into
Open
[NPUW][MoE Scaling]Support MoE expert layout variants and add FoldShapeComputeChain pass#36184intelgaoxiong wants to merge 1 commit into
intelgaoxiong wants to merge 1 commit into
Conversation
a7baa19 to
919ab95
Compare
…ze to constant. Support Qwen/GPT-OSS MoE layout differences throughout inference pipeline GPT-OSS and Qwen use different 4-D tensor layouts for MoE expert output: GPT-OSS: [N, 1, T, H] (singleton at dim 1) Qwen: [N, T, 1, H] (singleton at dim 2) Both have identical flat memory strides; only shape metadata differs. Changes: - moe_transformation.cpp: fix_token_count_for_expert_iterative now scans middle dims (1..n-2) by value instead of hardcoding second-to-last index, so both layouts are correctly patched for chunked prefill. - moe_transformation.cpp: detect_and_transform_moe_downstream accepts both [N,1,H,W] and [N,H,1,W] parameter shapes for the ReduceSum pattern. - moe_infer_utils.cpp: parse_selected_experts_from_router and gather_router_scores detect layout by checking which dim equals 1. - moe_resources.cpp: expert_output_accumulator shape is derived from the compiled model output shape template instead of hardcoded [K,1,T,H]. Solved layout issue for GPT-OSS. Fixed link error. Runs the full shape-compute-chain folding pipeline in a single pass. Add FoldConstTest. Refine code. Signed-off-by: intelgaoxiong <xiong.gao@intel.com>
dc2cc23 to
65f67e9
Compare
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.
Details:
MoE models exported with different opset versions produce expert output tensors with the singleton dimension in different positions:
[num_experts, 1, token_num, hidden][num_experts, token_num, 1, hidden]Previously only Layout A was handled. This PR makes NPUW MoE inference layout-agnostic and adds a constant-folding pass to unblock pattern matching on static-shape graphs.
Validation job: https://cje-ir-prod01.devtools.intel.com/sai-npu-experience/job/Staging/job/ding/job/Validate/29/Validation_20report/
Changes
New:
FoldShapeComputeChainpass (fold_const.hpp/cpp)Four
MatcherPassclasses (FoldShapeOf,FoldGatherOfConst,FoldUnsqueezeOfConst,FoldConcatOfConsts) plus aModelPasswrapper that runs the full pipeline in one call, which makes partitioning easier.moe.cppGPTOSSRouter: removesShapeOf/topk_convertfrom the formal pattern (both are resolved before matching) and usesany_input()for all Slice shape inputs.GPTOSSExpert: decoding/prefill detection scans middle dims instead of assuming a fixedrank-2token dimension, accepting both layout variants.moe_transformation.cppReplaces
update_reshape_constant_dimension(fixed negative index) withupdate_reshape_dimensions(range-based scan over middle dims), correctly handling both 3-D and 4-D reshape patterns for both layout variants.moe_infer_utils.cpp/moe_resources.cppget_router_token_count(router_shape)helper to unify the two-layout token-dim detection inparse_selected_experts_from_routerandgather_router_scores.chunk_size == embed_dim.Tests
fold_const_test.cpp: three GTest cases verifyFoldShapeComputeChainon a graph mirroring the actual router subgraph.Tickets:
AI Assistance: