Repository navigation
[PyTorch] Make fused RoPE take strided frequency tables - #3972
Draft
nvegesna-netizen wants to merge 1 commit into
Draft
nvegesna-netizen wants to merge 1 commit into
nvegesna-netizen wants to merge 1 commit into
Conversation
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>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fused RoPE silently gives wrong results when the frequency table is a strided view. The kernel reads the rows of
freqsthrough its data pointer and ignores strides, sotable[::2]reads rows 0, 1, 2 instead of 0, 2, 4.FusedRoPEFuncnow makesfreqscontiguous 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
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:
🤖 Generated with Claude Code