Skip to content
Snippets Groups Projects
Unverified Commit 3d08d626 authored by Yaman Umuroglu's avatar Yaman Umuroglu Committed by GitHub
Browse files

Merge pull request #139 from quetric/feature/LowerConvsToMatMul_no_im2col

Feature/lower convs to mat mul no im2col
parents c458760b 81628122
No related branches found
No related tags found
No related merge requests found
......@@ -80,14 +80,19 @@ class LowerConvsToMatMul(Transformation):
inp_trans_out = inp_trans_out.name
model.set_tensor_datatype(inp_trans_out, idt)
im2col_out = helper.make_tensor_value_info(
model.make_new_valueinfo_name(),
TensorProto.FLOAT,
(1, ofm_dim, ofm_dim, ifm_ch * k * k),
)
graph.value_info.append(im2col_out)
im2col_out = im2col_out.name
model.set_tensor_datatype(im2col_out, idt)
need_im2col = True
if k == 1 and pad == 0 and stride == 1:
need_im2col = False
if need_im2col:
im2col_out = helper.make_tensor_value_info(
model.make_new_valueinfo_name(),
TensorProto.FLOAT,
(1, ofm_dim, ofm_dim, ifm_ch * k * k),
)
graph.value_info.append(im2col_out)
im2col_out = im2col_out.name
model.set_tensor_datatype(im2col_out, idt)
matmul_out = helper.make_tensor_value_info(
model.make_new_valueinfo_name(),
......@@ -104,19 +109,23 @@ class LowerConvsToMatMul(Transformation):
"Transpose", [cnv_input], [inp_trans_out], perm=[0, 2, 3, 1]
)
# lower input tensor
im2col_node = helper.make_node(
"Im2Col",
[inp_trans_out],
[im2col_out],
domain="finn",
stride=stride,
kernel_size=k,
pad_amount=pad,
input_shape="(1,{},{},{})".format(ifm_dim, ifm_dim, ifm_ch),
)
matmul_input = inp_trans_out
if need_im2col:
matmul_input = im2col_out
im2col_node = helper.make_node(
"Im2Col",
[inp_trans_out],
[im2col_out],
domain="finn",
stride=stride,
kernel_size=k,
pad_amount=pad,
input_shape="(1,{},{},{})".format(ifm_dim, ifm_dim, ifm_ch),
)
# do matmul
matmul_node = helper.make_node(
"MatMul", [im2col_out, weight_name], [matmul_out]
"MatMul", [matmul_input, weight_name], [matmul_out]
)
# NHWC -> NCHW
out_trans_node = helper.make_node(
......@@ -124,9 +133,13 @@ class LowerConvsToMatMul(Transformation):
)
# insert nodes where the conv is to preserve topological ordering
graph.node.insert(node_ind, inp_trans_node)
graph.node.insert(node_ind + 1, im2col_node)
graph.node.insert(node_ind + 2, matmul_node)
graph.node.insert(node_ind + 3, out_trans_node)
if need_im2col:
graph.node.insert(node_ind + 1, im2col_node)
graph.node.insert(node_ind + 2, matmul_node)
graph.node.insert(node_ind + 3, out_trans_node)
else:
graph.node.insert(node_ind + 1, matmul_node)
graph.node.insert(node_ind + 2, out_trans_node)
# remove old nodes
graph.node.remove(n)
model = model.transform(InferShapes())
......
......@@ -26,12 +26,13 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import onnx.helper as oh
from onnx import TensorProto
import os
import pkg_resources as pk
import brevitas.onnx as bo
import numpy as np
from finn.core.modelwrapper import ModelWrapper
from finn.transformation.fold_constants import FoldConstants
from finn.transformation.infer_shapes import InferShapes
......@@ -65,3 +66,51 @@ def test_conv_lowering_cnv_w1a1():
assert np.isclose(produced, expected).all()
assert np.argmax(produced) == 3
os.remove(export_onnx_path)
def test_conv_lowering_conv_1x1():
np.random.seed(0)
in_feature_dim = 7
in_chn = 3
kernel_size = 1
out_feature_dim = in_feature_dim
input_shape = [1, in_chn, in_feature_dim, in_feature_dim]
output_shape = [1, in_chn, out_feature_dim, out_feature_dim]
conv_param_shape = [in_chn, in_chn, kernel_size, kernel_size]
conv_config = {}
conv_config["dilations"] = [1, 1]
conv_config["group"] = 1
conv_config["kernel_shape"] = [kernel_size, kernel_size]
conv_config["pads"] = [0, 0, 0, 0]
conv_config["strides"] = [1, 1]
top_in = oh.make_tensor_value_info("top_in", TensorProto.FLOAT, input_shape)
top_out = oh.make_tensor_value_info("top_out", TensorProto.FLOAT, output_shape)
value_info = [oh.make_tensor_value_info("p1", TensorProto.FLOAT, conv_param_shape)]
modelproto = oh.make_model(
oh.make_graph(
name="test",
inputs=[top_in],
outputs=[top_out],
value_info=value_info,
nodes=[oh.make_node("Conv", ["top_in", "p1"], ["top_out"], **conv_config)],
)
)
model = ModelWrapper(modelproto)
model = model.transform(InferShapes())
model.set_initializer("p1", np.random.rand(*conv_param_shape).astype(np.float32))
new_model = model.transform(LowerConvsToMatMul())
inp_dict = {"top_in": np.random.rand(*input_shape).astype(np.float32)}
assert oxe.compare_execution(model, new_model, inp_dict)
assert new_model.graph.node[0].op_type == "Transpose"
assert new_model.graph.node[1].op_type == "MatMul"
assert new_model.graph.node[2].op_type == "Transpose"
assert len(new_model.graph.node) == 3
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment