Skip to content

Commit 84e9867

Browse files
[release/2.6] Fix dtype before comparing torch and numpy tensors (#2340)
Cast numpy dtype result to torch dtype result before compare Numpy returns `np.power(float32, int64) => float64` [Promotion rules for Python scalars](https://numpy.org/neps/nep-0050-scalar-promotion.html) Pytorch returns `torch.pow(float32, int64) => float32` Reverts #2287 and fixes tests in a different way Fixes: - SWDEV-538110 - `'dtype' do not match: torch.float32 != torch.float64` > - test_binary_ufuncs.py::TestBinaryUfuncsCUDA::test_cuda_tensor_pow_scalar_tensor_cuda - SWDEV-539171 - `AttributeError: 'float' object has no attribute 'dtype` > - test_binary_ufuncs.py::TestBinaryUfuncsCUDA::test_long_tensor_pow_floats_cuda > - test_binary_ufuncs.py::TestBinaryUfuncsCUDA::test_complex_scalar_pow_tensor_cuda_* > - test_binary_ufuncs.py::TestBinaryUfuncsCUDA::test_float_scalar_pow_float_tensor_cuda_*
1 parent 43000ca commit 84e9867

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

test/test_binary_ufuncs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ def to_np(value):
14461446
try:
14471447
np_res = np.power(to_np(base), to_np(np_exponent))
14481448
expected = (
1449-
torch.from_numpy(np_res).to(dtype=base.dtype)
1449+
torch.from_numpy(np_res)
14501450
if isinstance(np_res, np.ndarray)
14511451
else torch.tensor(np_res, dtype=base.dtype)
14521452
)
@@ -1479,8 +1479,8 @@ def to_np(value):
14791479
self.assertRaisesRegex(RuntimeError, regex, base.pow_, exponent)
14801480
elif torch.can_cast(torch.result_type(base, exponent), base.dtype):
14811481
actual2 = actual.pow_(exponent)
1482-
self.assertEqual(actual, expected)
1483-
self.assertEqual(actual2, expected)
1482+
self.assertEqual(actual, expected.to(actual))
1483+
self.assertEqual(actual2, expected.to(actual))
14841484
else:
14851485
self.assertRaisesRegex(
14861486
RuntimeError,

0 commit comments

Comments
 (0)