Repository navigation
fix: handle zero-strided tensors in fast_rope_embedding (#3781) - #4233
Conversation
# 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.
|
@codex review |
|
@codex security review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
| pytestmark = pytest.mark.skipif( | ||
| not torch.cuda.is_available(), reason = "Triton RoPE kernel needs CUDA" | ||
| ) |
There was a problem hiding this comment.
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 👍 / 👎.
There was a problem hiding this comment.
Fixed in 4f9d180: added pytest.mark.gpu alongside the CUDA skip; -m 'gpu or slow' now selects all 6 cases (was 6 deselected).
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Replacement for #3804. Fixes #3781.
Problem
fast_rope_embedding(Q, K, cos, sin)without rotary indices goes throughFast_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 anddY.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 earlier0 in X.stride()checks in this PR were therefore no-ops and have been dropped.Change
Fast_RoPE_Embedding.backward:dY.contiguous().view(...)instead ofdY.reshape(...). Normal gradients arrive as a transposed view thatreshapealready copied, so the cost is unchanged: one copy before, one copy now, and no copy whendYis 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)
.sum()grad (issue repro)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.