Skip to content

Faster two pass sdpa - #3023

Merged
awni merged 7 commits into
mainfrom
faster_two_pass_sdpa
Jan 24, 2026
Merged

Faster two pass sdpa#3023
awni merged 7 commits into
mainfrom
faster_two_pass_sdpa

Conversation

@awni

@awni awni commented Jan 20, 2026

Copy link
Copy Markdown
Member

This PR does two things:

  • Adds an option to use more than 32 blocks in the 2pass vector SDPA (it is set via function constant)
  • Changes the work distribution so that one thread group is responsible for all the queries for a given key to maximize locality

I tuned the block sizes on m3u, m4m and m5

@CC-Yeh CC-Yeh mentioned this pull request Jan 20, 2026
7 tasks
@awni
awni force-pushed the faster_two_pass_sdpa branch 2 times, most recently from bae68d6 to d9c9eab Compare January 21, 2026 06:13
@awni
awni force-pushed the faster_two_pass_sdpa branch from d9c9eab to 6c6de3c Compare January 22, 2026 19:21
@awni

awni commented Jan 22, 2026

Copy link
Copy Markdown
Member Author

Here's the pre-post on an M3 Ultra with KV heads = 8, head dim = 128, varying sequence length and gqa factor:
Screenshot 2026-01-22 at 12 40 28 PM

Other cases I tested have similar results and I think the tuning is mostly generalizable (as I tuned the block sizes for head dim 128 per machine).

@awni
awni marked this pull request as ready for review January 22, 2026 20:42
@awni

awni commented Jan 22, 2026

Copy link
Copy Markdown
Member Author

Closes ml-explore/mlx-lm#763

@awni
awni requested a review from angeloskath January 22, 2026 20:51
@awni
awni force-pushed the faster_two_pass_sdpa branch from 5cb12d8 to 5fdc7bb Compare January 22, 2026 21:16

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@awni
awni merged commit 1935ab4 into main Jan 24, 2026
16 checks passed
@awni
awni deleted the faster_two_pass_sdpa branch January 24, 2026 22:16
@awni awni mentioned this pull request Jan 27, 2026
CC-Yeh added a commit to CC-Yeh/mlx that referenced this pull request Jan 29, 2026
CC-Yeh added a commit to CC-Yeh/mlx that referenced this pull request Jan 29, 2026
CC-Yeh added a commit to CC-Yeh/mlx that referenced this pull request Feb 7, 2026
CC-Yeh added a commit to CC-Yeh/mlx that referenced this pull request Apr 16, 2026
jzdziarski pushed a commit to jzdziarski/mlx that referenced this pull request Jul 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants