[cherry-pick]Add fused_attention_op: add impl wrappers. #36673
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR types
Function optimization
PR changes
OPs
Describe
功能:本PR的目标是提高attention模块的计算性能。
为了减少框架层对op的调度开销,本PR通过在C++层手动实现attention模块,对外提供attention 大op;
为了减少防存开销,本PR采取了两种优化方法:
(1)在q,k,v计算时通过共享输入X,将该处的gemm,transpose和bias add从三次调用减少为一次;
(2)使用kernel融合优化技术,在不同cuda kernel之间通过寄存器传输数据;
fused_attention_op 实现的计算逻辑:
![image](https://user-images.githubusercontent.com/11663212/138407123-c46895dc-abec-4ac0-a05b-8db33c493864.png)
fused_attention_op与paddle已有的MultiHeadAttention layer的不同:
![image](https://user-images.githubusercontent.com/11663212/138406456-81fa2736-21fd-4f78-bade-f599623f21f1.png)
(1)计算逻辑范围扩大了,详见上面的伪代码。
(2)q, k, v的weight存储格式不一样。
原有的:保存在三个weight张量中,WQ, WK, WV
本PR:保存在一个weight张量中,qkv_weight
由WQ, WK, WV得到qkv_weight的方法:
本PR是新增fused_attention 的第一个PR,主要是为一些算子的实现增加一些wrapper,方便代码复用:
1.Add impl wrappers for gemm and fmha parts in fused_attention_op.
2.Fix bugs in layer_norm and attn_bias_add.cu.h.
3.Fix bugs in elementwise_op_impl.cu.h for ternary elementwise_add impl.