Cannot export to onnx : INTERNAL ASSERT FAILED

I converted the already created onnx model to phytorch, retrained it, but when I tried to export it back to onnx, I keep getting the following error. The same error appears when I export to onnx immediately without training after converting to pytorch so I don’t think there was a problem with training.

The same error appears every time I write ‘torch.onnx.export’. And I double checked the type and shape of the model input when writing ‘torch.onnx.export’.

Does anyone know why?

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/var/folders/49/9g9lxm9d3f3br8zlg2l2fmz80000gn/T/ipykernel_1349/2634282627.py in <module>
----> 1 torch.onnx.export(model, x, "output.onnx")

/opt/anaconda3/lib/python3.9/site-packages/torch/onnx/utils.py in export(model, args, f, export_params, verbose, training, input_names, output_names, operator_export_type, opset_version, do_constant_folding, dynamic_axes, keep_initializers_as_inputs, custom_opsets, export_modules_as_functions)
    502     """
    503 
--> 504     _export(
    505         model,
    506         args,

/opt/anaconda3/lib/python3.9/site-packages/torch/onnx/utils.py in _export(model, args, f, export_params, verbose, training, input_names, output_names, operator_export_type, export_type, opset_version, do_constant_folding, dynamic_axes, keep_initializers_as_inputs, fixed_batch_size, custom_opsets, add_node_names, onnx_shape_inference, export_modules_as_functions)
   1527             _validate_dynamic_axes(dynamic_axes, model, input_names, output_names)
   1528 
-> 1529             graph, params_dict, torch_out = _model_to_graph(
   1530                 model,
   1531                 args,

/opt/anaconda3/lib/python3.9/site-packages/torch/onnx/utils.py in _model_to_graph(model, args, verbose, input_names, output_names, operator_export_type, do_constant_folding, _disable_torch_constant_prop, fixed_batch_size, training, dynamic_axes)
   1113 
   1114     try:
-> 1115         graph = _optimize_graph(
   1116             graph,
   1117             operator_export_type,

/opt/anaconda3/lib/python3.9/site-packages/torch/onnx/utils.py in _optimize_graph(graph, operator_export_type, _disable_torch_constant_prop, fixed_batch_size, params_dict, dynamic_axes, input_names, module)
    580     _C._jit_pass_lint(graph)
    581     _C._jit_pass_onnx_autograd_function_process(graph)
--> 582     C._jit_pass_lower_all_tuples(graph)
    583 
    584     # we now record some ops like ones/zeros

RuntimeError: outerNode->outputs().size() == node->inputs().size() INTERNAL ASSERT FAILED at "/Users/runner/work/pytorch/pytorch/pytorch/torch/csrc/jit/passes/dead_code_elimination.cpp":140, please report a bug to PyTorch.

The error is caused by an assertion fail in the “dead_code_elimination” pass in PyTorch. This pass is responsible for removing unused code in the graph. The most likely cause of this error is that your code is not running as expected and is creating a graph that is not valid. This can be caused by many things, but the most likely cause is a bug in your code. To debug this, you can try to restart your kernel and run your code again. If the error still occurs, then it is likely that there is a bug in your code.