feat: Add FlashComm1 and MMRS fusion support for Qwen3.5 NPU.#1890
feat: Add FlashComm1 and MMRS fusion support for Qwen3.5 NPU.#1890Cooofish wants to merge 11 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces the Flash Communication 1 (FC1) sequence-parallel optimization for tensor parallel inference on NPU, integrating it into layers like RowParallelLinear and FusedMoE, and adding a fused matmul_reduce_scatter NPU kernel. The review feedback identifies several style guide violations, including the use of member functions in the FlashComm1Context struct and direct access to FLAGS_ global variables. It also highlights critical issues such as potential undefined behavior from missing tensor definition checks, duplicate helper functions, a performance regression in MoE reduction when ep_size == 1, and a type mismatch where int64_t is used instead of int32_t for CANN/ATB host data.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
a2db15a to
dd852e4
Compare
9bb1d73 to
2c1850e
Compare
- Infer NPU Python, PyTorch, and torch_npu paths during CMake configure when the environment is unset. - Propagate CARGO_HOME into Rust custom commands and align the workspace Cargo mirror name with the root Cargo config. - Remove stale selected-gather and Device::type_str() remnants after the FC1 context refactor. - Keep FC1 default-off and raise the activation threshold to long-prefill TP=8 cases.
- Keep the wrapper focused on validating inputs, resolving the HCCL group, selecting comm_mode, and invoking torch_npu npu_mm_reduce_scatter_base. - Return an empty tensor on unsupported inputs so the existing row-parallel matmul plus reduce_scatter fallback remains owned by the caller. - Remove the duplicate local torch::matmul fallback and reject-reason string builder to make the FC1 MMRS path easier to review and maintain.
- Keep FlashComm1Context as a plain data struct and move sequence-shard helpers to free functions. - Reuse the shared FC1 row-padding helper from linear callsites instead of duplicating it. - Add defensive defined checks before inspecting MMRS input tensor metadata. - Update FC1 callsites to use the free helper while preserving the existing config and MoE reduction fixes.
- Restore cmake helper files to match upstream main. - Restore third_party submodule pointers to match upstream main so the PR only carries FC1 source changes.
Remove non-essential build/runtime/header changes from the FlashComm1 PR diff, drop unused MMRS parameters, and remove noisy success-path FC1 logs. Keep the FC1 and MMRS functional paths intact while reducing unrelated review surface.
Summary
This PR adds FlashComm1 (FC1) support for NPU tensor-parallel inference.
FC1 is a prefill-side communication optimization for long-context workloads. It shards hidden states along the sequence dimension across TP ranks, lets supported row-parallel linear layers consume the sharded layout, and uses Matmul + ReduceScatter fusion to reduce part of the communication and launch overhead in prefill.
The feature is disabled by default:
To enable the full FC1 path:
More details about design, usage, suitable workloads, and validation guidance are documented in:
Main Changes
FlashComm1Contextand helper functions for prefill sequence padding, sequence sharding, reduce, gather, and row-parallel layout handling.matmul_reduce_scatterkernel API backed by torch_npunpu_mm_reduce_scatter_base.fc1_ctxargument through all forward signatures.dp=1,cp=1,tp>=8, andprefill_tokens>=8192.MMRS Integration
The MMRS path intentionally uses the torch_npu fused operator instead of implementing a local fused kernel in xLLM:
comm_mode, and callsnpu_mm_reduce_scatter_base.mmrs_comm_modevalues areaiv,ai_cpu, andnone.aiv.ai_cputhrough the existing mode selection logic.Default Activation Policy
FC1 and MMRS are both opt-in. Even when enabled by flags, FC1 only becomes active when runtime conditions are suitable.
Default suitable case:
prefill_tokens >= 8192.tp >= 8.dp = 1.cp = 1.Cases where FC1 is disabled or expected to have limited benefit:
tp < 8,dp > 1, orcp > 1.Scope Notes
This PR intentionally keeps FC1 opt-in and avoids unrelated cleanup.
Excluded from this PR:
Validation
Build validation performed locally:
Result: passed, producing
build_flash_npu3/xllm/xllm.Ascend A3 and ACL Graph compatibility
On Ascend A3 (
Ascend910_93), some CANN 9.0-based xllm_ops/ATBpackages may incorrectly select the 910A CustomPagedAttention runner
when ACL Graph is enabled. This can cause CustomPagedAttention setup
to fail with status 27.
This issue is independent of FlashComm1/MMRS. Use an xllm_ops build
that correctly classifies
Ascend910_93as a 910B-family device anddeploy the matching
libatb_customize.soandlibcustomize_ops.sotogether.
Correctness checks performed locally:
Performance and profiling checks performed locally:
graph=true, 1K output, 8K/16K/32K input, parallel=2/4/8/16, and two warmup requests.tp >= 8before FC1 becomes active.MatmulReduceScatterV2appears when FC1/MMRS is enabled.Performance Notes
FC1 is not a general-purpose speedup switch. In the latest TP=8 matrix, the most balanced results are 32K input at parallel=4/8/16: TTFT improves by 1.94%-3.95%, prompt/decode/request throughput improves by 2.10%-2.65%, and average latency improves by 1.71%-1.97%. The 32K, parallel=2 case also improves TTFT by 6.10%, with throughput and latency essentially unchanged to slightly better.
The 8K cases improve TTFT by 3.95%-5.57%, but throughput and average latency are flat to about 1.6% worse. At 16K, parallel=2 is balanced (+3.26% TTFT and about +1.1% throughput), while parallel=8/16 improves TTFT by about 2.6% but regresses end-to-end throughput substantially. The 16K, parallel=4 result was remeasured three times; the final repeat is effectively neutral (+0.40% TTFT, -0.33% throughput), showing why a single TTFT average must not be interpreted as a stable FC1 gain. These results reinforce that FC1 primarily targets prefill latency; TTFT improvement does not guarantee an end-to-end throughput improvement when decode and layout/communication overhead dominate.
Current profiling limitations:
For this reason, FC1 remains disabled by default and should be enabled only for suitable long-prefill deployments.
Reviewer Focus
Please focus review on:
comm_modeselection, and fallback behavior.Recent Updates
MMRS wrapper simplification
comm_mode, and callsnpu_mm_reduce_scatter_base.torch::matmulfallback from the wrapper. Unsupported MMRS cases now return an empty tensor and let the existing row-parallel fallback remain in the caller.Gemini review fixes
FlashComm1Contextas a plain data struct and moved sequence-sharding helpers to free functions.linear.cppand reused the shared FC1 helper.defined()checks before inspecting MMRS input tensor metadata.ModelConfig::get_instance().backend(),EPLBConfig::get_instance().expert_parallel_degree(), preserve theep_size == 1single-reduce MoE optimization, and no longer contain the flagged Qwen3 ATBint64_thostData vectors.Diff-scope cleanup
main.CMakeLists.txtto match upstreammain.