Skip to content

[PyTorch] Reduce redundant host-device syncs in attention and context parallel helpers - #4037

Open
wanglei875 wants to merge 1 commit into
NVIDIA:mainfrom
wanglei875:perf/reduce-attention-d2h-sync
Open

wanglei875 wants to merge 1 commit into
NVIDIA:mainfrom
wanglei875:perf/reduce-attention-d2h-sync

Conversation

@wanglei875

Copy link
Copy Markdown

Description

Coalesce redundant .item() host-device synchronizations in DotProductAttention, FusedAttention backend setup, and THD context parallel preprocessing helpers:

  1. dot_product_attention.py & backends.py: When both max_seqlen_q and max_seqlen_kv are None, replace two consecutive .max().item() calls with a single torch.stack((seqlens_q.max(), seqlens_kv.max())).tolist(), cutting the two serial D2H synchronizations in half.
  2. context_parallel.py (prepare_input_for_cp & generate_positional_ids_for_cp): Replace per-sequence .item() / int(tensor) calls inside list comprehensions ($O(B)$ host-device syncs per batch) with a single cu_seqlens.tolist() transfer, and preserve device=seq.device / device=cu_seqlens.device when creating padding and cumulative sequence length tensors.

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

  • Batch seqlens_q.max() and seqlens_kv.max() into one .tolist() call in dot_product_attention.py and backends.py when both max_seqlen_q and max_seqlen_kv are omitted.
  • Replace per-element .item() loops with a single cu_seqlens.tolist() in prepare_input_for_cp and generate_positional_ids_for_cp in context_parallel.py.

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

@wanglei875
wanglei875 requested a review from cyanguwa as a code owner December 5, 2026 08:08
Copilot AI balanced review requested due to automatic review settings December 5, 2026 08:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The changes affect THD attention setup and context-parallel sequence preparation.

The PR appears safe to merge, with a non-blocking request for CUDA padding tests.

Findings

  1. P2 CUDA padding lacks test coverage ▶

Summary

This PR reduces repeated GPU-to-CPU waits when preparing attention sequence lengths.

  • THD attention derives both missing sequence limits with one host transfer.
  • Context-parallel sequence prep reads boundaries once and keeps padding on-device.

No blocking correctness issue was established. Add CUDA padding tests to protect the newly supported path.

Reviews (1) · Last reviewed commit: "[PyTorch] Reduce redundant host-device s..." · Reviewed by Greptile

Comment on lines +5874 to 5876
cu_seqlens_padded = torch.tensor(
cu_seqlens_padded_list, dtype=cu_seqlens.dtype, device=cu_seqlens.device
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 CUDA padding lacks test coverage

The existing padding tests use CPU tensors, so they would still pass if device=seq.device or device=cu_seqlens.device were removed. Add a CUDA case with sequences that need padding, and check the returned values, devices, and cumulative-length dtype. This would protect the newly supported path from later regressions.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants