Skip to content

Commit

Permalink
Add support for filt_height==1 for streaming quartus conv2d (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmitrevs authored Oct 20, 2023
1 parent 62d5e03 commit 568f97f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ template <class data_T, typename CONFIG_T>
void shift_line_buffer_2d(
const data_T &in_elem,
nnet::shift_reg<typename data_T::value_type, CONFIG_T::pad_left + CONFIG_T::in_width + CONFIG_T::pad_right>
line_buffer[CONFIG_T::filt_height - 1][CONFIG_T::n_chan],
line_buffer[MAX(CONFIG_T::filt_height - 1, 1)][CONFIG_T::n_chan],
typename data_T::value_type shift_buffer[CONFIG_T::filt_height][CONFIG_T::n_chan]) {
// For every channel, insert the incoming pixel at end of the shift buffer
UpdateBuffer:
Expand Down
16 changes: 10 additions & 6 deletions test/pytest/test_pointwiseconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

padds_options = ['same', 'valid']
chans_options = ['channels_last']
io_type_options = ['io_parallel', 'io_stream']
strides1d_options = [(1,), (2,)]
strides2d_options = [(1, 1), (2, 2)]
strategy_options = ['Latency', 'Resource']


@pytest.mark.parametrize('chans', chans_options)
Expand All @@ -24,6 +22,7 @@
'backend, io_type, strategy',
[
('Quartus', 'io_parallel', 'resource'),
('Quartus', 'io_stream', 'resource'),
('Vivado', 'io_parallel', 'resource'),
('Vitis', 'io_parallel', 'resource'),
('Vivado', 'io_parallel', 'latency'),
Expand Down Expand Up @@ -54,7 +53,7 @@ def test_pointwiseconv1d(chans, padds, strides, backend, io_type, strategy):
X_input = np.random.rand(100, *input_shape)
keras_prediction = model.predict(X_input)

default_precision = 'ac_fixed<32,16,true>' if backend == 'Quartus' else 'ap_fixed<32,16>'
default_precision = 'fixed<32,16>'
config = hls4ml.utils.config_from_keras_model(model, default_precision=default_precision)
config['Model']['Strategy'] = strategy

Expand All @@ -70,7 +69,9 @@ def test_pointwiseconv1d(chans, padds, strides, backend, io_type, strategy):
hls_model.compile()
hls_prediction = hls_model.predict(X_input).reshape(keras_prediction.shape)

assert 'Pointwise' in list(hls_model.graph.values())[1].class_name
if not (backend == 'Quartus' and io_type == 'io_stream'):
# Quartus io_stream does not currently have a special pointwise implementation
assert 'Pointwise' in list(hls_model.graph.values())[1].class_name
np.testing.assert_allclose(hls_prediction, keras_prediction, rtol=0, atol=0.001)


Expand All @@ -81,6 +82,7 @@ def test_pointwiseconv1d(chans, padds, strides, backend, io_type, strategy):
'backend, io_type, strategy',
[
('Quartus', 'io_parallel', 'resource'),
('Quartus', 'io_stream', 'resource'),
('Vivado', 'io_parallel', 'resource'),
('Vivado', 'io_parallel', 'latency'),
('Vivado', 'io_stream', 'latency'),
Expand All @@ -107,7 +109,7 @@ def test_pointwiseconv2d(chans, padds, strides, backend, io_type, strategy):
X_input = np.random.rand(100, *input_shape)
keras_prediction = model.predict(X_input)

default_precision = 'ac_fixed<32, 9, true>' if backend == 'Quartus' else 'ap_fixed<32, 9>'
default_precision = 'fixed<32, 9>'

config = hls4ml.utils.config_from_keras_model(model, default_precision=default_precision)
config['Model']['Strategy'] = strategy
Expand All @@ -125,7 +127,9 @@ def test_pointwiseconv2d(chans, padds, strides, backend, io_type, strategy):
hls_model.compile()
hls_prediction = hls_model.predict(X_input).reshape(keras_prediction.shape)

assert 'Pointwise' in list(hls_model.graph.values())[1].class_name
if not (backend == 'Quartus' and io_type == 'io_stream'):
# Quartus io_stream does not currently have a special pointwise implementation
assert 'Pointwise' in list(hls_model.graph.values())[1].class_name
np.testing.assert_allclose(hls_prediction, keras_prediction, rtol=0, atol=0.001)


Expand Down

0 comments on commit 568f97f

Please sign in to comment.