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

[Test] add make_npy2apintstream_testcase and use it for bin test

parent d06100f1
No related branches found
No related tags found
No related merge requests found
import shutil
import subprocess
import tempfile as tmp
import numpy as np
from finn.backend.fpgadataflow.utils import numpy_to_hls_code
......@@ -5,6 +9,78 @@ from finn.core.datatype import DataType
from finn.core.utils import array2hexstring, pack_innermost_dim_as_hex_string
def make_npy2apintstream_testcase(ndarray, dtype):
test_dir = tmp.mkdtemp(prefix="test_npy2apintstream_")
shape = ndarray.shape
elem_bits = dtype.bitwidth()
packed_bits = shape[-1] * elem_bits
packed_hls_type = "ap_uint<%d>" % packed_bits
elem_hls_type = dtype.get_hls_datatype_str()
npy_in = test_dir + "/in.npy"
npy_out = test_dir + "/out.npy"
# restrict the np datatypes we can handle
npyt_to_ct = {
"float32": "float",
"float64": "double",
"int8": "int8_t",
"int32": "int32_t",
"int64": "int64_t",
"uint8": "uint8_t",
"uint32": "uint32_t",
"uint64": "uint64_t",
}
npy_type = npyt_to_ct[str(ndarray.dtype)]
shape_cpp_str = str(shape).replace("(", "{").replace(")", "}")
assert dtype.signed() is False
test_app_string = []
test_app_string += ['#include "ap_int.h"']
test_app_string += ['#include "stdint.h"']
test_app_string += ['#include "hls_stream.h"']
test_app_string += ['#include "cnpy.h"']
test_app_string += ['#include "npy2apintstream.hpp"']
test_app_string += ["int main(int argc, char *argv[]) {"]
test_app_string += ["hls::stream<%s> teststream;" % packed_hls_type]
test_app_string += [
'npy2apintstream<%s, %s, %d, %s>("%s", teststream);'
% (packed_hls_type, elem_hls_type, elem_bits, npy_type, npy_in)
]
test_app_string += [
'apintstream2npy<%s, %s, %d, %s>(teststream, %s, "%s");'
% (packed_hls_type, elem_hls_type, elem_bits, npy_type, shape_cpp_str, npy_out)
]
test_app_string += ["return 0;"]
test_app_string += ["}"]
with open(test_dir + "/test.cpp", "w") as f:
f.write("\n".join(test_app_string))
cmd_compile = """
g++ -o test_npy2apintstream test.cpp /workspace/cnpy/cnpy.cpp \
-I/workspace/cnpy/ -I/workspace/vivado-hlslib -I/workspace/finn/src/finn/data/cpp \
--std=c++11 -lz"""
with open(test_dir + "/compile.sh", "w") as f:
f.write(cmd_compile)
compile = subprocess.Popen(
["sh", "compile.sh"], stdout=subprocess.PIPE, cwd=test_dir
)
(stdout, stderr) = compile.communicate()
np.save(npy_in, ndarray)
execute = subprocess.Popen(
"./test_npy2apintstream", stdout=subprocess.PIPE, cwd=test_dir
)
(stdout, stderr) = execute.communicate()
produced = np.load(npy_out)
success = (produced == ndarray).all()
# only delete generated code if test has passed
# useful for debug otherwise
if success:
shutil.rmtree(test_dir)
assert success
def test_npy2apintstream_binary():
W = np.random.randint(2, size=(1, 2, 4)).astype(np.float32)
make_npy2apintstream_testcase(W, DataType.BINARY)
def test_array2hexstring():
assert array2hexstring([1, 1, 1, 0], DataType.BINARY, 4) == "e"
assert array2hexstring([1, 1, 1, 0], DataType.BINARY, 8) == "0e"
......
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