-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Use NAX attention for short causal D256 prefill #4476
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
Draft
wyanzhao
wants to merge
1
commit into
ml-explore:main
Choose a base branch
from
wyanzhao:pr/sdpa-nax-d256-window-rebased
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+61
−5
Draft
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
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 |
|---|---|---|
|
|
@@ -300,6 +300,55 @@ def test_sdpa_full_head_dim_256(self): | |
| tol = 5e-3 | ||
| self.assertTrue(mx.allclose(ref, out, atol=tol, rtol=tol)) | ||
|
|
||
| # The shapes cover the causal D=256 window (512 <= qL < 1024, | ||
| # kL <= 1536) and its kL edge. | ||
| @unittest.skipIf(not mx.metal.is_available(), "Metal kernel path only") | ||
| def test_sdpa_head_dim_256_causal_prefill_window(self): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From what I understand, this test would pass regardless of this PR or not, right? What is the reason this test is introduced? Seems to be testing numerics which would be correct regardless of the routing changes |
||
| if mx.default_device() != mx.gpu: | ||
| self.skipTest("requires GPU") | ||
| D, Nq, Nkv = 256, 16, 2 | ||
| scale = D**-0.5 | ||
| for dtype, (qL, kL) in product( | ||
| (mx.float16, mx.bfloat16), | ||
| ((512, 512), (512, 1536), (512, 1537), (768, 1024), (1023, 1536)), | ||
| ): | ||
| with self.subTest(dtype=dtype, qL=qL, kL=kL): | ||
| mx.random.seed(0) | ||
| q = (0.5 * mx.random.normal((1, Nq, qL, D))).astype(dtype) | ||
| k = (0.5 * mx.random.normal((1, Nkv, kL, D))).astype(dtype) | ||
| v = (0.5 * mx.random.normal((1, Nkv, kL, D))).astype(dtype) | ||
| ref = mlx_primitives_sdpa( | ||
| q, | ||
| mx.repeat(k, Nq // Nkv, axis=1), | ||
| mx.repeat(v, Nq // Nkv, axis=1), | ||
| scale, | ||
| mask="causal", | ||
| ) | ||
| out = mx.fast.scaled_dot_product_attention( | ||
| q, k, v, scale=scale, mask="causal" | ||
| ) | ||
| self.assertTrue(mx.allclose(ref, out, atol=5e-3, rtol=5e-3)) | ||
|
|
||
| # Exercise batch/head strides, an odd tail and sinks on the new path. | ||
| for dtype in (mx.float16, mx.bfloat16): | ||
| with self.subTest(dtype=dtype, sinks=True): | ||
| q = mx.random.normal((2, 6, 513, D)).astype(dtype) | ||
| k = mx.random.normal((2, 2, 1057, D)).astype(dtype)[:, :, 1:1026] | ||
| v = mx.random.normal((2, 2, 1057, D)).astype(dtype)[:, :, 1:1026] | ||
| sinks = mx.linspace(5, 10, 6).astype(dtype) | ||
| ref = mlx_ref_attn( | ||
| q.astype(mx.float32), | ||
| k.astype(mx.float32), | ||
| v.astype(mx.float32), | ||
| scale, | ||
| mask="causal", | ||
| sinks=sinks.astype(mx.float32), | ||
| ) | ||
| out = mx.fast.scaled_dot_product_attention( | ||
| q, k, v, scale=scale, mask="causal", sinks=sinks | ||
| ) | ||
| self.assertTrue(mx.allclose(ref, out, atol=5e-3, rtol=5e-3)) | ||
|
|
||
| def test_sdpa_vector_kv_transposed_head_seq(self): | ||
| D = 64 | ||
| Nq = 4 | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
How were 512/1534 chosen? I understand they could come from the qwen, but we could do a sweep over the shapes and find the point where it the fusion starts being useful, so its more generalisable maybe.