SwinTransformer / Video-Swin-Transformer

This is an official implementation for "Video Swin Transformers".
https://arxiv.org/abs/2106.13230
Apache License 2.0
1.42k stars 199 forks source link

Can't export ONNX transformer #74

Open dmenig opened 2 years ago

dmenig commented 2 years ago

This command python3 tools/deployment/pytorch2onnx.py configs/recognition/swin/swin_tiny_patch244_window877_kinetics400_1k.py swin_tiny_patch244_window877_kinetics400_1k.pth outputs this error :

/opt/conda/lib/python3.8/site-packages/torch/functional.py:478: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at /opt/pytorch/pytorch/aten/src/ATen/native/TensorShape.cpp:2966.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
Use load_from_local loader
Traceback (most recent call last):
  File "tools/deployment/pytorch2onnx.py", line 163, in <module>
    pytorch2onnx(
  File "tools/deployment/pytorch2onnx.py", line 67, in pytorch2onnx
    torch.onnx.export(
  File "/opt/conda/lib/python3.8/site-packages/torch/onnx/utils.py", line 479, in export
    _export(
  File "/opt/conda/lib/python3.8/site-packages/torch/onnx/utils.py", line 1411, in _export
    graph, params_dict, torch_out = _model_to_graph(
  File "/opt/conda/lib/python3.8/site-packages/torch/onnx/utils.py", line 1050, in _model_to_graph
    graph, params, torch_out, module = _create_jit_graph(model, args)
  File "/opt/conda/lib/python3.8/site-packages/torch/onnx/utils.py", line 925, in _create_jit_graph
    graph, torch_out = _trace_and_get_graph_from_model(model, args)
  File "/opt/conda/lib/python3.8/site-packages/torch/onnx/utils.py", line 833, in _trace_and_get_graph_from_model
    trace_graph, torch_out, inputs_states = torch.jit._get_trace_graph(
  File "/opt/conda/lib/python3.8/site-packages/torch/jit/_trace.py", line 1175, in _get_trace_graph
    outs = ONNXTracedModule(f, strict, _force_outplace, return_inputs, _return_inputs_states)(*args, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1186, in _call_impl
    return forward_call(*input, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/torch/jit/_trace.py", line 127, in forward
    graph, out = torch._C._create_graph_by_tracing(
  File "/opt/conda/lib/python3.8/site-packages/torch/jit/_trace.py", line 118, in wrapper
    outs.append(self.inner(*trace_inputs))
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1186, in _call_impl
    return forward_call(*input, **kwargs)
  File "/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1174, in _slow_forward
    result = self.forward(*input, **kwargs)
TypeError: forward_dummy() got multiple values for argument 'softmax'
leonid-pishchulin commented 1 year ago

I was able to resolve this issue by modifying this line as model.forward = partial(model.forward_dummy) Then, I faced another issue that I resolved by commenting out this line. Then, I got the whole bunch of warnings line that

TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!

and finally an error: Floating point exception (core dumped). Not sure what to do at that point

gigasurgeon commented 9 months ago

I was able to resolve this issue by modifying this line as model.forward = partial(model.forward_dummy) Then, I faced another issue that I resolved by commenting out this line. Then, I got the whole bunch of warnings line that

TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!

and finally an error: Floating point exception (core dumped). Not sure what to do at that point

I am also getting the same Floating point exception (core dumped) error. Were you able to resolve it somehow?

gigasurgeon commented 9 months ago

I was able to successfully convert it to ONNX by using this -> https://github.com/SwinTransformer/Video-Swin-Transformer/issues/89#issue-2034139366