Skip to content

[dev] [DeepSeek-v4] Part 4: Fusion Kernels for DSv4 Hybrid Attention#4894

Merged
hxbai merged 22 commits into
NVIDIA:devfrom
hxbai:dsv4_fused_attn
May 27, 2026
Merged

[dev] [DeepSeek-v4] Part 4: Fusion Kernels for DSv4 Hybrid Attention#4894
hxbai merged 22 commits into
NVIDIA:devfrom
hxbai:dsv4_fused_attn

Conversation

@hxbai

@hxbai hxbai commented May 20, 2026

Copy link
Copy Markdown
Contributor

What does this PR do ?

https://github.com/NVIDIA/cudnn-frontend release v1.24.0 added the fusion kernels for DeepSeek-V4 Hybrid Attention.

This PR integrated these kernel into MCore.

⚠️ 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 @NVIDIA/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 @NVIDIA/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 May 20, 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 requested a review from kunlunl May 20, 2026 12:27
@hxbai hxbai mentioned this pull request May 20, 2026
3 tasks
@hxbai hxbai marked this pull request as ready for review May 20, 2026 12:31
@hxbai hxbai requested review from a team as code owners May 20, 2026 12:31
@hxbai hxbai requested a review from a team as a code owner May 20, 2026 12:52
@BestJuly

Copy link
Copy Markdown
Contributor

/claude strict-review

Comment thread megatron/core/transformer/experimental_attention_variant/dsa_kernels.py Outdated
Comment thread megatron/core/transformer/experimental_attention_variant/csa.py Outdated
Comment thread megatron/core/transformer/experimental_attention_variant/csa.py Outdated

@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: 1 | IMPORTANT: 2 | SUGGESTION: 1

Critical

  1. Dense path forward/backward inconsistency (dsa_kernels.py:800-807): In FusedIndexerSparseAttnFunc.forward, the dense loss branch passes both pre-scaled weights (w_bsh_scaled = w_bsh * indexer_softmax_scale) AND indexer_softmax_scale as sm_scale to _compute_dense_indexer_score, double-applying the scale factor. The backward (line 908) correctly uses raw w_bsh + sm_scale (single application). This forward/backward mismatch produces incorrect optimizer updates for indexer parameters when sparse_loss=False. The parity test (test_dsa_kernels.py:3399) explicitly documents this as an "apparent bug" and works around it with indexer_softmax_scale ** 2 in the reference. Fix: pass w_bsh instead of w_bsh_scaled to _compute_dense_indexer_score.

Important

  1. Default change for force_unfused_dsa (csa.py:607): Changed from TrueFalse, silently switching all existing users to the fused path which requires flash_mla and cudnn.DSA. Users without these packages will get runtime ImportError instead of the previously-working unfused path.

  2. Potential numerical fragility in _kl_loss_from_dense_scores (dsa_kernels.py:648-676): Missing per-position masking for -inf entries at ratio-causal-masked positions. If the cuDNN kernel outputs -inf at masked positions, partially-masked rows will produce inf in the KL sum (eps * (log(eps) - (-inf)) = inf). The test reference _ref_dense_indexer_loss handles this explicitly but the production code does not.

Suggestion

  1. Missing NVTX annotations (csa.py:779-848): The new fused paths (A, B, C) lack the nvtx_range_push/pop calls present in the unfused path, reducing profiling visibility for what are now the default codepaths.

Overall Assessment

Risk: Medium-High. The core architecture — lazy kernel imports, autograd Functions with fwd/bwd pairing, index helpers, and the three-path integration into csa.py — is well-structured and thoroughly tested. The sparse loss path (the default) appears correct. However, the dense loss path has a forward/backward scale inconsistency that must be fixed before merge. The default-change for force_unfused_dsa also needs either a graceful fallback or explicit documentation of the new runtime dependencies.

Comment thread megatron/core/transformer/experimental_attention_variant/dsa_kernels.py Outdated
@hxbai

hxbai commented May 22, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 2b3c815

# weights-scaling trick so the effective weights match
# the pre-scale-split behaviour.
weights_for_unfused = weights_indexer * self.indexer.softmax_scale
weights_for_unfused = weights_indexer.float() * self.indexer.softmax_scale

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.

Why don't pass the softmax_scale into kernel directly?

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.

This is for the unfused path. Since the fused kernel accepts the scale, we only need to multiply the scale in the unfused path.

@yaox12

yaox12 commented May 22, 2026

Copy link
Copy Markdown
Member

/claude strict-review

Comment thread megatron/core/transformer/experimental_attention_variant/csa.py Outdated
Comment thread megatron/training/arguments.py
Comment thread megatron/core/transformer/experimental_attention_variant/csa.py Outdated
@claude

claude Bot commented May 22, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary — PR #4894

Scope: Fusion kernels for DSv4 Hybrid Attention (FlashMLA fwd + cuDNN DSA bwd integration)

Findings

# Severity File Line Summary
1 CRITICAL csa.py 812 Fused path save_loss_to_tracker missing mtp_num_layers — inflates indexer loss when MTP is enabled
2 IMPORTANT arguments.py 4605-4611 --no-dsa-kernel-fusion uses store_false without default=False, making fusion default-on and crashing users without flash_mla/cuDNN
3 SUGGESTION csa.py 788 getattr(..., 0.0) returns None for existing field, latent TypeError on None > 0 comparison

Risk Assessment

  • Finding 1 is a silent numerical correctness bug: the indexer loss will be over-weighted when MTP layers are present, leading to wrong training dynamics. One-line fix.
  • Finding 2 is a backward-compatibility break: any existing DSv4 hybrid-attention user who upgrades will crash on import unless they add --no-dsa-kernel-fusion to their launch command. One-line fix (default=False).
  • Finding 3 is low risk today (requires explicit opt-in to trigger) but should be cleaned up to avoid a future crash.

Verdict

Request changes. Findings 1 and 2 should be addressed before merge. Finding 3 is optional but recommended.

🤖 Generated with Claude Code

@hxbai

hxbai commented May 22, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test d91242a

@hxbai

hxbai commented May 22, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 8ba9cc0

@hxbai

hxbai commented May 22, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test fca33ac

@hxbai

hxbai commented May 23, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test b86e4c5

@hxbai

hxbai commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test a864690

@hxbai

hxbai commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 70a6673

@hxbai

hxbai commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test a31271e

@hxbai

hxbai commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 4e560b7

@ko3n1g ko3n1g 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.

question regarding pin strictness

Comment thread pyproject.toml Outdated
"megatron-energon[av_decode]~=6.0",
"av",
"flashinfer-python>=0.5.0,<0.7.0",
"nvidia-cudnn-frontend==1.24.0",

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.

do we really need such a strict pin for all of MLM? Can we have a version guard inside core instead?

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.

removed the pin

@hxbai

hxbai commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 04bade0

@ko3n1g ko3n1g 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.

ty!

@hxbai

hxbai commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test ccc8449

@hxbai hxbai force-pushed the dsv4_fused_attn branch from ccc8449 to 5a806d1 Compare May 27, 2026 01:42
@hxbai

hxbai commented May 27, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 5a806d1

@hxbai hxbai added this pull request to the merge queue May 27, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown

🔄 Merge queue validation started!

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

Merged via the queue into NVIDIA:dev with commit f553f2f May 27, 2026
65 of 66 checks passed
@hxbai hxbai deleted the dsv4_fused_attn branch May 27, 2026 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants