forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_type_sharing.py
410 lines (335 loc) · 12 KB
/
test_type_sharing.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import os
import sys
import torch
# Make the helper files in test/ importable
pytorch_test_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(pytorch_test_dir)
from jit_utils import JitTestCase
from common_utils import suppress_warnings
if __name__ == '__main__':
raise RuntimeError("This test file is not meant to be run directly, use:\n\n"
"\tpython test/test_jit.py TESTNAME\n\n"
"instead.")
class TestTypeSharing(JitTestCase):
def assertSameType(self, m1, m2):
if not isinstance(m1, torch.jit.ScriptModule):
m1 = torch.jit.script(m1)
if not isinstance(m2, torch.jit.ScriptModule):
m2 = torch.jit.script(m2)
self.assertEqual(m1._c._type(), m2._c._type())
def assertDifferentType(self, m1, m2):
if not isinstance(m1, torch.jit.ScriptModule):
m1 = torch.jit.script(m1)
if not isinstance(m2, torch.jit.ScriptModule):
m2 = torch.jit.script(m2)
self.assertNotEqual(m1._c._type(), m2._c._type())
def test_basic(self):
class M(torch.nn.Module):
def __init__(self, a, b, c):
super(M, self).__init__()
self.a = a
self.b = b
self.c = c
def forward(self, x):
return x
a = torch.rand(2, 3)
b = torch.rand(2, 3)
c = torch.rand(2, 3)
m1 = M(a, b, c)
m2 = M(a, b, c)
self.assertSameType(m1, m2)
def test_diff_attr_values(self):
"""
Types should be shared even if attribute values differ
"""
class M(torch.nn.Module):
def __init__(self, a, b, c):
super(M, self).__init__()
self.a = a
self.b = b
self.c = c
def forward(self, x):
return x
a = torch.rand(2, 3)
b = torch.rand(2, 3)
c = torch.rand(2, 3)
m1 = M(a, b, c)
m2 = M(a * 2, b * 3, c * 4)
self.assertSameType(m1, m2)
def test_constants(self):
"""
Types should be shared for identical constant values, and different for different constant values
"""
class M(torch.nn.Module):
__constants__ = ["const"]
def __init__(self, attr, const):
super(M, self).__init__()
self.attr = attr
self.const = const
def forward(self):
return self.const
attr = torch.rand(2, 3)
m1 = M(attr, 1)
m2 = M(attr, 1)
self.assertSameType(m1, m2)
# a different constant value
m3 = M(attr, 2)
self.assertDifferentType(m1, m3)
def test_linear(self):
"""
Simple example with a real nn Module
"""
a = torch.nn.Linear(5, 5)
b = torch.nn.Linear(5, 5)
c = torch.nn.Linear(10, 10)
a = torch.jit.script(a)
b = torch.jit.script(b)
c = torch.jit.script(c)
self.assertSameType(a, b)
self.assertDifferentType(a, c)
def test_submodules(self):
"""
If submodules differ, the types should differ.
"""
class M(torch.nn.Module):
def __init__(self, in1, out1, in2, out2):
super(M, self).__init__()
self.submod1 = torch.nn.Linear(in1, out1)
self.submod2 = torch.nn.Linear(in2, out2)
def forward(self, x):
x = self.submod1(x)
x = self.submod2(x)
return x
a = M(1, 1, 2, 2)
b = M(1, 1, 2, 2)
self.assertSameType(a, b)
self.assertSameType(a.submod1, b.submod1)
c = M(2, 2, 2, 2)
self.assertDifferentType(a, c)
self.assertSameType(b.submod2, c.submod1)
self.assertDifferentType(a.submod1, b.submod2)
def test_param_vs_attribute(self):
"""
The same module with an `foo` as a parameter vs. attribute shouldn't
share types
"""
class M(torch.nn.Module):
def __init__(self, foo):
super(M, self).__init__()
self.foo = foo
def forward(self, x):
return x + self.foo
as_param = torch.nn.Parameter(torch.ones(2, 2))
as_attr = torch.ones(2, 2)
param_mod = M(as_param)
attr_mod = M(as_attr)
self.assertDifferentType(attr_mod, param_mod)
def test_same_but_different_classes(self):
"""
Even if everything about the module is the same, different originating
classes should prevent type sharing.
"""
class A(torch.nn.Module):
__constants__ = ["const"]
def __init__(self, in1, out1, in2, out2):
super(A, self).__init__()
self.submod1 = torch.nn.Linear(in1, out1)
self.submod2 = torch.nn.Linear(in2, out2)
self.const = 5
def forward(self, x):
x = self.submod1(x)
x = self.submod2(x)
return x * self.const
class B(torch.nn.Module):
__constants__ = ["const"]
def __init__(self, in1, out1, in2, out2):
super(B, self).__init__()
self.submod1 = torch.nn.Linear(in1, out1)
self.submod2 = torch.nn.Linear(in2, out2)
self.const = 5
def forward(self, x):
x = self.submod1(x)
x = self.submod2(x)
return x * self.const
a = A(1, 1, 2, 2)
b = B(1, 1, 2, 2)
self.assertDifferentType(a, b)
def test_mutate_attr_value(self):
"""
Mutating the value of an attribute should not change type sharing
"""
class M(torch.nn.Module):
def __init__(self, in1, out1, in2, out2):
super(M, self).__init__()
self.submod1 = torch.nn.Linear(in1, out1)
self.submod2 = torch.nn.Linear(in2, out2)
self.foo = torch.ones(in1, in1)
def forward(self, x):
x = self.submod1(x)
x = self.submod2(x)
return x + self.foo
a = M(1, 1, 2, 2)
b = M(1, 1, 2, 2)
a.foo = torch.ones(2, 2)
b.foo = torch.rand(2, 2)
self.assertSameType(a, b)
def test_assign_python_attr(self):
"""
Assigning a new (python-only) attribute should not change type sharing
"""
class M(torch.nn.Module):
def __init__(self, in1, out1, in2, out2):
super(M, self).__init__()
self.submod1 = torch.nn.Linear(in1, out1)
self.submod2 = torch.nn.Linear(in2, out2)
self.foo = torch.ones(in1, in1)
def forward(self, x):
x = self.submod1(x)
x = self.submod2(x)
return x + self.foo
# explicitly call script() to freeze the type
a = torch.jit.script(M(1, 1, 2, 2))
b = torch.jit.script(M(1, 1, 2, 2))
a.new_attr = "foo bar baz"
self.assertSameType(a, b)
# but if we assign attributes *before* calling script(), the types
# should be different, since `new_attr` should be turned into a Script
# attribute
a = M(1, 1, 2, 2)
b = M(1, 1, 2, 2)
a.new_attr = "foo bar baz"
self.assertDifferentType(a, b)
def test_failed_attribute_compilation(self):
"""
Attributes whose type cannot be inferred should fail cleanly with nice hints
"""
class NotScriptable(object):
pass
class M(torch.nn.Module):
def __init__(self):
super(M, self).__init__()
# assign a type we know can't be converted to TorchScript
self.foo = NotScriptable()
def forward(self):
# try to use it in forward
return self.foo
m = M()
with self.assertRaisesRegex(RuntimeError, "failed to convert Python type"):
torch.jit.script(m)
def test_script_function_attribute_different(self):
"""
Different functions passed in should lead to different types
"""
@torch.jit.script
def fn1(x):
return x + x
@torch.jit.script
def fn2(x):
return x - x
class M(torch.nn.Module):
def __init__(self, fn):
super(M, self).__init__()
self.fn = fn
def forward(self, x):
return self.fn(x)
fn1_mod = M(fn1)
fn2_mod = M(fn2)
self.assertDifferentType(fn1_mod, fn2_mod)
def test_script_function_attribute_same(self):
"""
Same functions passed in should lead to same types
"""
@torch.jit.script
def fn(x):
return x + x
class M(torch.nn.Module):
def __init__(self, fn):
super(M, self).__init__()
self.fn = fn
def forward(self, x):
return self.fn(x)
fn1_mod = M(fn)
fn2_mod = M(fn)
self.assertSameType(fn1_mod, fn2_mod)
def test_python_function_attribute_different(self):
"""
Different functions passed in should lead to different types
"""
def fn1(x):
return x + x
def fn2(x):
return x - x
class M(torch.nn.Module):
def __init__(self, fn):
super(M, self).__init__()
self.fn = fn
def forward(self, x):
return self.fn(x)
fn1_mod = M(fn1)
fn2_mod = M(fn2)
self.assertDifferentType(fn1_mod, fn2_mod)
def test_python_function_attribute_same(self):
"""
Same functions passed in should lead to same types
"""
def fn(x):
return x + x
class M(torch.nn.Module):
def __init__(self, fn):
super(M, self).__init__()
self.fn = fn
def forward(self, x):
return self.fn(x)
fn1_mod = M(fn)
fn2_mod = M(fn)
self.assertSameType(fn1_mod, fn2_mod)
@suppress_warnings
def test_tracing_gives_different_types(self):
"""
Since we can't guarantee that methods are the same between different
trace runs, tracing must always generate a unique type.
"""
class M(torch.nn.Module):
def __init__(self):
super(M, self).__init__()
def forward(self, x, y):
if x.sum() > y.sum():
return x
else:
return y
a = torch.jit.trace(M(), (torch.zeros(1, 1), torch.ones(1, 1)))
b = torch.jit.trace(M(), (torch.ones(1, 1), torch.zeros(1, 1)))
self.assertDifferentType(a, b)
def test_ignored_fns(self):
class M(torch.nn.Module):
def __init__(self, foo):
super(M, self).__init__()
self.foo = foo
@torch.jit.ignore
def ignored(self):
return self.foo
def forward(self):
return self.ignored()
a = torch.jit.script(M(torch.ones(1)))
b = torch.jit.script(M(torch.ones(2)))
self.assertSameType(a, b)
self.assertNotEqual(a(), b())
@suppress_warnings
def test_script_module_containing_traced_module(self):
class Traced(torch.nn.Module):
def __init__(self):
super(Traced, self).__init__()
def forward(self, x):
if x.sum() > 0:
return x
else:
return x + x
class M(torch.nn.Module):
def __init__(self, input):
super(M, self).__init__()
self.traced = torch.jit.trace(Traced(), input)
def forward(self, x):
return self.traced(x)
a = M((torch.ones(1), ))
b = M((torch.zeros(1), ))
self.assertDifferentType(a, b)