onnx-tensorflow icon indicating copy to clipboard operation
onnx-tensorflow copied to clipboard

ValueError: shapes must be equal rank, but are 0 and 3 for 'onnx_tf_prefix_Where_1751' (op: 'Select') with input:[32,1,120], [], [32,3072,120]

Open keanucui opened this issue 3 years ago • 4 comments

Describe the bug Traceback (most recent call last): File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 1607, in _create_c_op c_op = c_api.TF_FinishOperation(op_desc) tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes must be equal rank, but are 0 and 3 for 'onnx_tf_prefix_Where_1751' (op: 'Select') with input shapes: [32,1,120], [], [32,3072,120].

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "torch_to_tf.py", line 621, in onnx_to_tf("model/embedding_model.onnx") File "torch_to_tf.py", line 614, in onnx_to_tf tf_rep = prepare(onnx_model,opset_version=11) File "/home/text/onnx-tensorflow/onnx_tf/backend.py", line 69, in prepare return cls.onnx_model_to_tensorflow_rep(model, strict) File "/home/text/onnx-tensorflow/onnx_tf/backend.py", line 89, in onnx_model_to_tensorflow_rep return cls._onnx_graph_to_tensorflow_rep(model.graph, opset_import, strict) File "/home/text/onnx-tensorflow/onnx_tf/backend.py", line 150, in _onnx_graph_to_tensorflow_rep strict=strict) File "/home/text/onnx-tensorflow/onnx_tf/backend.py", line 257, in _onnx_node_to_tensorflow_op return handler.handle(node, tensor_dict=tensor_dict, strict=strict) File "/home/text/onnx-tensorflow/onnx_tf/handlers/handler.py", line 60, in handle return ver_handle(node, **kwargs) File "/home/text/onnx-tensorflow/onnx_tf/handlers/backend/where.py", line 14, in version_9 return [cls.make_tensor_from_onnx_node(node, **kwargs)] File "/home/text/onnx-tensorflow/onnx_tf/handlers/backend_handler.py", line 111, in make_tensor_from_onnx_node return cls._run_tf_func(tf_func, inputs, attrs) File "/home/text/onnx-tensorflow/onnx_tf/handlers/backend_handler.py", line 191, in _run_tf_func return tf_func(**kwargs) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/util/deprecation.py", line 324, in new_func return func(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/util/dispatch.py", line 180, in wrapper return target(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/array_ops.py", line 3759, in where return gen_math_ops.select(condition=condition, x=x, y=y, name=name) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/ops/gen_math_ops.py", line 9439, in select "Select", condition=condition, t=x, e=y, name=name) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/op_def_library.py", line 794, in _apply_op_helper op_def=op_def) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/util/deprecation.py", line 507, in new_func return func(*args, **kwargs) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 3357, in create_op attrs, op_def, compute_device) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 3426, in _create_op_internal op_def=op_def) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 1770, in init control_input_ops) File "/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/framework/ops.py", line 1610, in _create_c_op raise ValueError(str(e)) ValueError: Shapes must be equal rank, but are 0 and 3 for 'onnx_tf_prefix_Where_1751' (op: 'Select') with input shapes: [32,1,120], [], [32,3072,120].

The code

    onnx_model = onnx.load(onnx_filename)
    tf_rep = prepare(onnx_model,opset_version=11)
    tf_rep.export_graph(tf_pb_path)

Hello, @chinhuang007. As mentioned above. The onnx model test is fine, exactly output with PyTorch model. if need onnx model, I'll attached it.

  • Python version: 3.7.10
  • ONNX version: 1.7.0
  • ONNX-TF version: 1.7.0 (install with source code tf-1.x )
  • Tensorflow version: 1.15.0

Thanks!!

keanucui avatar Jan 21 '22 07:01 keanucui

Have you solved this problem? I have the same too.

L-Zhe avatar Nov 03 '22 17:11 L-Zhe

Have you solved this problem? I have the same too.

no.

keanucui avatar Jan 10 '23 03:01 keanucui

I got the similar problem when converting torch to tf through onnx with error: ValueError: Shapes must be equal rank, but are 0 and 3 for 'onnx_tf_prefix_Where_118' (op: 'Select') with input shapes: [?,16,?], [], [2,16,16]. Then, I use this website to show the network structure and find the the op onnx_tf_prefix_Where_118 with name Where_118. After checking my torch code, I found this operator corresponds to code:

attn = attn.masked_fill(mask, -np.inf)

So I think maybe masked_fill is not supported. Combining the example of the Where op from onnx, I change this line to:

attn = torch.where(mask, torch.full(mask.shape, -np.inf), attn)

And the problem is solved. Hope this helps. Good luck. 😊

yangqinj avatar Mar 23 '23 07:03 yangqinj

I got the similar problem when converting torch to tf through onnx with error: ValueError: Shapes must be equal rank, but are 0 and 3 for 'onnx_tf_prefix_Where_118' (op: 'Select') with input shapes: [?,16,?], [], [2,16,16]. Then, I use this website to show the network structure and find the the op onnx_tf_prefix_Where_118 with name Where_118. After checking my torch code, I found this operator corresponds to code:

attn = attn.masked_fill(mask, -np.inf)

So I think maybe masked_fill is not supported. Combining the example of the Where op from onnx, I change this line to:

attn = torch.where(mask, torch.full(mask.shape, -np.inf), attn)

And the problem is solved. Hope this helps. Good luck. 😊

OK. I'll verify it later. thank you!

keanucui avatar Apr 03 '23 06:04 keanucui