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

[Util] add 0x to hex strings to bypass hls bug

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