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

Merge branch 'feature/notebook_end_to_end_flow' of https://github.com/Xilinx/finn into dev

parents 04f59ea5 65dee8b9
No related branches found
No related tags found
No related merge requests found
......@@ -103,7 +103,9 @@ dma.sendchannel.wait()
dma.recvchannel.wait()
# unpack the packed output buffer from accelerator
obuf_folded = packed_bytearray_to_finnpy(obuf_packed, odt, oshape_folded)
obuf_folded = packed_bytearray_to_finnpy(
obuf_packed, odt, oshape_folded, reverse_endian=True
)
# convert to normal reshape and save
obuf_normal = obuf_folded.reshape(oshape_normal)
np.save("output.npy", obuf_normal)
......
......@@ -152,7 +152,7 @@ def unpack_innermost_dim_from_hex_string(
# interpret values as bipolar
if dtype == DataType.BIPOLAR:
ar_list = [2 * x - 1 for x in ar_list]
# interpret values as signed values
# interpret values as signed values
elif dtype.name.startswith("INT"):
mask = 2 ** (dtype.bitwidth() - 1)
ar_list = [-(x & mask) + (x & ~mask) for x in ar_list]
......@@ -277,7 +277,13 @@ def finnpy_to_packed_bytearray(ndarray, dtype):
return np.apply_along_axis(fn, packed_hexstring.ndim - 1, packed_hexstring)
def packed_bytearray_to_finnpy(packed_bytearray, dtype, output_shape=None, reverse_inner=False):
def packed_bytearray_to_finnpy(
packed_bytearray,
dtype,
output_shape=None,
reverse_inner=False,
reverse_endian=False,
):
"""Given a packed numpy uint8 ndarray, unpack it into a FINN array of
given DataType. output_shape can be specified to remove padding from the
packed dimension, or set to None to be inferred from the input."""
......@@ -296,10 +302,20 @@ def packed_bytearray_to_finnpy(packed_bytearray, dtype, output_shape=None, rever
assert packed_bits % target_bits == 0
n_target_elems = packed_bits // target_bits
output_shape = packed_bytearray.shape[:-1] + (n_target_elems,)
if reverse_endian and target_bits > 8:
# revse the endianness of each element
orig_shape = packed_bytearray.shape
assert target_bits % 8 == 0
target_bytes = target_bits // 8
new_shape = orig_shape[:-1] + (-1, target_bytes)
packed_bytearray = np.flip(packed_bytearray.reshape(new_shape), axis=-1)
packed_bytearray = packed_bytearray.reshape(orig_shape)
# convert innermost dim of byte array to hex strings
packed_hexstring = np.apply_along_axis(
npbytearray2hexstring, packed_dim, packed_bytearray
)
ret = unpack_innermost_dim_from_hex_string(packed_hexstring, dtype, output_shape, reverse_inner)
ret = unpack_innermost_dim_from_hex_string(
packed_hexstring, dtype, output_shape, reverse_inner
)
return ret
import shutil
import os
import shutil
import subprocess
import numpy as np
......@@ -61,7 +61,9 @@ def make_npy2apintstream_testcase(ndarray, dtype):
cmd_compile = """
g++ -o test_npy2apintstream test.cpp /workspace/cnpy/cnpy.cpp \
-I/workspace/cnpy/ -I{}/include -I/workspace/finn/src/finn/data/cpp \
--std=c++11 -lz""".format(os.environ["VIVADO_PATH"])
--std=c++11 -lz""".format(
os.environ["VIVADO_PATH"]
)
with open(test_dir + "/compile.sh", "w") as f:
f.write(cmd_compile)
compile = subprocess.Popen(
......@@ -191,3 +193,13 @@ def test_packed_bytearray_to_finnpy():
eE = np.asarray(eE, dtype=np.float32)
shapeE = eE.shape
assert (packed_bytearray_to_finnpy(E, DataType.INT32, shapeE) == eE).all()
F = np.asarray(
[[252, 255, 255, 255, 0, 0, 0, 0, 252, 255, 255, 255, 252, 255, 255, 255]],
dtype=np.uint8,
)
eF = [[-4, 0, -4, -4]]
eF = np.asarray(eE, dtype=np.float32)
shapeF = eF.shape
assert (
packed_bytearray_to_finnpy(F, DataType.INT32, shapeF, reverse_endian=True) == eF
).all()
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