From 27b2852f5ac923d843aa8723874cd33be9d2f5c8 Mon Sep 17 00:00:00 2001
From: Yaman Umuroglu <yamanu@xilinx.com>
Date: Mon, 21 Oct 2019 18:03:42 +0100
Subject: [PATCH] [Exec] refactor valueinfo_to_tensor into utils

---
 src/finn/core/onnx_exec.py | 18 ++++--------------
 src/finn/core/utils.py     | 11 +++++++++++
 2 files changed, 15 insertions(+), 14 deletions(-)
 create mode 100644 src/finn/core/utils.py

diff --git a/src/finn/core/onnx_exec.py b/src/finn/core/onnx_exec.py
index 6b8c01958..ffe586096 100644
--- a/src/finn/core/onnx_exec.py
+++ b/src/finn/core/onnx_exec.py
@@ -26,25 +26,15 @@
 
 import copy
 
-import numpy as np
-import onnx
 import onnx.helper as helper
 import onnx.shape_inference as si
 import onnxruntime as rt
 from onnx import numpy_helper as np_helper
 
+import finn.core.utils as util
 import finn.transformation.general as tx
 
 
-def valueinfo_to_tensor(vi):
-    """Creates an all-zeroes numpy tensor from a ValueInfoProto."""
-
-    dims = [x.dim_value for x in vi.type.tensor_type.shape.dim]
-    return np.zeros(
-        dims, dtype=onnx.mapping.TENSOR_TYPE_TO_NP_TYPE[vi.type.tensor_type.elem_type]
-    )
-
-
 def execute_node(node, context, graph):
     """Call onnxruntime to execute a single node. Input/output provided via context."""
 
@@ -94,14 +84,14 @@ def execute_onnx(model, input_dict, return_full_exec_context=False):
     execution_context = dict()
     # make empty tensors for all the graph inputs and outputs
     for vi in graph.input:
-        new_tensor = valueinfo_to_tensor(vi)
+        new_tensor = util.valueinfo_to_tensor(vi)
         execution_context[vi.name] = new_tensor
     for vi in graph.output:
-        new_tensor = valueinfo_to_tensor(vi)
+        new_tensor = util.valueinfo_to_tensor(vi)
         execution_context[vi.name] = new_tensor
     # make empty tensors for all intermediate buffers
     for vi in graph.value_info:
-        new_tensor = valueinfo_to_tensor(vi)
+        new_tensor = util.valueinfo_to_tensor(vi)
         execution_context[vi.name] = new_tensor
     # fill in the constants provided by the initializers (TensorProto to npy)
     for t in graph.initializer:
diff --git a/src/finn/core/utils.py b/src/finn/core/utils.py
new file mode 100644
index 000000000..b49c3b223
--- /dev/null
+++ b/src/finn/core/utils.py
@@ -0,0 +1,11 @@
+import numpy as np
+import onnx
+
+
+def valueinfo_to_tensor(vi):
+    """Creates an all-zeroes numpy tensor from a ValueInfoProto."""
+
+    dims = [x.dim_value for x in vi.type.tensor_type.shape.dim]
+    return np.zeros(
+        dims, dtype=onnx.mapping.TENSOR_TYPE_TO_NP_TYPE[vi.type.tensor_type.elem_type]
+    )
-- 
GitLab