Skip to content

Conversation

@xiuhu17
Copy link

@xiuhu17 xiuhu17 commented Jan 20, 2026

No description provided.

@gemini-code-assist
Copy link
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 primarily focuses on refactoring and optimizing the attention mechanism within the megatron/core/transformer module. The changes aim to enhance the efficiency and maintainability of the attention context parallel implementation by removing an external dependency for tensor manipulation, streamlining buffer management, and refining gradient computations.

Highlights

  • Removed Einops Dependency: The external einops library has been removed, with its tensor reshaping functionalities (einops.rearrange) replaced by native PyTorch operations like transpose and view for improved efficiency and reduced dependencies.
  • Optimized KV Buffer Management: The initialization of Key-Value (KV) buffers has been modified, specifically changing the structure and handling of the k_buffer within the attention mechanism.
  • Refined Gradient Calculation: The method for aggregating the dk gradient (gradient with respect to the Key tensor) in the backward pass has been updated, now utilizing torch.cat and torch.sum for potentially more precise or efficient computation.
  • Streamlined Attention Implementation: The eager_attn_fwd function, along with its associated HAVE_EINOPS checks, has been removed, simplifying the attention forward pass 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.

@xiuhu17 xiuhu17 changed the title update fix bugs with non contiguous tensors are doing communication Jan 20, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

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 updates a patch file, primarily refactoring attention mechanisms. Key changes include replacing einops with native PyTorch operations, introducing a custom tilelang kernel for sparse multi-head attention, and adding context parallelism support to DSAttention. My review focuses on improving code clarity, consistency, and removing redundancies. I've suggested simplifying tensor manipulations for better readability and removing a newly introduced einops dependency to align with the refactoring goals.

Comment on lines 233 to +236
+ s_, b_, h_, d_ = q_i.shape
+ q_i = einops.rearrange(q_i, 's b h d -> b s h d').flatten().view(b_, s_, h_, d_)
+ q_i = q_i.transpose(0, 1).flatten().view(b_, s_, h_, d_)
+ s_, b_, h_, d_ = k_i.shape
+ k_i = einops.rearrange(k_i, 's b h d -> b s h d').flatten().view(b_, s_, h_, d_)
+ k_i = k_i.transpose(0, 1).flatten().view(b_, s_, h_, d_)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The .flatten().view(...) pattern is used here to make the tensor contiguous after a transpose operation. While functionally correct, using .contiguous() is more explicit and readable. This pattern appears multiple times in this file (e.g., for q, k_i, dout, out in the backward pass). Consider replacing it for better code clarity.

        s_, b_, h_, d_ = q_i.shape
        q_i = q_i.transpose(0, 1).contiguous()
        s_, b_, h_, d_ = k_i.shape
        k_i = k_i.transpose(0, 1).contiguous()

Comment on lines +247 to +248
+ b_, s_, h_, d_ = out_i.shape
+ out_i = out_i.transpose(0, 1).flatten().view(s_, b_, h_, d_).contiguous()
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The .flatten().view(...) pattern already returns a contiguous tensor, so the final .contiguous() call is redundant. For improved readability and to avoid the unnecessary operation, you can replace the entire chain with .transpose(0, 1).contiguous().

        b_, s_, h_, d_ = out_i.shape
        out_i = out_i.transpose(0, 1).contiguous()

Comment on lines +383 to +386
+ b_, s_, h_, d_ = dq_i.shape
+ dq_i = dq_i.transpose(0, 1).flatten().view(s_, b_, h_, d_).contiguous()
+ b_, s_, h_, d_ = _dk_i.shape
+ _dk_i = _dk_i.transpose(0, 1).flatten().view(s_, b_, h_, d_).contiguous()
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Similar to the forward pass, the .flatten().view(...) pattern makes the tensor contiguous, so the trailing .contiguous() call is redundant for both dq_i and _dk_i. Consider simplifying this to .transpose(0, 1).contiguous() for better clarity and consistency.

            b_, s_, h_, d_ = dq_i.shape
            dq_i = dq_i.transpose(0, 1).contiguous()
            b_, s_, h_, d_ = _dk_i.shape
            _dk_i = _dk_i.transpose(0, 1).contiguous()

@xiuhu17 xiuhu17 changed the title fix bugs with non contiguous tensors are doing communication fix bugs with non contiguous tensors while doing communication Jan 20, 2026
@xiuhu17 xiuhu17 closed this Jan 24, 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.

1 participant