-
Notifications
You must be signed in to change notification settings - Fork 764
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
Add API flash_attn_qkvpacked and flash_attn_varlen_qkvpacked #6608
Open
kircle888
wants to merge
2
commits into
PaddlePaddle:develop
Choose a base branch
from
kircle888:develop
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
.. _cn_api_paddle_nn_functional_flash_attn_qkvpacked: | ||
|
||
flash_attn_qkvpacked | ||
------------------------------- | ||
|
||
.. py:function:: paddle.nn.functional.flash_attn_qkvpacked(qkv, dropout=0.0, causal=False, return_softmax=False, return_softmax_lse=False, return_seed_offset=False, training=True, name=None) | ||
|
||
QKVPacked的 flash_attention。 | ||
|
||
.. math:: | ||
|
||
result = softmax(\frac{ Q * K^T }{\sqrt{d}} + mask) * V | ||
|
||
参数 | ||
:::::::::::: | ||
|
||
- **qkv** (Tensor) - 输入 Query+Key+Value Tensor,shape =[batch_size, seq_len, num_group + 2, num_heads_k, head_dim],数据类型为 float16 或 bfloat16,其中num_group=num_heads_q/num_heads_k,并且qkv[:,:,:num_group,:,:]是Query Tensor,qkv[:,:,num_group,:,:]是Key Tensor,qkv[:,:,num_group+1,:,:]是Value Tensor | ||
- **dropout** (bool,可选) – dropout 概率值,默认值为 0。 | ||
- **causal** (bool,可选) - 是否使用 causal 模式,默认值:False。 | ||
- **return_softmax** (bool,可选) - 是否返回 softmax 的结果。默认值 False。 | ||
- **return_softmax_lse** (bool,可选) - 是否返回 return_softmax_lse 的结果。默认值为 False。 | ||
- **return_seed_offset** (bool,可选) - 是否返回 return_seed_offset 的结果。默认值为 False。 | ||
- **training** (bool,可选) - 指示是否为训练模式。默认值为 True。 | ||
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。 | ||
|
||
|
||
返回 | ||
:::::::::::: | ||
`Tensor`,attention 的结果。 | ||
|
||
|
||
代码示例 | ||
:::::::::::: | ||
COPY-FROM: paddle.nn.functional.flash_attn_qkvpacked |
40 changes: 40 additions & 0 deletions
40
docs/api/paddle/nn/functional/flash_attn_varlen_qkvpacked_cn.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
.. _cn_api_paddle_nn_functional_flash_attn_varlen_qkvpacked: | ||
|
||
flash_attn_varlen_qkvpacked | ||
------------------------------- | ||
|
||
.. py:function:: paddle.nn.functional.flash_attn_varlen_qkvpacked(qkv, cu_seqlens_q, cu_seqlens_k, max_seqlen_q, max_seqlen_k, scale, dropout=0.0, causal=False, return_softmax=False, return_softmax_lse=False, return_seed_offset=False, varlen_padded=True, training=True, name=None) | ||
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. 函数签名跟 PaddlePaddle/Paddle#63289 不一致 |
||
|
||
支持变长batch的QKVPacked的 flash_attention。 | ||
|
||
.. math:: | ||
|
||
result = softmax(\frac{ Q * K^T }{\sqrt{d}} + mask) * V | ||
|
||
参数 | ||
:::::::::::: | ||
|
||
- **qkv** (Tensor) - 输入 Query+Key+Value Tensor,shape =[total_seqlen, num_group + 2, num_heads_k, head_dim],数据类型为 float16 或 bfloat16,其中num_group=num_heads_q/num_heads_k,并且qkv[:,:num_group,:,:]是Query Tensor,qkv[:,num_group,:,:]是Key Tensor,qkv[:,num_group+1,:,:]是Value Tensor | ||
- **cu_seqlens_q** (Tensor) - batch中Query序列的累积序列长度 | ||
- **cu_seqlens_k** (Tensor) - batch中Key/Value序列的累积序列长度 | ||
- **max_seqlen_q** (int) - batch中单个Query序列最大长度 | ||
- **max_seqlen_k** (int) - batch中单个Key/Value序列最大长度 | ||
- **scale** (float) - QK^T在执行Softmax前的缩放因子 | ||
- **dropout** (bool,可选) – dropout 概率值,默认值为 0。 | ||
- **causal** (bool,可选) - 是否使用 causal 模式,默认值:False。 | ||
- **return_softmax** (bool,可选) - 是否返回 softmax 的结果。默认值 False。 | ||
- **return_softmax_lse** (bool,可选) - 是否返回 return_softmax_lse 的结果。默认值为 False。 | ||
- **return_seed_offset** (bool,可选) - 是否返回 return_seed_offset 的结果。默认值为 False。 | ||
- **varlen_padded** (bool,可选) - 输入输出是否采用Padded模式,当设置为True时输入输出应按照max_seqlen序列长度Padding,否则输入输出为Unpad格式,默认值为 True | ||
- **training** (bool,可选) - 指示是否为训练模式。默认值为 True。 | ||
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。 | ||
|
||
|
||
返回 | ||
:::::::::::: | ||
`Tensor`,attention 的结果。 | ||
|
||
|
||
代码示例 | ||
:::::::::::: | ||
COPY-FROM: paddle.nn.functional.flash_attn_varlen_qkvpacked |
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.
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.
函数签名为什么跟 PaddlePaddle/Paddle#63289 中的函数签名不一样?