From ffce886ff103b485646b1cac5b894ff8add2bf44 Mon Sep 17 00:00:00 2001 From: Yaman Umuroglu <yamanu@xilinx.com> Date: Fri, 12 Feb 2021 00:56:38 +0100 Subject: [PATCH] [ConvertToHLS] fix conversion for bipolar outputs --- .../fpgadataflow/convert_to_hls_layers.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/finn/transformation/fpgadataflow/convert_to_hls_layers.py b/src/finn/transformation/fpgadataflow/convert_to_hls_layers.py index 39b17e3e6..a221b510a 100644 --- a/src/finn/transformation/fpgadataflow/convert_to_hls_layers.py +++ b/src/finn/transformation/fpgadataflow/convert_to_hls_layers.py @@ -566,7 +566,7 @@ class InferQuantizedStreamingFCLayer(Transformation): wmem = mw * mh // (pe * simd) assert ( mw * mh == wmem * pe * simd - ), """Requirement (MW * MH) divisiable by + ), """Requirement (MW * MH) divisible by (WMEM * PE * SIMD) is violated.""" # see if we have any following thresholds consumer = model.find_consumer(mm_output) @@ -583,20 +583,27 @@ class InferQuantizedStreamingFCLayer(Transformation): thresholds neither 1 nor MH.""" odt = model.get_tensor_datatype(mt_output) scale = getCustomOp(consumer).get_nodeattr("out_scale") - bipolar_ok = odt == DataType.BIPOLAR and scale == 2.0 - assert ( - scale == 1.0 or bipolar_ok - ), "out_scale must be equal to 1.0 for HLS conversion." actval = getCustomOp(consumer).get_nodeattr("out_bias") assert ( int(actval) == actval ), "out_bias must be integer for HLS conversion." actval = int(actval) + odt_is_bipolar = odt == DataType.BIPOLAR + bipolar_ok = ( + odt_is_bipolar and (scale == 2.0) and (actval == -1) + ) + assert ( + scale == 1.0 or bipolar_ok + ), "out_scale = 1.0 or bipolar output needed for conversion." assert (not odt.signed()) or ( actval < 0 ), "Signed output requres actval < 0" model.set_tensor_shape(mm_input, mm_in_shape) model.set_tensor_shape(mt_output, mt_out_shape) + if bipolar_ok: + # remove bias for bipolar, since + # binary->bipolar is achieved by reinterpretation + actval = 0 # create and insert new StreamingFCLayer node new_node = helper.make_node( "StreamingFCLayer_Batch", -- GitLab