votes up 5

Unsupported: ONNX export of (param1) in opset (param1). (param1). Please try opset version (param1).

Package:
torch
github stars 50580
Exception Class:
RuntimeError

Raise code

f _onnx_opset_unsupported(op_name, current_opset, supported_opset):
    raise RuntimeError('Unsupported: ONNX export of {} in '
                       'opset {}. Please try opset version {}.'.format(op_name, current_opset, supported_opset))

def _onnx_opset_unsupported_detailed(op_name, current_opset, supported_opset, reason):
    raise RuntimeError('Unsupported: ONNX export of {} in '
                       'opset {}. {}. Please try opset version {}.'.format(op_name, current_opset, reason, supported_opset))


def _block_list_in_opset(name):
    def symbolic_fn(*args, **kwargs):
        raise RuntimeError("ONNX export failed on {}, which is not implemented for opset {}. "
                           "Try exporting with other opset versions."
    
😲  Walkingbet is Android app that pays you real bitcoins for a walking. Withdrawable real money bonus is available now, hurry up! 🚶

Ways to fix

votes up 2 votes down

This error is raised when a Pytorch model with torch operation index_put_ is exported with parameter

opset_version set to less than 11.

How to reproduce the error:

pipenv install torch numpy

import torch
import torch.nn as nn
import torch.nn.init as init
import numpy as np
import os
import tempfile


class CustomAdder(torch.nn.Module):
    def __init__(self):
      super().__init__()
    def forward(self, x):
      a = torch.zeros(2, 5)
      a.index_put_([torch.tensor(1), torch.tensor([1, 1])], torch.tensor(1.))
      a = a.reshape(10,1)
      return x+a
    def string(self):
      return f'y = a+x'


device = 'cuda' if torch.cuda.is_available() else 'cpu'

x = np.arange(10).reshape(10,1)
x_tensor = torch.from_numpy(x).float().to(device)
model = CustomAdder() 
model.eval()


y = model(x_tensor) # test the models forward method

#exporting the model to onnx format
with tempfile.TemporaryDirectory() as tmpdir:
    model_path = os.path.join(tmpdir,"customAdder.onnx")  
    torch.onnx.export(model,
                      x_tensor,
                      model_path,
                      opset_version=10,
                      export_params=True,
                      do_constant_folding=True,
                      input_names = ['input'],
                      output_names = ['output'],
                      dynamic_axes={'input' : {0 : 'batch_size'},
                                    'output' : {0 : 'batch_size'}})

Output error:

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:13: TracerWarning: torch.tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.
  del sys.path[0]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-2-7a5a024f8b94> in <module>()
     43                       output_names = ['output'],
     44                       dynamic_axes={'input' : {0 : 'batch_size'},
---> 45                                     'output' : {0 : 'batch_size'}})

/usr/local/lib/python3.7/dist-packages/torch/onnx/__init__.py in export(model, args, f, export_params, verbose, training, input_names, output_names, aten, export_raw_ir, operator_export_type, opset_version, _retain_param_name, do_constant_folding, example_outputs, strip_doc_string, dynamic_axes, keep_initializers_as_inputs, custom_opsets, enable_onnx_checker, use_external_data_format)
    278                         do_constant_folding, example_outputs,
    279                         strip_doc_string, dynamic_axes, keep_initializers_as_inputs,
--> 280                         custom_opsets, enable_onnx_checker, use_external_data_format)
    281 
    282 

/usr/local/lib/python3.7/dist-packages/torch/onnx/utils.py in export(model, args, f, export_params, verbose, training, input_names, output_names, aten, export_raw_ir, operator_export_type, opset_version, _retain_param_name, do_constant_folding, example_outputs, strip_doc_string, dynamic_axes, keep_initializers_as_inputs, custom_opsets, enable_onnx_checker, use_external_data_format)
     92             dynamic_axes=dynamic_axes, keep_initializers_as_inputs=keep_initializers_as_inputs,
     93             custom_opsets=custom_opsets, enable_onnx_checker=enable_onnx_checker,
---> 94             use_external_data_format=use_external_data_format)
     95 
     96 

/usr/local/lib/python3.7/dist-packages/torch/onnx/utils.py in _export(model, args, f, export_params, verbose, training, input_names, output_names, operator_export_type, export_type, example_outputs, opset_version, _retain_param_name, do_constant_folding, strip_doc_string, dynamic_axes, keep_initializers_as_inputs, fixed_batch_size, custom_opsets, add_node_names, enable_onnx_checker, use_external_data_format, onnx_shape_inference)
    693                                 fixed_batch_size=fixed_batch_size,
    694                                 training=training,
--> 695                                 dynamic_axes=dynamic_axes)
    696 
    697             # TODO: Don't allocate a in-memory string for the protobuf

/usr/local/lib/python3.7/dist-packages/torch/onnx/utils.py in _model_to_graph(model, args, verbose, input_names, output_names, operator_export_type, example_outputs, _retain_param_name, do_constant_folding, _disable_torch_constant_prop, fixed_batch_size, training, dynamic_axes)
    465                             fixed_batch_size=fixed_batch_size, params_dict=params_dict,
    466                             dynamic_axes=dynamic_axes, input_names=input_names,
--> 467                             module=module)
    468     from torch.onnx.symbolic_helper import _onnx_shape_inference
    469     if isinstance(model, torch.jit.ScriptModule) or isinstance(model, torch.jit.ScriptFunction):

/usr/local/lib/python3.7/dist-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)
    198             dynamic_axes = {} if dynamic_axes is None else dynamic_axes
    199             torch._C._jit_pass_onnx_set_dynamic_input_shape(graph, dynamic_axes, input_names)
--> 200         graph = torch._C._jit_pass_onnx(graph, operator_export_type)
    201         torch._C._jit_pass_lint(graph)
    202 

/usr/local/lib/python3.7/dist-packages/torch/onnx/__init__.py in _run_symbolic_function(*args, **kwargs)
    311 def _run_symbolic_function(*args, **kwargs):
    312     from torch.onnx import utils
--> 313     return utils._run_symbolic_function(*args, **kwargs)
    314 
    315 

/usr/local/lib/python3.7/dist-packages/torch/onnx/utils.py in _run_symbolic_function(g, block, n, inputs, env, operator_export_type)
    969         if ns == "onnx":
    970             if op_name == "Placeholder":
--> 971                 return torch._C._jit_onnx_convert_pattern_from_subblock(block, n, env)
    972             else:
    973                 # Use the original node directly

/usr/local/lib/python3.7/dist-packages/torch/onnx/__init__.py in _run_symbolic_function(*args, **kwargs)
    311 def _run_symbolic_function(*args, **kwargs):
    312     from torch.onnx import utils
--> 313     return utils._run_symbolic_function(*args, **kwargs)
    314 
    315 

/usr/local/lib/python3.7/dist-packages/torch/onnx/utils.py in _run_symbolic_function(g, block, n, inputs, env, operator_export_type)
    992                     return None
    993                 attrs = {k: n[k] for k in n.attributeNames()}
--> 994                 return symbolic_fn(g, *inputs, **attrs)
    995 
    996         elif ns == "prim":

/usr/local/lib/python3.7/dist-packages/torch/onnx/symbolic_opset9.py in index_put(g, self, indices_list_value, values, accumulate)
   1486             return values
   1487     else:
-> 1488         sym_help._onnx_opset_unsupported('index_put', 9, 11)
   1489 
   1490 

/usr/local/lib/python3.7/dist-packages/torch/onnx/symbolic_helper.py in _onnx_opset_unsupported(op_name, current_opset, supported_opset)
    251 def _onnx_opset_unsupported(op_name, current_opset, supported_opset):
    252     raise RuntimeError('Unsupported: ONNX export of {} in '
--> 253                        'opset {}. Please try opset version {}.'.format(op_name, current_opset, supported_opset))
    254 
    255 def _onnx_opset_unsupported_detailed(op_name, current_opset, supported_opset, reason):

RuntimeError: Unsupported: ONNX export of index_put in opset 9. Please try opset version 11.

How to fix the error:

In order to fix this error we have to set the opset_version to a value greater or equal than 11 when calling torch.onnx.export function.

import torch
import torch.nn as nn
import torch.nn.init as init
import numpy as np
import os
import tempfile


class CustomAdder(torch.nn.Module):
    def __init__(self):
      super().__init__()
    def forward(self, x):
      a = torch.zeros(2, 5)
      a.index_put_([torch.tensor(1), torch.tensor([1, 1])], torch.tensor(1.))
      a = a.reshape(10,1)
      return x+a
    def string(self):
      return f'y = a+x'


device = 'cuda' if torch.cuda.is_available() else 'cpu'
# initialize the x arary
x = np.arange(10).reshape(10,1)
x_tensor = torch.from_numpy(x).float().to(device)
model = CustomAdder() 
model.eval()


y = model(x_tensor)


with tempfile.TemporaryDirectory() as tmpdir:
    model_path = os.path.join(tmpdir,"customAdder.onnx")  
    torch.onnx.export(model,
                      x_tensor,
                      model_path,
                      opset_version=11,
                      export_params=True,
                      do_constant_folding=True,
                      input_names = ['input'],
                      output_names = ['output'],
                      dynamic_axes={'input' : {0 : 'batch_size'},
                                    'output' : {0 : 'batch_size'}})

Jul 01, 2021 kellemnegasi answer
kellemnegasi 31.6k

Add a possible fix

Please authorize to post fix