@@ -11063,6 +11063,95 @@ def test_exponential(self, device, dtype):
11063
11063
with self.assertRaises(RuntimeError):
11064
11064
torch.empty((1,), device=device, dtype=dtype).exponential_(-0.5)
11065
11065
11066
+ @unittest.skipIf(not TEST_NUMPY, "Numpy not found")
11067
+ @dtypes(*(torch.testing.get_all_fp_dtypes(include_half=False) +
11068
+ torch.testing.get_all_complex_dtypes()))
11069
+ @dtypesIfCUDA(*(torch.testing.get_all_fp_dtypes(include_half=True) +
11070
+ torch.testing.get_all_complex_dtypes()))
11071
+ def test_exp(self, device, dtype):
11072
+ for v in (2, -2) + ((1j, 1 + 1j) if dtype.is_complex else ()):
11073
+ if dtype == torch.bfloat16:
11074
+ # Currently multiply a bfloat16 type with floating-point causes error:
11075
+ # RuntimeError: dtype != ScalarType::Undefined INTERNAL ASSERT FAILED at
11076
+ # "/pytorch/aten/src/ATen/native/TensorIterator.cpp":125, please report a bug to PyTorch.
11077
+ # We skip bfloat16 for now, but we should fix it. https://github.com/pytorch/pytorch/issues/40580
11078
+ if self.device_type == 'cpu' or self.device_type == 'cuda':
11079
+ with self.assertRaises(RuntimeError):
11080
+ torch.tensor(v, dtype=dtype, device=device) * torch.arange(18, device=device)
11081
+ return
11082
+ elif self.device_type == 'xla':
11083
+ # Error:
11084
+ # Traceback (most recent call last):
11085
+ # File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_device_type.py",
11086
+ # line 241, in instantiated_test
11087
+ # result = test(self, device_arg, dtype)
11088
+ # File "/var/lib/jenkins/workspace/xla/test/../../test/test_torch.py", line 11062, in test_exp
11089
+ # self.compare_with_numpy(torch.exp, np.exp, a)
11090
+ # File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_utils.py", line 878,
11091
+ # in compare_with_numpy
11092
+ # a = tensor_like.detach().cpu().numpy()
11093
+ # TypeError: Got unsupported ScalarType BFloat16
11094
+ return
11095
+
11096
+ a = torch.tensor(v, dtype=dtype, device=device) * torch.arange(18, device=device) / 3 * math.pi
11097
+ a = a.to(dtype)
11098
+ self.compare_with_numpy(torch.exp, np.exp, a)
11099
+
11100
+ if dtype.is_complex:
11101
+ inf_real_zero_imag_in = torch.tensor(complex(float('inf'), 0), device=device, dtype=dtype)
11102
+ inf_real_zero_imag_out = torch.exp(inf_real_zero_imag_in).item()
11103
+ self.assertTrue(math.isinf(inf_real_zero_imag_out.real))
11104
+ if self.device_type == 'cpu':
11105
+ if not IS_WINDOWS: # Windows tests don't show some bugs consistently
11106
+ # This is incorrect. It should be zero. Need fix!
11107
+ # https://github.com/pytorch/pytorch/issues/40590
11108
+ self.assertNotEqual(inf_real_zero_imag_out.imag, 0)
11109
+ # This is incorrect. They should equal. Need fix!
11110
+ # https://github.com/pytorch/pytorch/issues/40590
11111
+ with self.assertRaises(AssertionError):
11112
+ self.compare_with_numpy(torch.exp, np.exp, inf_real_zero_imag_in)
11113
+ else:
11114
+ self.assertEqual(inf_real_zero_imag_out.imag, 0, atol=0, rtol=0)
11115
+ self.compare_with_numpy(torch.exp, np.exp, inf_real_zero_imag_in)
11116
+
11117
+ zero_real_inf_imag_in = torch.tensor(complex(0, float('inf')), device=device, dtype=dtype)
11118
+ zero_real_inf_imag_out = torch.exp(zero_real_inf_imag_in).item()
11119
+ self.assertTrue(math.isnan(zero_real_inf_imag_out.real))
11120
+ self.assertTrue(math.isnan(zero_real_inf_imag_out.imag))
11121
+ # Ensure we are notified when NumPy changes its behavior
11122
+ self.compare_with_numpy(torch.exp, np.exp, zero_real_inf_imag_in)
11123
+
11124
+ inf_real_imag_in = torch.tensor(complex(float('inf'), float('inf')), device=device, dtype=dtype)
11125
+ inf_real_imag_out = torch.exp(inf_real_imag_in).item()
11126
+ if self.device_type == 'cpu':
11127
+ if not IS_WINDOWS: # Windows tests don't show some bugs consistently
11128
+ # This is incorrect. Need fix! https://github.com/pytorch/pytorch/issues/40590
11129
+ with self.assertRaises(AssertionError):
11130
+ self.compare_with_numpy(torch.exp, np.exp, inf_real_imag_in)
11131
+ else:
11132
+ self.assertTrue(math.isinf(inf_real_imag_out.real))
11133
+ self.assertTrue(math.isnan(inf_real_imag_out.imag))
11134
+ self.compare_with_numpy(torch.exp, np.exp, inf_real_imag_in)
11135
+
11136
+ inf_real_nan_imag_in = torch.tensor(complex(float('inf'), float('nan')), device=device, dtype=dtype)
11137
+ inf_real_nan_imag_out = torch.exp(inf_real_nan_imag_in).item()
11138
+ if self.device_type == 'cpu':
11139
+ if not IS_WINDOWS: # Windows tests don't show some bugs consistently
11140
+ # This is incorrect. It should be inf. Need fix! https://github.com/pytorch/pytorch/issues/40590
11141
+ with self.assertRaises(AssertionError):
11142
+ self.compare_with_numpy(torch.exp, np.exp, inf_real_nan_imag_in)
11143
+ else:
11144
+ self.assertTrue(math.isinf(inf_real_nan_imag_out.real))
11145
+ self.assertTrue(math.isnan(inf_real_nan_imag_out.imag))
11146
+ self.compare_with_numpy(torch.exp, np.exp, inf_real_nan_imag_in)
11147
+
11148
+ nan_real_inf_imag_in = torch.tensor(complex(float('nan'), float('inf')), device=device, dtype=dtype)
11149
+ nan_real_inf_imag_out = torch.exp(nan_real_inf_imag_in).item()
11150
+ self.assertTrue(math.isnan(nan_real_inf_imag_out.real))
11151
+ self.assertTrue(math.isnan(nan_real_inf_imag_out.imag))
11152
+ # Ensure we are notified when NumPy changes its behavior
11153
+ self.compare_with_numpy(torch.exp, np.exp, nan_real_inf_imag_in)
11154
+
11066
11155
@skipIfNoSciPy
11067
11156
@dtypes(*torch.testing.get_all_fp_dtypes())
11068
11157
def test_uniform_kstest(self, device, dtype):
0 commit comments