From bf5d91245af26376bc124d558db0bbde6de99858 Mon Sep 17 00:00:00 2001
From: Hendrik Borras <hendrikborras@web.de>
Date: Thu, 7 Oct 2021 17:42:12 +0100
Subject: [PATCH] Correct for fp accuracy issues during Quant constant folding.

---
 src/finn/transformation/qonnx/convert_qonnx_to_finn.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/finn/transformation/qonnx/convert_qonnx_to_finn.py b/src/finn/transformation/qonnx/convert_qonnx_to_finn.py
index f4c69ca53..5984082a4 100644
--- a/src/finn/transformation/qonnx/convert_qonnx_to_finn.py
+++ b/src/finn/transformation/qonnx/convert_qonnx_to_finn.py
@@ -26,6 +26,7 @@
 # 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 numpy as np
 from onnx import TensorProto, helper
 from qonnx.transformation.quant_constant_folding import FoldTransposeIntoQuantInit
 
@@ -139,7 +140,10 @@ class FoldQuantWeights(Transformation):
                         # For buth mul and Add:
                         # Move the scale factor behind the next operator
                         scale = model.get_initializer(n.input[1])
-                        model.set_initializer(node_out, q_node_output / scale)
+                        new_initializer = q_node_output / scale
+                        # Round, to correct for floating point errors
+                        new_initializer = np.round(new_initializer)
+                        model.set_initializer(node_out, new_initializer)
                         new_dtype = DataType[dtype.name.replace("SCALED", "")]
                         model.set_tensor_datatype(node_out, new_dtype)
 
-- 
GitLab