Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ using namespace metal;
instantiate_sdpa_vector(type, 512, 512) \
instantiate_sdpa_vector_gqa(type, 64, 64, 8, 8) \
instantiate_sdpa_vector_gqa(type, 128, 128, 8, 4) \
instantiate_sdpa_vector_gqa(type, 256, 256, 8, 2) \
instantiate_sdpa_vector_gqa(type, 128, 128, 12, 4) \
instantiate_sdpa_vector_gqa(type, 128, 128, 16, 2) \
instantiate_sdpa_vector_aggregation(type, 64) \
Expand Down
3 changes: 2 additions & 1 deletion mlx/backend/metal/scaled_dot_product_attention.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,8 @@ void sdpa_vector_2pass(
kname += "sdpa_vector_2pass_1";
int gqa_factor = q.shape(1) / k.shape(1);
bool gqa_dims =
(gqa_factor == 8 && (q.shape(-1) == 64 || q.shape(-1) == 128)) ||
(gqa_factor == 8 &&
(q.shape(-1) == 64 || q.shape(-1) == 128 || q.shape(-1) == 256)) ||
((gqa_factor == 12 || gqa_factor == 16) && q.shape(-1) == 128);
if (!mask && !sinks && q.shape(2) == 1 &&
q.shape(1) == gqa_factor * k.shape(1) && q.shape(-1) == v.shape(-1) &&
Expand Down
25 changes: 25 additions & 0 deletions python/tests/test_fast_sdpa.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,31 @@ def test_sdpa_vector_gqa_long(self):
out = mx.fast.scaled_dot_product_attention(q, k, v, scale=scale)
self.assertTrue(mx.allclose(ref, out, atol=1e-4, rtol=1e-4))

@unittest.skipUnless(mx.metal.is_available(), "Metal is not available")
def test_sdpa_vector_gqa_d256(self):

@RohanGautam RohanGautam Sep 12, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same concern i had in #4476 , this mainly tests numerics and not necessarily the changes you introduced. For your changes, good benchmarking should be sufficient. But let me know if you disagree!

if mx.default_device() != mx.gpu:
self.skipTest("requires GPU")
mx.random.seed(0)
for dtype, atol in [
(mx.float32, 1e-4),
(mx.float16, 1e-3),
(mx.bfloat16, 5e-3),
]:
for B, length in [(1, 8192), (1, 8201), (2, 8192), (2, 32768)]:
with self.subTest(dtype=dtype, B=B, length=length):
q = mx.random.normal((B, 16, 1, 256)).astype(dtype)
k = mx.random.normal((B, 2, length + 32, 256)).astype(dtype)
v = mx.random.normal((B, 2, length + 32, 256)).astype(dtype)
k, v = k[:, :, :length], v[:, :, :length]
ref = mlx_primitives_sdpa(
q.astype(mx.float32),
mx.repeat(k.astype(mx.float32), 8, axis=1),
mx.repeat(v.astype(mx.float32), 8, axis=1),
256**-0.5,
)
out = mx.fast.scaled_dot_product_attention(q, k, v, scale=256**-0.5)
self.assertTrue(mx.allclose(ref, out, atol=atol, rtol=1e-3))

@unittest.skipIf(not mx.metal.is_available(), "Metal kernel path only")
def test_sdpa_vector_head_dim_512(self):
if mx.default_device() != mx.gpu:
Expand Down
Loading