From f5960a9c69b4f7f3c40f7960ff60224074470e2a Mon Sep 17 00:00:00 2001
From: Yaman Umuroglu <maltanar@gmail.com>
Date: Thu, 21 Nov 2019 22:12:55 +0000
Subject: [PATCH] [Util, Test] allow numpy_to_hls_code to produce no var
 name/type

---
 src/finn/backend/fpgadataflow/utils.py | 13 ++++++++++---
 tests/test_npy2hls.py                  |  4 ++++
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/finn/backend/fpgadataflow/utils.py b/src/finn/backend/fpgadataflow/utils.py
index b02682377..0f3049ec7 100644
--- a/src/finn/backend/fpgadataflow/utils.py
+++ b/src/finn/backend/fpgadataflow/utils.py
@@ -6,11 +6,15 @@ from finn.core.datatype import DataType
 from finn.core.utils import pack_innermost_dim_as_hex_string
 
 
-def numpy_to_hls_code(ndarray, dtype, hls_var_name, pack_innermost_dim=True):
+def numpy_to_hls_code(
+    ndarray, dtype, hls_var_name, pack_innermost_dim=True, no_decl=False
+):
     """Return C++ code representation of a numpy ndarray with FINN DataType
     dtype, using hls_var_name as the resulting C++ variable name. If
     pack_innermost_dim is specified, the innermost dimension of the ndarray
-    will be packed into a hex string using array2hexstring.
+    will be packed into a hex string using array2hexstring. If no_decl is
+    set to True, no variable name and type will be generated as part of the
+    emitted string.
     """
     hls_dtype = dtype.get_hls_datatype_str()
     if type(ndarray) != np.ndarray or ndarray.dtype != np.float32:
@@ -47,5 +51,8 @@ def numpy_to_hls_code(ndarray, dtype, hls_var_name, pack_innermost_dim=True):
     strarr = np.array2string(ndarray, separator=", ", formatter={"all": elem2str})
     np.set_printoptions(**orig_printops)
     strarr = strarr.replace("[", "{").replace("]", "}")
-    ret = ret + " = \n" + strarr + ";"
+    if no_decl:
+        ret = strarr + ";"
+    else:
+        ret = ret + " = \n" + strarr + ";"
     return ret
diff --git a/tests/test_npy2hls.py b/tests/test_npy2hls.py
index ea54d900a..34893251e 100644
--- a/tests/test_npy2hls.py
+++ b/tests/test_npy2hls.py
@@ -41,3 +41,7 @@ def test_numpy_to_hls_code():
     {{ap_uint<4>("f", 16), ap_uint<4>("f", 16)},
      {ap_uint<4>("7", 16), ap_uint<4>("d", 16)}};"""
     assert remove_all_whitespace(ret) == remove_all_whitespace(eB)
+    ret = numpy_to_hls_code(B, DataType.UINT2, "test", True, True)
+    eB = """{{ap_uint<4>("f", 16), ap_uint<4>("f", 16)},
+     {ap_uint<4>("7", 16), ap_uint<4>("d", 16)}};"""
+    assert remove_all_whitespace(ret) == remove_all_whitespace(eB)
-- 
GitLab