From 20b7b4f6ad6c8ae88b5facc150da3fd9a46c88e3 Mon Sep 17 00:00:00 2001 From: Yaman Umuroglu <maltanar@gmail.com> Date: Fri, 6 Dec 2019 02:22:24 +0000 Subject: [PATCH] [Util] add 0x to hex strings to bypass hls bug --- src/finn/core/utils.py | 4 ++-- tests/fpgadataflow/test_npy2hls.py | 32 +++++++++++++++--------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/finn/core/utils.py b/src/finn/core/utils.py index 0af44de3a..0ea2392d9 100644 --- a/src/finn/core/utils.py +++ b/src/finn/core/utils.py @@ -41,7 +41,7 @@ def random_string(stringLength=6): return "".join(random.choice(lettersAndDigits) for i in range(stringLength)) -def array2hexstring(array, dtype, pad_to_nbits): +def array2hexstring(array, dtype, pad_to_nbits, prefix="0x"): """ Pack given one-dimensional NumPy array with FINN DataType dtype into a hex string. @@ -85,7 +85,7 @@ def array2hexstring(array, dtype, pad_to_nbits): else: raise Exception("Number of bits is greater than pad_to_nbits") # represent as hex - return lineval.hex + return prefix + lineval.hex def pack_innermost_dim_as_hex_string(ndarray, dtype, pad_to_nbits): diff --git a/tests/fpgadataflow/test_npy2hls.py b/tests/fpgadataflow/test_npy2hls.py index f2956a85a..47b4eb410 100644 --- a/tests/fpgadataflow/test_npy2hls.py +++ b/tests/fpgadataflow/test_npy2hls.py @@ -94,23 +94,23 @@ def test_npy2apintstream_int2(): def test_array2hexstring(): - assert cutil.array2hexstring([1, 1, 1, 0], DataType.BINARY, 4) == "e" - assert cutil.array2hexstring([1, 1, 1, 0], DataType.BINARY, 8) == "0e" - assert cutil.array2hexstring([1, 1, 1, -1], DataType.BIPOLAR, 8) == "0e" - assert cutil.array2hexstring([3, 3, 3, 3], DataType.UINT2, 8) == "ff" - assert cutil.array2hexstring([1, 3, 3, 1], DataType.UINT2, 8) == "7d" - assert cutil.array2hexstring([1, -1, 1, -1], DataType.INT2, 8) == "77" - assert cutil.array2hexstring([1, 1, 1, -1], DataType.INT4, 16) == "111f" - assert cutil.array2hexstring([-1], DataType.FLOAT32, 32) == "bf800000" - assert cutil.array2hexstring([17.125], DataType.FLOAT32, 32) == "41890000" + assert cutil.array2hexstring([1, 1, 1, 0], DataType.BINARY, 4) == "0xe" + assert cutil.array2hexstring([1, 1, 1, 0], DataType.BINARY, 8) == "0x0e" + assert cutil.array2hexstring([1, 1, 1, -1], DataType.BIPOLAR, 8) == "0x0e" + assert cutil.array2hexstring([3, 3, 3, 3], DataType.UINT2, 8) == "0xff" + assert cutil.array2hexstring([1, 3, 3, 1], DataType.UINT2, 8) == "0x7d" + assert cutil.array2hexstring([1, -1, 1, -1], DataType.INT2, 8) == "0x77" + assert cutil.array2hexstring([1, 1, 1, -1], DataType.INT4, 16) == "0x111f" + assert cutil.array2hexstring([-1], DataType.FLOAT32, 32) == "0xbf800000" + assert cutil.array2hexstring([17.125], DataType.FLOAT32, 32) == "0x41890000" def test_pack_innermost_dim_as_hex_string(): A = [[1, 1, 1, 0], [0, 1, 1, 0]] - eA = np.asarray(["0e", "06"]) + eA = np.asarray(["0x0e", "0x06"]) assert (cutil.pack_innermost_dim_as_hex_string(A, DataType.BINARY, 8) == eA).all() B = [[[3, 3], [3, 3]], [[1, 3], [3, 1]]] - eB = np.asarray([["0f", "0f"], ["07", "0d"]]) + eB = np.asarray([["0x0f", "0x0f"], ["0x07", "0x0d"]]) assert (cutil.pack_innermost_dim_as_hex_string(B, DataType.UINT2, 8) == eB).all() @@ -121,15 +121,15 @@ def test_numpy_to_hls_code(): A = [[1, 1, 1, 0], [0, 1, 1, 0]] ret = numpy_to_hls_code(A, DataType.BINARY, "test", True) eA = """ap_uint<4> test[2] = - {ap_uint<4>("e", 16), ap_uint<4>("6", 16)};""" + {ap_uint<4>("0xe", 16), ap_uint<4>("0x6", 16)};""" assert remove_all_whitespace(ret) == remove_all_whitespace(eA) B = [[[3, 3], [3, 3]], [[1, 3], [3, 1]]] ret = numpy_to_hls_code(B, DataType.UINT2, "test", True) eB = """ap_uint<4> test[2][2] = - {{ap_uint<4>("f", 16), ap_uint<4>("f", 16)}, - {ap_uint<4>("7", 16), ap_uint<4>("d", 16)}};""" + {{ap_uint<4>("0xf", 16), ap_uint<4>("0xf", 16)}, + {ap_uint<4>("0x7", 16), ap_uint<4>("0xd", 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)}};""" + eB = """{{ap_uint<4>("0xf", 16), ap_uint<4>("0xf", 16)}, + {ap_uint<4>("0x7", 16), ap_uint<4>("0xd", 16)}};""" assert remove_all_whitespace(ret) == remove_all_whitespace(eB) -- GitLab