Skip to content

Dev - #406

Merged
yueming-yuan merged 16 commits into
radixark:dsv32from
xiuhu17:dev
Jan 7, 2026
Merged

Dev#406
yueming-yuan merged 16 commits into
radixark:dsv32from
xiuhu17:dev

Conversation

@xiuhu17

@xiuhu17 xiuhu17 commented Jan 6, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @xiuhu17, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the DeepSeekV32 model's attention mechanism by integrating Context Parallelism (CP) support for sparse Multi-Latent Attention (MLA). The changes involve a refactoring of the attention backward pass to handle combined Key-Value (KV) tensors, alongside a more flexible approach to generating and applying attention masks in a distributed environment. A new wrapper leverages optimized sparse MLA kernels, and a dedicated test ensures the functional correctness of these context-parallel optimizations. This work enables the DeepSeekV32 model to scale more efficiently by distributing attention computations across multiple devices.

Highlights

  • Context Parallelism for Sparse MLA: Introduced and implemented Context Parallelism (CP) support for sparse Multi-Latent Attention (MLA) within the DeepSeekV32 model, enabling distributed attention computations.
  • Refined KV Buffer Handling: Modified the Key-Value (KV) buffer management in attention mechanisms, particularly for all-gather operations in CP settings, to efficiently handle combined KV tensors.
  • Dynamic Attention Masking: Implemented dynamic generation and application of attention masks, including causal and 'zz' masks, specifically adapted for distributed context parallelism.
  • Sparse MLA Kernel Integration: Integrated optimized sparse MLA forward and backward kernels within a new context-parallel wrapper, facilitating efficient sparse attention with distributed data.
  • Comprehensive Validation Test: Added a new test suite to rigorously validate the correctness of the context-parallel sparse MLA implementation by comparing it against a reference eager attention implementation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request introduces significant changes to enable context parallelism for Dynamic Sparse Attention (DSA), including new and updated patch files for both the Megatron core and custom TileLang kernels. The changes are extensive and add complex logic for distributed attention computation.

While the effort to add context parallelism is appreciated, I've found several critical issues that need to be addressed:

  • There's a critical bug in the backward pass of the attention mechanism in both cp_kernel/megatron.patch and cp_kernel/tilelang.patch where gradients for key and value vectors are handled incorrectly. This will lead to incorrect model training.
  • A class was renamed in one file but not in the file that uses it, which will cause a runtime import error.
  • The code contains hardcoded 'magic numbers' for tensor dimensions, which makes it fragile and hard to maintain.
  • There are inconsistencies between the Python wrapper code and the TileLang kernels regarding causal masking.

These issues are detailed in the specific comments. Addressing them is crucial for the correctness and maintainability of the code. In the future, please provide a more descriptive title and a summary in the pull request body to help reviewers understand the context of the changes.

Comment thread cp_kernel/megatron.patch Outdated


-class AttentionFuncionWithContextParallel(torch.autograd.Function):
+class Ref(torch.autograd.Function):

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.

critical

This class was renamed to Ref, but it's still imported and used as AttentionFuncionWithContextParallel in megatron/core/transformer/experimental_attention_variant/dsa.py (also part of this patch). This will cause an ImportError at runtime. Please use a consistent name across files.

Additionally, Ref is not a descriptive name. I'd recommend reverting to AttentionFuncionWithContextParallel or choosing a more meaningful name.

class AttentionFuncionWithContextParallel(torch.autograd.Function):

Comment thread cp_kernel/megatron.patch Outdated

# Initialize or resume constants and communication group
- q, k, v, attention_mask, *rest = ctx.saved_tensors
+ q, kv, _, attention_mask, *rest = ctx.saved_tensors

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.

critical

There's a critical bug in how k and v tensors are handled in the backward pass. The forward pass saves k and v as separate tensors via ctx.save_for_backward(q, k, v, ...). However, here in the backward pass, you unpack them as q, kv, _, ..., treating the saved k as a combined kv tensor and completely discarding the saved v. The subsequent logic, especially in eager_attn_bwd, assumes v can be derived from k (as kv), which is not true for the general case, for instance, when called from unfused_dsa_fn_with_cp in dsa.py with separate key and value tensors. This will lead to incorrect gradient computation for v and k. You should handle k and v as separate tensors throughout the backward pass, consistent with the forward signature.

Comment thread cp_kernel/tilelang.patch Outdated
+ '''Backward pass for the native attention function with context parallelism'''
+
+ # Initialize or resume constants and communication group
+ q, kv, _, attention_mask, *rest = ctx.saved_tensors

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.

critical

There's a critical bug in how k and v tensors are handled in the backward pass. The forward pass saves k and v as separate tensors, but here you unpack them as q, kv, _, ..., effectively treating the saved k as a combined kv tensor and discarding the saved v. The subsequent logic assumes v can be derived from k, which is not guaranteed by the forward signature. This will lead to incorrect gradient computation. The test case in test.py happens to satisfy this assumption, but the function itself is not safe for general use where k and v might be independent.

Comment thread cp_kernel/megatron.patch Outdated
Comment on lines +26 to +27
+ v = kv[:,:,:,:512]
+ q_tail = q[:,:,:,512:]

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.

high

Using hardcoded values like 512 makes the code fragile. These dimensions should be derived from tensor shapes dynamically. For example, you could get the value dimension from v.shape[-1] (though v is not available here due to another issue) or pass it as an argument. The same applies to 576 used later, which seems to be 512 + 64. Please replace these magic numbers with dynamically determined values or constants defined elsewhere.

Comment thread cp_kernel/tilelang.patch Outdated
Comment on lines +70 to +71
+ v = kv[:,:,:,:512]
+ q_tail = q[:,:,:,512:]

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.

high

Using hardcoded values like 512 makes the code fragile and hard to maintain. These dimensions should be derived from tensor shapes dynamically or passed as arguments. The same applies to 576 used later. Please replace these magic numbers with dynamically determined values or constants.

Comment thread cp_kernel/tilelang.patch Outdated
# Compute attention scores
for h_i, bi_i in T.Parallel(block_H, BS):
- acc_p[h_i, bi_i] = T.if_then_else(mask[bi_i], 0, -T.infinity(acc_p.dtype))
+ acc_p[h_i, bi_i] = 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.

high

The causal masking logic has been removed from this kernel (and from the forward kernel as well) by initializing acc_p to 0. However, the Python wrapper sparse_mla_bwd in ring_wrapper.py is called with is_causal=True, and the wrapper function in sparse_mla_bwd.py still has logic depending on this flag. This is inconsistent and misleading. If causality is no longer supported by the kernel, the is_causal parameter and related logic should be removed from the Python wrappers to avoid confusion and potential bugs.

@yueming-yuan
yueming-yuan merged commit 90bf768 into radixark:dsv32 Jan 7, 2026
xiuhu17 added a commit to xiuhu17/miles that referenced this pull request Jan 22, 2026
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.

2 participants