Skip to content

Commit 50b86ff

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent c33c568 commit 50b86ff

1 file changed

Lines changed: 29 additions & 52 deletions

File tree

transformer_engine/pytorch/attention/dot_product_attention/context_parallel.py

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,7 @@ def _get_thd_partition_cu_seqlens(cu_seqlens_padded, device=None):
282282
return cu_seqlens_padded.to(device=target_device, dtype=target_dtype)
283283

284284

285-
def _get_thd_partitioned_indices_reference(
286-
cu_seqlens_padded, total_tokens, cp_size, cp_rank
287-
):
285+
def _get_thd_partitioned_indices_reference(cu_seqlens_padded, total_tokens, cp_size, cp_rank):
288286
"""CPU fallback for dataloader-side THD partitioning."""
289287
total_chunks = 2 * cp_size
290288
chunk_sizes = (cu_seqlens_padded[1:] - cu_seqlens_padded[:-1]) // total_chunks
@@ -337,9 +335,7 @@ def get_thd_partitioned_indices(
337335
)
338336
if cu_seqlens_padded.dtype != torch.int32:
339337
cu_seqlens_padded = cu_seqlens_padded.to(torch.int32)
340-
return tex.thd_get_partitioned_indices(
341-
cu_seqlens_padded, total_tokens, cp_size, cp_rank
342-
)
338+
return tex.thd_get_partitioned_indices(cu_seqlens_padded, total_tokens, cp_size, cp_rank)
343339

344340

345341
def validate_packed_contiguous_thd_metadata(
@@ -3229,23 +3225,23 @@ def forward(
32293225
packed_contiguous = qkv_format == "thd" and _use_packed_contiguous_thd()
32303226
if packed_contiguous:
32313227
assert qkv_format == "thd"
3232-
assert use_fused_attention or use_flash_attn_3, (
3233-
"Packed THD partitioning requires FusedAttention or FlashAttention 3."
3234-
)
3235-
assert not (use_flash_attn_3 and pad_between_seqs), (
3236-
"Packed THD partitioning with FlashAttention 3 does not support "
3237-
"padding yet."
3238-
)
3239-
assert causal and window_size == (-1, 0), (
3240-
"Packed THD partitioning currently supports full causal attention only."
3241-
)
3228+
assert (
3229+
use_fused_attention or use_flash_attn_3
3230+
), "Packed THD partitioning requires FusedAttention or FlashAttention 3."
3231+
assert not (
3232+
use_flash_attn_3 and pad_between_seqs
3233+
), "Packed THD partitioning with FlashAttention 3 does not support padding yet."
3234+
assert causal and window_size == (
3235+
-1,
3236+
0,
3237+
), "Packed THD partitioning currently supports full causal attention only."
32423238
assert not fp8, "Packed THD partitioning does not support FP8 yet."
3243-
assert not is_graph_capturing(), (
3244-
"Packed THD partitioning does not support CUDA graph capture yet."
3245-
)
3246-
assert q.shape[0] == k.shape[0] == v.shape[0], (
3247-
"Packed THD partitioning requires equal local Q/K/V physical lengths."
3248-
)
3239+
assert (
3240+
not is_graph_capturing()
3241+
), "Packed THD partitioning does not support CUDA graph capture yet."
3242+
assert (
3243+
q.shape[0] == k.shape[0] == v.shape[0]
3244+
), "Packed THD partitioning requires equal local Q/K/V physical lengths."
32493245
assert cu_seqlens_q is cu_seqlens_kv and (
32503246
cu_seqlens_q_padded is cu_seqlens_kv_padded
32513247
), "Packed THD self-attention requires shared Q/KV sequence metadata tensors."
@@ -3396,12 +3392,8 @@ def forward(
33963392

33973393
if qkv_format == "thd":
33983394
# [cp*t, h, d] -> reorder to sequence order -> [t_full, h, d]
3399-
k_ag = restore_thd_gathered_kv(
3400-
k_ag, cu_seqlens_kv_padded, cp_size
3401-
)
3402-
v_ag = restore_thd_gathered_kv(
3403-
v_ag, cu_seqlens_kv_padded, cp_size
3404-
)
3395+
k_ag = restore_thd_gathered_kv(k_ag, cu_seqlens_kv_padded, cp_size)
3396+
v_ag = restore_thd_gathered_kv(v_ag, cu_seqlens_kv_padded, cp_size)
34053397
else:
34063398
# [cp, s, b, h, d] -> [cp*2, s//2, b, h, d]
34073399
k_ag = k_ag.view(2 * cp_size, k.shape[0] // 2, *k.shape[1:])
@@ -3432,9 +3424,7 @@ def forward(
34323424
# create two streams to resolve wave quantization issue of Flash Attn in each step
34333425
flash_attn_streams = [torch.cuda.current_stream(), cp_stream]
34343426
# prepare per-step tensors
3435-
local_seq_chunk_ids = (
3436-
[rank] if packed_contiguous else [rank, 2 * cp_size - rank - 1]
3437-
)
3427+
local_seq_chunk_ids = [rank] if packed_contiguous else [rank, 2 * cp_size - rank - 1]
34383428
kv_seq_range_per_step = [None, None]
34393429
window_size_per_step = [None, None]
34403430
cu_seqlens_kv_per_step = [None, None]
@@ -3998,12 +3988,8 @@ def backward(ctx, dout, *_args):
39983988
cu_seqlens_kv_padded = ctx.cu_seqlens_kv_padded
39993989
thd_cu_seqlens_q_per_step = ctx.thd_cu_seqlens_q_per_step
40003990
# [cp*t, h, d] -> reorder to sequence order
4001-
k_ag = restore_thd_gathered_kv(
4002-
k_ag, cu_seqlens_kv_padded, cp_size
4003-
)
4004-
v_ag = restore_thd_gathered_kv(
4005-
v_ag, cu_seqlens_kv_padded, cp_size
4006-
)
3991+
k_ag = restore_thd_gathered_kv(k_ag, cu_seqlens_kv_padded, cp_size)
3992+
v_ag = restore_thd_gathered_kv(v_ag, cu_seqlens_kv_padded, cp_size)
40073993

40083994
thd_cu_seqlens_q_padded_per_step = ctx.thd_cu_seqlens_q_padded_per_step
40093995
else:
@@ -4051,11 +4037,7 @@ def backward(ctx, dout, *_args):
40514037
if fa_utils.v2_6_0_plus:
40524038
fa_backward_kwargs["softcap"] = 0.0
40534039

4054-
local_seq_chunk_ids = (
4055-
[rank]
4056-
if ctx.packed_contiguous
4057-
else [rank, 2 * cp_size - rank - 1]
4058-
)
4040+
local_seq_chunk_ids = [rank] if ctx.packed_contiguous else [rank, 2 * cp_size - rank - 1]
40594041
for i in range(len(local_seq_chunk_ids) + 1):
40604042
if i < len(local_seq_chunk_ids):
40614043
# FA3 uses internal per-call workspace. Consecutive AG per-step
@@ -4317,12 +4299,8 @@ def backward(ctx, dout, *_args):
43174299
if ctx.qkv_format == "thd":
43184300
# Reorder dK/dV from sequence order back to dual-chunk CP rank order,
43194301
# then reduce-scatter across CP ranks.
4320-
dk = unrestore_thd_gathered_kv(
4321-
dk, cu_seqlens_kv_padded, cp_size
4322-
)
4323-
dv = unrestore_thd_gathered_kv(
4324-
dv, cu_seqlens_kv_padded, cp_size
4325-
)
4302+
dk = unrestore_thd_gathered_kv(dk, cu_seqlens_kv_padded, cp_size)
4303+
dv = unrestore_thd_gathered_kv(dv, cu_seqlens_kv_padded, cp_size)
43264304
dk, _ = reduce_scatter_along_first_dim(dk, ctx.cp_group)
43274305
dv, _ = reduce_scatter_along_first_dim(dv, ctx.cp_group)
43284306
# dQ is already [t_rank, h, d], no reshape needed
@@ -5271,10 +5249,9 @@ def attn_forward_func_with_cp(
52715249
], f"Context parallelism does not support {qkv_format=}!"
52725250
packed_contiguous = qkv_format == "thd" and _use_packed_contiguous_thd()
52735251
if packed_contiguous:
5274-
assert qkv_format == "thd" and cp_comm_type == "all_gather", (
5275-
"Packed THD partitioning requires qkv_format='thd' and "
5276-
"cp_comm_type='all_gather'."
5277-
)
5252+
assert (
5253+
qkv_format == "thd" and cp_comm_type == "all_gather"
5254+
), "Packed THD partitioning requires qkv_format='thd' and cp_comm_type='all_gather'."
52785255
assert (
52795256
qkv_format != "sbhd" or use_fused_attention
52805257
), "Context parallelism does not support FlashAttention backend with qkv_format = 'sbhd'!"

0 commit comments

Comments
 (0)