daquexian / onnx-simplifier

Simplify your onnx model
Apache License 2.0
3.67k stars 377 forks source link

[BUG] onnx.onnx_cpp2py_export.checker.ValidationError: Field 'type' of 'value_info' is required but missing. #287

Open yqzhishen opened 1 year ago

yqzhishen commented 1 year ago

Describe the bug

ONNX Simplifier fails to simplify models with optional types.

The following minimum demo:

from typing import Optional

import onnx
import onnxsim
import torch

class MyModule(torch.nn.Module):
    def forward(self, x: Optional[torch.Tensor]):
        return x is not None

module = MyModule()
module = torch.jit.script(module, example_inputs=[(torch.tensor(1),), (None,)])
torch.onnx.export(
    module, (torch.tensor(1),), 'optional.onnx',
    input_names=['x'], output_names=['y'], opset_version=15
)
model = onnx.load('optional.onnx')
model, _ = onnxsim.simplify(model, include_subgraph=True)
onnx.save(model, 'optional.onnx')

will get the following error:

Traceback (most recent call last):
  File "E:\OpenVPI\DiffSinger\optional_test.py", line 20, in <module>
    model, _ = onnxsim.simplify(model, include_subgraph=True)
  File "E:\OpenVPI\DiffSinger\venv\lib\site-packages\onnxsim\onnx_simplifier.py", line 199, in simplify
    model_opt_bytes = C.simplify(
onnx.onnx_cpp2py_export.checker.ValidationError: Field 'type' of 'value_info' is required but missing.

Model

optional.zip

Alicture commented 3 months ago

Bug still triged by optional forward arg.