forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_complex.py
171 lines (149 loc) · 9.13 KB
/
test_complex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Owner(s): ["module: complex"]
import torch
from torch.testing._internal.common_device_type import (
instantiate_device_type_tests,
dtypes,
onlyCPU,
)
from torch.testing._internal.common_utils import TestCase, run_tests
from torch.testing._internal.common_dtype import complex_types
devices = (torch.device('cpu'), torch.device('cuda:0'))
class TestComplexTensor(TestCase):
@dtypes(*complex_types())
def test_to_list(self, device, dtype):
# test that the complex float tensor has expected values and
# there's no garbage value in the resultant list
self.assertEqual(torch.zeros((2, 2), device=device, dtype=dtype).tolist(), [[0j, 0j], [0j, 0j]])
@dtypes(torch.float32, torch.float64)
def test_dtype_inference(self, device, dtype):
# issue: https://github.com/pytorch/pytorch/issues/36834
default_dtype = torch.get_default_dtype()
torch.set_default_dtype(dtype)
x = torch.tensor([3., 3. + 5.j], device=device)
torch.set_default_dtype(default_dtype)
self.assertEqual(x.dtype, torch.cdouble if dtype == torch.float64 else torch.cfloat)
@onlyCPU
@dtypes(*complex_types())
def test_eq(self, device, dtype):
"Test eq on complex types"
nan = float("nan")
# Non-vectorized operations
for a, b in (
(torch.tensor([-0.0610 - 2.1172j], device=device, dtype=dtype),
torch.tensor([-6.1278 - 8.5019j], device=device, dtype=dtype)),
(torch.tensor([-0.0610 - 2.1172j], device=device, dtype=dtype),
torch.tensor([-6.1278 - 2.1172j], device=device, dtype=dtype)),
(torch.tensor([-0.0610 - 2.1172j], device=device, dtype=dtype),
torch.tensor([-0.0610 - 8.5019j], device=device, dtype=dtype)),
):
actual = torch.eq(a, b)
expected = torch.tensor([False], device=device, dtype=torch.bool)
self.assertEqual(actual, expected, msg=f"\neq\nactual {actual}\nexpected {expected}")
actual = torch.eq(a, a)
expected = torch.tensor([True], device=device, dtype=torch.bool)
self.assertEqual(actual, expected, msg=f"\neq\nactual {actual}\nexpected {expected}")
actual = torch.full_like(b, complex(2, 2))
torch.eq(a, b, out=actual)
expected = torch.tensor([complex(0)], device=device, dtype=dtype)
self.assertEqual(actual, expected, msg=f"\neq(out)\nactual {actual}\nexpected {expected}")
actual = torch.full_like(b, complex(2, 2))
torch.eq(a, a, out=actual)
expected = torch.tensor([complex(1)], device=device, dtype=dtype)
self.assertEqual(actual, expected, msg=f"\neq(out)\nactual {actual}\nexpected {expected}")
# Vectorized operations
for a, b in (
(torch.tensor([
-0.0610 - 2.1172j, 5.1576 + 5.4775j, complex(2.8871, nan), -6.6545 - 3.7655j, -2.7036 - 1.4470j, 0.3712 + 7.989j,
-0.0610 - 2.1172j, 5.1576 + 5.4775j, complex(nan, -3.2650), -6.6545 - 3.7655j, -2.7036 - 1.4470j, 0.3712 + 7.989j],
device=device, dtype=dtype),
torch.tensor([
-6.1278 - 8.5019j, 0.5886 + 8.8816j, complex(2.8871, nan), 6.3505 + 2.2683j, 0.3712 + 7.9659j, 0.3712 + 7.989j,
-6.1278 - 2.1172j, 5.1576 + 8.8816j, complex(nan, -3.2650), 6.3505 + 2.2683j, 0.3712 + 7.9659j, 0.3712 + 7.989j],
device=device, dtype=dtype)),
):
actual = torch.eq(a, b)
expected = torch.tensor([False, False, False, False, False, True,
False, False, False, False, False, True],
device=device, dtype=torch.bool)
self.assertEqual(actual, expected, msg=f"\neq\nactual {actual}\nexpected {expected}")
actual = torch.eq(a, a)
expected = torch.tensor([True, True, False, True, True, True,
True, True, False, True, True, True],
device=device, dtype=torch.bool)
self.assertEqual(actual, expected, msg=f"\neq\nactual {actual}\nexpected {expected}")
actual = torch.full_like(b, complex(2, 2))
torch.eq(a, b, out=actual)
expected = torch.tensor([complex(0), complex(0), complex(0), complex(0), complex(0), complex(1),
complex(0), complex(0), complex(0), complex(0), complex(0), complex(1)],
device=device, dtype=dtype)
self.assertEqual(actual, expected, msg=f"\neq(out)\nactual {actual}\nexpected {expected}")
actual = torch.full_like(b, complex(2, 2))
torch.eq(a, a, out=actual)
expected = torch.tensor([complex(1), complex(1), complex(0), complex(1), complex(1), complex(1),
complex(1), complex(1), complex(0), complex(1), complex(1), complex(1)],
device=device, dtype=dtype)
self.assertEqual(actual, expected, msg=f"\neq(out)\nactual {actual}\nexpected {expected}")
@onlyCPU
@dtypes(*complex_types())
def test_ne(self, device, dtype):
"Test ne on complex types"
nan = float("nan")
# Non-vectorized operations
for a, b in (
(torch.tensor([-0.0610 - 2.1172j], device=device, dtype=dtype),
torch.tensor([-6.1278 - 8.5019j], device=device, dtype=dtype)),
(torch.tensor([-0.0610 - 2.1172j], device=device, dtype=dtype),
torch.tensor([-6.1278 - 2.1172j], device=device, dtype=dtype)),
(torch.tensor([-0.0610 - 2.1172j], device=device, dtype=dtype),
torch.tensor([-0.0610 - 8.5019j], device=device, dtype=dtype)),
):
actual = torch.ne(a, b)
expected = torch.tensor([True], device=device, dtype=torch.bool)
self.assertEqual(actual, expected, msg=f"\nne\nactual {actual}\nexpected {expected}")
actual = torch.ne(a, a)
expected = torch.tensor([False], device=device, dtype=torch.bool)
self.assertEqual(actual, expected, msg=f"\nne\nactual {actual}\nexpected {expected}")
actual = torch.full_like(b, complex(2, 2))
torch.ne(a, b, out=actual)
expected = torch.tensor([complex(1)], device=device, dtype=dtype)
self.assertEqual(actual, expected, msg=f"\nne(out)\nactual {actual}\nexpected {expected}")
actual = torch.full_like(b, complex(2, 2))
torch.ne(a, a, out=actual)
expected = torch.tensor([complex(0)], device=device, dtype=dtype)
self.assertEqual(actual, expected, msg=f"\nne(out)\nactual {actual}\nexpected {expected}")
# Vectorized operations
for a, b in (
(torch.tensor([
-0.0610 - 2.1172j, 5.1576 + 5.4775j, complex(2.8871, nan), -6.6545 - 3.7655j, -2.7036 - 1.4470j, 0.3712 + 7.989j,
-0.0610 - 2.1172j, 5.1576 + 5.4775j, complex(nan, -3.2650), -6.6545 - 3.7655j, -2.7036 - 1.4470j, 0.3712 + 7.989j],
device=device, dtype=dtype),
torch.tensor([
-6.1278 - 8.5019j, 0.5886 + 8.8816j, complex(2.8871, nan), 6.3505 + 2.2683j, 0.3712 + 7.9659j, 0.3712 + 7.989j,
-6.1278 - 2.1172j, 5.1576 + 8.8816j, complex(nan, -3.2650), 6.3505 + 2.2683j, 0.3712 + 7.9659j, 0.3712 + 7.989j],
device=device, dtype=dtype)),
):
actual = torch.ne(a, b)
expected = torch.tensor([True, True, True, True, True, False,
True, True, True, True, True, False],
device=device, dtype=torch.bool)
self.assertEqual(actual, expected, msg=f"\nne\nactual {actual}\nexpected {expected}")
actual = torch.ne(a, a)
expected = torch.tensor([False, False, True, False, False, False,
False, False, True, False, False, False],
device=device, dtype=torch.bool)
self.assertEqual(actual, expected, msg=f"\nne\nactual {actual}\nexpected {expected}")
actual = torch.full_like(b, complex(2, 2))
torch.ne(a, b, out=actual)
expected = torch.tensor([complex(1), complex(1), complex(1), complex(1), complex(1), complex(0),
complex(1), complex(1), complex(1), complex(1), complex(1), complex(0)],
device=device, dtype=dtype)
self.assertEqual(actual, expected, msg=f"\nne(out)\nactual {actual}\nexpected {expected}")
actual = torch.full_like(b, complex(2, 2))
torch.ne(a, a, out=actual)
expected = torch.tensor([complex(0), complex(0), complex(1), complex(0), complex(0), complex(0),
complex(0), complex(0), complex(1), complex(0), complex(0), complex(0)],
device=device, dtype=dtype)
self.assertEqual(actual, expected, msg=f"\nne(out)\nactual {actual}\nexpected {expected}")
instantiate_device_type_tests(TestComplexTensor, globals())
if __name__ == '__main__':
run_tests()