[MPS] Fix unary ops over sparse-mapped tensors (#100765) If input tensor is backed by a sparse view, create a dense copy before running unary op, otherwise op will be applied against the wrong elements. Introduce `is_dense_in_storage` that returns true if tensor/view are mapped to a dense area in the tensor storage. Add unit test to validate the fix. Fixes https://github.com/pytorch/pytorch/issues/98074 Pull Request resolved: https://github.com/pytorch/pytorch/pull/100765 Approved by: https://github.com/albanD
diff --git a/test/test_mps.py b/test/test_mps.py index 7ae5e21..a31cc50 100644 --- a/test/test_mps.py +++ b/test/test_mps.py
@@ -6620,6 +6620,13 @@ helper((2, 8, 4, 5)) + def test_neg_strided_input(self): + # See https://github.com/pytorch/pytorch/issues/98074#issuecomment-1496088337 + x = torch.arange(18.0, device='mps').reshape(2, 3, 3) + y = x.permute(1, 0, 2)[..., 1] + z = y + y.neg() + self.assertEqual(z.abs().max().item(), 0.0) + # Test index add def test_index_add(self): def helper(shape, dim, index, source_shape, alpha, x_dtype=torch.float32, idx_dtype=torch.int32):