From 1663022fb8508cf5fe4cf5b988b1361676167033 Mon Sep 17 00:00:00 2001 From: Yaman Umuroglu <yamanu@xilinx.com> Date: Mon, 11 Oct 2021 15:41:28 +0200 Subject: [PATCH] [Refactor] use RandomNormal for faster/more compact shape inference --- src/finn/custom_op/fpgadataflow/addstreams_batch.py | 13 +++---------- .../custom_op/fpgadataflow/channelwise_op_batch.py | 12 +++--------- .../fpgadataflow/convolutioninputgenerator.py | 12 +++--------- .../fpgadataflow/convolutioninputgenerator1d.py | 13 +++---------- src/finn/custom_op/fpgadataflow/downsampler.py | 13 +++---------- src/finn/custom_op/fpgadataflow/fmpadding_batch.py | 13 +++---------- .../custom_op/fpgadataflow/globalaccpool_batch.py | 13 +++---------- src/finn/custom_op/fpgadataflow/iodma.py | 13 +++---------- .../custom_op/fpgadataflow/labelselect_batch.py | 12 +++--------- src/finn/custom_op/fpgadataflow/pool_batch.py | 13 +++---------- .../streamingdatawidthconverter_batch.py | 13 +++---------- .../fpgadataflow/streamingfclayer_batch.py | 13 +++---------- src/finn/custom_op/fpgadataflow/streamingfifo.py | 13 +++---------- .../fpgadataflow/streamingmaxpool_batch.py | 13 +++---------- .../custom_op/fpgadataflow/thresholding_batch.py | 13 +++---------- src/finn/custom_op/fpgadataflow/upsampler.py | 13 +++---------- .../fpgadataflow/vector_vector_activate_batch.py | 13 +++---------- 17 files changed, 51 insertions(+), 167 deletions(-) diff --git a/src/finn/custom_op/fpgadataflow/addstreams_batch.py b/src/finn/custom_op/fpgadataflow/addstreams_batch.py index 856f84fae..aaf8687de 100644 --- a/src/finn/custom_op/fpgadataflow/addstreams_batch.py +++ b/src/finn/custom_op/fpgadataflow/addstreams_batch.py @@ -29,7 +29,7 @@ import numpy as np import os import warnings -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -84,18 +84,11 @@ class AddStreams_Batch(HLSCustomOp): assert ishape == exp_ishape, "Unexpected input1 shape." ishape = tuple(model.get_tensor_shape(self.onnx_node.input[1])) assert ishape == exp_ishape, "Unexpected input2 shape." - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/channelwise_op_batch.py b/src/finn/custom_op/fpgadataflow/channelwise_op_batch.py index 073d6620a..b274bb8dc 100644 --- a/src/finn/custom_op/fpgadataflow/channelwise_op_batch.py +++ b/src/finn/custom_op/fpgadataflow/channelwise_op_batch.py @@ -30,7 +30,7 @@ import numpy as np import os import warnings from math import ceil -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -125,17 +125,11 @@ class ChannelwiseOp_Batch(HLSCustomOp): def make_shape_compatible_op(self, model): oshape = self.get_normal_output_shape() # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/convolutioninputgenerator.py b/src/finn/custom_op/fpgadataflow/convolutioninputgenerator.py index 9ec7bc662..8566ee035 100644 --- a/src/finn/custom_op/fpgadataflow/convolutioninputgenerator.py +++ b/src/finn/custom_op/fpgadataflow/convolutioninputgenerator.py @@ -29,7 +29,7 @@ import math import numpy as np import os -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -148,17 +148,11 @@ class ConvolutionInputGenerator(HLSCustomOp): ishape = tuple(model.get_tensor_shape(self.onnx_node.input[0])) assert ishape == exp_ishape, "Unexpect input shape for ConvInpGen." # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/convolutioninputgenerator1d.py b/src/finn/custom_op/fpgadataflow/convolutioninputgenerator1d.py index b428210ac..c76cb47d2 100644 --- a/src/finn/custom_op/fpgadataflow/convolutioninputgenerator1d.py +++ b/src/finn/custom_op/fpgadataflow/convolutioninputgenerator1d.py @@ -29,7 +29,7 @@ import math import numpy as np import os -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -137,18 +137,11 @@ class ConvolutionInputGenerator1D(HLSCustomOp): oshape = self.get_normal_output_shape() ishape = tuple(model.get_tensor_shape(self.onnx_node.input[0])) assert ishape == exp_ishape, "Unexpect input shape for ConvInpGen." - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/downsampler.py b/src/finn/custom_op/fpgadataflow/downsampler.py index 2313ab28b..de251a848 100644 --- a/src/finn/custom_op/fpgadataflow/downsampler.py +++ b/src/finn/custom_op/fpgadataflow/downsampler.py @@ -1,7 +1,7 @@ import numpy as np import os import warnings -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -83,18 +83,11 @@ class DownSampler(HLSCustomOp): oshape = self.get_normal_output_shape() ishape = tuple(model.get_tensor_shape(self.onnx_node.input[0])) assert ishape == exp_ishape, "Unexpect input shape for DownSampler." - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/fmpadding_batch.py b/src/finn/custom_op/fpgadataflow/fmpadding_batch.py index ca0b2f12a..ec2a2b690 100644 --- a/src/finn/custom_op/fpgadataflow/fmpadding_batch.py +++ b/src/finn/custom_op/fpgadataflow/fmpadding_batch.py @@ -1,7 +1,7 @@ import numpy as np import os import warnings -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -99,18 +99,11 @@ class FMPadding_Batch(HLSCustomOp): oshape = self.get_normal_output_shape() ishape = tuple(model.get_tensor_shape(self.onnx_node.input[0])) assert ishape == exp_ishape, "Unexpect input shape for SameResize." - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/globalaccpool_batch.py b/src/finn/custom_op/fpgadataflow/globalaccpool_batch.py index eabdcf599..d6f860db1 100644 --- a/src/finn/custom_op/fpgadataflow/globalaccpool_batch.py +++ b/src/finn/custom_op/fpgadataflow/globalaccpool_batch.py @@ -29,7 +29,7 @@ import numpy as np import os import warnings -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -95,18 +95,11 @@ class GlobalAccPool_Batch(HLSCustomOp): oshape = self.get_normal_output_shape() ishape = tuple(model.get_tensor_shape(self.onnx_node.input[0])) assert ishape == exp_ishape, "Unexpected input shape." - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten(), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/iodma.py b/src/finn/custom_op/fpgadataflow/iodma.py index 4fa74e35d..34842181c 100644 --- a/src/finn/custom_op/fpgadataflow/iodma.py +++ b/src/finn/custom_op/fpgadataflow/iodma.py @@ -29,7 +29,7 @@ import math import numpy as np import warnings -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -146,18 +146,11 @@ class IODMA(HLSCustomOp): oshape = self.get_normal_output_shape() ishape = tuple(model.get_tensor_shape(self.onnx_node.input[0])) assert ishape == exp_ishape, "Unexpected input shape." - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/labelselect_batch.py b/src/finn/custom_op/fpgadataflow/labelselect_batch.py index d70d0f6a9..4d34de8fc 100644 --- a/src/finn/custom_op/fpgadataflow/labelselect_batch.py +++ b/src/finn/custom_op/fpgadataflow/labelselect_batch.py @@ -102,18 +102,12 @@ class LabelSelect_Batch(HLSCustomOp): oshape = self.get_normal_output_shape() ishape = tuple(model.get_tensor_shape(self.onnx_node.input[0])) assert ishape == exp_ishape, "Unexpected input shape." - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.int64) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.INT64, - dims=values.shape, - vals=values.flatten(), - ), + shape=list(oshape), + dtype=TensorProto.INT64, ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/pool_batch.py b/src/finn/custom_op/fpgadataflow/pool_batch.py index cef964acd..5bcc65359 100644 --- a/src/finn/custom_op/fpgadataflow/pool_batch.py +++ b/src/finn/custom_op/fpgadataflow/pool_batch.py @@ -28,7 +28,7 @@ import numpy as np import os -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -163,18 +163,11 @@ class Pool_Batch(HLSCustomOp): oshape = self.get_normal_output_shape() ishape = tuple(model.get_tensor_shape(self.onnx_node.input[0])) assert ishape == exp_ishape, "Unexpected input shape for Pool_Batch." - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/streamingdatawidthconverter_batch.py b/src/finn/custom_op/fpgadataflow/streamingdatawidthconverter_batch.py index 67e3cd365..aab87be52 100644 --- a/src/finn/custom_op/fpgadataflow/streamingdatawidthconverter_batch.py +++ b/src/finn/custom_op/fpgadataflow/streamingdatawidthconverter_batch.py @@ -30,7 +30,7 @@ import math import numpy as np import os import warnings -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -165,18 +165,11 @@ class StreamingDataWidthConverter_Batch(HLSCustomOp): oshape = self.get_normal_output_shape() ishape = tuple(model.get_tensor_shape(self.onnx_node.input[0])) assert ishape == tuple(exp_ishape), "Unexpect input shape for StreamingDWC." - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/streamingfclayer_batch.py b/src/finn/custom_op/fpgadataflow/streamingfclayer_batch.py index 96594d441..2f1097d71 100644 --- a/src/finn/custom_op/fpgadataflow/streamingfclayer_batch.py +++ b/src/finn/custom_op/fpgadataflow/streamingfclayer_batch.py @@ -31,7 +31,7 @@ import numpy as np import os import textwrap import warnings -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -151,18 +151,11 @@ class StreamingFCLayer_Batch(HLSCustomOp): def make_shape_compatible_op(self, model): oshape = self.get_normal_output_shape() - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/streamingfifo.py b/src/finn/custom_op/fpgadataflow/streamingfifo.py index 9653d698f..ce09d5565 100644 --- a/src/finn/custom_op/fpgadataflow/streamingfifo.py +++ b/src/finn/custom_op/fpgadataflow/streamingfifo.py @@ -30,7 +30,7 @@ import numpy as np import os import subprocess import warnings -from onnx import TensorProto, helper +from onnx import helper from shutil import copy from finn.core.datatype import DataType @@ -78,18 +78,11 @@ class StreamingFIFO(HLSCustomOp): oshape = self.get_normal_output_shape() ishape = tuple(model.get_tensor_shape(self.onnx_node.input[0])) assert ishape == tuple(exp_ishape), "Unexpect input shape for StreamingFIFO." - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/streamingmaxpool_batch.py b/src/finn/custom_op/fpgadataflow/streamingmaxpool_batch.py index 19a42fe2d..c0641ffb7 100644 --- a/src/finn/custom_op/fpgadataflow/streamingmaxpool_batch.py +++ b/src/finn/custom_op/fpgadataflow/streamingmaxpool_batch.py @@ -29,7 +29,7 @@ import numpy as np import os import warnings -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -140,18 +140,11 @@ class StreamingMaxPool_Batch(HLSCustomOp): oshape = self.get_normal_output_shape() ishape = tuple(model.get_tensor_shape(self.onnx_node.input[0])) assert ishape == exp_ishape, "Unexpect input shape for StreamingMaxPool." - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/thresholding_batch.py b/src/finn/custom_op/fpgadataflow/thresholding_batch.py index 7fb7634dc..df562f545 100644 --- a/src/finn/custom_op/fpgadataflow/thresholding_batch.py +++ b/src/finn/custom_op/fpgadataflow/thresholding_batch.py @@ -31,7 +31,7 @@ import os import textwrap import warnings from math import ceil, log2 -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -112,18 +112,11 @@ class Thresholding_Batch(HLSCustomOp): def make_shape_compatible_op(self, model): oshape = self.get_normal_output_shape() - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/upsampler.py b/src/finn/custom_op/fpgadataflow/upsampler.py index e8aa09b1c..fcdf9d7d2 100644 --- a/src/finn/custom_op/fpgadataflow/upsampler.py +++ b/src/finn/custom_op/fpgadataflow/upsampler.py @@ -1,7 +1,7 @@ import numpy as np import os import warnings -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -69,18 +69,11 @@ class UpsampleNearestNeighbour_Batch(HLSCustomOp): assert ( ishape == exp_ishape ), "Unexpect input shape for UpsampleNearestNeighbour_Batch." - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): diff --git a/src/finn/custom_op/fpgadataflow/vector_vector_activate_batch.py b/src/finn/custom_op/fpgadataflow/vector_vector_activate_batch.py index 9fc133b9b..d9e7e566e 100644 --- a/src/finn/custom_op/fpgadataflow/vector_vector_activate_batch.py +++ b/src/finn/custom_op/fpgadataflow/vector_vector_activate_batch.py @@ -2,7 +2,7 @@ import math import numpy as np import os import warnings -from onnx import TensorProto, helper +from onnx import helper from finn.core.datatype import DataType from finn.custom_op.fpgadataflow.hlscustomop import HLSCustomOp @@ -129,18 +129,11 @@ class Vector_Vector_Activate_Batch(HLSCustomOp): def make_shape_compatible_op(self, model): oshape = self.get_normal_output_shape() - # implement tensor with correct shape - values = np.random.randn(*oshape).astype(np.float32) return helper.make_node( - "Constant", + "RandomNormal", inputs=[], outputs=[self.onnx_node.output[0]], - value=helper.make_tensor( - name="const_tensor", - data_type=TensorProto.FLOAT, - dims=values.shape, - vals=values.flatten().astype(float), - ), + shape=list(oshape), ) def infer_node_datatype(self, model): -- GitLab