Skip to content

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

Merged
danielhanchen merged 5 commits into
mainfrom
dh/recover-3804-zero-stride-fix
Sep 29, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
dh/recover-3804-zero-stride-fix

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Mar 12, 2026 •

Copy link
Copy Markdown
Member

Replacement for #3804. Fixes #3781.

Problem

fast_rope_embedding(Q, K, cos, sin) without rotary indices goes through Fast_RoPE_Embedding, whose backward rotates the incoming gradient in place. When that gradient is expanded, for example from (q.sum() + k.sum()).backward(), every stride is 0 and dY.reshape(...) stays a stride-0 view, so the kernel repeatedly rotates one shared element and returns a wrong gradient (max abs error ~4.5 vs a torch reference on main).

The indexed path (Fast_RoPE_Embedding_QK) was already fixed by #3782: is_contiguous() is False for any expanded tensor with a dimension larger than 1, so it clones those. The earlier 0 in X.stride() checks in this PR were therefore no-ops and have been dropped.

Change

  • Fast_RoPE_Embedding.backward: dY.contiguous().view(...) instead of dY.reshape(...). Normal gradients arrive as a transposed view that reshape already copied, so the cost is unchanged: one copy before, one copy now, and no copy when dY is already contiguous.
  • tests/test_rope_zero_stride_grad.py: forward and backward vs a torch reference, with and without rotary indices, for .sum(), expanded-row and dense gradients.

Testing (B200, torch 2.13 + cu130)

Case main this PR
no indices, .sum() grad (issue repro) FAIL, bwd err 4.5 pass, 2.4e-7
no indices, expanded-row / dense grad pass pass
indices, all three grads pass pass

Overlaps #12171, which makes the same one-line change inside a larger torch.compile refactor. This PR can land first; #12171 then conflicts only on that line.

gemini-code-assist[bot]

This comment was marked as outdated.

# Conflicts:
#	unsloth/kernels/rope_embedding.py
main already clones non-contiguous Q/K and dQ/dK in the indexed path (#3782), which
covers every zero-stride tensor, so the extra stride checks here were no-ops. The
issue #3781 repro takes the non-indexed path, where reshape of an all-zero-stride
grad stays a view and the in-place kernel rotated a single shared element. Add a
CUDA regression test comparing forward and backward against a torch reference.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-29T13:42:02.097660Z 4f9d180 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7aad4f2cb9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/test_rope_zero_stride_grad.py Outdated
Comment on lines +8 to +10
pytestmark = pytest.mark.skipif(
not torch.cuda.is_available(), reason = "Triton RoPE kernel needs CUDA"
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Mark the CUDA regression test as GPU

When the documented GPU suite is invoked with pytest -m gpu tests/, pytest's -m option only selects tests matching that marker expression, but this file has only a skipif mark. Consequently all six new cases are deselected from the GPU run, while CPU runs merely skip them, so the regression is not exercised by the intended test paths. Add pytest.mark.gpu while retaining the hardware skip, as expected by pyproject.toml:1733-1736.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 4f9d180: added pytest.mark.gpu alongside the CUDA skip; -m 'gpu or slow' now selects all 6 cases (was 6 deselected).

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 29, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 4f9d18014a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 42cffd5 into main Sep 29, 2026
21 of 24 checks passed
@danielhanchen
danielhanchen deleted the dh/recover-3804-zero-stride-fix branch September 29, 2026 14:04
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.

[Bug] fast rope backpropagation broken for zero strided tensors

2 participants