-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_window_convolution_activation.py
279 lines (247 loc) · 9.29 KB
/
test_window_convolution_activation.py
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from dataclasses import dataclass
import math
import pathlib
import random
from random import choice, randint
from typing import List
import cocotb
from cocotb.clock import Clock
from cocotb.triggers import Timer
from cocotb_test.simulator import run
import pytest
import larq as lq
import numpy as np
import tensorflow as tf
from test_utils.cocotb_helpers import ImageMonitor, Tick
from test_utils.general import (
concatenate_channel,
concatenate_integers,
get_files,
)
def tensor_to_list(tensor):
return list(tensor.numpy().astype("uint8").flat)
@cocotb.test()
async def run_test(dut):
# layer parameter
kernel_size = (dut.C_KERNEL_SIZE.value.integer,) * 2
stride = (dut.C_STRIDE.value.integer,) * 2
image_shape = (
dut.C_IMG_WIDTH.value.integer,
dut.C_IMG_HEIGHT.value.integer,
dut.C_INPUT_CHANNEL.value.integer,
)
output_channel = dut.C_OUTPUT_CHANNEL.value.integer
output_channel_bitwidth = dut.C_OUTPUT_CHANNEL_BITWIDTH.value.integer
# Needed to compensate the offset caused by converting between -1 (LARQ) and 0 (hdl).
# Conversion from LARQ format [-1, 1] to pocket-bnn format [0, 1] (positive only):
# make the threshold compatible to positive only values
fan_in = kernel_size[0] ** 2 * image_shape[2]
# define the reference model
batch_shape = (1,) + image_shape
input_ = tf.keras.Input(batch_shape=batch_shape, name="img")
x = lq.layers.QuantConv2D(
output_channel, kernel_size, strides=stride, use_bias=False, name="test_conv",
)(input_)
if output_channel_bitwidth == 1:
# Scale is not needed, since we clip afterwards anyway.
x = tf.keras.layers.BatchNormalization(name="test_batchnorm", scale=False)(x)
output_ = lq.quantizers.SteHeaviside()(x)
else:
# There is no batchnorm for output bitwidth > 1
output_ = x
model = tf.keras.Model(inputs=input_, outputs=output_)
if output_channel_bitwidth == 1:
# Try to set realistic batchnorm parameter.
input_channel_bitwidth = dut.C_INPUT_CHANNEL_BITWIDTH.value.integer
model.get_layer("test_batchnorm").set_weights(
[
np.array([random.uniform(0, 0.5) for _ in range(output_channel)]),
np.array(
[
random.uniform(-1, 1) * input_channel_bitwidth
for _ in range(output_channel)
]
),
np.array(
[
random.uniform(0, 1) * fan_in * input_channel_bitwidth
for _ in range(output_channel)
]
),
]
)
# define the testcases
@dataclass
class Testcase:
input_image: List[int]
weights: List[int]
@staticmethod
def replace_minus(values):
"""Convert from LARQ format [-1, 1] to pocket-bnn format [0, 1].
This gets compensated by batchnorm/activation later.
"""
return [0 if v == -1 else v for v in values]
@property
def input_data(self) -> int:
# send all channels (i. e. one pixel) at a time
return concatenate_channel(
self.replace_minus(self.input_image), image_shape[2], 1
)
@property
def output_data(self) -> int:
# inference
image_tensor = tf.convert_to_tensor(self.input_image)
reshaped_tensor = tf.reshape(image_tensor, batch_shape)
extractor = tf.keras.Model(
inputs=model.inputs, outputs=[layer.output for layer in model.layers]
)
features = extractor(reshaped_tensor)
result = features[-1].numpy()
if output_channel_bitwidth > 1:
# compensate (see also threshold for batchnorm)
result = (result + fan_in) / 2
result_list = list(result.astype("uint8").flat)
assert all(r >= 0 for r in result_list)
return concatenate_channel(
result_list, output_channel, output_channel_bitwidth
)
def get_weights(self):
# Only binary weights are supported.
return concatenate_integers(self.replace_minus(self.weights), bitwidth=1)
def get_threshold(self):
# There is no batchnorm for output bitwidth > 1
if output_channel_bitwidth > 1:
return 0
threshold = []
batchnorm_params = [
a.tolist() for a in model.get_layer("test_batchnorm").get_weights()
]
for beta, mean, variance in zip(*batchnorm_params):
# use batch normalization as activation
# see also: https://arxiv.org/pdf/1612.07119.pdf, 4.2.2 Batchnorm-activation as Threshold
# 0.001 is added to avoid division by 0.
threshold_batchnorm = mean - beta * math.sqrt(variance + 0.001)
# get the following formula by solving:
# x - y = fan_in; x + y = threshold
threshold_pos = (threshold_batchnorm + fan_in) / 2
threshold.append(int(threshold_pos))
return concatenate_integers(
self.replace_minus(threshold),
bitwidth=1
+ math.ceil(math.log2(kernel_size[0] ** 2 * image_shape[2] + 1))
+ 1,
)
cases = (
# zero activations, zero weights -> result should be all zeros
Testcase(
[-1] * math.prod(image_shape),
[-1] * (image_shape[2] * output_channel * kernel_size[0] ** 2),
),
# one activations, one weights -> result should be all zeros
Testcase(
[1] * math.prod(image_shape),
[1] * (image_shape[2] * output_channel * kernel_size[0] ** 2),
),
# one activations, zero weights -> result should be all ones
Testcase(
[1] * math.prod(image_shape),
[-1] * (image_shape[2] * output_channel * kernel_size[0] ** 2),
),
# zero activations, one weights -> result should be all ones
Testcase(
[-1] * math.prod(image_shape),
[1] * (image_shape[2] * output_channel * kernel_size[0] ** 2),
),
# mixed
Testcase(
[choice([-1, 1]) for _ in range(math.prod(image_shape))],
[
choice([-1, 1])
for _ in range(image_shape[2] * output_channel * kernel_size[0] ** 2)
],
),
)
# prepare coroutines
clock_period = 10 # ns
tick = Tick(clock_period=clock_period)
cocotb.fork(Clock(dut.isl_clk, clock_period, units="ns").start())
output_mon = ImageMonitor(
"output",
dut.oslv_data,
dut.osl_valid,
dut.isl_clk,
1,
output_channel * output_channel_bitwidth,
)
dut.isl_valid <= 0
dut.isl_start <= 0
await tick.wait()
# run the specific testcases
for case in cases:
reshaped_weights = [
np.array(case.weights).reshape(
kernel_size + (image_shape[2], output_channel)
)
]
model.get_layer("test_conv").set_weights(reshaped_weights)
# This is the way how we convert the weights of the model back.
assert list(reshaped_weights[0].flat) == case.weights
dut.islv_weights <= case.get_weights()
dut.islv_threshold <= case.get_threshold()
dut.isl_start <= 1
await tick.wait()
dut.isl_start <= 0
await tick.wait()
for datum in case.input_data:
dut.isl_valid <= 1
dut.islv_data <= datum
await tick.wait()
dut.isl_valid <= 0
await tick.wait()
dut.isl_valid <= 0
await tick.wait_multiple(40)
print("expected result:", case.output_data)
print("actual result:", output_mon.output)
assert output_mon.output == case.output_data
output_mon.clear()
# Don't run the full test matrix. Only the most common configs.
@pytest.mark.parametrize(
"kernel_size,stride,input_channel,output_channel,output_channel_bitwidth",
[
(1, 1, 4, 8, 1),
(2, 1, 4, 8, 1),
(2, 2, 4, 8, 1),
(3, 1, 4, 8, 1),
(3, 1, 4, 8, 8),
(3, 1, 1, 4, 1),
(3, 1, 3, 8, 1),
(3, 1, 8, 16, 1),
(3, 2, 4, 8, 1),
(5, 1, 4, 8, 1),
(7, 1, 4, 8, 1),
],
)
def test_window_convolution_activation(
kernel_size, stride, input_channel, output_channel, output_channel_bitwidth
):
# Input channel bitwidth > 1 is tested at convolution level.
input_channel_bitwidth = 1
generics = {
"C_KERNEL_SIZE": kernel_size,
"C_STRIDE": stride,
"C_INPUT_CHANNEL": input_channel,
"C_INPUT_CHANNEL_BITWIDTH": input_channel_bitwidth,
"C_OUTPUT_CHANNEL": output_channel,
"C_OUTPUT_CHANNEL_BITWIDTH": output_channel_bitwidth,
"C_IMG_WIDTH": 8,
"C_IMG_HEIGHT": 8,
}
run(
vhdl_sources=get_files(
pathlib.Path(__file__).parent.absolute() / ".." / "src", "*.vhd"
),
toplevel="window_convolution_activation",
module="test_window_convolution_activation",
compile_args=["--work=bnn_lib", "--std=08"],
parameters=generics,
)