-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into HGQ-integration
- Loading branch information
Showing
12 changed files
with
180 additions
and
32 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
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
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
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,32 @@ | ||
import ast | ||
import io | ||
from contextlib import redirect_stdout | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
import hls4ml | ||
|
||
test_root_path = Path(__file__).parent | ||
|
||
|
||
@pytest.mark.parametrize('backend', ['Vivado', 'Vitis', 'Quartus']) | ||
def test_fetch_example_utils(backend): | ||
f = io.StringIO() | ||
with redirect_stdout(f): | ||
hls4ml.utils.fetch_example_list() | ||
out = f.getvalue() | ||
|
||
model_list = ast.literal_eval(out) # Check if we indeed got a dictionary back | ||
|
||
assert 'qkeras_mnist_cnn.json' in model_list['keras'] | ||
|
||
# This model has an example config that is also downloaded. Stored configurations don't set "Backend" value. | ||
config = hls4ml.utils.fetch_example_model('qkeras_mnist_cnn.json', backend=backend) | ||
config['KerasJson'] = 'qkeras_mnist_cnn.json' | ||
config['KerasH5'] | ||
config['Backend'] = backend | ||
config['OutputDir'] = str(test_root_path / f'hls4mlprj_fetch_example_{backend}') | ||
|
||
hls_model = hls4ml.converters.keras_to_hls(config) | ||
hls_model.compile() # For now, it is enough if it compiles, we're only testing downloading works as expected |
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,27 @@ | ||
from tensorflow import keras | ||
|
||
from hls4ml.converters import convert_from_keras_model | ||
|
||
|
||
def test_repack_precision(): | ||
inp = keras.Input(shape=(3, 3), name='inp') | ||
out = keras.layers.Reshape((3, 3), name='reshape')(inp) | ||
out = keras.layers.Conv1D(2, 2, name='conv')(out) | ||
model = keras.Model(inp, out) | ||
|
||
layer_conf = { | ||
'inp': {'Precision': 'fixed<20,10>'}, | ||
'reshape': {'Precision': 'fixed<20,10>'}, | ||
'conv': {'Precision': 'fixed<20,10>'}, | ||
} | ||
|
||
hls_config = {'Model': {'Precision': 'fixed<2,1>', 'ReuseFactor': 1}, 'LayerName': layer_conf} | ||
|
||
# Repack only happens in io_stream | ||
model_hls = convert_from_keras_model(model, hls_config=hls_config, io_type='io_stream') | ||
assert 'repack_reshape' in model_hls.graph, 'repack_reshape not found in graph' | ||
repack_precision = model_hls.graph['repack_reshape'].attributes['result_t'].precision | ||
assert repack_precision.integer == 10, 'Precision mismatch' | ||
assert repack_precision.fractional == 10, 'Precision mismatch' | ||
assert repack_precision.width == 20, 'Precision mismatch' | ||
assert repack_precision.signed is True, 'Precision mismatch' |
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,59 @@ | ||
import os | ||
import random | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
import pytest | ||
import tensorflow as tf | ||
from keras.layers import Add, Dense | ||
from tensorflow import keras | ||
|
||
from hls4ml.converters import convert_from_keras_model | ||
|
||
test_root_path = Path(__file__).parent | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def model(): | ||
seed = 42 | ||
os.environ['RANDOM_SEED'] = f'{seed}' | ||
np.random.seed(seed) | ||
tf.random.set_seed(seed) | ||
tf.get_logger().setLevel('ERROR') | ||
random.seed(seed) | ||
|
||
inp = keras.Input(shape=(10,)) | ||
x = Dense(10)(inp) | ||
y = Dense(10)(inp) | ||
z = Dense(10)(inp) | ||
xy = Add()([x, y]) # 5 | ||
xy = Add()([xy, y]) # 5 | ||
out = Add()([xy, z]) # 5 | ||
model = keras.Model(inp, out) | ||
return model | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def data(): | ||
rng = np.random.RandomState(42) | ||
X = rng.normal(0, 1, (1000, 10)) | ||
X = np.clip(X, -16, 15) | ||
return X | ||
|
||
|
||
@pytest.mark.parametrize('backend', ['Vivado', 'Quartus', 'Vitis']) | ||
def test_multi_clone(model, data, backend: str): | ||
output_dir = str(test_root_path / f'hls4mlprj_stream_multi_clone_{backend}') | ||
hls_config = {'Model': {'Precision': 'fixed<32,5>', 'ReuseFactor': 1}} | ||
model_hls = convert_from_keras_model( | ||
model, | ||
backend=backend, | ||
output_dir=output_dir, | ||
hls_config=hls_config, | ||
io_type='io_stream', # clone only happens with stream io. | ||
) | ||
model_hls.compile() | ||
r_hls = model_hls.predict(data) | ||
r_keras = model(data).numpy() | ||
|
||
assert np.allclose(r_hls, r_keras, atol=1e-5, rtol=0) |