feat: add FlashComm1 and MMRS fusion support for Qwen3.5 NPU.#1890
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.
Remove the unused preallocated MMRS output parameter because torch_npu returns the result tensor directly. Also delete the unreferenced maybe_pad_for_reduce helper.
Route the existing and FC1-aware RowParallelLinear forward overloads through one implementation. Preserve the default reduction behavior while applying sequence-sharded handling only to explicit FC1 reduce modes.
|
最新commit 971dd79已本地build test通过 |
|
commit 6fa9994 本地build test已通过 I20260723 15:10:44.881584 2590209 setup.py:797] I20260723 15:10:44.881612 2590209 setup.py:798] I20260723 15:10:44.881636 2590209 setup.py:799] |
Invalidate the derived transposed weight cache when row-parallel weights are reloaded so FC1 MMRS cannot use stale weights.\n\nReject matmul_reduce_scatter on non-NPU backends instead of silently dropping its reduce-scatter semantics.
Make gather_sequence consume only the padded, equal-size TP shards produced by the FC1 reduce-scatter path. This removes the ambiguous per-rank length inference that could select inconsistent all-gather metadata when a real shard length happened to equal the padded length.\n\nRemove the unused FlashComm1Context is_prefill/local token state and helper APIs because prefill eligibility is already enforced while building the context and all active FC1 shards use padded_local_num_tokens.\n\nRemove stream_mode from the MMRS wrapper because it was never forwarded to torch_npu and therefore had no runtime effect.\n\nAdd focused tests for row padding and fused versus unfused reduce-scatter mode selection.
Remove the unused AttentionMetadata declaration, pass the stored FlashComm1 options directly, restore DenseMLP's const-reference forward signature, and drop an unrelated worker whitespace change. These are early implementation leftovers with no required FC1 behavior. Restore the upstream stage comments in Qwen3.5 attention and decoder code so the PR does not remove unrelated documentation, and group the NPU-only FC1 flags with the existing kernel configuration declarations. Remove the narrowly scoped tests added in the previous commit because they were mistakenly mapped to review item 7; item 7 concerns these small code residues, while comprehensive FC1 layout testing is a separate review item.
|
最新commit本地build test已通过 |
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 kernel-launch overhead in prefill.
The feature is disabled by default:
To enable the full FC1 path:
More details about the design, usage, suitable workloads, dependencies, 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_scatterAPI backed by torch_npunpu_mm_reduce_scatter_base.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.aivandai_cpu.ai_cpucan be selected explicitly through--mmrs_comm_mode=ai_cpufor environment-level diagnosis or fallback.ops-transformers dependency
The torch_npu MMRS path relies on the operator implementation provided by ops-transformers. The Ascend A3 validation environment uses CANN 9.0.0 together with the MMRS-related operator built from ops-transformers 9.1.0.
When
mmrs_comm_mode=aivis used, an ops-transformers 9.1.0 or newer operator library containing the MMRS AIV fix must be built, installed, and loaded at runtime. This resolves the earlierFIXP configuration illegal/0x3000084failure observed for some shapes with the older AIV operator.Only the required ops-transformers operator may be upgraded while retaining CANN 9.0.0, but the compile-time headers and runtime shared libraries must match. The loaded ops-transformers, torch_npu, ATB, and CANN components must remain ABI-compatible. The operator library path should be fixed explicitly in the deployment environment so that the tested build is loaded consistently.
The old shape-specific
aivtoai_cpuswitching logic has been removed because it encoded incomplete risky-shape knowledge and could not guarantee safety for arbitrary production shapes.ai_cpuremains available as an explicit configuration option, not an automatic per-shape policy.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.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/ATB packages 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 and deploy the matchinglibatb_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.Prefill-focused profiling confirmed that FC1+MMRS is active:
MatmulReduceScatterV2appears when FC1/MMRS is enabled.Performance Notes
FC1 is not a general-purpose speedup switch. In the selected multi-round 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 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); the remaining 16K points are more sensitive to run-to-run variation and should not be treated as stable headline gains. These results reinforce that FC1 primarily targets prefill latency: a TTFT improvement does not guarantee an end-to-end throughput improvement when decode, layout conversion, and 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 caller-side fallback behavior.Recent Updates
MMRS wrapper simplification
comm_mode, and callsnpu_mm_reduce_scatter_base.torch::matmulfallback. Unsupported MMRS cases return an undefined tensor and let the existing row-parallel caller use its fallback path.defined()checks before reading MMRS tensor dimensions or metadata.Sequence-layout and model-scope review fixes
mlp_(x)ModuleHolder invocation after FC1 context propagation moved to thread-local scope.Configuration and diff-scope cleanup
aivtoai_cpuswitching after validating the fixed ops-transformers 9.1.0 operator.FlashComm1Contextas a plain data struct and moved sequence-sharding helpers to free functions.linear.cppand reused the shared FC1 helper.CMakeLists.txt, and third-party submodule pointers to match upstreammain.