-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathtest_data_packing.py
More file actions
250 lines (230 loc) · 9.28 KB
/
Copy pathtest_data_packing.py
File metadata and controls
250 lines (230 loc) · 9.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# Copyright (c) 2020, Xilinx
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of FINN nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import pytest
import numpy as np
import os
import shutil
import subprocess
import time
from qonnx.core.datatype import DataType
from qonnx.util.basic import gen_finn_dt_tensor
from finn.util.basic import make_build_dir
from finn.util.data_packing import (
npy_to_rtlsim_input,
numpy_to_hls_code,
pack_innermost_dim_as_hex_string,
)
@pytest.mark.util
@pytest.mark.parametrize(
"dtype",
[
DataType["BINARY"],
DataType["INT2"],
DataType["INT32"],
DataType["FIXED<9,6>"],
DataType["FLOAT32"],
],
)
@pytest.mark.parametrize("test_shape", [(1, 2, 4), (1, 1, 64), (2, 64)])
@pytest.mark.vivado
def test_npy2apintstream(test_shape, dtype):
ndarray = gen_finn_dt_tensor(dtype, test_shape)
test_dir = make_build_dir(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(")", "}")
test_app_string = []
test_app_string += ["#include <cstddef>"]
test_app_string += ["#define AP_INT_MAX_W 4096"]
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 $FINN_ROOT/deps/cnpy/cnpy.cpp \
-I$FINN_ROOT/deps/cnpy/ -I{}/include -I$FINN_ROOT/src/finn/qnn-data/cpp \
--std=c++11 -lz""".format(
os.environ["HLS_PATH"]
)
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()
# make copy before saving the array
ndarray = ndarray.copy()
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
@pytest.mark.util
def test_numpy_to_hls_code():
def remove_all_whitespace(s):
return "".join(s.split())
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>("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>("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>("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)
@pytest.mark.util
@pytest.mark.parametrize(
"dtype",
[
DataType["BINARY"],
DataType["BIPOLAR"],
DataType["TERNARY"],
DataType["INT2"],
DataType["INT7"],
DataType["INT8"],
DataType["INT22"],
DataType["INT32"],
DataType["UINT7"],
DataType["UINT8"],
DataType["UINT15"],
DataType["FIXED<9,6>"],
DataType["FLOAT32"],
],
)
def test_npy_to_rtlsim_input(dtype):
# check if slow and fast data packing produce the same non-sign-extended input for rtlsim
# fast mode is triggered for certain data types if last (SIMD) dim = 1
inp_fast = gen_finn_dt_tensor(dtype, (1, 8, 8, 8 // 1, 1)) # N H W FOLD SIMD
inp_slow = inp_fast.reshape((1, 8, 8, 8 // 2, 2)) # N H W FOLD SIMD
output_fast = npy_to_rtlsim_input(inp_fast, dtype, 1 * dtype.bitwidth())
output_slow = npy_to_rtlsim_input(inp_slow, dtype, 2 * dtype.bitwidth())
output_slow_split = []
for x in output_slow:
# least significant bits = first element:
output_slow_split.append(x & ((1 << dtype.bitwidth()) - 1))
# remaining bits = second element:
output_slow_split.append(x >> dtype.bitwidth())
assert all([(x >> dtype.bitwidth()) == 0 for x in output_fast]), "extraneous bits detected"
assert np.all(output_fast == output_slow_split), "different behavior of packing modes detected"
@pytest.mark.util
@pytest.mark.parametrize("tensorshape", [(1, 2, 16384, 64), (1, 1024, 2048)])
def test_pack_innermost_dim_to_hexstring_fast(tensorshape: tuple[int]):
# check that the sped up function call in pack_inermost_dim_to_hex_string() is valid
tensor_count = 5
assert tensorshape[-1] % 4 == 0, "Smallest tensorshape dimension must be divisible by 4"
# Create random binary tensor by simply rounding a random tensor
tensors = [
np.round(np.random.random(tensorshape)).astype(np.float32) for i in range(tensor_count)
]
results_python = []
results_c = []
# Test C impl
start_c = time.time()
for count in range(tensor_count):
c_result = pack_innermost_dim_as_hex_string(
tensors[count],
DataType["BINARY"],
tensorshape[-1] * 2,
reverse_inner=False,
prefix="0x",
use_fastpack=True,
)
results_c.append(c_result)
end_c = time.time()
# Test python impl
start_python = time.time()
for count in range(tensor_count):
python_result = pack_innermost_dim_as_hex_string(
tensors[count],
DataType["BINARY"],
tensorshape[-1] * 2,
reverse_inner=False,
prefix="0x",
use_fastpack=False,
)
results_python.append(python_result)
end_python = time.time()
assert np.array_equal(np.array(results_python), np.array(results_c))
# Write timing results
with open(
os.path.join(
os.path.dirname(__file__),
"fastpack_benchmark" + "_".join(map(lambda x: str(x), list(tensorshape))) + ".txt",
),
"w+",
) as f:
f.write("Pack_innermost_dim_to_hexstring benchmark test results\n")
f.write("Shape: " + str(tensorshape) + "\n")
f.write(f"Ran {tensor_count} times\n")
python_time = end_python - start_python
c_time = end_c - start_c
f.write(
f"Python: {python_time}s overall | {python_time / tensor_count}s on avg. per sample\n"
)
f.write(f"C: {c_time}s overall | {c_time / tensor_count}s on avg. per sample\n")