10000 [MPS] tril op not handling infs correctly by pytorchbot · Pull Request #150479 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[MPS] tril op not handling infs correctly #150479

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

Merged
merged 1 commit into from
Apr 1, 2025
Merged
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
7 changes: 6 additions & 1 deletion aten/src/ATen/native/mps/operations/TriangularOps.mm
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@
numLowerTensor:negDiagMinusOneTensor
numUpperTensor:minusOneTensor
name:nil];
outputTensor = [mpsGraph subtractionWithPrimaryTensor:inputTensor secondaryTensor:complementTensor name:nil];
MPSGraphTensor* zeroTensor = [mpsGraph constantWithScalar:0.0 dataType:getMPSDataType(self)];
MPSGraphTensor* mask = [mpsGraph equalWithPrimaryTensor:complementTensor secondaryTensor:zeroTensor name:nil];
outputTensor = [mpsGraph selectWithPredicateTensor:mask
truePredicateTensor:inputTensor
falsePredicateTensor:zeroTensor
name:nil];
}

newCachedGraph->inputTensor_ = inputTensor;
Expand Down
22 changes: 15 additions & 7 deletions test/test_mps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7849,13 +7849,21 @@ def helper(shape, diag=0):
self.assertEqual(tril_result, tril_result_cpu)
self.assertEqual(x.grad, cpu_x.grad)

helper((2, 8, 4, 5))
helper((2, 8, 4, 5), diag=1)
helper((2, 8, 4, 5), diag=2)
helper((2, 8, 4, 5), diag=3)
helper((2, 8, 4, 5), diag=-1)
helper((2, 8, 4, 5), diag=-2)
helper((2, 8, 4, 5), diag=-3)
for diag in [0, 1, 2, 3, -1, -2, -3]:
helper((2, 8, 4, 5), diag=diag)

def helper_nans_infs(value, diag_vals=(0, 1, -2)):
"""For nans and infs"""
mps_tensor = torch.full((2, 2, 5, 5), value, device="mps")
cpu_tensor = torch.full((2, 2, 5, 5), value, device="cpu")
for diag in diag_vals:
mps_result = torch.tril(mps_tensor, diagonal=diag)
cpu_result = torch.tril(cpu_tensor, diagonal=diag)
self.assertEqual(mps_result, cpu_result, f"Mismatch for diag={diag}")

helper_nans_infs(float("inf"))
helper_nans_infs(float("-inf"))
helper_nans_infs(float("nan"))

# test eye
def test_eye(self):
Expand Down
Loading
0