-
Notifications
You must be signed in to change notification settings - Fork 39
Fixes mask comparison and scaling logic in attention kernel #58
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -132,10 +132,10 @@ def _fwd_kernel( | |||||
| ) | ||||||
|
|
||||||
| # Check if any element in mask is non-zero | ||||||
| any_active = tl.sum(mask > 0) > 0 | ||||||
| any_active = tl.sum(mask) > 0 | ||||||
|
|
||||||
| # compute acc_s | ||||||
| acc_s = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) | ||||||
| acc_s = tl.zeros([BLOCK_M, BLOCK_N], dtype=tl.float32) | ||||||
| if any_active: | ||||||
| # Load k | ||||||
| if EVEN_N & EVEN_M: # If we just do "if EVEN_N", there seems to be some race condition | ||||||
|
|
@@ -166,7 +166,7 @@ def _fwd_kernel( | |||||
| if IS_CAUSAL: | ||||||
| acc_s += tl.where(offs_m[:, None] >= (start_n + offs_n)[None, :], 0, float("-inf")) | ||||||
| # Apply dynamic mask | ||||||
| acc_s += tl.where(mask > 0.0, 0, float("-inf")) | ||||||
| acc_s += tl.where(mask > 0, 0, float("-inf")) | ||||||
|
|
||||||
| # Load bias | ||||||
| if EVEN_M & EVEN_N: | ||||||
|
|
@@ -184,6 +184,7 @@ def _fwd_kernel( | |||||
| # can then fuse the mult and add into an fma instruction. But if we have bias we need to | ||||||
| # to multiply with softmax_scale here. | ||||||
| acc_s = acc_s * softmax_scale + bias | ||||||
|
||||||
| acc_s = acc_s * softmax_scale + bias | |
| # Removed duplicate scaling to avoid double application of softmax_scale. |
Copilot
AI
Jul 4, 2025
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.
[nitpick] The comment # load v is vague; consider expanding it to clarify what data is being loaded and why, e.g., # load V tensor block for value projection.
| # load v | |
| # Load the V tensor block for value projection in the attention mechanism. |
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.
Consider using
tl.any(mask)(ortl.any(mask != 0)) instead oftl.sum(mask) > 0to more directly express 'any active elements' and potentially improve efficiency.