Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnnxConstantOfShape Cannot Properly Infer Data Type for Argument value #703

Closed
busFred opened this issue Aug 19, 2021 · 3 comments · Fixed by #709
Closed

OnnxConstantOfShape Cannot Properly Infer Data Type for Argument value #703

busFred opened this issue Aug 19, 2021 · 3 comments · Fixed by #709

Comments

@busFred
Copy link

busFred commented Aug 19, 2021

I am testing my custom converter for relevance vector machien that I just created with input type np.float64 type:

onx = to_onnx(rvr, X_train.astype(np.float64), target_opset=13)

Scenario 1:
With the following code:

shape: np.ndarray = np.array([n_features], dtype=int)
OnnxConstantOfShape(shape, value=np.float64(1.0), op_version=op_version)

The model could be converted without error.
However, at inference time, the following error is raised:

InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_GRAPH : This is an invalid model. Error in Node:Co_ConstantOfShape : Mismatched attribute type in 'Co_ConstantOfShape : value'
---------------------------------------------------------------------------
InvalidGraph                              Traceback (most recent call last)
~/Documents/research/sklearn_plugins/test/sklearn_plugins/rvm/test_rvr_export.py in 
----> 63 sess = rt.InferenceSession(onx.SerializeToString())
      64 results = sess.run(None, {'X': X_train.astype(np.float64)})

~/.local/lib/python3.8/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py in __init__(self, path_or_bytes, sess_options, providers, provider_options)
    278 
    279         try:
--> 280             self._create_inference_session(providers, provider_options)
    281         except RuntimeError:
    282             if self._enable_fallback:

~/.local/lib/python3.8/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py in _create_inference_session(self, providers, provider_options)
    307             sess = C.InferenceSession(session_options, self._model_path, True, self._read_config_from_model)
    308         else:
--> 309             sess = C.InferenceSession(session_options, self._model_bytes, False, self._read_config_from_model)
    310 
    311         # initialize the C++ InferenceSession

InvalidGraph: [ONNXRuntimeError] : 10 : INVALID_GRAPH : This is an invalid model. Error in Node:Co_ConstantOfShape : Mismatched attribute type in 'Co_ConstantOfShape : value'

I visualized the exported model with Netron and found out value is of type float32.

image

Scenario 2:
However, if I try the following code:

shape: np.ndarray = np.array([n_features], dtype=int)
OnnxConstantOfShape(shape, value=np.array(1.0, dtype=np.float64), op_version=op_version)

The to_onnx call couldn't even finish:

Traceback (most recent call last):
  File \"<ipython-input-6-ef63ae625366>\", line 6, in <module>
    onx = to_onnx(rvr, X_train.astype(np.float64), target_opset=13)
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/convert.py\", line 216, in to_onnx
    return convert_sklearn(model, initial_types=initial_types,
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/convert.py\", line 162, in convert_sklearn
    onnx_model = convert_topology(topology, name, doc_string, target_opset,
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/common/_topology.py\", line 1202, in convert_topology
    conv(scope, operator, container)
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/common/_registration.py\", line 26, in __call__
    return self._fct(*args)
  File \"<ipython-input-2-c107a6998a24>\", line 3, in converter
    rvr_converter(scope=scope,
  File \"/home/fred/Documents/research/sklearn_plugins/src/sklearn_plugins/rvm/onnx_transfrom.py\", line 146, in rvr_converter
    y.add_to(scope=scope, container=container)
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/onnx_operator.py\", line 549, in add_to
    self.state.run()
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 414, in run
    v = self._get_var_name(i, False, index=None)
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 124, in _get_var_name
    var.add_to(self.scope, self.container, operator=operator,
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/onnx_operator.py\", line 549, in add_to
    self.state.run()
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 414, in run
    v = self._get_var_name(i, False, index=None)
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 124, in _get_var_name
    var.add_to(self.scope, self.container, operator=operator,
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/onnx_operator.py\", line 549, in add_to
    self.state.run()
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 414, in run
    v = self._get_var_name(i, False, index=None)
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 124, in _get_var_name
    var.add_to(self.scope, self.container, operator=operator,
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/onnx_operator.py\", line 549, in add_to
    self.state.run()
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 414, in run
    v = self._get_var_name(i, False, index=None)
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 124, in _get_var_name
    var.add_to(self.scope, self.container, operator=operator,
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/onnx_operator.py\", line 549, in add_to
    self.state.run()
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 414, in run
    v = self._get_var_name(i, False, index=None)
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 124, in _get_var_name
    var.add_to(self.scope, self.container, operator=operator,
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/onnx_operator.py\", line 549, in add_to
    self.state.run()
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 414, in run
    v = self._get_var_name(i, False, index=None)
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 124, in _get_var_name
    var.add_to(self.scope, self.container, operator=operator,
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/onnx_operator.py\", line 549, in add_to
    self.state.run()
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 414, in run
    v = self._get_var_name(i, False, index=None)
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 124, in _get_var_name
    var.add_to(self.scope, self.container, operator=operator,
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/onnx_operator.py\", line 549, in add_to
    self.state.run()
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/algebra/graph_state.py\", line 503, in run
    self.container.add_node(
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/common/_container.py\", line 555, in add_node
    node = make_node(op_type, inputs, outputs, name=name,
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/proto/onnx_helper_modified.py\", line 68, in make_node
    node.attribute.extend(
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/proto/onnx_helper_modified.py\", line 69, in <genexpr>
    make_attribute(key, value, dtype=_dtype, domain=domain)
  File \"/home/fred/anaconda3/envs/sklearn_plugins/lib/python3.8/site-packages/skl2onnx/proto/onnx_helper_modified.py\", line 130, in make_attribute
    byte_array = [_to_bytes_or_false(v) for v in value]
TypeError: iteration over a 0-d **array**

I set the data type to np.float64 for testing purpose.

@busFred
Copy link
Author

busFred commented Aug 19, 2021

Update
The same error shows up even when all the tensor is running in np.float32, that is the same error shows up even with the following two code snippets.

onx = to_onnx(rvr, X_train.astype(np.float64), target_opset=13)
shape: np.ndarray = np.array([n_features], dtype=int)
OnnxConstantOfShape(shape, value=np.float32(1.0), op_version=op_version)

This makes me believe it is expecting the value argument is expecting things like np.array(1.0, dtype=np.float32) or np.array(1.0, dtype=np.float64). But if I try the np.array() approach, the error described in Scenario 2 shows up when I try to do this.

@xadupre
Copy link
Collaborator

xadupre commented Aug 20, 2021

For this operator (following the example in ONNX specification for ConstantOfShape), you can use this code:

tensor_value = onnx.helper.make_tensor("value", onnx.TensorProto.FLOAT, [1], [5])
cst = OnnxConstantOfShape(shape, value=tensor_value, op_version=opset)

I'll create a PR to make it easier with numpy.

@busFred
Copy link
Author

busFred commented Aug 20, 2021

For this operator (following the example in ONNX specification for ConstantOfShape), you can use this code:

tensor_value = onnx.helper.make_tensor("value", onnx.TensorProto.FLOAT, [1], [5])
cst = OnnxConstantOfShape(shape, value=tensor_value, op_version=opset)

I'll create a PR to make it easier with numpy.

With the onnx.helper.make_tensor, I can convert the model without error.
Thank you for your assistance.

xadupre added a commit that referenced this issue Aug 26, 2021
* Interpet parameter value in ConstantOfShape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants