Repository navigation
[Common] Zero THD rows past the last sequence in fused RoPE - #3973
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
For THD input, the fused RoPE kernels skip the rows at or past cu_seqlens[-1] / cp_size, while the PyTorch binding allocates the output and the input gradient with at::empty. Those rows kept uninitialized memory, which then reached the gradients of the layer that produced the input. Write zeros there instead. The linear-grid kernels already launch one block per row; the original grid gets one more row of blocks, which covers the tail and exits at once when there is none. The C API docs for the output and input gradient now say so. Test: with new allocations filled with NaN, the rows past the last sequence are zero in the output and the input gradient, and the other rows match the unfused path, on both grids and with cp_size 1 and 2. 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
For THD input, fused RoPE leaves the rows past the last sequence uninitialized, in both the output and the input gradient. The kernels skip rows at or past
cu_seqlens[-1] / cp_size, while the PyTorch binding allocates both tensors withat::empty. A packed batch can end with such rows, and their garbage gradient then reaches the layer that produced the input, such as the QKV projection. This PR writes zeros to those rows instead.Found by guihong-nv while reviewing NVIDIA/Megatron-LM#9442, which works around it on the Megatron-LM side.
Type of change
Changes
fused_rope.h: the docs foroutputandinput_gradssay that, for thd, rows pastcu_seqlens[b] / cp_sizeare set to zero.test_fused_rope_thd_rows_past_last_sequence: with new allocations filled with NaN (torch.utils.deterministic.fill_uninitialized_memory), those rows are zero in the output and the input gradient, and the other rows match the unfused path. It covers both grids andcp_size1 and 2, and fails without the change.Performance: fused THD RoPE forward + backward takes the same time before and after this change on both grids, with and without trailing rows. That includes a single long sequence, where the original grid gets twice as many blocks.
Known limit: the extra grid row means exactly 65,535 sequences now exceeds the CUDA grid-y limit (65,536 or more already did).
Checklist:
🤖 Generated with Claude Code