-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Integrate Cutlass Fused Multihead Attention in PHI #49910
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
@@ -7,7 +7,8 @@ exclude: | | |||
python/paddle/utils/gast/.+| | |||
.+_pb2\.py| | |||
python/paddle/fluid/tests/unittests/npu/.+| | |||
python/paddle/fluid/tests/unittests/mlu/.+ | |||
python/paddle/fluid/tests/unittests/mlu/.+| | |||
paddle/phi/kernels/fusion/cutlass/fused_multi_head_attention/.+ |
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.
这里引入的是外部xformers代码,暂时不做format
from op_test import OpTest | ||
|
||
# Ensure we use float type to accumulate | ||
os.environ["FLAGS_gemm_use_half_precision_compute_type"] = "0" |
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.
保证对比的naive实现gemm使用float累加
# https://github.com/facebookresearch/xformers/blob/main/xformers/csrc/attention/cuda/fmha/kernels/generate_kernels.sh | ||
|
||
#!/bin/bash | ||
set -ex |
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.
这里参考使用xformers的算子模板生成脚本,以实现并行编译,加快速度
(tb_tile_offset.n() * MM0::Mma::WarpCount::kN) + | ||
(my_warp_id / MM0::Mma::WarpCount::kM)}; | ||
|
||
if (kAddMask) { |
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.
如果要在QKmatmul后加mask,则需要将scale提前在寄存器计算好,而不是放到最后的tiledsoftmax里一起做
cutlass::multiplies<typename MM0::Mma::FragmentC>()(p.scale, accum); | ||
} | ||
|
||
int32_t mask_iter_m = kMaskBroadcastRow ? 1 : problem_size_0_m; |
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.
这里对mask的行broadcast做了一个特化
paddle/phi/kernels/fusion/cutlass/fused_multi_head_attention/gemm/custom_mma.h
Show resolved
Hide resolved
paddle/phi/kernels/fusion/cutlass/fused_multi_head_attention/kernel_forward.h
Show resolved
Hide resolved
LGTM |
paddle/phi/kernels/fusion/cutlass/fused_multi_head_attention/kernels/forward.h
Outdated
Show resolved
Hide resolved
paddle/phi/kernels/fusion/cutlass/fused_multi_head_attention.cu
Outdated
Show resolved
Hide resolved
0a01d0b
|
PR types
New features
PR changes
OPs
Describe
Integrate Cutlass fused multihead attention
You can Add custom attention_mask
cutlass2.11.0兼容问题,参考 #50073 (comment) PR修改即可
文档:
![image](https://user-images.githubusercontent.com/42901638/215963866-08c1f590-f9fb-425b-8769-8265b0af3120.png)
Benchmark
dev: cuda11.6 A100 40G
The case is borrowed from xformers
FP16:
Without mask:
With mask:
InferCase FP16
Compare script
TODO List
generate.sh “借鉴”自xformers,通过shell脚本生成对应模板特化kernel,实现并行编译,加快编译速度
后续可以考虑采用python脚本来实现Kernel生成。