Skip to content

Commit 3b64bce

Browse files
committed
Quartus test for Softmax
1 parent 409bc13 commit 3b64bce

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

test/pytest/test_softmax.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,39 @@ def generate_data(function, input_shape):
2525

2626

2727
# TODO: include latency strategy with flat_distribution when it can be made to pass
28-
@pytest.mark.parametrize('strategy,function,input_shape,io_type', [#('latency', flat_distribution, (8,), 'io_parallel'),
29-
#('latency', flat_distribution, (8, 8, 3), 'io_stream'),
30-
('stable', flat_distribution, (8,), 'io_parallel'),
31-
('stable', high_accuracy_distribution, (8,), 'io_parallel'),
32-
('stable', flat_distribution, (8,), 'io_stream'),
33-
('stable', high_accuracy_distribution, (8,), 'io_stream'),
34-
# Multi-dimensional tests, only for io_stream for now
35-
('stable', flat_distribution, (8, 8, 3), 'io_stream'),
36-
('stable', high_accuracy_distribution, (8, 8, 3), 'io_stream')])
37-
def test_softmax(strategy, generate_data, input_shape, io_type):
28+
@pytest.mark.parametrize('backend,strategy,function,input_shape,io_type', [
29+
#('latency', flat_distribution, (8,), 'io_parallel'),
30+
#('latency', flat_distribution, (8, 8, 3), 'io_stream'),
31+
('Vivado', 'stable', flat_distribution, (8,), 'io_parallel'),
32+
('Vivado', 'stable', high_accuracy_distribution, (8,), 'io_parallel'),
33+
('Quartus', 'resource', flat_distribution, (8,), 'io_parallel'),
34+
('Quartus', 'resource', high_accuracy_distribution, (8,), 'io_parallel'),
35+
('Vivado', 'stable', flat_distribution, (8,), 'io_stream'),
36+
('Vivado', 'stable', high_accuracy_distribution, (8,), 'io_stream'),
37+
# Multi-dimensional tests, only for io_stream for now
38+
('Vivado', 'stable', flat_distribution, (8, 8, 3), 'io_stream'),
39+
('Vivado', 'stable', high_accuracy_distribution, (8, 8, 3), 'io_stream')
40+
41+
])
42+
def test_softmax(backend, strategy, generate_data, input_shape, io_type):
3843
X = generate_data
3944
model = tf.keras.models.Sequential()
4045
model.add(tf.keras.layers.Activation(input_shape=input_shape, activation='softmax', name='softmax'))
4146
model.compile()
47+
48+
f_type = 'ac_fixed<18,8,true,AC_RND,AC_SAT>' if backend == 'Quartus' else 'ap_fixed<18,8,AP_RND,AP_SAT>'
4249
cfg = hls4ml.utils.config_from_keras_model(model, granularity='name')
4350
cfg['LayerName']['softmax']['Strategy'] = strategy
44-
cfg['LayerName']['softmax']['inv_table_t'] = 'ap_fixed<18,8,AP_RND,AP_SAT>'
45-
cfg['LayerName']['softmax']['exp_table_t'] = 'ap_fixed<18,8,AP_RND,AP_SAT>'
51+
cfg['LayerName']['softmax']['inv_table_t'] = f_type
52+
cfg['LayerName']['softmax']['exp_table_t'] = f_type
53+
4654
odir = str(test_root_path / 'hls4mlprj_softmax_{}'.format(strategy))
4755
hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=cfg, io_type=io_type,
48-
output_dir=odir)
56+
output_dir=odir, backend=backend)
4957
hls_model.compile()
58+
5059
y_keras = model.predict(X)
5160
y_hls4ml = hls_model.predict(X).reshape(y_keras.shape)
52-
5361
acc_hls4ml = accuracy_score(np.argmax(y_keras, axis=-1).ravel(), np.argmax(y_hls4ml, axis=-1).ravel())
5462

5563
print('Accuracy hls4ml relative to keras: {}'.format(acc_hls4ml))

0 commit comments

Comments
 (0)