From 1ebc02cd594b1d7fdb385a9516523fdc7bfb19a1 Mon Sep 17 00:00:00 2001
From: Yaman Umuroglu <yamanu@xilinx.com>
Date: Fri, 23 Aug 2019 16:35:35 +0000
Subject: [PATCH] use numpy arrays instead of TensorProto for storage

---
 src/finn/onnx_exec.py | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)

diff --git a/src/finn/onnx_exec.py b/src/finn/onnx_exec.py
index b7b98caea..86b3caa6e 100644
--- a/src/finn/onnx_exec.py
+++ b/src/finn/onnx_exec.py
@@ -33,18 +33,10 @@ model = onnx.load_model("model.onnx")
 graph = model.graph
 
 def valueinfo_to_tensor(vi):
-  """Creates an empty TensorProto from a ValueInfoProto."""
+  """Creates an all-zeroes numpy tensor from a ValueInfoProto."""
 
   dims = [x.dim_value for x in vi.type.tensor_type.shape.dim]
-  n_elems = reduce(lambda x,y: x*y, dims, 1)
-  return helper.make_tensor(
-    name = vi.name,
-    #data_type = vi.type.tensor_type.elem_type,
-    data_type = 1,
-    dims = dims,
-    vals = np.zeros((n_elems,)),  # TODO always float32 for now - respect type?
-    raw = False
-  )
+  return np.zeros(dims, dtype = onnx.mapping.TENSOR_TYPE_TO_NP_TYPE[vi.type.tensor_type.elem_type])
 
 # first, we need to make sure that every variable required by the graph has
 # some buffer associated with it. this includes graph inputs (which includes
@@ -55,19 +47,19 @@ execution_context = dict()
 # make empty tensors for all the graph inputs and outputs
 for vi in graph.input:
   new_tensor = valueinfo_to_tensor(vi)
-  execution_context[new_tensor.name] = new_tensor
+  execution_context[vi.name] = new_tensor
 for vi in graph.output:
   new_tensor = valueinfo_to_tensor(vi)
-  execution_context[new_tensor.name] = new_tensor
+  execution_context[vi.name] = new_tensor
 # make empty tensors for all intermediate buffers
 # TODO are we guaranteed to have the .value_info filled?
 # do we need to call ONNX shape inference first?
 for vi in graph.value_info:
   new_tensor = valueinfo_to_tensor(vi)
-  execution_context[new_tensor.name] = new_tensor
-# fill in the constants provided by the initializers
+  execution_context[vi.name] = new_tensor
+# fill in the constants provided by the initializers (TensorProto to npy)
 for t in graph.initializer:
-  execution_context[t.name] = t
+  execution_context[t.name] = np_helper.to_array(t)
 
 # now call each node in the graph nodes list
 # we can simply walk down the list since the ONNX spec guarantees that it is
-- 
GitLab