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] Avoid saving fp8_tensors in certain scenarios #1143

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
60 changes: 39 additions & 21 deletions transformer_engine/pytorch/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -4691,12 +4691,15 @@ def forward(
fp8_dtype_forward,
qkv_dtype,
).view(out_fp8.shape)
fp8_tensors = (
qkv_fp8,
out_fp8,
fp8_meta["scaling_fwd"].scale.clone(),
fp8_meta["scaling_fwd"].scale_inv.clone(),
)
if fp8_meta["recipe"].fp8_mha or not int(os.getenv("NVTE_FP8_DPA_BWD", "1")):
fp8_tensors = (None, None, None, None)
else:
fp8_tensors = (
qkv_fp8,
out_fp8,
fp8_meta["scaling_fwd"].scale.clone(),
fp8_meta["scaling_fwd"].scale_inv.clone(),
)
else:
out_ret, aux_ctx_tensors = fused_attn_fwd_qkvpacked(
is_training,
Expand Down Expand Up @@ -5074,13 +5077,16 @@ def forward(
fp8_dtype_forward,
qkv_dtype,
).view(out_fp8.shape)
fp8_tensors = (
q_fp8,
kv_fp8,
out_fp8,
fp8_meta["scaling_fwd"].scale.clone(),
fp8_meta["scaling_fwd"].scale_inv.clone(),
)
if fp8_meta["recipe"].fp8_mha or not int(os.getenv("NVTE_FP8_DPA_BWD", "1")):
fp8_tensors = (None, None, None, None, None)
else:
fp8_tensors = (
q_fp8,
kv_fp8,
out_fp8,
fp8_meta["scaling_fwd"].scale.clone(),
fp8_meta["scaling_fwd"].scale_inv.clone(),
)
else:
out_ret, aux_ctx_tensors = fused_attn_fwd_kvpacked(
is_training,
Expand Down Expand Up @@ -5575,14 +5581,26 @@ def forward(
qkv_dtype,
).view(out_fp8.shape)

fp8_tensors = (
q_fp8,
k_fp8,
v_fp8,
out_fp8,
fp8_meta["scaling_fwd"].scale.clone(),
fp8_meta["scaling_fwd"].scale_inv.clone(),
)
if not int(os.getenv("NVTE_FP8_DPA_BWD", "1")):
fp8_tensors = (None, None, None, None, None, None)
# elif fp8_meta["recipe"].fp8_mha:
# fp8_tensors = (
# None,
# None,
# None,
# None,
# fp8_meta["scaling_fwd"].scale.clone(),
# fp8_meta["scaling_fwd"].scale_inv.clone(),
# )
Comment on lines +5586 to +5594
Copy link
Collaborator

Choose a reason for hiding this comment

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

Debugging code?

Suggested change
# elif fp8_meta["recipe"].fp8_mha:
# fp8_tensors = (
# None,
# None,
# None,
# None,
# fp8_meta["scaling_fwd"].scale.clone(),
# fp8_meta["scaling_fwd"].scale_inv.clone(),
# )

Also, why does the unfused case have different logic for fp8_meta["recipe"].fp8_mha than the QKV-fused and KV-fused cases?

else:
fp8_tensors = (
q_fp8,
k_fp8,
v_fp8,
out_fp8,
fp8_meta["scaling_fwd"].scale.clone(),
fp8_meta["scaling_fwd"].scale_inv.clone(),
)
else:
out_ret, aux_ctx_tensors = fused_attn_fwd(
is_training,
Expand Down