-
Notifications
You must be signed in to change notification settings - Fork 39
Adds unified mask application function with causal support #71
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,22 +4,59 @@ | |||||
|
|
||||||
| #pragma once | ||||||
| #include "namespace_config.h" | ||||||
| #include <cute/tensor.hpp> | ||||||
| #include <cutlass/cutlass.h> | ||||||
| #include <cutlass/array.h> | ||||||
| #include <cutlass/numeric_types.h> | ||||||
| #include <cutlass/numeric_conversion.h> | ||||||
| #include <cutlass/fast_math.h> | ||||||
| #include <cub/block/block_merge_sort.cuh> | ||||||
|
|
||||||
| #ifndef ITEMS_PER_THREAD | ||||||
| #define ITEMS_PER_THREAD 32 | ||||||
| #endif | ||||||
| #include <cute/tensor.hpp> | ||||||
|
|
||||||
| namespace FLASH_NAMESPACE { | ||||||
|
|
||||||
| using namespace cute; | ||||||
|
|
||||||
| template <bool Causal_mask=false, typename TensorType, typename MaskType, typename BiasType> | ||||||
| __forceinline__ __device__ void apply_mask( | ||||||
| TensorType &tensor, | ||||||
| MaskType &Mask, | ||||||
| BiasType &Bias, | ||||||
| const float scale_softmax, | ||||||
| const int col_idx_offset_, | ||||||
| const int max_seqlen_k, | ||||||
| const int row_idx_offset, | ||||||
| const int max_seqlen_q, | ||||||
| const int warp_row_stride | ||||||
| ) { | ||||||
| // tensor has shape (nrow=(2, MMA_M), ncol=(2, MMA_N)) | ||||||
| static_assert(TensorType::rank == 2, "Only support 2D Tensor"); | ||||||
| static_assert(MaskType::rank == 2, "Only support 2D Mask"); | ||||||
| static_assert(BiasType::rank == 2, "Only support 2D Bias"); | ||||||
| const int lane_id = threadIdx.x % 32; | ||||||
| const int col_idx_offset = col_idx_offset_ + (lane_id % 4) * 2; | ||||||
| #pragma unroll | ||||||
| for (int mi = 0; mi < size<0, 1>(tensor); ++mi) { | ||||||
| const int row_idx_base = row_idx_offset + mi * warp_row_stride; | ||||||
| #pragma unroll | ||||||
| for (int i = 0; i < size<0, 0>(tensor); ++i) { | ||||||
| const int row_idx = row_idx_base + i * 8; | ||||||
| const int col_idx_limit = Causal_mask ? std::min(max_seqlen_k, row_idx + 1 + max_seqlen_k - max_seqlen_q) : max_seqlen_k; | ||||||
|
||||||
| const int col_idx_limit = Causal_mask ? std::min(max_seqlen_k, row_idx + 1 + max_seqlen_k - max_seqlen_q) : max_seqlen_k; | |
| const int col_idx_limit = Causal_mask ? std::min(max_seqlen_k, compute_causal_col_idx_limit(row_idx, max_seqlen_k, max_seqlen_q)) : max_seqlen_k; |
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.
The new apply_mask function only supports 2D tensors, but the existing Mask::apply_mask method in the codebase works with 3D tensors (rank == 3). This inconsistency could cause confusion and limit interoperability between the two implementations.