forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytorch_test_common.py
420 lines (310 loc) · 12.1 KB
/
pytorch_test_common.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
# Owner(s): ["module: onnx"]
from __future__ import annotations
import functools
import os
import random
import sys
import unittest
from enum import auto, Enum
from typing import Optional
import numpy as np
import packaging.version
import pytest
import torch
from torch.autograd import function
from torch.onnx._internal import diagnostics
from torch.testing._internal import common_utils
pytorch_test_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.insert(-1, pytorch_test_dir)
torch.set_default_dtype(torch.float)
BATCH_SIZE = 2
RNN_BATCH_SIZE = 7
RNN_SEQUENCE_LENGTH = 11
RNN_INPUT_SIZE = 5
RNN_HIDDEN_SIZE = 3
class TorchModelType(Enum):
TORCH_NN_MODULE = auto()
TORCH_EXPORT_EXPORTEDPROGRAM = auto()
def _skipper(condition, reason):
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
if condition():
raise unittest.SkipTest(reason)
return f(*args, **kwargs)
return wrapper
return decorator
skipIfNoCuda = _skipper(lambda: not torch.cuda.is_available(), "CUDA is not available")
skipIfTravis = _skipper(lambda: os.getenv("TRAVIS"), "Skip In Travis")
skipIfNoBFloat16Cuda = _skipper(
lambda: not torch.cuda.is_bf16_supported(), "BFloat16 CUDA is not available"
)
skipIfQuantizationBackendQNNPack = _skipper(
lambda: torch.backends.quantized.engine == "qnnpack",
"Not compatible with QNNPack quantization backend",
)
# skips tests for all versions below min_opset_version.
# add this wrapper to prevent running the test for opset_versions
# smaller than `min_opset_version`.
def skipIfUnsupportedMinOpsetVersion(min_opset_version):
def skip_dec(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if self.opset_version < min_opset_version:
raise unittest.SkipTest(
f"Unsupported opset_version: {self.opset_version} < {min_opset_version}"
)
return func(self, *args, **kwargs)
return wrapper
return skip_dec
# skips tests for all versions above max_opset_version.
# add this wrapper to prevent running the test for opset_versions
# higher than `max_opset_version`.
def skipIfUnsupportedMaxOpsetVersion(max_opset_version):
def skip_dec(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if self.opset_version > max_opset_version:
raise unittest.SkipTest(
f"Unsupported opset_version: {self.opset_version} > {max_opset_version}"
)
return func(self, *args, **kwargs)
return wrapper
return skip_dec
# skips tests for all opset versions.
def skipForAllOpsetVersions():
def skip_dec(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if self.opset_version:
raise unittest.SkipTest(
"Skip verify test for unsupported opset_version"
)
return func(self, *args, **kwargs)
return wrapper
return skip_dec
def skipTraceTest(skip_before_opset_version: Optional[int] = None, reason: str = ""):
"""Skip tracing test for opset version less than skip_before_opset_version.
Args:
skip_before_opset_version: The opset version before which to skip tracing test.
If None, tracing test is always skipped.
reason: The reason for skipping tracing test.
Returns:
A decorator for skipping tracing test.
"""
def skip_dec(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if skip_before_opset_version is not None:
self.skip_this_opset = self.opset_version < skip_before_opset_version
else:
self.skip_this_opset = True
if self.skip_this_opset and not self.is_script:
raise unittest.SkipTest(f"Skip verify test for torch trace. {reason}")
return func(self, *args, **kwargs)
return wrapper
return skip_dec
def skipScriptTest(skip_before_opset_version: Optional[int] = None, reason: str = ""):
"""Skip scripting test for opset version less than skip_before_opset_version.
Args:
skip_before_opset_version: The opset version before which to skip scripting test.
If None, scripting test is always skipped.
reason: The reason for skipping scripting test.
Returns:
A decorator for skipping scripting test.
"""
def skip_dec(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if skip_before_opset_version is not None:
self.skip_this_opset = self.opset_version < skip_before_opset_version
else:
self.skip_this_opset = True
if self.skip_this_opset and self.is_script:
raise unittest.SkipTest(f"Skip verify test for TorchScript. {reason}")
return func(self, *args, **kwargs)
return wrapper
return skip_dec
# NOTE: This decorator is currently unused, but we may want to use it in the future when
# we have more tests that are not supported in released ORT.
def skip_min_ort_version(reason: str, version: str, dynamic_only: bool = False):
def skip_dec(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if (
packaging.version.parse(self.ort_version).release
< packaging.version.parse(version).release
):
if dynamic_only and not self.dynamic_shapes:
return func(self, *args, **kwargs)
raise unittest.SkipTest(
f"ONNX Runtime version: {version} is older than required version {version}. "
f"Reason: {reason}."
)
return func(self, *args, **kwargs)
return wrapper
return skip_dec
def xfail_dynamic_fx_test(
error_message: str,
model_type: Optional[TorchModelType] = None,
reason: Optional[str] = None,
):
"""Xfail dynamic exporting test.
Args:
reason: The reason for xfailing dynamic exporting test.
model_type (TorchModelType): The model type to xfail dynamic exporting test for.
When None, model type is not used to xfail dynamic tests.
Returns:
A decorator for xfailing dynamic exporting test.
"""
def skip_dec(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if self.dynamic_shapes and (
not model_type or self.model_type == model_type
):
return xfail(error_message, reason)(func)(self, *args, **kwargs)
return func(self, *args, **kwargs)
return wrapper
return skip_dec
def skip_dynamic_fx_test(reason: str, model_type: TorchModelType = None):
"""Skip dynamic exporting test.
Args:
reason: The reason for skipping dynamic exporting test.
model_type (TorchModelType): The model type to skip dynamic exporting test for.
When None, model type is not used to skip dynamic tests.
Returns:
A decorator for skipping dynamic exporting test.
"""
def skip_dec(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if self.dynamic_shapes and (
not model_type or self.model_type == model_type
):
raise unittest.SkipTest(
f"Skip verify dynamic shapes test for FX. {reason}"
)
return func(self, *args, **kwargs)
return wrapper
return skip_dec
def skip_in_ci(reason: str):
"""Skip test in CI.
Args:
reason: The reason for skipping test in CI.
Returns:
A decorator for skipping test in CI.
"""
def skip_dec(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if os.getenv("CI"):
raise unittest.SkipTest(f"Skip test in CI. {reason}")
return func(self, *args, **kwargs)
return wrapper
return skip_dec
def xfail(error_message: str, reason: Optional[str] = None):
"""Expect failure.
Args:
reason: The reason for expected failure.
Returns:
A decorator for expecting test failure.
"""
def wrapper(func):
@functools.wraps(func)
def inner(self, *args, **kwargs):
try:
func(self, *args, **kwargs)
except Exception as e:
if isinstance(e, torch.onnx.OnnxExporterError):
# diagnostic message is in the cause of the exception
assert (
error_message in str(e.__cause__)
), f"Expected error message: {error_message} NOT in {str(e.__cause__)}"
else:
assert error_message in str(
e
), f"Expected error message: {error_message} NOT in {str(e)}"
pytest.xfail(reason if reason else f"Expected failure: {error_message}")
else:
pytest.fail("Unexpected success!")
return inner
return wrapper
# skips tests for opset_versions listed in unsupported_opset_versions.
# if the PyTorch test cannot be run for a specific version, add this wrapper
# (for example, an op was modified but the change is not supported in PyTorch)
def skipIfUnsupportedOpsetVersion(unsupported_opset_versions):
def skip_dec(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if self.opset_version in unsupported_opset_versions:
raise unittest.SkipTest(
"Skip verify test for unsupported opset_version"
)
return func(self, *args, **kwargs)
return wrapper
return skip_dec
def skipShapeChecking(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
self.check_shape = False
return func(self, *args, **kwargs)
return wrapper
def skipDtypeChecking(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
self.check_dtype = False
return func(self, *args, **kwargs)
return wrapper
def xfail_if_model_type_is_exportedprogram(
error_message: str, reason: Optional[str] = None
):
"""xfail test with models using ExportedProgram as input.
Args:
error_message: The error message to raise when the test is xfailed.
reason: The reason for xfail the ONNX export test.
Returns:
A decorator for xfail tests.
"""
def xfail_dec(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if self.model_type == TorchModelType.TORCH_EXPORT_EXPORTEDPROGRAM:
return xfail(error_message, reason)(func)(self, *args, **kwargs)
return func(self, *args, **kwargs)
return wrapper
return xfail_dec
def xfail_if_model_type_is_not_exportedprogram(
error_message: str, reason: Optional[str] = None
):
"""xfail test without models using ExportedProgram as input.
Args:
reason: The reason for xfail the ONNX export test.
Returns:
A decorator for xfail tests.
"""
def xfail_dec(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
if self.model_type != TorchModelType.TORCH_EXPORT_EXPORTEDPROGRAM:
return xfail(error_message, reason)(func)(self, *args, **kwargs)
return func(self, *args, **kwargs)
return wrapper
return xfail_dec
def flatten(x):
return tuple(function._iter_filter(lambda o: isinstance(o, torch.Tensor))(x))
def set_rng_seed(seed):
torch.manual_seed(seed)
random.seed(seed)
np.random.seed(seed)
class ExportTestCase(common_utils.TestCase):
"""Test case for ONNX export.
Any test case that tests functionalities under torch.onnx should inherit from this class.
"""
def setUp(self):
super().setUp()
# TODO(#88264): Flaky test failures after changing seed.
set_rng_seed(0)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(0)
diagnostics.engine.clear()