Skip to content
Snippets Groups Projects
Commit ddd4fdbf authored by Yaman Umuroglu's avatar Yaman Umuroglu
Browse files

[ConvertBipolarMatMulToXnorPopcount] support bipolar -> binary inp

parent b805e0bd
No related branches found
No related tags found
No related merge requests found
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import numpy as np import numpy as np
import warnings
from onnx import TensorProto from onnx import TensorProto
from onnx import helper as oh from onnx import helper as oh
...@@ -66,26 +67,40 @@ class ConvertBipolarMatMulToXnorPopcount(Transformation): ...@@ -66,26 +67,40 @@ class ConvertBipolarMatMulToXnorPopcount(Transformation):
mt_chain = model.find_upstream(mm_input, find_prod_mt) mt_chain = model.find_upstream(mm_input, find_prod_mt)
if len(mt_chain) == 0: if len(mt_chain) == 0:
raise Exception( if mm_input == graph.input[0].name:
"""Could not find upstream bipolar # change input datatype to BINARY
MultiThreshold""" model.set_tensor_datatype(mm_input, DataType.BINARY)
) graph_modified = True
graph_modified = True warnings.warn(
mt = mt_chain[-1] """IMPORTANT: Changing graph input DataType
mt_inst = getCustomOp(mt) to BINARY instead of BIPOLAR. Ensure this is respected
# ensure old scale/bias were correct for BIPOLAR when checking for correctness.
scale_ok = mt_inst.get_nodeattr("out_scale") == 2.0 """
bias_ok = mt_inst.get_nodeattr("out_bias") == -1.0 )
assert ( else:
scale_ok and bias_ok raise Exception(
), """Unexpected scale/bias """Could not find upstream bipolar
attributes for BIPOLAR MultiThreshold node.""" MultiThreshold, and the MatMul is not the
# start conversion, set MT output to binary first node on graph input. Unable to convert
# (this is what XnorPopcountMatMul expects) input tensor from BIPOLAR to BINARY."""
mt_inst.set_nodeattr("out_dtype", "BINARY") )
mt_inst.set_nodeattr("out_scale", 1.0) else:
mt_inst.set_nodeattr("out_bias", 0.0) graph_modified = True
model.set_tensor_datatype(mm_input, DataType.BINARY) mt = mt_chain[-1]
mt_inst = getCustomOp(mt)
# ensure old scale/bias were correct for BIPOLAR
scale_ok = mt_inst.get_nodeattr("out_scale") == 2.0
bias_ok = mt_inst.get_nodeattr("out_bias") == -1.0
assert (
scale_ok and bias_ok
), """Unexpected scale/bias
attributes for BIPOLAR MultiThreshold node."""
# start conversion, set MT output to binary
# (this is what XnorPopcountMatMul expects)
mt_inst.set_nodeattr("out_dtype", "BINARY")
mt_inst.set_nodeattr("out_scale", 1.0)
mt_inst.set_nodeattr("out_bias", 0.0)
model.set_tensor_datatype(mm_input, DataType.BINARY)
# change node type and domain # change node type and domain
n.op_type = "XnorPopcountMatMul" n.op_type = "XnorPopcountMatMul"
n.domain = "finn" n.domain = "finn"
......
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