Skip to content

Commit

Permalink
Fix slice from view tensors (#129)
Browse files Browse the repository at this point in the history
* Fix slice from view tensors

* Update test name
  • Loading branch information
DenisVieriu97 authored and pytorchmergebot committed Dec 7, 2022
1 parent c92cf6b commit a12ac33
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/test_mps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,31 @@ def test_expand_cpu_to_mps_copy(self):

self.assertEqual(x_cpu, x.cpu())

def test_view_slice(self):
# https://github.com/pytorch/pytorch/issues/83995
NUM_SAMPLES=60
s = (0,1)

X = torch.rand(8000, 3, dtype=torch.float32, device='cpu')
X_mps = X.detach().clone().to("cpu")

idx = torch.randint(0, X.shape[0], (1,)).repeat(len(s))
pts = torch.randint(0, X.shape[0], (NUM_SAMPLES, X.shape[1]))
idx_mps = idx.to("mps")
pts_mps = pts.to("mps")
pts[:, s] = idx
pts_mps[:, s] = idx_mps

actual_pts = torch.zeros(NUM_SAMPLES, X.shape[1], dtype=torch.float)
actual_pts_mps = torch.zeros(NUM_SAMPLES, X.shape[1], dtype=torch.float, device="mps")

for i in range(NUM_SAMPLES):
for j in range(X.shape[1]):
actual_pts_mps[i,j] = X_mps[pts_mps[i,j],j]
actual_pts[i,j] = X[pts[i,j],j]
self.assertEqual(actual_pts[i,j], actual_pts_mps[i,j])


def test_slice(self):
values = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]
cpu_x = torch.tensor(values, device='cpu')
Expand Down

0 comments on commit a12ac33

Please sign in to comment.