feat: add flashcomm1 feature for deepseek v4 on NPU#2022
Draft
chenchuw886 wants to merge 3 commits into
Draft
Conversation
Add the reusable FlashComm1 (sequence-parallel) primitives, independent of any model: - FlashComm1Context / FlashComm1Guard RAII + current_flash_comm1_context() and flash_comm1_active() in parallel_state, so a decoder stack can publish its per-forward SP state (enabled, num_tokens, tp_rank, tp_world_size, tp_group) to the layers below without threading it through every signature. - Collective helpers in parallel_state: all_gather_dim0_unpad, reduce_scatter_padded_dim0, shard_dim0_padded (dim-0 token sharding with padding for non-divisible token counts). - RowParallelLinear flash_comm1_eligible_ flag: when the active context is enabled and the linear is marked eligible, its all-reduce tail becomes a reduce_scatter_padded_dim0 so the output stays token-sharded. - Unit test for FlashComm1Context/Guard state and activation semantics.
Drive FlashComm1 from the DeepSeek-V4-Flash decoder stack: - deepseek_v4.h: token-shard the residual stream across the TP group for the whole stack, publish the per-forward FlashComm1Guard, and restore full tokens right before hc_head. Gating policy: engage ONLY in prefill (q_max_seq_len > 1, which also covers chunked-prefill) AND with enough tokens (>= flashcomm1_sp_min_token_num); decode is disabled unconditionally. Empty DP ranks and ACL-graph forwards are excluded. Capture pre_hc_head_hidden_states AFTER the final all-gather so the MTP draft model receives full-token (not sharded) hidden states. - deepseek_v4_decoder_layer.cpp: all-gather the sharded stream to full tokens at the attention and MoE boundaries and re-shard afterwards, so norm / hc / gate run on 1/tp_world_size tokens while attention and MoE see byte-identical full-token input. - deepseek_sparse_attention.cpp: mark o_b_proj as flash_comm1_eligible so its RowParallel tail reduce-scatters back to a token shard.
Expose FlashComm1 through configuration:
- --enable_flashcomm1 (bool, default false): master switch for
DeepSeek-V4-Flash sequence parallelism.
- --flashcomm1_sp_min_token_num (int, default 1000): token threshold
below which SP stays off even in prefill, since SP only amortizes its
shard / all-gather / reduce-scatter fixed overhead with enough tokens.
Wired through global_flags.h and ParallelConfig (parallel_config.{h,cpp}).
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.
Description
Related Issues
Change Type
Pull Request Checklist
Thank you for contributing to xLLM. Before requesting review, please make sure the following items are complete.
PR Title and Commit Messages
<type>: <subject>.Pre-commit Checks
pre-commitby runningpip install pre-commitor an equivalent command.pre-commit install.pre-commit run --all-filesand fixed any reported issues.Self Review
.agents/skills/code-review/references/custom-code-style.md, especially code written or assisted by AI.mainbranch.Build and Test Coverage
python setup.py build testhas passed on a CUDA machine.python setup.py build testhas passed on an NPU machine.python setup.py build testhas passed on an MLU machine.Reviewer Notes