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

[Util, Test] allow numpy_to_hls_code to produce no var name/type

parent b18a9c31
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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)
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