Repository: apache/tvm

Issue:
[Relay][ONNX] KeyError: ‘W’ when compiling from onnx
Dear tvm team

I'm aware that I'm not supposed to use this issue tracker as a first instance. However, in the community was no reaction and in the meantime the model I uploaded was deleted. So I added it here as an attachment, so it won't get lost anymore.
[yolo_failing.zip](https://github.com/dmlc/tvm/files/3140832/yolo_failing.zip)

The attached model leads to an exception during the compilation:

```python
 File "C:\src\xyz\src\tvm\compile_model.py", line 78, in compile_model
    relay_func, params = relay.frontend.from_onnx(onnx_model, shape=input_shape_dict)
  File "C:\Users\bmar\AppData\Roaming\Python\Python36\site-packages\tvm-0.6.dev0-py3.6-win-amd64.egg\tvm\relay\frontend\onnx.py", line 1139, in from_onnx
    sym, params = g.from_onnx(graph, opset)
  File "C:\Users\bmar\AppData\Roaming\Python\Python36\site-packages\tvm-0.6.dev0-py3.6-win-amd64.egg\tvm\relay\frontend\onnx.py", line 960, in from_onnx
    inputs = [self._nodes[self._renames.get(i, i)] for i in node.input]
  File "C:\Users\bmar\AppData\Roaming\Python\Python36\site-packages\tvm-0.6.dev0-py3.6-win-amd64.egg\tvm\relay\frontend\onnx.py", line 960, in <listcomp>
    inputs = [self._nodes[self._renames.get(i, i)] for i in node.input]
KeyError: 'W'
```


Discussion:
cross ref https://discuss.tvm.ai/t/relay-onnx-keyerror-w-when-compiling-from-onnx/2102/3

Given that you have a good idea about what is happening, can you attempt a fix to the problem?
I will clarify with my boss and come back to you.
When I load RNN onnx model to relay, I have encountered similar issue:


Traceback (most recent call last):
  File "from_onnx.py", line 96, in <module>
    sym, params = relay.frontend.from_onnx(onnx_model, shape_dict, dtype='float32')
  File "/tvm/python/tvm/relay/frontend/onnx.py", line 1144, in from_onnx
    sym, params = g.from_onnx(graph, opset)
  File "/tvm/python/tvm/relay/frontend/onnx.py", line 965, in from_onnx
    inputs = [self._nodes[self._renames.get(i, i)] for i in node.input]
  File "//tvm/python/tvm/relay/frontend/onnx.py", line 965, in <listcomp>
    inputs = [self._nodes[self._renames.get(i, i)] for i in node.input]
KeyError: ' '
Could you send the parameters to the `from_onnx` call? 

For example with:

```
import onnx
from tvm import relay

yolo = onnx.load("/Users/jroesch/Downloads/yolo_failing.onnx")
import pdb; pdb.set_trace()
# What shapes do we pass?
relay.frontend.from_onnx(yolo)
```
cc @zhreshold could you look into this?

Resolving patch:
diff --git a/python/tvm/relay/frontend/onnx.py b/python/tvm/relay/frontend/onnx.py
--- a/python/tvm/relay/frontend/onnx.py
+++ b/python/tvm/relay/frontend/onnx.py
@@ -934,7 +934,7 @@ def __init__(self, shape, dtype):
         self._renames = {}
         self._num_input = 0
         self._num_param = 0
-        self._shape = shape
+        self._shape = shape if shape else {}
         self._dtype = dtype
 
     def from_onnx(self, graph, opset):
@@ -966,6 +966,9 @@ def from_onnx(self, graph, opset):
             if not init_tensor.name.strip():
                 raise ValueError("Tensor's name is required.")
             self._params[init_tensor.name] = self._parse_array(init_tensor)
+            self._nodes[init_tensor.name] = new_var(init_tensor.name,
+                                                    shape=self._params[init_tensor.name].shape,
+                                                    dtype=self._params[init_tensor.name].dtype)
         for i in graph.input:
             # from onnx v0.2, GraphProto.input has type ValueInfoProto,
             #  and the name is 'i.name'
@@ -1179,6 +1182,18 @@ def from_onnx(model,
     params : dict of str to tvm.NDArray
         The parameter dict to be used by relay
     """
+    try:
+        import onnx
+        if hasattr(onnx.checker, 'check_model'):
+            # try use onnx's own model checker before converting any model
+            try:
+                onnx.checker.check_model(model)
+            except onnx.onnx_cpp2py_export.checker.ValidationError as e:
+                import warnings
+                # the checker is a bit violent about errors, so simply print warnings here
+                warnings.warn(str(e))
+    except ImportError:
+        pass
     g = GraphProto(shape, dtype)
     graph = model.graph
     try:


Test patch: