From e05154e60dcf05c69408c5c951476df15c57ed44 Mon Sep 17 00:00:00 2001
From: Yaman Umuroglu <maltanar@gmail.com>
Date: Thu, 11 Aug 2022 16:22:26 +0200
Subject: [PATCH] [FIFO] also do accumulation as part of DeriveCharacteristic

---
 src/finn/custom_op/fpgadataflow/hlscustomop.py      |  2 +-
 .../fpgadataflow/derive_characteristic.py           | 13 +++++++++++++
 tests/fpgadataflow/test_fpgadataflow_mvau.py        |  6 +++---
 3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/finn/custom_op/fpgadataflow/hlscustomop.py b/src/finn/custom_op/fpgadataflow/hlscustomop.py
index 85f5bfd3f..bb359ef0b 100644
--- a/src/finn/custom_op/fpgadataflow/hlscustomop.py
+++ b/src/finn/custom_op/fpgadataflow/hlscustomop.py
@@ -111,7 +111,7 @@ class HLSCustomOp(CustomOp):
             "inFIFODepth": ("i", False, 2),
             "outFIFODepth": ("i", False, 2),
             "output_hook": ("s", False, ""),
-            # characterization of stream input-output behavior per cycle
+            # accumulated characteristic function over two periods
             "io_characteristic": ("ints", False, []),
             # the period for which the characterization was run
             "io_characteristic_period": ("i", False, 0),
diff --git a/src/finn/transformation/fpgadataflow/derive_characteristic.py b/src/finn/transformation/fpgadataflow/derive_characteristic.py
index 0a32ba7e7..72573f250 100644
--- a/src/finn/transformation/fpgadataflow/derive_characteristic.py
+++ b/src/finn/transformation/fpgadataflow/derive_characteristic.py
@@ -140,6 +140,19 @@ class DeriveCharacteristic(NodeLocalTransformation):
                     txns_in += [0 for x in range(self.period - len(txns_in))]
                 if len(txns_out) < self.period:
                     txns_out += [0 for x in range(self.period - len(txns_out))]
+
+                def accumulate_char_fxn(chrc):
+                    p = len(chrc)
+                    ret = []
+                    for t in range(2 * p):
+                        if t == 0:
+                            ret.append(chrc[0])
+                        else:
+                            ret.append(ret[-1] + chrc[t % p])
+                    return ret
+
+                txns_in = accumulate_char_fxn(txns_in)
+                txns_out = accumulate_char_fxn(txns_out)
                 io_characteristic = txns_in + txns_out
                 inst.set_nodeattr("io_characteristic", io_characteristic)
                 inst.set_nodeattr("io_characteristic_period", self.period)
diff --git a/tests/fpgadataflow/test_fpgadataflow_mvau.py b/tests/fpgadataflow/test_fpgadataflow_mvau.py
index 87c30a00b..22ff36f53 100644
--- a/tests/fpgadataflow/test_fpgadataflow_mvau.py
+++ b/tests/fpgadataflow/test_fpgadataflow_mvau.py
@@ -475,9 +475,9 @@ def test_fclayer_fifocharacterize(mem_mode, idt, wdt, act, nf, sf, mw, mh):
     period_attr = node_inst.get_nodeattr("io_characteristic_period")
     assert period_attr == exp_total_cycles
     chrc = node_inst.get_nodeattr("io_characteristic")
-    assert len(chrc) == 2 * exp_total_cycles
+    assert len(chrc) == 4 * exp_total_cycles
     chrc = np.asarray(chrc, dtype=np.uint8).reshape(2, -1)
     # first sf cycles should read input continuously
-    assert (chrc[0, :sf] == 1).all()
+    assert (chrc[0, :sf] == range(1, sf + 1)).all()
     # all outputs should be produced within the exp n of cycles
-    assert sum(chrc[1]) == nf
+    assert chrc[1, exp_total_cycles] == nf
-- 
GitLab