forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_reproduction.py
178 lines (134 loc) · 3.41 KB
/
error_reproduction.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
"""Error reproduction utilities for op consistency tests."""
from __future__ import annotations
import difflib
import pathlib
import platform
import sys
import time
import traceback
import numpy as np
import onnx
import onnxruntime as ort
import onnxscript
import torch
_MISMATCH_MARKDOWN_TEMPLATE = """\
### Summary
The output of ONNX Runtime does not match that of PyTorch when executing test
`{test_name}`, `sample {sample_num}` in ONNX Script `TorchLib`.
To recreate this report, use
```bash
CREATE_REPRODUCTION_REPORT=1 python -m pytest onnxscript/tests/function_libs/torch_lib/ops_test.py -k {short_test_name}
```
### ONNX Model
```
{onnx_model_text}
```
### Inputs
Shapes: `{input_shapes}`
<details><summary>Details</summary>
<p>
```python
kwargs = {kwargs}
inputs = {inputs}
```
</p>
</details>
### Expected output
Shape: `{expected_shape}`
<details><summary>Details</summary>
<p>
```python
expected = {expected}
```
</p>
</details>
### Actual output
Shape: `{actual_shape}`
<details><summary>Details</summary>
<p>
```python
actual = {actual}
```
</p>
</details>
### Difference
<details><summary>Details</summary>
<p>
```diff
{diff}
```
</p>
</details>
### Full error stack
```
{error_stack}
```
### Environment
```
{sys_info}
```
"""
def create_mismatch_report(
test_name: str,
sample_num: int,
onnx_model: onnx.ModelProto,
inputs,
kwargs,
actual,
expected,
error: Exception,
) -> None:
torch.set_printoptions(threshold=sys.maxsize)
error_text = str(error)
error_stack = error_text + "\n" + "".join(traceback.format_tb(error.__traceback__))
short_test_name = test_name.split(".")[-1]
diff = difflib.unified_diff(
str(actual).splitlines(),
str(expected).splitlines(),
fromfile="actual",
tofile="expected",
lineterm="",
)
onnx_model_text = onnx.printer.to_text(onnx_model)
input_shapes = repr(
[
f"Tensor<{inp.shape}, dtype={inp.dtype}>"
if isinstance(inp, torch.Tensor)
else inp
for inp in inputs
]
)
sys_info = f"""\
OS: {platform.platform()}
Python version: {sys.version}
onnx=={onnx.__version__}
onnxruntime=={ort.__version__}
onnxscript=={onnxscript.__version__}
numpy=={np.__version__}
torch=={torch.__version__}"""
markdown = _MISMATCH_MARKDOWN_TEMPLATE.format(
test_name=test_name,
short_test_name=short_test_name,
sample_num=sample_num,
input_shapes=input_shapes,
inputs=inputs,
kwargs=kwargs,
expected=expected,
expected_shape=expected.shape if isinstance(expected, torch.Tensor) else None,
actual=actual,
actual_shape=actual.shape if isinstance(actual, torch.Tensor) else None,
diff="\n".join(diff),
error_stack=error_stack,
sys_info=sys_info,
onnx_model_text=onnx_model_text,
)
markdown_file_name = f'mismatch-{short_test_name.replace("/", "-").replace(":", "-")}-{str(time.time()).replace(".", "_")}.md'
markdown_file_path = save_error_report(markdown_file_name, markdown)
print(f"Created reproduction report at {markdown_file_path}")
def save_error_report(file_name: str, text: str):
reports_dir = pathlib.Path("error_reports")
reports_dir.mkdir(parents=True, exist_ok=True)
file_path = reports_dir / file_name
with open(file_path, "w", encoding="utf-8") as f:
f.write(text)
return file_path