diff --git a/src/finn/core/utils.py b/src/finn/core/utils.py index 62ee9e9000806b46c0b39868606a1b3dc1050ba8..398814005a2eb26d48b848004ad3cf35fc311a7d 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