Skip to content

[PyTorch] Make fused RoPE take strided frequency tables - #3972

Draft
nvegesna-netizen wants to merge 1 commit into
NVIDIA:mainfrom
nvegesna-netizen:nvegesna/fused-rope-contiguous-freqs
Draft

nvegesna-netizen wants to merge 1 commit into
NVIDIA:mainfrom
nvegesna-netizen:nvegesna/fused-rope-contiguous-freqs

Conversation

@nvegesna-netizen

Copy link
Copy Markdown
Contributor

Description

Fused RoPE silently gives wrong results when the frequency table is a strided view. The kernel reads the rows of freqs through its data pointer and ignores strides, so table[::2] reads rows 0, 1, 2 instead of 0, 2, 4. FusedRoPEFunc now makes freqs contiguous before calling the kernel, which costs nothing when it already is.

The fused QKV RoPE already asserts that its tables are contiguous, so only this path was silent. Found by guihong-nv while reviewing NVIDIA/Megatron-LM#9442, which works around it on the Megatron-LM side.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • FusedRoPEFunc.forward: freqs = freqs.contiguous(). The tensor saved for backward is the contiguous one.
  • test_fused_rope_strided_freqs: a strided table gives the same outputs and gradients as the unfused path, for sbhd, bshd and thd. It fails without the change.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

🤖 Generated with Claude Code

The fused RoPE kernel indexes the rows of freqs from its data pointer and
ignores strides, so a strided view such as table[::2] silently read rows
0, 1, 2 instead of 0, 2, 4. The fused QKV RoPE asserts contiguity, so only
this path was silent. Make freqs contiguous in FusedRoPEFunc, which costs
nothing when it already is. The tensor saved for backward is the
contiguous one.

Test: a strided table gives the same outputs and gradients as the unfused
path, for sbhd, bshd and thd.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Nitin Vegesna <nvegesna@nvidia.com>
@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Dec 1, 2026

This branch has not been deployed

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

Labels

community-contribution PRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant