Skip to content
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

up the priority of numpy array comparisons in self.assertEqual #59067

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions test/test_binary_ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,10 +1262,10 @@ def test_maximum_minimum_float(self, device, dtype):

if alias is not None:
alias_result = alias(a_tensor, b_tensor)
self.assertEqual(alias_result, tensor_result)
self.assertEqual(alias_result, tensor_result, exact_dtype=False)

self.assertEqual(tensor_result, numpy_result)
self.assertEqual(out, numpy_result)
self.assertEqual(tensor_result, numpy_result, exact_dtype=False)
self.assertEqual(out, numpy_result, exact_dtype=False)

@dtypes(*(torch.testing.get_all_fp_dtypes()))
def test_maximum_minimum_float_nan_and_inf(self, device, dtype):
Expand Down Expand Up @@ -1713,7 +1713,7 @@ def test_hypot(self, device, dtype):
expected = torch.sqrt(input[0] * input[0] + input[1] * input[1])
else:
expected = np.hypot(input[0].cpu().numpy(), input[1].cpu().numpy())
self.assertEqual(actual, expected)
self.assertEqual(actual, expected, exact_dtype=False)

@onlyOnCPUAndCUDA
@dtypes(torch.uint8, torch.int8, torch.int16, torch.int32, torch.int64)
Expand All @@ -1723,14 +1723,15 @@ def test_gcd(self, device, dtype):
t2 = torch.tensor([0, 0, 10], dtype=dtype, device=device)
actual = torch.gcd(t1, t2)
expected = np.gcd([0, 10, 0], [0, 0, 10])
self.assertEqual(actual, expected)
self.assertEqual(actual, expected, exact_dtype=False)

if dtype == torch.uint8:
# Test unsigned integers with potential sign issues (i.e., uint8 with value >= 128)
a = torch.tensor([190, 210], device=device, dtype=dtype)
b = torch.tensor([190, 220], device=device, dtype=dtype)
actual = torch.gcd(a, b)
expected = torch.tensor([190, 10], device=device, dtype=dtype)
self.assertEqual(actual, expected)
else:
# Compares with NumPy
a = torch.randint(-20, 20, (1024,), device=device, dtype=dtype)
Expand All @@ -1747,14 +1748,14 @@ def test_lcm(self, device, dtype):
t2 = torch.tensor([0, 0, 10], dtype=dtype, device=device)
actual = torch.lcm(t1, t2)
expected = np.lcm([0, 10, 0], [0, 0, 10])
self.assertEqual(actual, expected)
self.assertEqual(actual, expected, exact_dtype=False)

# Compares with NumPy
a = torch.randint(-20, 20, (1024,), device=device, dtype=dtype)
b = torch.randint(-20, 20, (1024,), device=device, dtype=dtype)
actual = torch.lcm(a, b)
expected = np.lcm(a.cpu().numpy(), b.cpu().numpy())
self.assertEqual(actual, expected)
self.assertEqual(actual, expected, exact_dtype=False)

@onlyOnCPUAndCUDA
@dtypes(torch.float32, torch.float64)
Expand Down Expand Up @@ -2476,14 +2477,14 @@ def test_dx(sizes, dim, dx, device):
actual = torch.trapz(t, dx=dx, dim=dim)
expected = np.trapz(t.cpu().numpy(), dx=dx, axis=dim)
self.assertEqual(expected.shape, actual.shape)
self.assertEqual(expected, actual)
self.assertEqual(expected, actual, exact_dtype=False)

def test_x(sizes, dim, x, device):
t = torch.randn(sizes, device=device)
actual = torch.trapz(t, x=torch.tensor(x, device=device), dim=dim)
expected = np.trapz(t.cpu().numpy(), x=x, axis=dim)
self.assertEqual(expected.shape, actual.shape)
self.assertEqual(expected, actual.cpu())
self.assertEqual(expected, actual.cpu(), exact_dtype=False)

test_dx((2, 3, 4), 1, 1, device)
test_dx((10, 2), 0, 0.1, device)
Expand Down
6 changes: 3 additions & 3 deletions test/test_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8267,7 +8267,7 @@ def _gelu_ref(X):
X = torch.rand(n, m, dtype=dtype, requires_grad=True, device=d)[:, ::2]
res = F.gelu(X)
ref = _gelu_ref(X.to(numpy_dtype).cpu().detach().numpy())
self.assertEqual(res, ref, rtol=rtol, atol=atol)
self.assertEqual(res, ref, rtol=rtol, atol=atol, exact_dtype=False)
if dtype == torch.float64:
gradcheck(F.gelu, [X], eps=1e-4)

Expand Down Expand Up @@ -12077,7 +12077,7 @@ def test_affine_2d_rotateRandom(self, device):
for r in range(affine_tensor.size(1)):
for c in range(affine_tensor.size(2)):
grid_out = np.dot(grid_ary, [r, c, 1])
self.assertEqual(affine_tensor[0, r, c], grid_out[:2])
self.assertEqual(affine_tensor[0, r, c], grid_out[:2], exact_dtype=False)

self.assertEqual(scipy_ary, gridsample_ary.reshape_as(scipy_ary))

Expand Down Expand Up @@ -12132,7 +12132,7 @@ def test_affine_3d_rotateRandom(self, device):
for r in range(affine_tensor.size(2)):
for c in range(affine_tensor.size(3)):
grid_out = np.dot(grid_ary, [i, r, c, 1])
self.assertEqual(affine_tensor[0, i, r, c], grid_out[:3])
self.assertEqual(affine_tensor[0, i, r, c], grid_out[:3], exact_dtype=False)

self.assertEqual(scipy_ary, gridsample_ary.reshape_as(scipy_ary))

Expand Down
22 changes: 12 additions & 10 deletions test/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1814,7 +1814,7 @@ def test_median_real_values(self, device, dtype):
if size % 2 == 1:
# We can only test agains numpy for odd reductions because numpy
# returns the mean of the two medians and torch returns the lower
self.assertEqual(res[0].cpu().numpy(), np.median(t_numpy, dim, keepdims=True))
self.assertEqual(res[0].cpu().numpy(), np.median(t_numpy, dim, keepdims=True), exact_dtype=False)

@dtypes(torch.float, torch.double)
@dtypesIfCUDA(torch.half, torch.float, torch.double)
Expand Down Expand Up @@ -2086,7 +2086,7 @@ def _compare_std_var_with_numpy(self, op, device, dtype, input, dim,
return
self.fail("Failed to hit RuntimeError!")

exact_dtype = input.dtype != torch.bfloat16
exact_dtype = input.dtype not in (torch.bfloat16, torch.complex32, torch.complex64, torch.complex128)
pmeier marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(torch_result, numpy_result, exact_dtype=exact_dtype)

@dtypes(torch.float, torch.double, torch.cfloat, torch.cdouble)
Expand Down Expand Up @@ -2397,21 +2397,21 @@ def test_tensor_compare_ops_empty(self, device):
error_msg = f"test function: {name}"
self.assertEqual(torch.empty((2, 0), device=device), fn(master_input, dim=2), msg=error_msg)
self.assertEqual(np_function(np_input, axis=2),
fn(master_input, dim=2).cpu().numpy(), msg=error_msg)
fn(master_input, dim=2).cpu().numpy(), msg=error_msg, exact_dtype=False)

self.assertEqual(torch.empty((2, 0), device=device), fn(master_input, dim=-1), msg=error_msg)
self.assertEqual(np_function(np_input, axis=-1),
fn(master_input, dim=-1).cpu().numpy(), msg=error_msg)
fn(master_input, dim=-1).cpu().numpy(), msg=error_msg, exact_dtype=False)

self.assertEqual(torch.empty((2, 0, 1), device=device), fn(master_input, dim=2, keepdim=True),
msg=error_msg)
self.assertEqual(np_function(np_input, axis=2, keepdims=True),
fn(master_input, dim=2, keepdim=True).cpu().numpy(), msg=error_msg)
fn(master_input, dim=2, keepdim=True).cpu().numpy(), msg=error_msg, exact_dtype=False)

self.assertEqual(torch.empty((2, 0, 1), device=device), fn(master_input, dim=-1, keepdim=True),
msg=error_msg)
self.assertEqual(np_function(np_input, axis=-1, keepdims=True),
fn(master_input, dim=-1, keepdim=True).cpu().numpy(), msg=error_msg)
fn(master_input, dim=-1, keepdim=True).cpu().numpy(), msg=error_msg, exact_dtype=False)

# Check if function raises error on specified zero'd dimension as reduction dim.
self.assertRaisesRegex(IndexError, "Expected reduction dim", lambda: fn(master_input, dim=1))
Expand Down Expand Up @@ -2476,18 +2476,20 @@ def test_tensor_reduce_ops_empty(self, device):
# Check if reduction happens along the specified dimension.
error_msg = f"test function: {name}"
self.assertEqual(torch.empty((2, 0), device=device), fn(master_input, dim=2), msg=error_msg)
self.assertEqual(np_function(np_input, axis=2), fn(master_input, dim=2).cpu().numpy(), msg=error_msg)
self.assertEqual(np_function(np_input, axis=2), fn(master_input, dim=2).cpu().numpy(), msg=error_msg,
exact_dtype=False)

self.assertEqual(torch.empty((2, 0), device=device), fn(master_input, dim=-1), msg=error_msg)
self.assertEqual(np_function(np_input, axis=-1), fn(master_input, dim=-1).cpu().numpy(), msg=error_msg)
self.assertEqual(np_function(np_input, axis=-1), fn(master_input, dim=-1).cpu().numpy(), msg=error_msg,
exact_dtype=False)

self.assertEqual(torch.empty((2, 0, 1), device=device), fn(master_input, dim=2, keepdim=True), msg=error_msg)
self.assertEqual(np_function(np_input, axis=2, keepdims=True), fn(master_input, dim=2, keepdim=True),
msg=error_msg)
msg=error_msg, exact_dtype=False)

self.assertEqual(torch.empty((2, 0, 1), device=device), fn(master_input, dim=-1, keepdim=True), msg=error_msg)
self.assertEqual(np_function(np_input, axis=-1, keepdims=True), fn(master_input, dim=-1, keepdim=True),
msg=error_msg)
msg=error_msg, exact_dtype=False)

# Check if returned data is correct.
check_func = (torch.testing.assert_allclose if math.isnan(return_value) or math.isinf(return_value) else
Expand Down
12 changes: 6 additions & 6 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4955,15 +4955,15 @@ def filter_shape(shape, dim):
spacing = [space.cpu().detach().numpy() for space in spacing]
expected = np.gradient(t_np, *self._wrap_to_list(spacing), axis=dims, edge_order=1)
actual, expected = self._inf_nan_preprocess(list(actual), self._wrap_to_list(expected))
self.assertEqual(actual, expected, equal_nan="relaxed")
self.assertEqual(actual, expected, equal_nan="relaxed", exact_dtype=False)

@onlyOnCPUAndCUDA
@dtypes(torch.long, torch.float32, torch.complex64)
def test_gradient_extreme_cases(self, device, dtype):
# Test behaviour for inf and nan values
actual = torch.gradient(torch.tensor([2, -2, inf, inf, -inf, -inf, inf, 3, -inf, 2, nan, nan, 3, inf, nan]))
expected = np.gradient(np.array([2, -2, inf, inf, -inf, -inf, inf, 3, -inf, 2, nan, nan, 3, inf, nan]))
self.assertEqual(actual, self._wrap_to_list(expected),)
self.assertEqual(actual, self._wrap_to_list(expected), exact_dtype=False)

# Test behaviour in very big tensors
large_size = 100000
Expand All @@ -4973,7 +4973,7 @@ def test_gradient_extreme_cases(self, device, dtype):
coordinates = [torch.tensor(coordinates_np, device=device)]
actual = torch.gradient(t, spacing=coordinates, dim=0, edge_order=1)
expected = [np.gradient(t_np, coordinates_np, axis=0, edge_order=1)]
self.assertEqual(actual, expected)
self.assertEqual(actual, expected, exact_dtype=False)

@onlyOnCPUAndCUDA
def test_gradient_type_promotion(self, device):
Expand Down Expand Up @@ -5012,13 +5012,13 @@ def test_gradient_type_promotion(self, device):
expected = np.gradient(input_np, *spacing_or_coord_np, axis=(0, 1), edge_order=1)
if actual[0].dtype == torch.complex64 and input.dtype != torch.complex64:
for i in range(len(actual)):
self.assertEqual(actual[i].real, expected[i].real)
self.assertEqual(actual[i].real, expected[i].real, exact_dtype=False)
# Type promotion fails on Numpy when spacing is given as complex number and input is given as real.
# Result is given just as real number and all the imaginary parts to be equal to zero.
self.assertEqual(expected[i].imag, torch.zeros(actual[i].shape))
self.assertEqual(expected[i].imag, torch.zeros(actual[i].shape), exact_dtype=False)
else:
actual, expected = self._inf_nan_preprocess(list(actual), expected)
self.assertEqual(actual, expected, equal_nan="relaxed")
self.assertEqual(actual, expected, equal_nan="relaxed", exact_dtype=False)

@onlyOnCPUAndCUDA
@dtypes(torch.long, torch.float32, torch.complex64)
Expand Down
24 changes: 12 additions & 12 deletions torch/testing/_internal/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,18 @@ def assertEqual(self, x, y, msg: Optional[str] = None, *,
assert debug_msg_generic is not None
debug_msg = "Tensors failed to compare as equal!" + debug_msg_generic
super().assertTrue(result, msg=self._get_assert_msg(msg, debug_msg=debug_msg))
# Tensor x Numpy array
elif isinstance(x, torch.Tensor) and isinstance(y, np.ndarray):
self.assertEqual(x, torch.from_numpy(y), atol=atol, rtol=rtol, msg=msg,
exact_dtype=exact_dtype, exact_device=exact_device)
# Numpy array x Tensor
elif isinstance(x, np.ndarray) and isinstance(y, torch.Tensor):
self.assertEqual(torch.from_numpy(x), y, atol=atol, rtol=rtol, msg=msg,
exact_dtype=exact_dtype, exact_device=exact_device)
# Numpy array x Numpy array
elif isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
self.assertEqual(torch.from_numpy(x), torch.from_numpy(y), atol=atol, rtol=rtol, msg=msg,
exact_dtype=exact_dtype, exact_device=exact_device)
elif isinstance(x, string_classes) and isinstance(y, string_classes):
debug_msg = ("Attempted to compare [string] types: "
f"Expected: {repr(x)}; Actual: {repr(y)}.")
Expand Down Expand Up @@ -1401,18 +1413,6 @@ def assertEqual(self, x, y, msg: Optional[str] = None, *,
assert debug_msg_scalars is not None
debug_msg = "Scalars failed to compare as equal! " + debug_msg_scalars
super().assertTrue(result, msg=self._get_assert_msg(msg, debug_msg=debug_msg))
# Tensor x Numpy array
elif isinstance(x, torch.Tensor) and isinstance(y, np.ndarray):
self.assertEqual(x, torch.from_numpy(y), atol=atol, rtol=rtol, msg=msg,
exact_dtype=exact_dtype, exact_device=exact_device)
# Numpy array x Tensor
elif isinstance(x, np.ndarray) and isinstance(y, torch.Tensor):
self.assertEqual(torch.from_numpy(x), y, atol=atol, rtol=rtol, msg=msg,
exact_dtype=exact_dtype, exact_device=exact_device)
# Numpy array x Numpy array
elif isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
self.assertEqual(torch.from_numpy(x), torch.from_numpy(y), atol=atol, rtol=rtol, msg=msg,
exact_dtype=exact_dtype, exact_device=exact_device)
else:
super().assertEqual(x, y, msg=msg)

Expand Down