-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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 attn_mask supported for FlashAttnKernel. #55969
Merged
Merged
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
63289f4
add mask
faf07d7
add backword
43303fa
add enforce info
99ff7fd
update scale
130034c
integrate code
61fe34d
update enforce
fd4ce6a
add enforce eq
14a7911
add error type
70f8e75
update enforce
ffeaa14
add test_flash_attention
5327835
Merge branch 'develop' into new_add_mask
Xreki 1c2c592
Polish codes and fix compiling errors.
Xreki 28a40ab
Set num_splits to 0 for flash-attn with tensor mask.
Xreki ec4bcc4
Fix the compiling error for non flash-attn case.
Xreki 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
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
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
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 |
---|---|---|
|
@@ -19,6 +19,74 @@ | |
|
||
namespace phi { | ||
|
||
template <typename T, typename Context> | ||
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. 该头文件中,不需要加这个函数声明。 |
||
void FlashAttnFwdWithBiasAndMask( | ||
const Context& ctx, | ||
const void* | ||
q, // total_q x num_heads x head_size, total_q := \sum_{i=0}^{b} s_i | ||
const void* | ||
k, // total_k x num_heads x head_size, total_k := \sum_{i=0}^{b} s_i | ||
const void* | ||
v, // total_k x num_heads x head_size, total_k := \sum_{i=0}^{b} s_i | ||
void* | ||
out, // total_q x num_heads x head_size, total_k := \sum_{i=0}^{b} s_i | ||
const int32_t* | ||
cu_seqlens_q, // int32, batch_size+1, starting offset of each sequence | ||
const int32_t* | ||
cu_seqlens_k, // int32, batch_size+1, starting offset of each sequence | ||
const int total_q, | ||
const int total_k, | ||
const int batch_size, | ||
const int num_heads, | ||
const int head_size, | ||
const int max_seqlen_q, | ||
const int max_seqlen_k, | ||
const float dropout, | ||
const float scale, | ||
const bool zero_tensors, | ||
const bool is_bf16, | ||
const int num_splits, // SMs per attention matrix, can be 1 | ||
void* softmax_lse_ptr, // softmax log_sum_exp | ||
cudaStream_t stream, | ||
uint64_t seed, | ||
uint64_t offset, | ||
const void* attn_mask, | ||
const int64_t* mask_dims); | ||
|
||
template <typename T, typename Context> | ||
void FlashAttnFwd( | ||
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. 该头文件中,不需要加这个函数声明。 |
||
const Context& ctx, | ||
const void* | ||
q, // total_q x num_heads x head_size, total_q := \sum_{i=0}^{b} s_i | ||
const void* | ||
k, // total_k x num_heads x head_size, total_k := \sum_{i=0}^{b} s_i | ||
const void* | ||
v, // total_k x num_heads x head_size, total_k := \sum_{i=0}^{b} s_i | ||
void* | ||
out, // total_q x num_heads x head_size, total_k := \sum_{i=0}^{b} s_i | ||
const void* | ||
cu_seqlens_q, // int32, batch_size+1, starting offset of each sequence | ||
const void* | ||
cu_seqlens_k, // int32, batch_size+1, starting offset of each sequence | ||
const int total_q, | ||
const int total_k, | ||
const int batch_size, | ||
const int num_heads, | ||
const int head_size, | ||
const int max_seqlen_q, | ||
const int max_seqlen_k, | ||
const float dropout, | ||
const float scale, | ||
const bool zero_tensors, | ||
const bool causal, | ||
const bool is_bf16, | ||
const int num_splits, // SMs per attention matrix, can be 1 | ||
void* softmax_lse_ptr, // softmax log_sum_exp | ||
const bool return_softmax, | ||
cudaStream_t stream, | ||
uint64_t seed, | ||
uint64_t offset); | ||
|
||
template <typename T, typename Context> | ||
void FlashAttnUnpaddedKernel( | ||
const Context& ctx, | ||
|
@@ -28,6 +96,7 @@ void FlashAttnUnpaddedKernel( | |
const DenseTensor& cu_seqlens_q, | ||
const DenseTensor& cu_seqlens_k, | ||
const paddle::optional<DenseTensor>& fixed_seed_offset, | ||
const paddle::optional<DenseTensor>& attn_mask, | ||
int64_t max_seqlen_q, | ||
int64_t max_seqlen_k, | ||
float scale, | ||
|
@@ -47,6 +116,7 @@ void FlashAttnKernel(const Context& ctx, | |
const DenseTensor& k, | ||
const DenseTensor& v, | ||
const paddle::optional<DenseTensor>& fixed_seed_offset, | ||
const paddle::optional<DenseTensor>& attn_mask, | ||
float dropout, | ||
bool causal, | ||
bool return_softmax, | ||
|
Oops, something went wrong.
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.
fixed_seed_offset
也要加到optional吧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.
fixed_seed_offset是原有参数,类型为const Tensor,不是optional
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.
感觉原写法也不是很合理,先保持原样吧
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.
没问题了,反向的输入是
seed_offset
,是前向的输出,是必须的。