Skip to content

Preserve DSA output across fused inverse RoPE#5526

Open
kunlunl wants to merge 1 commit into
NVIDIA:devfrom
kunlunl:codex/fix-dsv4-fused-rope-output-alias
Open

Preserve DSA output across fused inverse RoPE#5526
kunlunl wants to merge 1 commit into
NVIDIA:devfrom
kunlunl:codex/fix-dsv4-fused-rope-output-alias

Conversation

@kunlunl

@kunlunl kunlunl commented Jun 28, 2026

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do ?

Fix a bug:

Fused DSA forward saves the raw attention output O for use during backward, while the value returned to the caller is only a reshape/view backed by the same storage. without creating independent storage.

Post-attention inverse RoPE then operates in-place on this returned view. The Triton kernel writes directly back through the input pointer (fused_mla_yarn_rope_apply.py). As a result, the original O saved for DSA backward is overwritten by the inverse-RoPE output before backward executes, causing the backward kernel to consume an incorrect attention output.

Bug reported by 5780122+shyoshyo@users.noreply.github.com

⚠️ 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.

@copy-pr-bot

copy-pr-bot Bot commented Jun 28, 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.

@kunlunl kunlunl marked this pull request as ready for review June 28, 2026 05:36
@kunlunl kunlunl requested review from a team as code owners June 28, 2026 05:36
@kunlunl

kunlunl commented Jun 28, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test ddf5e29

@hxbai

hxbai commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

/claude strict-review

@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: 0 | SUGGESTION: 0

This is a clean, well-targeted correctness bug fix. The problem and solution are both clearly understood:

Bug: _FusedMLARoPEInplace.forward saves cos/sin for backward but the caller (DSv4HybridAttention.forward) passes the attention output O as a view sharing storage with what the upstream fused DSA autograd function saved for its backward. The in-place inverse RoPE kernel then corrupts that saved tensor, causing incorrect gradients during backward.

Fix: Clone the tensor before applying the in-place kernel, giving inverse RoPE its own storage and leaving the DSA-saved output intact.

Verification:

  • fused_mla_rope_out_of_place is a minimal wrapper — t.clone() then delegate to the existing in-place path. Gradient flow is correct: _FusedMLARoPEInplace.backward only needs cos/sin (not the forward input), so the clone introduces no autograd complications.
  • The only other call sites of fused_mla_rope_inplace are forward RoPE on freshly-projected Q/K tensors (lines 657, 669 in the same file; line 365 in csa.py) — these operate on independent storage with no saved-for-backward aliasing, so they remain correct as in-place.
  • The test is thorough: it first demonstrates the bug with the in-place version (proving the aliasing corruption), then verifies the out-of-place version preserves the original tensor bitwise (rtol=0, atol=0), produces correct output, and propagates gradients correctly through backward.
  • All new identifiers (fused_mla_rope_out_of_place, _SaveOutputForBackward) have meaningful use paths.

LGTM — no issues found.

@kunlunl

kunlunl commented Jun 28, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test ddf5e29

Signed-off-by: kunlunl <kunlunl@nvidia.com>
Co-authored-by: Kaixiang Lei <5780122+shyoshyo@users.noreply.github.com>
@kunlunl kunlunl force-pushed the codex/fix-dsv4-fused-rope-output-alias branch from ddf5e29 to 526e198 Compare June 28, 2026 13:26
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.

3 participants