-
Notifications
You must be signed in to change notification settings - Fork 81
fix bugs with non contiguous tensors while doing communication #497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Summary of ChangesHello @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 Highlights
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this 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.
| + 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_) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()
| + b_, s_, h_, d_ = out_i.shape | ||
| + out_i = out_i.transpose(0, 1).flatten().view(s_, b_, h_, d_).contiguous() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()
| + 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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()
No description provided.