Skip to content

Commit

Permalink
Quartus test for Softmax
Browse files Browse the repository at this point in the history
  • Loading branch information
bo3z committed Apr 21, 2022
1 parent df206d2 commit 0b7eb38
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions test/pytest/test_softmax.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,39 @@ def generate_data(function, input_shape):


# TODO: include latency strategy with flat_distribution when it can be made to pass
@pytest.mark.parametrize('strategy,function,input_shape,io_type', [#('latency', flat_distribution, (8,), 'io_parallel'),
#('latency', flat_distribution, (8, 8, 3), 'io_stream'),
('stable', flat_distribution, (8,), 'io_parallel'),
('stable', high_accuracy_distribution, (8,), 'io_parallel'),
('stable', flat_distribution, (8,), 'io_stream'),
('stable', high_accuracy_distribution, (8,), 'io_stream'),
# Multi-dimensional tests, only for io_stream for now
('stable', flat_distribution, (8, 8, 3), 'io_stream'),
('stable', high_accuracy_distribution, (8, 8, 3), 'io_stream')])
def test_softmax(strategy, generate_data, input_shape, io_type):
@pytest.mark.parametrize('backend,strategy,function,input_shape,io_type', [
#('latency', flat_distribution, (8,), 'io_parallel'),
#('latency', flat_distribution, (8, 8, 3), 'io_stream'),
('Vivado', 'stable', flat_distribution, (8,), 'io_parallel'),
('Vivado', 'stable', high_accuracy_distribution, (8,), 'io_parallel'),
('Quartus', 'resource', flat_distribution, (8,), 'io_parallel'),
('Quartus', 'resource', high_accuracy_distribution, (8,), 'io_parallel'),
('Vivado', 'stable', flat_distribution, (8,), 'io_stream'),
('Vivado', 'stable', high_accuracy_distribution, (8,), 'io_stream'),
# Multi-dimensional tests, only for io_stream for now
('Vivado', 'stable', flat_distribution, (8, 8, 3), 'io_stream'),
('Vivado', 'stable', high_accuracy_distribution, (8, 8, 3), 'io_stream')
])
def test_softmax(backend, strategy, generate_data, input_shape, io_type):
X = generate_data
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Activation(input_shape=input_shape, activation='softmax', name='softmax'))
model.compile()

f_type = 'ac_fixed<18,8,true,AC_RND,AC_SAT>' if backend == 'Quartus' else 'ap_fixed<18,8,AP_RND,AP_SAT>'
cfg = hls4ml.utils.config_from_keras_model(model, granularity='name')
cfg['LayerName']['softmax']['Strategy'] = strategy
cfg['LayerName']['softmax']['inv_table_t'] = 'ap_fixed<18,8,AP_RND,AP_SAT>'
cfg['LayerName']['softmax']['exp_table_t'] = 'ap_fixed<18,8,AP_RND,AP_SAT>'
cfg['LayerName']['softmax']['inv_table_t'] = f_type
cfg['LayerName']['softmax']['exp_table_t'] = f_type

odir = str(test_root_path / 'hls4mlprj_softmax_{}'.format(strategy))
hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=cfg, io_type=io_type,
output_dir=odir)
output_dir=odir, backend=backend)
hls_model.compile()

y_keras = model.predict(X)
y_hls4ml = hls_model.predict(X).reshape(y_keras.shape)

acc_hls4ml = accuracy_score(np.argmax(y_keras, axis=-1).ravel(), np.argmax(y_hls4ml, axis=-1).ravel())

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

0 comments on commit 0b7eb38

Please sign in to comment.