Skip to content

fix: handle zero-strided tensors in fast_rope_embedding (#3781)#4233

Open
danielhanchen wants to merge 1 commit into
mainfrom
dh/recover-3804-zero-stride-fix
Open

fix: handle zero-strided tensors in fast_rope_embedding (#3781)#4233
danielhanchen wants to merge 1 commit into
mainfrom
dh/recover-3804-zero-stride-fix

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Replacement for #3804 due to Studio rebasing

Summary

Fix #3781: Handle zero-strided tensors in fast_rope_embedding forward and backward passes.

When gradient tensors (dQ, dK) have zero strides (e.g., from expanded/broadcast tensors during debugging scenarios like (out[0].sum() + out[1].sum()).backward()), the triton kernel fails because all stride values become zero, causing incorrect memory access patterns.

Changes

  • Add zero-stride check alongside contiguity check in both forward and backward passes
  • Clone tensors that have any zero stride to ensure proper memory layout for triton kernel

Code Changes

# Before
Q_out = Q.clone() if not Q.is_contiguous() else Q

# After  
Q_out = Q.clone() if not Q.is_contiguous() or 0 in Q.stride() else Q

Test plan

🤖 Generated with Claude Code

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 resolves an issue where the fast_rope_embedding function would fail when processing tensors that have zero strides, typically encountered during debugging or specific broadcasting scenarios. By introducing additional checks and cloning such tensors, the change ensures the stability and correctness of the rotary embedding operation across a wider range of input tensor configurations, particularly in gradient computations.

Highlights

  • Zero-Strided Tensor Handling: Implemented checks for zero-strided tensors in the fast_rope_embedding forward and backward passes. Tensors with zero strides are now explicitly cloned to ensure correct memory layout for the Triton kernel, preventing failures that occur when such tensors are expanded or broadcasted.
  • Improved Robustness: Enhanced the robustness of the fast_rope_embedding function by addressing a specific edge case where gradient tensors with zero strides could lead to incorrect memory access patterns and kernel failures.
Changelog
  • unsloth/kernels/rope_embedding.py
    • Added a condition to clone input tensors (Q, K, dQ, dK) if they are not contiguous or contain zero strides, to prevent Triton kernel failures.
    • Updated comments to reflect the new cloning logic for non-contiguous or zero-strided tensors.
Activity
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 correctly fixes a bug where fast_rope_embedding would fail on zero-strided tensors, which can occur with expanded or broadcast tensors. The fix, which involves cloning the tensor if it's not contiguous or contains a zero in its strides, is applied to both the forward and backward passes and seems robust. My feedback includes a suggestion to refactor the duplicated logic into a helper method to improve code maintainability.

Comment on lines +315 to +316
Q_out = Q.clone() if not Q.is_contiguous() or 0 in Q.stride() else Q
K_out = K.clone() if not K.is_contiguous() or 0 in K.stride() else K

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.

medium

The logic to clone the tensor if it's not contiguous or has a zero stride is duplicated here and in the backward pass for dQ and dK (lines 387-388). To improve maintainability and adhere to the Don't Repeat Yourself (DRY) principle, consider extracting this logic into a helper function or a static method within the Fast_RoPE_Embedding_QK class.

For example:

@staticmethod
def _clone_if_needed(tensor):
    # Clone if not contiguous or has zero strides, such as expanded tensors.
    if not tensor.is_contiguous() or 0 in tensor.stride():
        return tensor.clone()
    return tensor

You could then call _clone_if_needed(Q) and _clone_if_needed(K) here, and similarly in the backward pass.

@nidhishgajjar

This comment was marked as low quality.

1 similar comment
@nidhishgajjar

This comment was marked as low quality.

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.

[Bug] fast rope backpropagation broken for zero strided tensors

3 participants