Repository navigation
[CUDA] Faster and more memory efficient boolean masks in attention - #4934
Merged
Merged
Conversation
Contributor
Author
full results
|
pierreablin
marked this pull request as ready for review
November 22, 2026 17:25
Contributor
Author
|
Thanks @zcbenz , I have removed the tests I added. |
Open
2 of 4 tasks
rcfa
pushed a commit
to cubiculum-ai/mlx
that referenced
this pull request
Nov 26, 2026
…l-explore#4934) (cherry picked from commit f04e1f2)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This pr makes
mx.fast.scaled_dot_product_attention(q, k, v, scale=D**-0.5, mask=m)faster and more memory efficient when the maskmis a boolean array, by broadcastingm.Benchmark setup
I time the new functions forward and backward with
with different shapes for
q, k, v, andm.This is run on one h100, with the following shapes studied :
[Lq,Lk][B,1,Lq,Lk][Lq,Lk][B,1,Lq,Lk][Lq,Lk][B,1,Lq,Lk][Lq,Lk][Lq,Lk][Lq,Lk][B,Hq,Lq,Lk][1,Hq,Lq,Lk][B,1,1,Lk][Lk][][B,1,Lq,1][Lq,Lk][Lq,Lk][B,1,Lq,Lk][B,1,Lq,Lk][Lq,Lk][B,1,1,Lk][Lq,Lk][Lq,Lk][B,1,Lq,Lk][B,1,Lq,Lk][B,1,Lq,Lk]For each, I benchmarked
fwdorfwd_bwd,bfloat16orfloat16andeagerorcompiled, giving26 * 2 * 2 *2 = 208configurations.Results
Here are only some selected configurations, I will put all the results in a follow up comment.
Overall, everything is faster and more memory efficient.
(Claude wrote this table)