Skip to content

Commit 3b9b649

Browse files
committed
Add explicit DepthwiseConv tests and simpligy SepConv tests
1 parent b33b7c9 commit 3b9b649

File tree

4 files changed

+141
-14
lines changed

4 files changed

+141
-14
lines changed

test/pytest/test_depthconv1d.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from pathlib import Path
2+
3+
import numpy as np
4+
import pytest
5+
import tensorflow as tf
6+
7+
import hls4ml
8+
9+
test_root_path = Path(__file__).parent
10+
11+
padds_options = ['same', 'valid']
12+
chans_options = ['channels_last']
13+
strides_options = [(1), (2)]
14+
kernel_options = [(2), (3)]
15+
bias_options = [False]
16+
17+
18+
@pytest.mark.parametrize('chans', chans_options)
19+
@pytest.mark.parametrize('padds', padds_options)
20+
@pytest.mark.parametrize('strides', strides_options)
21+
@pytest.mark.parametrize('kernels', kernel_options)
22+
@pytest.mark.parametrize('bias', bias_options)
23+
@pytest.mark.parametrize(
24+
'backend, io_type',
25+
[
26+
('Vivado', 'io_parallel'),
27+
('Vitis', 'io_parallel'),
28+
('Vivado', 'io_stream'),
29+
('Vitis', 'io_stream'),
30+
('Catapult', 'io_stream'),
31+
],
32+
)
33+
def test_depthconv1d(chans, padds, strides, kernels, bias, io_type, backend):
34+
model = tf.keras.models.Sequential()
35+
input_shape = (16, 3)
36+
model.add(
37+
tf.keras.layers.DepthwiseConv1D(
38+
kernel_size=kernels,
39+
strides=strides,
40+
padding=padds,
41+
input_shape=input_shape,
42+
kernel_initializer='normal',
43+
use_bias=bias,
44+
data_format=chans,
45+
)
46+
)
47+
48+
model.compile(optimizer='adam', loss='mse')
49+
X_input = np.random.rand(100, *input_shape)
50+
keras_prediction = model.predict(X_input)
51+
config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,16>')
52+
stride_cfg = str(strides).replace(', ', '_').replace('(', '').replace(')', '')
53+
kernel_cfg = str(kernels).replace(', ', '_').replace('(', '').replace(')', '')
54+
output_dir = str(
55+
test_root_path
56+
/ 'hls4mlprj_depthconv1d_{}_strides_{}_kernels_{}_{}_padding_{}_{}'.format(
57+
chans, stride_cfg, kernel_cfg, padds, backend, io_type
58+
)
59+
)
60+
hls_model = hls4ml.converters.convert_from_keras_model(
61+
model, hls_config=config, output_dir=output_dir, io_type=io_type, backend=backend
62+
)
63+
hls_model.compile()
64+
hls_prediction = hls_model.predict(X_input).reshape(keras_prediction.shape)
65+
66+
np.testing.assert_allclose(hls_prediction, keras_prediction, rtol=0, atol=0.001)

test/pytest/test_depthconv2d.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from pathlib import Path
2+
3+
import numpy as np
4+
import pytest
5+
import tensorflow as tf
6+
7+
import hls4ml
8+
9+
test_root_path = Path(__file__).parent
10+
11+
padds_options = ['same', 'valid']
12+
chans_options = ['channels_last']
13+
io_type_options = ['io_parallel', 'io_stream']
14+
strides_options = [(1, 1), (2, 2)]
15+
kernel_options = [(2, 2), (3, 3)]
16+
bias_options = [False]
17+
18+
19+
@pytest.mark.parametrize('chans', chans_options)
20+
@pytest.mark.parametrize('padds', padds_options)
21+
@pytest.mark.parametrize('strides', strides_options)
22+
@pytest.mark.parametrize('kernels', kernel_options)
23+
@pytest.mark.parametrize('bias', bias_options)
24+
@pytest.mark.parametrize(
25+
'backend, io_type',
26+
[
27+
('Vivado', 'io_parallel'),
28+
('Vitis', 'io_parallel'),
29+
('Vivado', 'io_stream'),
30+
('Vitis', 'io_stream'),
31+
('Catapult', 'io_stream'),
32+
],
33+
)
34+
def test_depthconv2d(chans, padds, strides, kernels, bias, io_type, backend):
35+
model = tf.keras.models.Sequential()
36+
input_shape = (16, 16, 3)
37+
model.add(
38+
tf.keras.layers.DepthwiseConv2D(
39+
kernel_size=kernels,
40+
strides=strides,
41+
padding=padds,
42+
input_shape=input_shape,
43+
kernel_initializer='normal',
44+
use_bias=bias,
45+
data_format=chans,
46+
)
47+
)
48+
49+
model.compile(optimizer='adam', loss='mse')
50+
X_input = np.random.rand(100, *input_shape)
51+
keras_prediction = model.predict(X_input)
52+
config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,16>')
53+
stride_cfg = str(strides).replace(', ', '_').replace('(', '').replace(')', '')
54+
kernel_cfg = str(kernels).replace(', ', '_').replace('(', '').replace(')', '')
55+
output_dir = str(
56+
test_root_path
57+
/ 'hls4mlprj_depthconv2d_{}_strides_{}_kernels_{}_{}_padding_{}_{}'.format(
58+
chans, stride_cfg, kernel_cfg, padds, backend, io_type
59+
)
60+
)
61+
hls_model = hls4ml.converters.convert_from_keras_model(
62+
model, hls_config=config, output_dir=output_dir, io_type=io_type, backend=backend
63+
)
64+
hls_model.compile()
65+
hls_prediction = hls_model.predict(X_input).reshape(keras_prediction.shape)
66+
67+
np.testing.assert_allclose(hls_prediction, keras_prediction, rtol=0, atol=0.001)

test/pytest/test_sepconv1d.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@
33
import numpy as np
44
import pytest
55
import tensorflow as tf
6-
from tensorflow.keras.layers import SeparableConv1D
76

87
import hls4ml
98

109
test_root_path = Path(__file__).parent
1110

12-
keras_conv1d = [SeparableConv1D]
1311
padds_options = ['same', 'valid']
1412
chans_options = ['channels_last']
1513
strides_options = [(1), (2)]
1614
kernel_options = [(2), (3)]
1715
bias_options = [False]
1816

1917

20-
@pytest.mark.parametrize('conv1d', keras_conv1d)
2118
@pytest.mark.parametrize('chans', chans_options)
2219
@pytest.mark.parametrize('padds', padds_options)
2320
@pytest.mark.parametrize('strides', strides_options)
@@ -33,11 +30,11 @@
3330
('Catapult', 'io_stream'),
3431
],
3532
)
36-
def test_sepconv1d(conv1d, chans, padds, strides, kernels, bias, io_type, backend):
33+
def test_sepconv1d(chans, padds, strides, kernels, bias, io_type, backend):
3734
model = tf.keras.models.Sequential()
3835
input_shape = (16, 3)
3936
model.add(
40-
conv1d(
37+
tf.keras.layers.SeparableConv1D(
4138
filters=8,
4239
kernel_size=kernels,
4340
strides=strides,
@@ -57,8 +54,8 @@ def test_sepconv1d(conv1d, chans, padds, strides, kernels, bias, io_type, backen
5754
kernel_cfg = str(kernels).replace(', ', '_').replace('(', '').replace(')', '')
5855
output_dir = str(
5956
test_root_path
60-
/ 'hls4mlprj_{}_{}_strides_{}_kernels_{}_{}_padding_{}_{}'.format(
61-
conv1d.__name__.lower(), chans, stride_cfg, kernel_cfg, padds, backend, io_type
57+
/ 'hls4mlprj_sepconv1d_{}_strides_{}_kernels_{}_{}_padding_{}_{}'.format(
58+
chans, stride_cfg, kernel_cfg, padds, backend, io_type
6259
)
6360
)
6461
hls_model = hls4ml.converters.convert_from_keras_model(

test/pytest/test_sepconv2d.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
import numpy as np
44
import pytest
55
import tensorflow as tf
6-
from tensorflow.keras.layers import SeparableConv2D
76

87
import hls4ml
98

109
test_root_path = Path(__file__).parent
1110

12-
keras_conv2d = [SeparableConv2D]
1311
padds_options = ['same', 'valid']
1412
chans_options = ['channels_last']
1513
io_type_options = ['io_parallel', 'io_stream']
@@ -18,7 +16,6 @@
1816
bias_options = [False]
1917

2018

21-
@pytest.mark.parametrize('conv2d', keras_conv2d)
2219
@pytest.mark.parametrize('chans', chans_options)
2320
@pytest.mark.parametrize('padds', padds_options)
2421
@pytest.mark.parametrize('strides', strides_options)
@@ -34,11 +31,11 @@
3431
('Catapult', 'io_stream'),
3532
],
3633
)
37-
def test_sepconv2d(conv2d, chans, padds, strides, kernels, bias, io_type, backend):
34+
def test_sepconv2d(chans, padds, strides, kernels, bias, io_type, backend):
3835
model = tf.keras.models.Sequential()
3936
input_shape = (16, 16, 3)
4037
model.add(
41-
conv2d(
38+
tf.keras.layers.SeparableConv2D(
4239
filters=8,
4340
kernel_size=kernels,
4441
strides=strides,
@@ -58,8 +55,8 @@ def test_sepconv2d(conv2d, chans, padds, strides, kernels, bias, io_type, backen
5855
kernel_cfg = str(kernels).replace(', ', '_').replace('(', '').replace(')', '')
5956
output_dir = str(
6057
test_root_path
61-
/ 'hls4mlprj_{}_{}_strides_{}_kernels_{}_{}_padding_{}_{}'.format(
62-
conv2d.__name__.lower(), chans, stride_cfg, kernel_cfg, padds, backend, io_type
58+
/ 'hls4mlprj_sepconv2d_{}_strides_{}_kernels_{}_{}_padding_{}_{}'.format(
59+
chans, stride_cfg, kernel_cfg, padds, backend, io_type
6360
)
6461
)
6562
hls_model = hls4ml.converters.convert_from_keras_model(

0 commit comments

Comments
 (0)