Skip to content

[dev] [DeepSeek-v4] Part 1: Hybrid Attention with CSA and HCA - #4458

Merged
hxbai merged 16 commits into
NVIDIA:devfrom
hxbai:dsv4_attn
Apr 30, 2026
Merged

[dev] [DeepSeek-v4] Part 1: Hybrid Attention with CSA and HCA#4458
hxbai merged 16 commits into
NVIDIA:devfrom
hxbai:dsv4_attn

Conversation

@hxbai

@hxbai hxbai commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

What does this PR do ?

We will create several PRs to functionally support DeepSeek-v4 training. This is the first one.

Add DeepSeek-v4 Hybrid Attention with CSA and HCA.

We reuse some arguments from DSA to avoid duplication.

DeepSeek-V4 training has several phases, and here are the setting examples (use DSv4-Flash size):

  • Dense mode:
    • --experimental-attention-variant dsv4_hybrid
    • --csa-window-size 128
    • --csa-compress-ratios ([0,0,4]+[128,4]*20+[0])
    • --csa-compress-rotary-base: 40000
    • --dsa-indexer-n-heads 64
    • --dsa-indexer-head-dim 128
    • --dsa-indexer-topk 512
    • --csa-dense-mode
  • Sparse warmup mode:
    • --experimental-attention-variant dsv4_hybrid
    • --csa-window-size 128
    • --csa-compress-ratios ([0,0,4]+[128,4]*20+[0])
    • --csa-compress-rotary-base: 40000
    • --dsa-indexer-n-heads 64
    • --dsa-indexer-head-dim 128
    • --dsa-indexer-topk 512
  • Sparse mode:
    • --experimental-attention-variant dsv4_hybrid
    • --csa-window-size 128
    • --csa-compress-ratios ([0,0,4]+[128,4]*20+[0])
    • --csa-compress-rotary-base: 40000
    • --dsa-indexer-n-heads 64
    • --dsa-indexer-head-dim 128
    • --dsa-indexer-topk 512
    • --dsa-indexer-use-sparse-loss

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact the @mcore-oncall.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue:

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge your PR.

For MRs into `dev` branch The proposed review process for `dev` branch is under active discussion.

MRs are mergable after one approval by either eharper@nvidia.com or zijiey@nvidia.com.

@copy-pr-bot

copy-pr-bot Bot commented Apr 24, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@hxbai hxbai self-assigned this Apr 24, 2026
@hxbai hxbai added the dev branch Dev branch related issues and development label Apr 24, 2026
@sbhavani sbhavani mentioned this pull request Apr 24, 2026
18 tasks
@FDecaYed

Copy link
Copy Markdown
Contributor

/claude review

@hxbai
hxbai marked this pull request as ready for review April 29, 2026 07:53
@hxbai
hxbai requested review from a team as code owners April 29, 2026 07:53
@hxbai

hxbai commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 503445a

@yaox12

yaox12 commented Apr 29, 2026

Copy link
Copy Markdown
Member

/claude strict-review

Comment thread megatron/core/transformer/experimental_attention_variant/csa.py
Comment thread megatron/core/transformer/experimental_attention_variant/csa.py
Comment thread megatron/core/models/common/embeddings/rope_utils.py
Comment thread megatron/core/transformer/transformer_config.py
Comment thread megatron/training/arguments.py
Comment on lines +29 to +34
from megatron.core.transformer.transformer_config import TransformerConfig
from megatron.core.utils import get_pg_size, nvtx_range_pop, nvtx_range_push


# ---------------------------------------------------------------------------
# Helper functions for index computation

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[SUGGESTION Naming] _get_window_topk_idxs_cached and _get_compress_topk_idxs_cached use lru_cache keyed on device_str: str. Since lru_cache uses the string representation of the device, this is correct. However, the cache never gets invalidated. For long training runs where seqlen changes (e.g., curriculum learning, variable-length batching), the cache will grow up to maxsize=8 entries per function. This is fine for the current maxsize but worth noting if the maxsize is increased.

Also, since torch.Tensor objects held in the cache will pin GPU memory, consider using weakref or clearing the cache between epochs if memory becomes a concern.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave it for now. Will consider it when we add THD support.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

CRITICAL: 0 | IMPORTANT: 4 | SUGGESTION: 5

Important Findings

  1. [IMPORTANT Correctness] Grouped output projection broken with TP > 1 (deepseek_v4_hybrid_attention.py:188-198): linear_o_group_proj is allocated with the full o_groups * o_lora_rank rows but viewed as (o_local_groups, o_lora_rank, -1). When TP > 1, the inferred last dimension becomes tp * group_proj_in_size instead of group_proj_in_size, causing the einsum to contract over a mismatched dimension. The weight must be either allocated per-TP-shard or sliced before the einsum.

  2. [IMPORTANT Correctness] KV layernorm shape mismatch with TP > 1 (deepseek_v4_hybrid_attention.py:489-507): linear_kv_up_proj is a ColumnParallelLinear with gather_output=False, outputting v_head_dim / tp per rank. But kv_layernorm is created with hidden_size=v_head_dim (full). The learnable parameters and normalization stats will mismatch when TP > 1.

  3. [IMPORTANT Correctness] clip_qk assumes MLA multi-head weight layout (deepseek_v4_hybrid_attention.py:863-887): _clip_kv_proj_weight reshapes assuming (num_heads, qk_head_dim + v_head_dim, ...), but DSv4's single-head linear_kv_up_proj has shape (v_head_dim, hidden_size). This will crash or produce wrong results if qk_clip is ever enabled.

  4. [IMPORTANT Compatibility] Missing CLI arguments (arguments.py): Only --csa-compress-ratios is added. --csa-window-size, --csa-compress-rotary-base, and --csa-dense-mode (mentioned in the PR description) are missing from the argument parser.

Notable Design Changes

  • Triton RoPE kernel output layout change: The fused MLA RoPE kernels (_mla_rope_fwd_inplace_kernel, _mla_rope_fwd_kv_split_kernel) now preserve the interleaved layout on write (previously they de-interleaved). The unfused path in rope_utils.py was updated to match. This is backward-compatible for attention computation (QK^T dot product is permutation-invariant for matching Q/K layouts), but changes the intermediate tensor layout visible to downstream code. Backward-compat aliases are provided.

  • Inverse RoPE: The inverse=True flag correctly negates sin in both forward and backward kernels. The Jacobian analysis confirms the backward gradient computation is correct for the inverse case.

Overall Assessment

Risk level: Medium. The core algorithm (CSA + HCA hybrid attention, inverse RoPE, compressed sparse attention with learned indexing) is well-structured and the numerical correctness for TP=1 looks solid. The Triton kernel changes are consistent across forward/backward, Q/KV paths. The DSA indexer loss extensions (custom causal mask, row-validity masking) are correctly implemented.

The main risks are the TP > 1 bugs in the grouped output projection and KV layernorm — these would produce silently wrong results or crash at TP > 1. Since the PR targets dev and notes it's under development, these may be known limitations, but they should be tracked.

The test coverage is good: unit tests for helper functions, Compressor, CSAIndexer, CompressedSparseAttention (window-only, compressed, dense mode), and the full DSv4HybridSelfAttention forward/backward paths.

@hxbai

hxbai commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 6f5b92f

@chenhongyu2048

Copy link
Copy Markdown

Hello @hxbai I'm also very interested in implementing sparse attention, and I'd like to know if CP/SP support will be added in the future?

@hxbai

hxbai commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 8470a3b

@hxbai
hxbai enabled auto-merge April 30, 2026 15:08
@hxbai
hxbai added this pull request to the merge queue Apr 30, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/25173867126

Merged via the queue into NVIDIA:dev with commit bf4e1db Apr 30, 2026
63 of 65 checks passed
@hxbai
hxbai deleted the dsv4_attn branch April 30, 2026 16:13
hxbai added a commit to hxbai/Megatron-LM that referenced this pull request May 19, 2026
zhaoyinglia pushed a commit to flagos-ai/Megatron-LM-FL that referenced this pull request May 24, 2026
### PR Category
<!-- One of [ Train | Inference | Compress | Serve | RL | Core |
Hardware | CICD | Tools | Others ] -->
[Train] Most of codes are copied from Megatron-LM Dev branch. The dev
branch is different with main branch or release version.
Megatron LM PR:
DeepSeek-V4:
NVIDIA#4458
NVIDIA#4481
NVIDIA#4518
mHC:
NVIDIA#2943
### PR Types
<!-- One of [ User Experience | New Features | Bug Fixes | Improvements
| Performance | Breaking Change| Deprecations | Test Case | Docs |
Others ] -->
[New features]
### PR Description
<!-- Describe what you’ve done -->
Add DeepSeek V4 model into FlagScale and Megatron-FL
Supported:
1. CSA and HCA
2. Hash Router
3. mHC
4. Engram(optional)

Unsupported:
1. Sqrtsoftpuls router score function. ✅
2. mHC recompute. ✅
3. Overlap_grad_reduce and overlap_param_gather when Zero 1. ✅
4. Any infra optimizations.

### NOTE: This is only a draft pr, please reivew to give more
suggestions.
such as:
1. File structure.
    - All modules are moved into Megatron-FL

### Next plan:
1. Distributed training. ✅
3. Muon optimizer with Zero 1 adaptation. 🚧
4. Low precision is out of scope of this pr, limited by resource. 
5. Maybe context parallel for sparse attention.
6. Welcome to give more suggestions.

---------

Co-authored-by: Hongxiao Bai <hongxiaob@nvidia.com>
Co-authored-by: Yuzhong Wang <yuzhongw@nvidia.com>
zhaoyinglia added a commit to flagos-ai/FlagScale that referenced this pull request May 24, 2026
### PR Category
<!-- One of [ Train | Inference | Compress | Serve | RL | Core |
Hardware | CICD | Tools | Others ] -->
[Train] Most of codes are copied from Megatron-LM Dev branch. The dev
branch is different with main branch or release version.
Megatron LM PR:
DeepSeek-V4:
NVIDIA/Megatron-LM#4458
NVIDIA/Megatron-LM#4481
NVIDIA/Megatron-LM#4518
mHC:
NVIDIA/Megatron-LM#2943
### PR Types
<!-- One of [ User Experience | New Features | Bug Fixes | Improvements
| Performance | Breaking Change| Deprecations | Test Case | Docs |
Others ] -->
[New features]
### PR Description
<!-- Describe what you’ve done -->
Add DeepSeek V4 model into FlagScale and Megatron-FL
Supported:
1. CSA and HCA
2. Hash Router
3. mHC
4. Engram(optional)

Unsupported:
1. Sqrtsoftpuls router score function. ✅
2. mHC recompute. ✅
3. Overlap_grad_reduce and overlap_param_gather when Zero 1. ✅
4. Any infra optimizations.

### NOTE: This is only a draft pr, please reivew to give more
suggestions.
such as:
1. File structure.  
- **All modules are moved to Megatron-FL. Only model_builder is left in
Flagscale.**
    - Delete Engram related CI or not?

### Next plan:
1. Distributed training. ✅
3. Muon optimizer with Zero 1 adaptation. 😢
4. Low precision is out of scope of this pr, limited by resource.
5. Maybe context parallel for sparse attention.
6. Welcome to give more suggestions.

---------

Co-authored-by: zhaoyingli <86812880+zhaoyinglia@users.noreply.github.com>
hxbai added a commit to hxbai/Megatron-LM that referenced this pull request Jun 10, 2026
FDecaYed added a commit to FDecaYed/Megatron-LM that referenced this pull request Jul 21, 2026
Add the unfused compressed sparse attention algorithm, inert configuration fields, and focused algorithm coverage without enabling a model variant.

Reconstructed from NVIDIA#4458 and its corrections as part of the frozen feature set in NVIDIA#5795.

Signed-off-by: Deyu Fu <Deyu.Foo@gmail.com>
FDecaYed added a commit to FDecaYed/Megatron-LM that referenced this pull request Jul 21, 2026
Enable SBHD-only DSv4 Hybrid attention orchestration, model-variant validation, backend-explicit specs, and MTP layer-ratio plumbing.

Reconstructed from NVIDIA#4458, NVIDIA#4518, NVIDIA#5018, and NVIDIA#5526 as part of the frozen feature set in NVIDIA#5795.

Signed-off-by: Deyu Fu <Deyu.Foo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

complexity: high dev branch Dev branch related issues and development

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants