-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_generator.py
More file actions
220 lines (194 loc) · 6.9 KB
/
test_generator.py
File metadata and controls
220 lines (194 loc) · 6.9 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Copyright (c) 2023, DeepLink.
import torch
import torch_dipu
from torch_dipu import diputype
from torch_dipu.testing._internal.common_utils import (
TestCase,
run_tests,
onlyOn,
)
class TestGenerator(TestCase):
def test_python_api(self):
torch.seed()
torch.cuda.seed_all()
torch.cuda.random.seed_all()
torch.cuda.manual_seed_all(1)
rngs = torch.cuda.get_rng_state_all()
torch.cuda.set_rng_state_all(rngs)
torch.manual_seed(1)
self.assertEqual(torch.cuda.initial_seed(), 1)
self.assertEqual(torch.initial_seed(), 1)
for i in range(torch.cuda.device_count()):
torch.cuda.manual_seed(i)
state = torch.cuda.get_rng_state(0)
new_state = torch.ones_like(state) * 4
torch.cuda.set_rng_state(new_state, 0)
current_state = torch.cuda.get_rng_state(0)
self.assertTrue(
torch.allclose(
current_state,
torch.tensor(4, device=current_state.device, dtype=current_state.dtype),
)
)
def test_torch_generator(self):
gen = torch.Generator()
self.assertEqual(gen.device.type, "cpu")
gen.manual_seed(1)
self.assertEqual(gen.initial_seed(), 1)
gen = torch.Generator("cpu")
self.assertEqual(gen.device.type, "cpu")
gen = torch.Generator("cuda")
self.assertEqual(gen.device.type, diputype)
gen = torch.Generator("cuda:0")
self.assertEqual(gen.device, torch.device(diputype + ":0"))
gen = torch.Generator("dipu")
self.assertEqual(gen.device.type, diputype)
gen.manual_seed(1)
self.assertEqual(gen.initial_seed(), 1)
def test_randn_with_generator(self):
gen = torch.Generator()
gen.manual_seed(1)
data1 = torch.randn(2, 3, generator=gen)
gen.manual_seed(1)
data2 = torch.randn(2, 3, generator=gen)
self.assertEqual(data1, data2)
data2 = torch.randn(2, 3, generator=gen)
self.assertNotEqual(data1, data2)
gen = torch.Generator("cuda")
gen.manual_seed(1)
data1 = torch.randn(2, 3, generator=gen, device="cuda")
gen.manual_seed(1)
data2 = torch.randn(2, 3, generator=gen, device="cuda")
self.assertEqual(data1, data2)
data2 = torch.randn(2, 3, generator=gen, device="cuda")
self.assertNotEqual(data1, data2)
def test_uniform_(self):
t1 = torch.arange(0, 100, dtype=torch.float32).cuda()
t2 = t1.clone()
torch.manual_seed(1)
t1.uniform_()
torch.manual_seed(1)
t2.uniform_()
self.assertEqual(t1, t2)
t2.uniform_()
self.assertNotEqual(t1, t2)
# print("uniform_ allclose success")
def test_normal_(self):
t1 = torch.arange(0, 100, dtype=torch.float32).cuda()
t2 = t1.clone()
torch.manual_seed(1)
t1.normal_()
torch.manual_seed(1)
t2.normal_()
self.assertEqual(t1, t2)
t2.normal_()
self.assertNotEqual(t1, t2)
# print("normal_ allclose success")
def test_random_(self):
t1 = torch.arange(0, 100, dtype=torch.float32).cuda()
t2 = t1.clone()
torch.manual_seed(1)
t1.random_(0, 100)
torch.manual_seed(1)
t2.random_(0, 100)
self.assertEqual(t1, t2)
t2.random_(0, 100)
self.assertNotEqual(t1, t2)
torch.manual_seed(1)
t1.random_()
torch.manual_seed(1)
t2.random_()
self.assertEqual(t1, t2)
t2.random_()
self.assertNotEqual(t1, t2)
# print("random_ allclose success")
def test_multinomial(self):
data = torch.arange(0, 100, dtype=torch.float).cuda()
torch.manual_seed(1)
data1 = torch.multinomial(data, 10)
torch.manual_seed(1)
data2 = torch.multinomial(data, 10)
self.assertEqual(data1, data2)
data2 = torch.multinomial(data, 10)
self.assertNotEqual(data1, data2)
# print("multinomial allclose success")
def test_randn(self):
torch.manual_seed(1)
t1 = torch.randn(100, device="cuda")
torch.manual_seed(1)
t2 = torch.randn(100, device="cuda")
self.assertEqual(t1, t2)
t2 = torch.randn(100, device="cuda")
self.assertNotEqual(t1, t2)
# print("randn allclose success")
def test_bernoulli(self):
x = 2 * torch.ones(100, device="cuda")
torch.manual_seed(1)
t1 = x.bernoulli_(0.5)
self.assertTrue(torch.all((t1 == 0) | (t1 == 1)))
x = torch.zeros(100, device="cuda")
torch.manual_seed(1)
t2 = x.bernoulli_(0.5)
self.assertTrue(torch.all((t2 == 0) | (t2 == 1)))
self.assertEqual(t1, t2)
t2 = x.bernoulli_(0.5)
self.assertTrue(torch.all((t2 == 0) | (t2 == 1)))
self.assertNotEqual(t1, t2)
# print("bernoulli allclose success")
def test_randperm(self):
if torch_dipu.dipu.vendor_type == "MLU":
return
torch.manual_seed(1)
t1 = torch.randperm(100, device="cuda")
torch.manual_seed(1)
t2 = torch.randperm(100, device="cuda")
self.assertEqual(t1, t2)
t2 = torch.randperm(100, device="cuda")
self.assertNotEqual(t1, t2)
# print("randperm allclose success")
def test_dropout(self):
m = torch.nn.Dropout(p=0.2).cuda()
input = torch.randn(20, 16).cuda()
torch.manual_seed(1)
t1 = m(input)
torch.manual_seed(1)
t2 = m(input)
self.assertEqual(t1, t2)
t2 = m(input)
self.assertNotEqual(t1, t2)
# print("dropout allclose success")
def test_dropout_(self):
m = torch.nn.Dropout(p=0.2, inplace=True).cuda()
input = torch.randn(20, 16).cuda()
torch.manual_seed(1)
t1 = input.clone()
m(t1)
torch.manual_seed(1)
t2 = input.clone()
m(t2)
self.assertEqual(t1, t2)
t2 = input.clone()
m(t2)
self.assertNotEqual(t1, t2)
# print("dropout_ allclose success")
def test_default_generators(self):
self.assertGreater(len(torch.cuda.default_generators), 0)
torch.cuda.default_generators[0].manual_seed(1)
self.assertEqual(torch.cuda.default_generators[0].initial_seed(), 1)
@onlyOn("CUDA")
def test_cuda_generator(self):
state = torch.cuda.get_rng_state(0)
state[-16] = 4
state[-15:-8] = 0
state[-8:] = 0
torch.cuda.set_rng_state(state)
self.assertEqual(torch.cuda.initial_seed(), 4)
# invalid offset, offset must be a multiple of 4
state[-8:] = 1
try:
torch.cuda.set_rng_state(state)
self.assertTrue(False, "should not go here")
except Exception as ex:
self.assertIn("offset must be a multiple of 4", ex.args[0])
if __name__ == "__main__":
run_tests()