Skip to content

feat: add flashcomm1 feature for deepseek v4 on NPU#2022

Draft
chenchuw886 wants to merge 3 commits into
xLLM-AI:mainfrom
chenchuw886:flashcomm1-clean
Draft

feat: add flashcomm1 feature for deepseek v4 on NPU#2022
chenchuw886 wants to merge 3 commits into
xLLM-AI:mainfrom
chenchuw886:flashcomm1-clean

Conversation

@chenchuw886

Copy link
Copy Markdown

Description

Related Issues

Change Type

  • Bug fix
  • New feature
  • Performance improvement
  • Refactor
  • Documentation
  • Test
  • Build or CI

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

  • The PR title and each commit message follow the xLLM commit format: <type>: <subject>.

Allowed types: feat, bugfix, docs, test, refactor, chore, style, revert, perf, model, build, release.
The subject should use clear English, start with a verb, include at least 4 words, and end with ..

Pre-commit Checks

  • I have installed pre-commit by running pip install pre-commit or an equivalent command.
  • I have installed the hooks with pre-commit install.
  • I have run pre-commit run --all-files and fixed any reported issues.

If you are unsure how to set up pre-commit, see the pre-commit documentation.

Self Review

  • I have self-reviewed the code according to .agents/skills/code-review/references/custom-code-style.md, especially code written or assisted by AI.
  • I have rebased this PR onto the latest main branch.

Build and Test Coverage

  • Tests have been added or updated as needed.
  • CUDA: python setup.py build test has passed on a CUDA machine.
  • NPU: python setup.py build test has passed on an NPU machine.
  • MLU: python setup.py build test has passed on an MLU machine.

Reviewer Notes

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}).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant