Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions csrc/src/flash_attention_fwd_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,10 @@ inline __device__ void compute_attn_1rowblock(const Params &params, const int bi
auto mask_values_row = sDynamicMaskValues(m_idx, _);
auto predicate_k_row = sPredicate(m_idx, _);
if (predicate_k_row(k_idx)) {
// Scale the attention score before adding mask value, matching Python's behavior
acc_s(mma, mi, ki) = acc_s(mma, mi, ki) * params.scale_softmax + static_cast<ElementAccum>(mask_values_row(k_idx));
// First scale the attention score
ElementAccum scaled_score = acc_s(mma, mi, ki) * params.scale_softmax;
// Then add the mask value, matching Python's behavior
acc_s(mma, mi, ki) = scaled_score + static_cast<ElementAccum>(mask_values_row(k_idx));
} else {
// For positions where mask is 0, set attention score to -INFINITY so they don't contribute to softmax
acc_s(mma, mi, ki) = -INFINITY;
Expand Down Expand Up @@ -571,8 +573,10 @@ inline __device__ void compute_attn_1rowblock(const Params &params, const int bi
auto mask_values_row = sDynamicMaskValues(m_idx, _);
auto predicate_k_row = sPredicate(m_idx, _);
if (predicate_k_row(k_idx)) {
// Scale the attention score before adding mask value, matching Python's behavior
acc_s(mma, mi, ki) = acc_s(mma, mi, ki) * params.scale_softmax + static_cast<ElementAccum>(mask_values_row(k_idx));
// First scale the attention score
ElementAccum scaled_score = acc_s(mma, mi, ki) * params.scale_softmax;
// Then add the mask value, matching Python's behavior
acc_s(mma, mi, ki) = scaled_score + static_cast<ElementAccum>(mask_values_row(k_idx));
} else {
// For positions where mask is 0, set attention score to -INFINITY so they don't contribute to softmax
acc_s(mma, mi, ki) = -INFINITY;
Expand Down
Loading