From 521450098f15c3dccae54e56208b9d0902e63068 Mon Sep 17 00:00:00 2001 From: auphelia <jakobapk@web.de> Date: Mon, 27 Jan 2020 12:02:34 +0000 Subject: [PATCH] [Utils] Added additional arguments for unpack hexstring function to interpret pyverilator outputs correctly --- src/finn/core/utils.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/finn/core/utils.py b/src/finn/core/utils.py index 62ee9e900..398814005 100644 --- a/src/finn/core/utils.py +++ b/src/finn/core/utils.py @@ -112,7 +112,7 @@ def pack_innermost_dim_as_hex_string(ndarray, dtype, pad_to_nbits): def unpack_innermost_dim_from_hex_string( - data, shape, packedBits, targetBits, reverseInnerDim=False + data, dtype, shape, packedBits, targetBits, rtlsim=False ): # function expects flattens array and returns an array in the desired shape outer_dim_elems = 1 @@ -138,9 +138,17 @@ def unpack_innermost_dim_from_hex_string( elem_str = "".join(map(str, elem)) ar_list.append(int(elem_str, 2)) # reverse inner dimension back to "normal" positions - if reverseInnerDim is False: + if rtlsim is False: ar_list.reverse() - ar_list = [int(x) for x in ar_list] + else: + # interpret output values correctly by flattening and adjusting the output + if dtype == DataType.BIPOLAR: + ar_list = [2 * x - 1 for x in ar_list] + # pyverilator interprets int2 as uint2, so output has to be corrected + elif dtype == DataType.INT2 or dtype == DataType.INT32: + mask = 2 ** (dtype.bitwidth() - 1) + ar_list = [-(x & mask) + (x & ~mask) for x in ar_list] + array.append(ar_list) array = np.asarray(array, dtype=np.float32).reshape(shape) return array -- GitLab