Skip to content

Commit 822d047

Browse files
malfetIsalia20
andauthored
[MPS] Fix bool mask handling in 1-pass SDPA decode kernel (#182285) (#182311)
Fix bool mask handling in 1-pass SDPA decode kernel. Small reproducer: ``` import torch import torch.nn.functional as F q = torch.randn(1, 1, 8, 64, device="mps") k = torch.randn(1, 1, 512, 64, device="mps") v = torch.randn(1, 1, 512, 64, device="mps") mask = torch.ones(8, 512, dtype=torch.bool, device="mps") mps_op = torch.ops.aten._scaled_dot_product_attention_math_for_mps out, _ = mps_op(q, k, v, mask) ref = F.scaled_dot_product_attention(q.cpu(), k.cpu(), v.cpu(), attn_mask=mask.cpu()) print("max |mps - cpu| =", (out.cpu() - ref).abs().max().item()) ``` Pull Request resolved: #182285 Approved by: https://github.com/malfet (cherry picked from commit f6c73b4) Co-authored-by: Isalia20 <[email protected]>
1 parent 5c5e523 commit 822d047

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎aten/src/ATen/native/mps/kernels/Attention.metal‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ template <typename T, int D, int V = D>
3434
const uint k_seq_stride = qkv_seq_strides.y;
3535
const uint v_head_stride = qkv_head_strides.z;
3636
const uint v_seq_stride = qkv_seq_strides.z;
37-
const uint mask_head_stride = mask_strides.x;
38-
const uint mask_kv_seq_stride = mask_strides.y;
39-
const uint mask_q_seq_stride = mask_strides.z;
37+
const uint mask_kv_seq_stride = mask_strides.x;
38+
const uint mask_q_seq_stride = mask_strides.y;
39+
const uint mask_head_stride = mask_strides.z;
4040
uint inner_k_stride = BN * int(k_seq_stride);
4141
uint inner_v_stride = BN * int(v_seq_stride);
4242

‎test/test_mps.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9953,6 +9953,22 @@ def test_sdpa_full_mask(self, dtype):
99539953
out_mps = F.scaled_dot_product_attention(q.to('mps'), k.to('mps'), v.to('mps'), attn_mask=mask.to('mps'))
99549954
self._compare_tensors(out_mps.cpu(), out_cpu)
99559955

9956+
@parametrize("dtype", [torch.float32, torch.bfloat16])
9957+
def test_sdpa_math_mps_bool_mask_1pass(self, dtype):
9958+
torch.manual_seed(0)
9959+
q = torch.randn(1, 1, 8, 64, dtype=dtype, device='mps')
9960+
k = torch.randn(1, 1, 512, 64, dtype=dtype, device='mps')
9961+
v = torch.randn(1, 1, 512, 64, dtype=dtype, device='mps')
9962+
mask = torch.eye(8, 512, dtype=torch.bool, device='mps')
9963+
9964+
out_mps, _ = torch.ops.aten._scaled_dot_product_attention_math_for_mps(
9965+
q, k, v, mask
9966+
)
9967+
out_cpu = F.scaled_dot_product_attention(
9968+
q.cpu(), k.cpu(), v.cpu(), attn_mask=mask.cpu()
9969+
)
9970+
self._compare_tensors(out_mps.cpu(), out_cpu)
9971+
99569972
@parametrize("dtype", [torch.bfloat16, torch.float16, torch.float])
99579973
def test_sdpa_2pass(self, dtype):
99589974
# Regression test for https://github.com/pytorch/pytorch/issues/174861

0 commit comments

Comments
 (0)