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

[CINN] 【Infer Symbolic Shape BUAA 】Add flashmask_attention op #68385

Merged
merged 3 commits into from
Oct 15, 2024

Conversation

crazyxiaoxi
Copy link
Contributor

@crazyxiaoxi crazyxiaoxi commented Sep 23, 2024

PR Category

CINN

PR Types

improvements

Description

添加flashmask_attention算子符号推导接口
找到单测
/test/legacy_test/test_flashmask.py
/test/legacy_test/test_flash_attention.py
只有unnitest
Pcard-67164

@crazyxiaoxi crazyxiaoxi changed the title [CINN] 【Infer Symbolic Shape BUAA 】Add flashmask_attn op [CINN] 【Infer Symbolic Shape BUAA 】Add flashmask_attention op Sep 25, 2024
@@ -1543,6 +1543,84 @@ bool FlashAttnOpInferSymbolicShape(
// return true;
// }

bool FlashmaskAttentionOpInferSymbolicShape(
Copy link
Contributor

@gongshaotian gongshaotian Sep 25, 2024

Choose a reason for hiding this comment

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

对比下和 FlashAttnOpInferSymbolicShape 的区别,看看是不是能直接复用

Copy link
Contributor Author

Choose a reason for hiding this comment

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

两个函数在qkv的处理和output的计算是一样的。
但是输入参数所取的operand位置不一样,同时输入的参数也不太相同,不能直接复用。
截屏2024-09-26 10 18 56
截屏2024-09-26 10 19 08

Comment on lines 1574 to 1582
PADDLE_ENFORCE_EQ(
infer_context->IsEqual(startend_row_indices[3], symbol::DimExpr{1}) ||
infer_context->IsEqual(startend_row_indices[3],
symbol::DimExpr{2}) ||
infer_context->IsEqual(startend_row_indices[3], symbol::DimExpr{4}),
true,
common::errors::InvalidArgument(
"flashmask_attention startend_row_indices "
"mask_bounds must in [1,2,4]"));
Copy link
Contributor

Choose a reason for hiding this comment

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

这个enforce先删掉吧,目前还不支持编译期在控制流里判等。要写的话需要先判断下startend_row_indices[3]是不是int类型

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已删除

Comment on lines +1598 to +1622
auto batch_size_expr = q.shape()[0];
auto num_heads_expr = q.shape()[2];
auto seqlen_q_rounded_expr = round_multiple(q.shape()[1]);
auto seqlen_k_rounded_expr = round_multiple(k.shape()[1]);

if (op->result(1)) {
std::vector<symbol::DimExpr> softmax_shape{batch_size_expr,
num_heads_expr,
seqlen_q_rounded_expr,
seqlen_k_rounded_expr};
infer_context->SetShapeOrDataForValue(
op->result(1), symbol::TensorShapeOrDataDimExprs(softmax_shape));
}
if (op->result(2)) {
std::vector<symbol::DimExpr> softmax_lse_shape{
batch_size_expr, num_heads_expr, seqlen_q_rounded_expr};
infer_context->SetShapeOrDataForValue(
op->result(2), symbol::TensorShapeOrDataDimExprs(softmax_lse_shape));
}
if (op->result(3)) {
std::vector<symbol::DimExpr> seed_offset_shape{symbol::DimExpr{2}};
infer_context->SetShapeOrDataForValue(
op->result(3), symbol::TensorShapeOrDataDimExprs(out_shape));
}
return true;
Copy link
Contributor

Choose a reason for hiding this comment

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

这部分推导逻辑没在kernel、infermeta、以及FlashAttnOpInferSymbolicShape中找到对应依据

Copy link
Contributor Author

Choose a reason for hiding this comment

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

与刘旭东的类似

Copy link

paddle-ci-bot bot commented Oct 8, 2024

Sorry to inform you that 266fe33's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually.

Copy link
Contributor

@gongshaotian gongshaotian left a comment

Choose a reason for hiding this comment

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

LGTM

@luotao1 luotao1 merged commit f56e672 into PaddlePaddle:develop Oct 15, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants