Repository navigation
[PyTorch] Reduce redundant host-device syncs in attention and context parallel helpers - #4037
wanglei875 wants to merge 1 commit into
Conversation
… parallel helpers Signed-off-by: wanglei875 <[email protected]>
Confidence Score: 4/5
[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
SummaryThis PR reduces repeated GPU-to-CPU waits when preparing attention sequence lengths.
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 |
| cu_seqlens_padded = torch.tensor( | ||
| cu_seqlens_padded_list, dtype=cu_seqlens.dtype, device=cu_seqlens.device | ||
| ) |
There was a problem hiding this comment.
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!
Description
Coalesce redundant
.item()host-device synchronizations inDotProductAttention,FusedAttentionbackend setup, and THD context parallel preprocessing helpers:dot_product_attention.py&backends.py: When bothmax_seqlen_qandmax_seqlen_kvareNone, replace two consecutive.max().item()calls with a singletorch.stack((seqlens_q.max(), seqlens_kv.max())).tolist(), cutting the two serial D2H synchronizations in half.context_parallel.py(prepare_input_for_cp&generate_positional_ids_for_cp): Replace per-sequence.item()/int(tensor)calls inside list comprehensions (cu_seqlens.tolist()transfer, and preservedevice=seq.device/device=cu_seqlens.devicewhen creating padding and cumulative sequence length tensors.Type of change
Changes
seqlens_q.max()andseqlens_kv.max()into one.tolist()call indot_product_attention.pyandbackends.pywhen bothmax_seqlen_qandmax_seqlen_kvare omitted..item()loops with a singlecu_seqlens.tolist()inprepare_input_for_cpandgenerate_positional_ids_for_cpincontext_parallel.py.Checklist: