-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Softsign LUT optimization * Test file: Activation softsign * Changing minimal accurancy to 9.8 and adding new texts using Vivado and Quartus Co-authored-by: Nemer Chiedde <[email protected]>
- Loading branch information
1 parent
00fbe04
commit 83dcf8e
Showing
3 changed files
with
80 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import hls4ml | ||
import tensorflow as tf | ||
import numpy as np | ||
import pytest | ||
from sklearn.metrics import accuracy_score | ||
from pathlib import Path | ||
|
||
test_root_path = Path(__file__).parent | ||
|
||
def flat_distribution(shape): | ||
return np.random.rand(*shape) | ||
|
||
|
||
@pytest.fixture() | ||
def generate_data(function, input_shape): | ||
return function((1000, *input_shape)) | ||
|
||
|
||
# TODO: include latency strategy with flat_distribution when it can be made to pass | ||
@pytest.mark.parametrize('backend,strategy,function,input_shape,io_type', [ | ||
('Vivado', 'stable', flat_distribution, (4,), 'io_parallel'), | ||
('Quartus', 'stable', flat_distribution, (4,), 'io_parallel'), | ||
# IO_stram avaliable just for VIVADO | ||
('Vivado', 'stable', flat_distribution, (4,), 'io_stream'), | ||
('Vivado', 'stable', flat_distribution, (4, 4, 3), 'io_stream') | ||
]) | ||
def test_softsign(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='softsign', name='softsign')) | ||
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']['softsign']['Strategy'] = strategy | ||
cfg['LayerName']['softsign']['inv_table_t'] = f_type | ||
cfg['LayerName']['softsign']['exp_table_t'] = f_type | ||
|
||
odir = str(test_root_path / 'hls4mlprj_softsign_{}'.format(strategy)) | ||
hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=cfg, io_type=io_type, | ||
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)) | ||
|
||
assert acc_hls4ml >= 0.98 | ||
|
||
|