forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_pytorch_onnx_no_runtime.py
580 lines (472 loc) · 19.9 KB
/
test_pytorch_onnx_no_runtime.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# Owner(s): ["module: onnx"]
"""Tests for onnx export that don't run the exported model."""
import contextlib
import io
import itertools
import unittest
from typing import Dict, Optional, Type, Callable, Iterable, Tuple, Union
import onnx
import torch
from torch import Tensor
from torch.onnx import symbolic_helper, utils, symbolic_registry
from torch.onnx._globals import GLOBALS
from torch.testing._internal import common_utils
def export_to_onnx(
model: Union[torch.nn.Module, torch.jit.ScriptFunction],
input: Tuple[torch.Tensor],
custom_ops: Optional[
Iterable[
Union[contextlib.AbstractContextManager, contextlib.ContextDecorator],
]
] = None,
mocks: Optional[Iterable] = None,
operator_export_type: torch.onnx.OperatorExportTypes = torch.onnx.OperatorExportTypes.ONNX,
opset_version: int = GLOBALS.export_onnx_opset_version,
) -> onnx.ModelProto:
"""Exports `model(input)` to ONNX and returns it.
Custom operators and/or unittest patches can be used help reproducing specific behaviors.
Args:
model: model to export
input: model input with same format as `torch.onnx.export(..,args,...)`
custom_ops: list of custom operators to use during export
mocks: list of mocks to use during export
operator_export_type: export type as described by `torch.onnx.export(...operator_export_type,...)`
opset_version: ONNX opset version as described by `torch.onnx.export(...opset_version,...)`
Returns:
A valid ONNX model (`onnx.ModelProto`)
"""
custom_ops = custom_ops or []
mocks = mocks or []
with contextlib.ExitStack() as stack:
for ctx in itertools.chain(custom_ops, mocks):
stack.enter_context(ctx)
f = io.BytesIO()
torch.onnx.export(
model,
input,
f,
operator_export_type=operator_export_type,
opset_version=opset_version,
)
# Validate ONNX graph before returning it
onnx_model = onnx.load_from_string(f.getvalue())
onnx.checker.check_model(onnx_model)
return onnx_model
@common_utils.instantiate_parametrized_tests
class TestOptionalOutput(common_utils.TestCase):
# TODO: Move these tests to test_pytorch_onnx_onnxruntime once
# ONNX Runtime 1.11 is released and supports opset 16.
class IfNoneInput(torch.nn.Module):
def forward(self, x) -> Optional[Tensor]:
y: Optional[Tensor] = None
if x.size(0) > 1:
y = x
return y
class IfNoneOutput(torch.nn.Module):
def forward(self, x) -> Optional[Tensor]:
y: Optional[Tensor] = x
if x.size(0) > 1:
y = None
return y
class LoopNoneInput(torch.nn.Module):
def forward(self, x) -> Optional[Tensor]:
y: Optional[Tensor] = None
for _ in range(x.size(0)):
y = x
return y
class LoopNoneOutput(torch.nn.Module):
def forward(self, x) -> Optional[Tensor]:
y: Optional[Tensor] = x
for _ in range(x.size(0)):
y = None
return y
@common_utils.parametrize(
"module_class",
(IfNoneInput, IfNoneOutput, LoopNoneInput, LoopNoneOutput),
name_fn=lambda module_class: module_class.__name__,
)
@common_utils.parametrize("x_size", (0, 1), name_fn=lambda x_size: str(x_size))
def test_optional_output(self, module_class: Type[torch.nn.Module], x_size: int):
# Need scripting to preserve control flow for this test to be
# meaningful.
model = torch.jit.script(module_class())
f = io.BytesIO()
x = torch.ones(x_size)
dynamic_axis_name = "condition"
torch.onnx.export(
model,
(x,),
f,
opset_version=15,
# Ensure condition is not constant
dynamic_axes={"x": {0: dynamic_axis_name}},
input_names=["x"],
)
exported = onnx.load_from_string(f.getvalue())
expected_elem_type = symbolic_helper.scalar_type_to_onnx[
symbolic_helper.scalar_type_to_pytorch_type.index(x.dtype)
].value
expected_output_type = onnx.helper.make_optional_type_proto(
onnx.helper.make_tensor_type_proto(expected_elem_type, (dynamic_axis_name,))
)
self.assertEqual(expected_output_type, exported.graph.output[0].type)
for node in exported.graph.node:
# Both branches output types should match.
if node.op_type == "If":
for attr in node.attribute:
if attr.name in ("then_branch", "else_branch"):
self.assertEqual(expected_output_type, attr.g.output[0].type)
def test_uninitialized_optional(self):
class Module(torch.nn.Module):
def forward(self, y: Optional[Tensor]) -> Optional[Tensor]:
if y is not None:
if y.shape[1] < 5:
if y.size(0) == 1:
y = y + 4
else:
return y
return y
y = torch.ones((3, 4), dtype=torch.int)
torch.onnx.export(
torch.jit.script(Module()),
y,
io.BytesIO(),
opset_version=15,
dynamic_axes={"y": {0: "y0", 1: "y1"}},
input_names=["y"],
)
class TestONNXExport(common_utils.TestCase):
def test_fuse_addmm(self):
class AddmmModel(torch.nn.Module):
def forward(self, x):
return torch.mm(x, x) + x
x = torch.ones(3, 3)
f = io.BytesIO()
torch.onnx._export(AddmmModel(), x, f, verbose=False)
def test_onnx_transpose_incomplete_tensor_type(self):
# Smoke test to get us into the state where we are attempting to export
# a transpose op, where the input is a TensorType without size information.
# This would previously not work, since we would
# take the size of the input and use the length of its sizes as the
# number of dimensions in the permutation.
class Foo(torch.jit.ScriptModule):
@torch.jit.script_method
def forward(self, x):
return x.contiguous().transpose(0, 1).sum()
class TraceMe(torch.nn.Module):
def __init__(self):
super(TraceMe, self).__init__()
self.foo = Foo()
def forward(self, x):
return self.foo(x)
tm = TraceMe()
tm = torch.jit.trace(tm, torch.rand(3, 4))
f = io.BytesIO()
torch.onnx._export(tm, (torch.rand(3, 4),), f)
def test_export_tensoroption_to(self):
def foo(x):
return x[0].clone().detach().cpu() + x
traced = torch.jit.trace(foo, (torch.rand([2])))
torch.onnx.export_to_pretty_string(traced, (torch.rand([2]),))
def test_onnx_export_script_module(self):
class ModuleToExport(torch.jit.ScriptModule):
def __init__(self):
super(ModuleToExport, self).__init__()
@torch.jit.script_method
def forward(self, x):
y = x - x
return x + x
mte = ModuleToExport()
torch.onnx.export_to_pretty_string(mte, (torch.zeros(1, 2, 3),), verbose=False)
@common_utils.suppress_warnings
def test_onnx_export_func_with_warnings(self):
@torch.jit.script
def func_with_warning(inp):
return torch.nn.functional.sigmoid(inp) # triggers a deprecation warning
class WarningTest(torch.nn.Module):
def __init__(self):
super(WarningTest, self).__init__()
def forward(self, x):
return func_with_warning(x)
# no exception
torch.onnx.export_to_pretty_string(
WarningTest(), torch.randn(42), verbose=False
)
def test_onnx_export_script_python_fail(self):
class PythonModule(torch.jit.ScriptModule):
def __init__(self):
super(PythonModule, self).__init__()
@torch.jit.ignore
def forward(self, x):
return torch.neg(x)
class ModuleToExport(torch.jit.ScriptModule):
def __init__(self):
super(ModuleToExport, self).__init__()
self.mod = PythonModule()
@torch.jit.script_method
def forward(self, x):
y = self.mod(x)
return y + y
mte = ModuleToExport()
f = io.BytesIO()
with self.assertRaisesRegex(RuntimeError, "Couldn't export Python"):
torch.onnx._export(mte, (torch.zeros(1, 2, 3),), f, verbose=False)
def test_onnx_export_script_inline_trace(self):
class ModuleToInline(torch.nn.Module):
def __init__(self):
super(ModuleToInline, self).__init__()
def forward(self, x):
return torch.neg(x)
class ModuleToExport(torch.jit.ScriptModule):
def __init__(self):
super(ModuleToExport, self).__init__()
self.mod = torch.jit.trace(ModuleToInline(), torch.zeros(1, 2, 3))
@torch.jit.script_method
def forward(self, x):
y = self.mod(x)
return y + y
mte = ModuleToExport()
torch.onnx.export_to_pretty_string(mte, (torch.zeros(1, 2, 3),), verbose=False)
def test_onnx_export_script_inline_script(self):
class ModuleToInline(torch.jit.ScriptModule):
def __init__(self):
super(ModuleToInline, self).__init__()
@torch.jit.script_method
def forward(self, x):
return torch.neg(x)
class ModuleToExport(torch.jit.ScriptModule):
def __init__(self):
super(ModuleToExport, self).__init__()
self.mod = ModuleToInline()
@torch.jit.script_method
def forward(self, x):
y = self.mod(x)
return y + y
mte = ModuleToExport()
torch.onnx.export_to_pretty_string(mte, (torch.zeros(1, 2, 3),), verbose=False)
def test_onnx_export_script_module_loop(self):
class ModuleToExport(torch.jit.ScriptModule):
def __init__(self):
super(ModuleToExport, self).__init__()
@torch.jit.script_method
def forward(self, x):
# test if we support end to end onnx export on loop and
# nested loops with and without loop index
for _ in range(5):
for i in range(3):
x = x + i
return x
mte = ModuleToExport()
torch.onnx.export_to_pretty_string(mte, (torch.zeros(1, 2, 3),), verbose=False)
@common_utils.suppress_warnings
def test_onnx_export_script_truediv(self):
class ModuleToExport(torch.jit.ScriptModule):
def __init__(self):
super(ModuleToExport, self).__init__()
@torch.jit.script_method
def forward(self, x):
z = x.size(0) / 2
return x + z
mte = ModuleToExport()
torch.onnx.export_to_pretty_string(
mte, (torch.zeros(1, 2, 3, dtype=torch.float),), verbose=False
)
def test_onnx_export_script_non_alpha_add_sub(self):
class ModuleToExport(torch.jit.ScriptModule):
def __init__(self):
super(ModuleToExport, self).__init__()
@torch.jit.script_method
def forward(self, x):
bs = x.size(0) + 1
return bs - 1
mte = ModuleToExport()
torch.onnx.export_to_pretty_string(mte, (torch.rand(3, 4),), verbose=False)
def test_onnx_export_script_module_if(self):
class ModuleToExport(torch.jit.ScriptModule):
def __init__(self):
super(ModuleToExport, self).__init__()
@torch.jit.script_method
def forward(self, x):
if bool(torch.sum(x) > 0):
x = torch.neg(x)
return x
mte = ModuleToExport()
torch.onnx.export_to_pretty_string(mte, (torch.zeros(1, 2, 3),), verbose=False)
def test_onnx_export_script_inline_params(self):
class ModuleToInline(torch.jit.ScriptModule):
def __init__(self):
super(ModuleToInline, self).__init__()
self.m = torch.nn.Parameter(torch.ones(3, 3))
self.unused = torch.nn.Parameter(torch.ones(1, 2, 3))
@torch.jit.script_method
def forward(self, x):
return torch.mm(x, self.m)
class ModuleToExport(torch.jit.ScriptModule):
def __init__(self):
super(ModuleToExport, self).__init__()
self.mod = ModuleToInline()
self.param = torch.nn.Parameter(torch.ones(3, 4))
@torch.jit.script_method
def forward(self, x):
y = self.mod(x)
return torch.mm(y, self.param)
mte = ModuleToExport()
result = mte(torch.zeros(2, 3))
reference = torch.mm(
torch.mm(torch.zeros(2, 3), torch.ones(3, 3)), torch.ones(3, 4)
)
self.assertEqual(result, reference)
torch.onnx.export_to_pretty_string(mte, (torch.ones(2, 3),), verbose=False)
def test_onnx_export_speculate(self):
class Foo(torch.jit.ScriptModule):
def __init__(self, m):
super(Foo, self).__init__()
self.m = m
@torch.jit.script_method
def forward(self, x):
x += x
# because we are testing if we emit `if` statement correctly
# we cannot use `True` as the condition. Constant prop
# would remove the `if` statements.
c = torch.sum(x) > 4
if bool(c):
if bool(c):
y = self.m(x)
else:
y = self.m(x)
else:
y = self.m(x)
return y
linear = torch.jit.trace(
torch.nn.Linear(10, 20).float(), torch.zeros(1, 10, dtype=torch.float)
)
@torch.jit.script
def transpose(x):
return x.t()
f1 = Foo(transpose)
f2 = Foo(linear)
torch.onnx.export_to_pretty_string(f1, (torch.ones(1, 10, dtype=torch.float),))
torch.onnx.export_to_pretty_string(f2, (torch.ones(1, 10, dtype=torch.float),))
def test_onnx_export_shape_reshape(self):
class Foo(torch.nn.Module):
def forward(self, x):
import torch.onnx.operators
x = x.repeat(5, 1, 1)
shape = torch.onnx.operators.shape_as_tensor(x)
reshaped = torch.onnx.operators.reshape_from_tensor_shape(x, shape)
return reshaped
foo = torch.jit.trace(Foo(), torch.zeros(1, 2, 3))
torch.onnx.export_to_pretty_string(foo, (torch.zeros(1, 2, 3)))
def test_listconstruct_erasure(self):
class FooMod(torch.nn.Module):
def forward(self, x):
mask = x < 0.0
return x[mask]
torch.onnx.export_to_pretty_string(
FooMod(),
(torch.rand(3, 4),),
add_node_names=False,
do_constant_folding=False,
operator_export_type=torch.onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK,
)
def test_export_dynamic_slice(self):
class DynamicSliceExportMod(torch.jit.ScriptModule):
@torch.jit.script_method
def forward(self, x):
retval = x[0]
for i in range(x.size(1)):
retval += torch.sum(x[0:i], dim=0)
return retval
mod = DynamicSliceExportMod()
input = torch.rand(3, 4, 5)
torch.onnx.export_to_pretty_string(
DynamicSliceExportMod(), (input,), opset_version=10
)
def test_export_dict(self):
class DictModule(torch.nn.Module):
def forward(self, x_in: torch.Tensor) -> Dict[str, torch.Tensor]:
return {"test_key_out": x_in}
x_in = torch.tensor(1)
mod = DictModule()
mod.train(False)
torch.onnx.export_to_pretty_string(mod, (x_in,))
with self.assertRaisesRegex(RuntimeError, r"DictConstruct.+is not supported."):
torch.onnx.export_to_pretty_string(torch.jit.script(mod), (x_in,))
def test_source_range_propagation(self):
class ExpandingModule(torch.nn.Module):
def __init__(self):
super().__init__()
# Will be expanded during ONNX export
self.ln = torch.nn.LayerNorm([1])
def forward(self, input):
return self.ln(input)
mod = ExpandingModule()
graph, _, _ = utils._model_to_graph(
mod,
(torch.zeros(1),),
operator_export_type=torch.onnx.OperatorExportTypes.ONNX,
)
# Ensure that every node in the graph has a valid source range
for node in graph.nodes():
self.assertTrue(node.sourceRange())
@common_utils.skipIfCaffe2
def test_clip_aten_fallback_due_exception(self):
def bad_clamp(g, self, min, max):
return symbolic_helper._onnx_unsupported("Bad boy!")
class MyClip(torch.nn.Module):
def forward(self, x):
return torch.clamp(x, min=-0.5, max=0.5)
onnx_model = export_to_onnx(
MyClip(),
torch.randn(3, 4, requires_grad=True),
custom_ops=[common_utils.custom_op("aten::clamp", bad_clamp, 9)],
operator_export_type=torch.onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK,
)
self.assertAtenOp(onnx_model, "clamp", "Tensor")
@common_utils.skipIfCaffe2
def test_clip_aten_fallback_explicit_request(self):
class MyClip(torch.nn.Module):
def forward(self, x):
return torch.clamp(x, min=-0.5, max=0.5)
def break_is_registered_op_api(opname, domain, version):
fake_missing_symbolics = ("clamp",)
if opname in fake_missing_symbolics:
return False
return (
(domain, version) in symbolic_registry._registry
and opname in symbolic_registry._registry[(domain, version)]
)
# Force missing symbolic for well-known op using a mock
onnx_model = export_to_onnx(
MyClip(),
torch.randn(3, 4, requires_grad=True),
mocks=[
unittest.mock.patch(
"torch.onnx.symbolic_registry.is_registered_op",
side_effect=break_is_registered_op_api,
)
],
operator_export_type=torch.onnx.OperatorExportTypes.ONNX_ATEN_FALLBACK,
)
self.assertAtenOp(onnx_model, "clamp", "Tensor")
def _helper_test_to_(self, cast_fn: Callable[[torch.Tensor], torch.Tensor]):
"""Helper to test aten::to(device) variants.
`cast_fn` is converted into a `torch.jit.script`. It wraps `aten::to`
during export to preventing the devices to be hard-coded.
Needed by detectron2 after https://github.com/facebookresearch/detectron2/pull/4132/
"""
cast_fn = torch.jit.script(cast_fn)
onnx_model = export_to_onnx(cast_fn, torch.zeros([1, 3, 32, 32]))
for n in onnx_model.graph.node:
self.assertNotEqual(n.op_type, "To")
self.assertNotEqual(n.op_type, "Cast")
def test_to__cpu_string(self):
def cast_cpu_string(src: torch.Tensor) -> torch.Tensor:
return src.to("cpu")
self._helper_test_to_(cast_cpu_string)
def test_to__device_cpu_string(self):
def cast_device_cpu_string(src: torch.Tensor) -> torch.Tensor:
return src.to(device="cpu")
self._helper_test_to_(cast_device_cpu_string)
if __name__ == "__main__":
common_utils.run_tests()