Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PyTorch] Fix multiple calls to saved_tensors in CP attention #1334

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions transformer_engine/pytorch/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -2528,12 +2528,13 @@ def backward(ctx, dout):
recv_src = ctx.cp_global_ranks[(rank + 1) % cp_size * cp_size_a2a + rank_a2a]
batch_p2p_comm = int(os.getenv("NVTE_BATCH_MHA_P2P_COMM", "0")) or (cp_size == 2)

(q, kv, out, softmax_lse, cu_seqlens_q_padded, cu_seqlens_kv_padded) = ctx.saved_tensors[:6]
(fp8_fwd_scales, fp8_fwd_scale_invs) = ctx.saved_tensors[6:8]
cu_seqlens_q_per_step = ctx.saved_tensors[8 : 8 + cp_size]
cu_seqlens_kv_per_step = ctx.saved_tensors[8 + cp_size : 8 + cp_size * 2]
rng_states = ctx.saved_tensors[8 + cp_size * 2 : 8 + cp_size * 3]
attn_biases = ctx.saved_tensors[8 + cp_size * 3 : 8 + cp_size * 4]
(*saved_tensors,) = ctx.saved_tensors
(q, kv, out, softmax_lse, cu_seqlens_q_padded, cu_seqlens_kv_padded) = saved_tensors[:6]
(fp8_fwd_scales, fp8_fwd_scale_invs) = saved_tensors[6:8]
cu_seqlens_q_per_step = saved_tensors[8 : 8 + cp_size]
cu_seqlens_kv_per_step = saved_tensors[8 + cp_size : 8 + cp_size * 2]
rng_states = saved_tensors[8 + cp_size * 2 : 8 + cp_size * 3]
attn_biases = saved_tensors[8 + cp_size * 3 : 8 + cp_size * 4]

causal = "causal" in ctx.attn_mask_type
padding = "padding" in ctx.attn_mask_type
Expand Down Expand Up @@ -3577,11 +3578,12 @@ def backward(ctx, dout):
cp_size = get_distributed_world_size(ctx.cp_group)
rank = get_distributed_rank(ctx.cp_group)

(q, k, v, cu_seqlens_q, cu_seqlens_q_padded) = ctx.saved_tensors[:5]
cu_seqlens_kv_per_step = ctx.saved_tensors[5:7]
out_per_step = ctx.saved_tensors[7:9]
softmax_lse_per_step = ctx.saved_tensors[9:11]
rng_states = ctx.saved_tensors[11:13]
(*saved_tensors,) = ctx.saved_tensors
(q, k, v, cu_seqlens_q, cu_seqlens_q_padded) = saved_tensors[:5]
cu_seqlens_kv_per_step = saved_tensors[5:7]
out_per_step = saved_tensors[7:9]
softmax_lse_per_step = saved_tensors[9:11]
rng_states = saved_tensors[11:13]
kv_seq_range_per_step = ctx.kv_seq_range_per_step
window_size_per_step = ctx.window_size_per_step

Expand Down Expand Up @@ -4056,12 +4058,11 @@ def backward(ctx, dout):
# pylint: disable=missing-function-docstring
cp_size = get_distributed_world_size(ctx.cp_group)

q, k, v, out = ctx.saved_tensors[:4]
cu_seqlens_q, cu_seqlens_kv, cu_seqlens_q_padded, cu_seqlens_kv_padded = ctx.saved_tensors[
4:8
]
fp8_fwd_scales, fp8_fwd_scale_invs = ctx.saved_tensors[8:10]
aux_ctx_tensors = ctx.saved_tensors[10:]
(*saved_tensors,) = ctx.saved_tensors
q, k, v, out = saved_tensors[:4]
cu_seqlens_q, cu_seqlens_kv, cu_seqlens_q_padded, cu_seqlens_kv_padded = saved_tensors[4:8]
fp8_fwd_scales, fp8_fwd_scale_invs = saved_tensors[8:10]
aux_ctx_tensors = saved_tensors[10:]

qkv_layout = ctx.qkv_format + "_" + ctx.qkv_format + "_" + ctx.qkv_format
causal = "causal" in ctx.attn_mask_type
Expand Down
Loading