Repository navigation
Description
Consider the fast rope code:
unsloth/unsloth/kernels/rope_embedding.py
Lines 377 to 400 in d83fbf6
| def backward(ctx, dQ, dK): | |
| batch, _, _, head_dim = dQ.shape | |
| rope_ptr = ( | |
| ctx.rope_indices | |
| if ctx.has_indices | |
| else ctx.cos.new_empty(1, dtype = torch.int32) | |
| ) | |
| Q_batch_stride, Q_head_stride, Q_seq_stride = ( | |
| dQ.stride(0), | |
| dQ.stride(1), | |
| dQ.stride(2), | |
| ) | |
| K_batch_stride, K_head_stride, K_seq_stride = ( | |
| dK.stride(0), | |
| dK.stride(1), | |
| dK.stride(2), | |
| ) | |
| # Inplace rotary embedding is generally fine | |
| dQ_out = dQ.clone() if not dQ.is_contiguous else dQ | |
| dK_out = dK.clone() if not dK.is_contiguous else dK | |
When a zero-strided tensor dQ or dK comes in the strides
Q_batch_stride, Q_head_stride, Q_seq_stride, and
K_batch_stride, K_head_stride, K_seq_stride are all set as zero.
To my knowledge, this is a bug that can happen with debugging losses. For example,
out = fast_rope_embedding(x.clone(), x.clone(), cos, sin)
(out[0].sum() + out[1].sum()).backward()
This code gives the backward function a zero-strided tensor, which should be fully materialized.
Furthermore,
Each forward/backward pass was never cloning the given Q, K, dQ, and dK tensors.
See,
unsloth/unsloth/kernels/rope_embedding.py
Lines 397 to 399 in d83fbf6
| # Inplace rotary embedding is generally fine | |
| dQ_out = dQ.clone() if not dQ.is_contiguous else dQ | |
| dK_out = dK.clone() if not dK.is_contiguous else dK |
and,
unsloth/unsloth/kernels/rope_embedding.py
Lines 314 to 316 in d83fbf6
| # Inplace rotary embedding is generally fine | |
| Q_out = Q.clone() if not Q.is_contiguous else Q | |
| K_out = K.clone() if not K.is_contiguous else K |
X.is_contiguous is a method, and it should be used as X.is_contiguous().
I have a PR with the fix I will submit briefly.
Activity
f14-bertolotti commentedon Dec 26, 2025
Proposed PR at #3782
Fix is_contiguous() method call and remove duplicate imports
Fix Boolean value of Tensor ambiguity error in mistral.py (#3790)
fix: handle zero-strided tensors in fast_rope_embedding backward pass
Fix Boolean value of Tensor ambiguity error in mistral.py (unslothai#…
danielhanchen commentedon Jun 17, 2026
This issue should be fixed now - sorry on the delay - please update with
uv pip install --upgrade unsloth unsloth_zoo --force-reinstall --no-cache-dir- if the issue is still there, please open another issue - appreciate it again!Materialize expanded grads in Fast_RoPE_Embedding backward
fix: handle zero-strided tensors in fast_rope_embedding (#3781) (#4233)