-
Notifications
You must be signed in to change notification settings - Fork 773
Abstract CUDA hardcodes into configurable te_device_type / te_platform #3113
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
base: release_v2.14
Are you sure you want to change the base?
Changes from 5 commits
cdefc94
e5e85a9
aff2e8a
561efb3
8ae24c3
7529fba
b6f31a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1191,6 +1191,9 @@ def cp_p2p_bwd_flash_attn( | |
| dq, dk, dv = [torch.empty_like(x) for x in [q_part, k_part, v_part]] | ||
| if fa_utils.v2_3_plus and not fa_utils.v2_7_0_plus: | ||
| fa_backward_kwargs["window_size"] = (-1, -1) | ||
| # Fix: flash-attn 2.3.x ~ 2.6.x also needs rng_state for dropout | ||
| if not use_flash_attn_3 and rng_states is not None: | ||
| fa_backward_kwargs["rng_state"] = rng_states[cp_size - step - 1] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change looks not connected to the rest of the PR - is it intended?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| elif use_flash_attn_3 or fa_utils.v2_7_0_plus: | ||
| fa_backward_kwargs["window_size_left"] = -1 | ||
| fa_backward_kwargs["window_size_right"] = -1 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| import torch.nn.functional as F | ||
| import transformer_engine_torch as tex | ||
| import transformer_engine as te | ||
| from transformer_engine import te_device_type | ||
| from transformer_engine.pytorch.cpp_extensions.fused_attn import ( | ||
| QKVLayout, | ||
| AttnBiasType, | ||
|
|
@@ -539,6 +540,9 @@ def get_attention_backend( | |
| if use_flash_attention: | ||
| use_flash_attention = False | ||
| logger.debug("Disabling FlashAttention for max_logit") | ||
| if use_fused_attention and qkv_format == "thd": | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes in this file also seem disconnected from the rest of this PR and should be in its own PR.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| use_fused_attention = False | ||
| logger.debug("Disabling FusedAttention for max_logit with qkv_format = thd") | ||
| if fp8 and fp8_meta["recipe"].fp8_dpa: | ||
| use_flash_attention = False | ||
| use_fused_attention = False | ||
|
|
@@ -1103,6 +1107,10 @@ def _is_fa3_supported(num_heads, num_gqa_groups, head_dim_qk, head_dim_v, qkv_dt | |
| logger.debug("Disabling FusedAttention for determinism reasons with post_scale_bias") | ||
| use_fused_attention = False | ||
| fused_attention_backend = None | ||
| if is_training and device_compute_capability >= (10, 0): | ||
| logger.debug("Disabling FusedAttention for determinism reasons on Blackwell") | ||
| use_fused_attention = False | ||
| fused_attention_backend = None | ||
|
|
||
| # use_flash_attention may have been set above | ||
| use_flash_attention_2 = use_flash_attention and use_flash_attention_2 | ||
|
|
@@ -1243,13 +1251,13 @@ def get_padding_mask( | |
| ], | ||
| dim=0, | ||
| ) | ||
| attention_mask_q = attention_mask_q.to(device="cuda") | ||
| attention_mask_q = attention_mask_q.to(device=te_device_type()) | ||
| if attention_type == "self": | ||
| attention_mask = attention_mask_q | ||
| else: | ||
| attention_mask = ( | ||
| attention_mask_q, | ||
| attention_mask_kv.to(device="cuda"), | ||
| attention_mask_kv.to(device=te_device_type()), | ||
| ) | ||
| return attention_mask | ||
|
|
||
|
|
@@ -1368,9 +1376,11 @@ def get_full_mask( | |
| actual_seqlens_kv = m[:, 0, 0, :].sum(dim=1) | ||
|
|
||
| # apply SWA mask | ||
| mask = torch.arange(max_seqlen_q, dtype=torch.int32, device="cuda").view( | ||
| mask = torch.arange(max_seqlen_q, dtype=torch.int32, device=te_device_type()).view( | ||
| 1, 1, max_seqlen_q, 1 | ||
| ) - torch.arange(max_seqlen_kv, dtype=torch.int32, device="cuda").view(1, 1, 1, max_seqlen_kv) | ||
| ) - torch.arange(max_seqlen_kv, dtype=torch.int32, device=te_device_type()).view( | ||
| 1, 1, 1, max_seqlen_kv | ||
| ) | ||
| swa_left = None | ||
| swa_right = None | ||
| if attn_mask_type == "causal_bottom_right" or ( | ||
|
|
@@ -1466,7 +1476,7 @@ def get_alibi( | |
| m_hat = torch.pow(m_hat_0, torch.arange(1, 1 + 2 * (num_heads - n), 2)) | ||
| m = torch.cat([m, m_hat]) | ||
|
|
||
| _alibi_cache["_alibi_slopes"] = m.to(dtype=torch.float32, device="cuda") | ||
| _alibi_cache["_alibi_slopes"] = m.to(dtype=torch.float32, device=te_device_type()) | ||
| _alibi_cache["_num_heads"] = num_heads | ||
| _alibi_cache["_alibi_slopes_require_update"] = False | ||
|
|
||
|
|
@@ -1479,9 +1489,9 @@ def get_alibi( | |
| else: | ||
| raise ValueError("ALiBi slopes cannot exceed 2 dimensions.") | ||
|
|
||
| bias = torch.arange(max_seqlen_q, dtype=torch.int32, device="cuda").view( | ||
| bias = torch.arange(max_seqlen_q, dtype=torch.int32, device=te_device_type()).view( | ||
| 1, 1, max_seqlen_q, 1 | ||
| ) - torch.arange(max_seqlen_kv, dtype=torch.int32, device="cuda").view( | ||
| ) - torch.arange(max_seqlen_kv, dtype=torch.int32, device=te_device_type()).view( | ||
| 1, 1, 1, max_seqlen_kv | ||
| ) | ||
| if actual_seqlens_q is None and actual_seqlens_kv is None: | ||
|
|
@@ -1501,7 +1511,9 @@ def get_alibi( | |
| _alibi_cache["_max_seqlen_q"], _alibi_cache["_max_seqlen_kv"] = max_seqlen_q, max_seqlen_kv | ||
| _alibi_cache["_bottom_right_alignment"] = bottom_right_alignment | ||
| bias_dtype = torch.float32 if bias_dtype is None else bias_dtype | ||
| _alibi_cache["_alibi_bias"] = bias.contiguous().to(dtype=bias_dtype, device="cuda") | ||
| _alibi_cache["_alibi_bias"] = bias.contiguous().to( | ||
| dtype=bias_dtype, device=te_device_type() | ||
| ) | ||
| _alibi_cache["_alibi_bias_require_update"] = False | ||
|
|
||
| return _alibi_cache["_alibi_slopes"], _alibi_cache["_alibi_bias"] | ||
|
|
@@ -1516,7 +1528,7 @@ def get_cu_seqlens(mask: torch.Tensor) -> torch.Tensor: | |
| mask = mask.squeeze(1).squeeze(1) | ||
| reduced_mask = mask.logical_not().sum(dim=1) | ||
| cu_seqlens = reduced_mask.cumsum(dim=0).to(torch.int32) | ||
| zero = torch.zeros(1, dtype=torch.int32, device="cuda") | ||
| zero = torch.zeros(1, dtype=torch.int32, device=te_device_type()) | ||
| cu_seqlens = torch.cat((zero, cu_seqlens)) | ||
|
|
||
| return cu_seqlens | ||
|
|
@@ -1534,7 +1546,7 @@ def get_cu_seqlens_and_indices(mask: torch.Tensor) -> Tuple[torch.Tensor, torch. | |
|
|
||
| reduced_mask = mask.logical_not().sum(dim=1) | ||
| cu_seqlens = reduced_mask.cumsum(dim=0).to(torch.int32) | ||
| zero = torch.zeros(1, dtype=torch.int32, device="cuda") | ||
| zero = torch.zeros(1, dtype=torch.int32, device=te_device_type()) | ||
| cu_seqlens = torch.cat((zero, cu_seqlens)) | ||
|
|
||
| mask = mask.reshape(-1) | ||
|
|
@@ -1559,7 +1571,12 @@ def get_indices(max_seqlen: int, cu_seqlens: torch.Tensor) -> torch.Tensor: | |
| bs = len(cu_seqlens) - 1 | ||
| seqlens = cu_seqlens[1:] - cu_seqlens[:-1] | ||
| indices = [i * max_seqlen + ii for i, j in enumerate(seqlens) for ii in range(j)] | ||
| indices = torch.Tensor(indices).unsqueeze(1).unsqueeze(1).to(dtype=torch.int64, device="cuda") | ||
| indices = ( | ||
| torch.Tensor(indices) | ||
| .unsqueeze(1) | ||
| .unsqueeze(1) | ||
| .to(dtype=torch.int64, device=te_device_type()) | ||
| ) | ||
|
|
||
| num_nonzeros = indices.shape[0] | ||
| pad_amount = bs * max_seqlen - num_nonzeros | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot import torch in this file - it is used both by pyTorch and JAX backends.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should go into
transformer_engine/pytorch/__init__.pyinstead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed, thanks