Skip to content

[Bug] fast rope backpropagation broken for zero strided tensors #3781

Closed

Description

@f14-bertolotti

Consider the fast rope code:

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,

# 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,
# 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

  1. f14-bertolotti commented on Dec 26, 2025

    @f14-bertolotti
    ContributorAuthor

    Proposed PR at #3782

  2. added a commit that references this issue on Dec 29, 2025
    c774d0d
  3. danielhanchen commented on Jun 17, 2026

    @danielhanchen
    Member

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions