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

Remove the padding from mask type without var seqlen #108

Closed
wants to merge 1 commit into from
Closed
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
26 changes: 26 additions & 0 deletions transformer_engine/common/fused_attn_rocm/fused_attn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ std::pair<int64_t, int64_t> check_set_window_size(NVTE_Mask_Type attn_mask_type,
return window_size;
}

// To align with NVTE, treat causal padding with BSHD/SBHD as causal no padding
NVTE_Mask_Type check_set_mask_type(NVTE_Mask_Type attn_mask_type, NVTE_QKV_Layout qkv_layout){
if(nvte_get_qkv_format(qkv_layout)!=NVTE_QKV_Format::NVTE_THD){
if(attn_mask_type==NVTE_Mask_Type::NVTE_PADDING_CAUSAL_MASK){
return NVTE_Mask_Type::NVTE_CAUSAL_MASK;
}else if(attn_mask_type==NVTE_Mask_Type::NVTE_PADDING_CAUSAL_BOTTOM_RIGHT_MASK){
return NVTE_Mask_Type::NVTE_CAUSAL_BOTTOM_RIGHT_MASK;
}
}
return attn_mask_type;
}

// select a backend for fused attention
NVTE_Fused_Attn_Backend nvte_get_fused_attn_backend(
NVTEDType q_dtype, NVTEDType kv_dtype, NVTE_QKV_Layout qkv_layout, NVTE_Bias_Type bias_type,
Expand Down Expand Up @@ -139,6 +151,8 @@ NVTE_Fused_Attn_Backend nvte_get_fused_attn_backend(

// fix the incompatible window size from upstream frameworks pytorch/jax
std::tie(window_size_left, window_size_right) = check_set_window_size(attn_mask_type, std::make_pair(window_size_left, window_size_right));
// remove "padding" from mask_type if not in var seqlen
attn_mask_type = check_set_mask_type(attn_mask_type, qkv_layout);

// first check whether ck can be used, then check aotriton
if(nvte_fused_attn_ck && fused_attn_rocm::is_ck_backend_supported(
Expand Down Expand Up @@ -211,6 +225,8 @@ void nvte_fused_attn_fwd_qkvpacked(const NVTETensor QKV, const NVTETensor Bias,

// fix the incompatible window size from upstream frameworks pytorch/jax
std::tie(window_size_left, window_size_right) = check_set_window_size(attn_mask_type, std::make_pair(window_size_left, window_size_right));
// remove "padding" from mask_type if not in var seqlen
attn_mask_type = check_set_mask_type(attn_mask_type, qkv_layout);

NVTE_Fused_Attn_Backend fused_attention_backend = nvte_get_fused_attn_backend(
QKV_type, QKV_type, qkv_layout, bias_type, attn_mask_type, dropout, h, h, max_seqlen,
Expand Down Expand Up @@ -286,6 +302,8 @@ void nvte_fused_attn_bwd_qkvpacked(const NVTETensor QKV, const NVTETensor O, con

// fix the incompatible window size from upstream frameworks pytorch/jax
std::tie(window_size_left, window_size_right) = check_set_window_size(attn_mask_type, std::make_pair(window_size_left, window_size_right));
// remove "padding" from mask_type if not in var seqlen
attn_mask_type = check_set_mask_type(attn_mask_type, qkv_layout);

NVTE_Fused_Attn_Backend fused_attention_backend = nvte_get_fused_attn_backend(
QKV_type, QKV_type, qkv_layout, bias_type, attn_mask_type, dropout, h, h, max_seqlen,
Expand Down Expand Up @@ -366,6 +384,8 @@ void nvte_fused_attn_fwd_kvpacked(const NVTETensor Q, const NVTETensor KV, const

// fix the incompatible window size from upstream frameworks pytorch/jax
std::tie(window_size_left, window_size_right) = check_set_window_size(attn_mask_type, std::make_pair(window_size_left, window_size_right));
// remove "padding" from mask_type if not in var seqlen
attn_mask_type = check_set_mask_type(attn_mask_type, qkv_layout);

NVTE_Fused_Attn_Backend fused_attention_backend = nvte_get_fused_attn_backend(
Q_type, KV_type, qkv_layout, bias_type, attn_mask_type, dropout, h_q, h_kv, max_seqlen_q,
Expand Down Expand Up @@ -449,6 +469,8 @@ void nvte_fused_attn_bwd_kvpacked(

// fix the incompatible window size from upstream frameworks pytorch/jax
std::tie(window_size_left, window_size_right) = check_set_window_size(attn_mask_type, std::make_pair(window_size_left, window_size_right));
// remove "padding" from mask_type if not in var seqlen
attn_mask_type = check_set_mask_type(attn_mask_type, qkv_layout);

NVTE_Fused_Attn_Backend fused_attention_backend = nvte_get_fused_attn_backend(
Q_type, KV_type, qkv_layout, bias_type, attn_mask_type, dropout, h_q, h_kv, max_seqlen_q,
Expand Down Expand Up @@ -526,6 +548,8 @@ void nvte_fused_attn_fwd(const NVTETensor Q, const NVTETensor K, const NVTETenso

// fix the incompatible window size from upstream frameworks pytorch/jax
std::tie(window_size_left, window_size_right) = check_set_window_size(attn_mask_type, std::make_pair(window_size_left, window_size_right));
// remove "padding" from mask_type if not in var seqlen
attn_mask_type = check_set_mask_type(attn_mask_type, qkv_layout);

NVTE_Fused_Attn_Backend fused_attention_backend = nvte_get_fused_attn_backend(
Q_type, KV_type, qkv_layout, bias_type, attn_mask_type, dropout, h_q, h_kv, max_seqlen_q,
Expand Down Expand Up @@ -605,6 +629,8 @@ void nvte_fused_attn_bwd(const NVTETensor Q, const NVTETensor K, const NVTETenso

// fix the incompatible window size from upstream frameworks pytorch/jax
std::tie(window_size_left, window_size_right) = check_set_window_size(attn_mask_type, std::make_pair(window_size_left, window_size_right));
// remove "padding" from mask_type if not in var seqlen
attn_mask_type = check_set_mask_type(attn_mask_type, qkv_layout);

NVTE_Fused_Attn_Backend fused_attention_backend = nvte_get_fused_attn_backend(
Q_type, KV_type, qkv_layout, bias_type, attn_mask_type, dropout, h_q, h_kv, max_seqlen_q,
Expand Down