forked from hls4ml-testing/hls4ml-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_qkeras_api.py
141 lines (107 loc) · 4.73 KB
/
test_qkeras_api.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
import string
import random
import pytest
import numpy as np
from qkeras import *
from tensorflow.keras.layers import Input
import hls4ml
from utils import _output_shape,_accuracy,_layer_number,_alpha,cleanup
# ternary_tanh
qactivation_list = ['quantized_relu', 'quantized_tanh', 'binary_tanh', 'quantized_bits']
qactivation_stochastic_kernel = ['stochastic_ternary', 'stochastic_binary']
qactivation_stochastic_bias = ['ternary', 'binary']
quantized_bit_list = ['2', '3', '4', '5', '6', '7', '8']
quantized_integer_list = ['0', '1']
letters = string.ascii_lowercase
# Acceptable accuracy error
error = 15
@pytest.mark.parametrize('activation_int', quantized_integer_list)
@pytest.mark.parametrize('activation_bit', quantized_bit_list)
def test_dense(activation_bit, activation_int):
test_input = np.random.randn(1,10)
x = x_in = Input(10)
x = QDense(
16,
kernel_quantizer='quantized_bits(' + activation_bit + ',' + activation_int + ',1)',
bias_quantizer='quantized_bits(' + activation_bit + ')',
name='Qdense',
)(x)
x = QActivation('quantized_relu')(x)
model = Model(inputs=x_in, outputs=x)
hls_model = hls4ml.converters.convert_from_keras_model(model,project_name=generate_project_name())
hls_model.compile()
model_pred=model.predict(test_input)
hls_pred=hls_model.predict(test_input)
assert(_output_shape(model_pred,hls_pred))
assert(_accuracy(model_pred,hls_pred,error))
assert(_layer_number(model,hls_model))
assert(_alpha(model,hls_model))
cleanup()
@pytest.mark.parametrize('activation_kernel', qactivation_stochastic_kernel)
@pytest.mark.parametrize('activation_bias', qactivation_stochastic_bias)
def test_dense_stochastic(activation_kernel, activation_bias):
test_input=np.random.randn(1,10)
x = x_in = Input(10)
x = QDense(10, kernel_quantizer=activation_kernel, bias_quantizer=activation_bias, name='Qdense',)(x)
x = QActivation('quantized_relu')(x)
model = Model(inputs=x_in, outputs=x)
hls_model = hls4ml.converters.convert_from_keras_model(model,project_name=generate_project_name())
hls_model.compile()
model_pred=model.predict(test_input)
hls_pred=hls_model.predict(test_input)
assert(_output_shape(model_pred,hls_pred))
assert(_accuracy(model_pred,hls_pred,error))
assert(_layer_number(model,hls_model))
assert(_alpha(model,hls_model))
cleanup()
@pytest.mark.parametrize('activation', qactivation_list)
def test_activation(activation):
test_input=np.random.randn(1,10)
x = x_in = Input(10)
x = QDense(10, kernel_quantizer='quantized_bits(3,0,1)', bias_quantizer='quantized_bits(3)', name='Qdense',)(x)
x = QActivation(activation)(x)
model = Model(inputs=x_in, outputs=x)
hls_model = hls4ml.converters.convert_from_keras_model(model,project_name=generate_project_name())
hls_model.compile()
model_pred=model.predict(test_input)
hls_pred=hls_model.predict(test_input)
assert(_output_shape(model_pred,hls_pred))
assert(_accuracy(model_pred,hls_pred,error))
if activation == 'quantized_bits':
assert len(model.layers) == len(hls_model.get_layers())
else:
assert len(model.layers) + 1 == len(hls_model.get_layers())
assert (
list(hls_model.get_layers())[-1].attributes['activation'] == activation.split('_')[1]
if 'quantized' in activation
else activation
)
cleanup()
@pytest.mark.parametrize('activation_kernel', qactivation_stochastic_kernel)
@pytest.mark.parametrize('activation_bias', qactivation_stochastic_bias)
def test_conv2d_stochastic(activation_kernel, activation_bias):
x = x_in = Input((28, 28, 1))
x = QConv2D(
18, (3, 3), kernel_quantizer=activation_kernel, bias_quantizer=activation_bias
)(x)
x = QActivation('quantized_relu')(x)
model = Model(inputs=x_in, outputs=x)
hls_model = hls4ml.converters.convert_from_keras_model(model,project_name=generate_project_name())
hls_model.compile()
assert(_layer_number(model,hls_model))
assert(_alpha(model,hls_model))
@pytest.mark.parametrize('activation_kernel', qactivation_stochastic_kernel)
@pytest.mark.parametrize('activation_bias', qactivation_stochastic_bias)
def test_conv1d_stochastic(activation_kernel, activation_bias):
x = x_in = Input((28, 1))
x = QConv1D(10, 3, kernel_quantizer=activation_kernel, bias_quantizer=activation_bias)(x)
model = Model(inputs=x_in, outputs=x)
hls_model = hls4ml.converters.convert_from_keras_model(model)
hls_model.compile()
assert(_layer_number(model,hls_model))
assert(_alpha(model,hls_model))
cleanup()
# NOTE : to be removed
def generate_project_name():
project_name=''.join(random.choice(letters) for i in range(10))
return project_name