Skip to content

[Common] Zero THD rows past the last sequence in fused RoPE - #3973

Draft
nvegesna-netizen wants to merge 1 commit into
NVIDIA:mainfrom
nvegesna-netizen:nvegesna/fused-rope-thd-zero-tail
Draft

nvegesna-netizen wants to merge 1 commit into
NVIDIA:mainfrom
nvegesna-netizen:nvegesna/fused-rope-thd-zero-tail

Conversation

@nvegesna-netizen

@nvegesna-netizen nvegesna-netizen commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

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 with at::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

  • 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

  • The linear-grid THD kernels already launch one block per row. For a row past the last sequence, the block now writes zeros instead of returning.
  • The original THD grid gets one more row of blocks, which zeroes the rows past the last sequence and exits at once when there are none.
  • fused_rope.h: the docs for output and input_grads say that, for thd, rows past cu_seqlens[b] / cp_size are 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 and cp_size 1 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:

  • 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

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>
@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