Azyka opened a new issue, #16056:
URL: https://github.com/apache/tvm/issues/16056
### Expected behavior
When using torch.matmul, 2 same model should output the same for the same
inputs.
### Actual behavior
The outputs of 2 same graphs turn out to be different and have strange large
numbers with and without optimization. (tvm_opt_0 can produce correct result by
chance)
```
=========================
tvm_opt_4 triggers assertion
Not equal to tolerance rtol=0.001, atol=1e-06
Mismatched elements: 7 / 11 (63.6%)
Max absolute difference: 657045672457503
Max relative difference: 9.38593725e+13
x: array([[ 49, 455, 30064771114,
980329671500368, 657015607688160, 7,
657015607688160, 657015607688216, 657015607688216,
903, 657015609103896]])
y: array([[ 49, 567, 657015598753024,
657015603057856, 7, 7,
657015601126304, 657015605455664, -30064769287,
657015606943248, 560]])
=========================
=========================
tvm_opt_0 triggers assertion
Not equal to tolerance rtol=0.001, atol=1e-06
Mismatched elements: 10 / 11 (90.9%)
Max absolute difference: 980329671500368
Max relative difference: 0.42477876
x: array([[ 49, 455, 30064771114,
980329671500368, 657015609537392, 7,
657015609537392, 657015609537448, 657015609537448,
343, 657015627399264]])
y: array([[ 49, 791, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
=========================
```
### Environment
- OS: Ubuntu 22.04.3 LTS (x86_64)
- TVM version: 0.14.dev189
- Execution provider: cpu
- ONNX opset version: 14
### Steps to reproduce
Sample code:
```
import onnx
import numpy as np
from numpy import testing
import tvm
from tvm import relay
import torch
class Model0(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self, *args):
_args = args
getitem = _args[0]
pad = torch.nn.functional.pad(getitem, (10, 0))
squeeze = pad.squeeze(0)
argmin = squeeze.argmin(1)
matmul = torch.matmul(argmin, argmin)
return (matmul)
model_0 = Model0()
input_data_0 = np.array([[[4.45420742], [4.5419035 ], [3.84637547],
[4.9328866 ], [3.71243978], [4.71293545], [3.41899776], [3.03590369],
[6.42920589], [3.96643543], [6.74063587], [6.43227768], [3.21090269],
[3.33323216], [4.09901142], [3.51859164], [4.03458452], [4.4983654 ],
[5.23693514], [3.62662458], [6.4685688 ], [5.75134039], [4.96017933],
[4.15254498], [6.92458534], [6.26394033], [4.50898981]]])
input_dict_0 = {"v4_0":input_data_0}
inputs_0 = tuple(torch.from_numpy(v).to('cpu') for _, v in
input_dict_0.items())
torch.onnx.export(model_0, inputs_0, '0.onnx', verbose=False,
input_names=['v4_0'], output_names=['v2_0'], opset_version=14,
do_constant_folding=False)
class Model1(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self, *args):
_args = args
getitem = _args[0]
pad = torch.nn.functional.pad(getitem, (10, 0))
squeeze = pad.squeeze(0)
argmin = squeeze.argmin(1)
matmul = torch.matmul(argmin, argmin)
return (matmul)
model_1 = Model1()
input_dict_1 = {"v0_0":input_data_0}
inputs_1 = tuple(torch.from_numpy(v).to('cpu') for _, v in
input_dict_1.items())
torch.onnx.export(model_1, inputs_1, '1.onnx', verbose=False,
input_names=['v0_0'], output_names=['v4_0'], opset_version=14,
do_constant_folding=False)
onnx_model_0 = onnx.load('0.onnx')
onnx_model_outputs_0 = [node.name for node in onnx_model_0.graph.output]
shape_dict_0 = {key: val.shape for key, val in input_dict_0.items()}
mod_0, params_0 = relay.frontend.from_onnx(onnx_model_0, shape_dict_0,
freeze_params=True)
with tvm.transform.PassContext(opt_level=4):
executor_0 = relay.build_module.create_executor("graph", mod_0,
tvm.cpu(), tvm.target.Target("llvm"), params_0).evaluate()
executor_res_0 = [tensor.numpy() for tensor in
[executor_0(**input_dict_0)]]
output_0 = dict(zip(onnx_model_outputs_0, executor_res_0))
onnx_model_1 = onnx.load('1.onnx')
onnx_model_outputs_1 = [node.name for node in onnx_model_1.graph.output]
shape_dict_1 = {key: val.shape for key, val in input_dict_1.items()}
mod_1, params_1 = relay.frontend.from_onnx(onnx_model_1, shape_dict_1,
freeze_params=True)
with tvm.transform.PassContext(opt_level=4):
executor_1 = relay.build_module.create_executor("graph", mod_1,
tvm.cpu(), tvm.target.Target("llvm"), params_1).evaluate()
executor_res_1 = [tensor.numpy() for tensor in
[executor_1(**input_dict_1)]]
output_1 = dict(zip(onnx_model_outputs_1, executor_res_1))
output_name_dict = {'v2_0': 'v4_0'}
print('=========================')
try:
for tensor_name_0, tensor_name_1 in output_name_dict.items():
testing.assert_allclose(output_0[tensor_name_0],
output_1[tensor_name_1], rtol=1e-3, atol=1e-6)
print("tvm_opt_4 does not trigger assertion")
except AssertionError as e:
print("tvm_opt_4 triggers assertion")
print(e)
print('=========================')
shape_dict_0 = {key: val.shape for key, val in input_dict_0.items()}
mod_0, params_0 = relay.frontend.from_onnx(onnx_model_0, shape_dict_0,
freeze_params=True)
with tvm.transform.PassContext(opt_level=0):
executor_0 = relay.build_module.create_executor("graph", mod_0,
tvm.cpu(), tvm.target.Target("llvm"), params_0).evaluate()
executor_res_0 = [tensor.numpy() for tensor in
[executor_0(**input_dict_0)]]
output_0 = dict(zip(onnx_model_outputs_0, executor_res_0))
shape_dict_1 = {key: val.shape for key, val in input_dict_1.items()}
mod_1, params_1 = relay.frontend.from_onnx(onnx_model_1, shape_dict_1,
freeze_params=True)
with tvm.transform.PassContext(opt_level=0):
executor_1 = relay.build_module.create_executor("graph", mod_1,
tvm.cpu(), tvm.target.Target("llvm"), params_1).evaluate()
executor_res_1 = [tensor.numpy() for tensor in
[executor_1(**input_dict_1)]]
output_1 = dict(zip(onnx_model_outputs_1, executor_res_1))
print('=========================')
try:
for tensor_name_0, tensor_name_1 in output_name_dict.items():
testing.assert_allclose(output_0[tensor_name_0],
output_1[tensor_name_1], rtol=1e-3, atol=1e-6)
print("tvm_opt_0 does not trigger assertion")
except AssertionError as e:
print("tvm_opt_0 triggers assertion")
print(e)
print('=========================')
```
1. Execute the code.
### Triage
* needs-triage
* frontend:onnx
* flow:relay
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]