-
Notifications
You must be signed in to change notification settings - Fork 424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add precision inherition for when generating stream clone #911
Merged
jmitrevs
merged 8 commits into
fastmachinelearning:main
from
calad0i:clone_stream_fix_2
Dec 13, 2023
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4f3b3e2
fix multi clones w/ diff outs in stream io
calad0i 1ba13e9
fix test
calad0i d05b866
Fix clone precision inheriting
calad0i 55e4ff3
use result_t for clone datatype src
calad0i f728964
use pytest-randomly behavior
calad0i 373925f
fix path
calad0i 15ec3aa
fix seed
calad0i 624bcb9
env RANDOM_SEED no longer necessary
calad0i File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,93 @@ | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
import pytest | ||
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_clone_precision_inherition(): | ||
inp = keras.Input(shape=(10,), name='inp') | ||
x = Dense(10, name='x')(inp) | ||
y = Dense(10, name='y')(inp) | ||
out = Add(name='out')([x, y]) | ||
model = keras.Model(inp, out) | ||
return model | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def model_multi_clone(): | ||
inp = keras.Input(shape=(10,)) | ||
x = Dense(10)(inp) | ||
y = Dense(10)(inp) | ||
z = Dense(10)(inp) | ||
xy = Add()([x, y]) | ||
xy = Add()([xy, y]) | ||
out = Add()([xy, z]) | ||
model = keras.Model(inp, out) | ||
return model | ||
|
||
|
||
@pytest.fixture(scope='module') | ||
def data(): | ||
X = np.random.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_multi_clone, data, backend: str): | ||
output_dir = str(test_root_path / f'hls4mlprj_stream_clone_multiclone_{backend}') | ||
hls_config = {'Model': {'Precision': 'fixed<32,5>', 'ReuseFactor': 1}} | ||
model_hls = convert_from_keras_model( | ||
model_multi_clone, | ||
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_multi_clone(data).numpy() | ||
|
||
assert np.allclose(r_hls, r_keras, atol=1e-5, rtol=0) | ||
|
||
|
||
@pytest.mark.parametrize('backend', ['Vivado', 'Quartus', 'Vitis']) | ||
def test_clone_precision_inherition(model_clone_precision_inherition, data, backend: str): | ||
output_dir = str(test_root_path / f'hls4mlprj_stream_clone_precision_{backend}') | ||
layer_config = { | ||
'inp': {'Precision': 'fixed<32,5>'}, | ||
'x': {'Precision': 'fixed<32,5>'}, | ||
'x_linear': {'Precision': 'fixed<32,5>'}, | ||
'y': {'Precision': 'fixed<32,5>'}, | ||
'y_linear': {'Precision': 'fixed<32,5>'}, | ||
'out': {'Precision': 'fixed<32,5>'}, | ||
} | ||
hls_config = {'Model': {'Precision': 'fixed<1,0>', 'ReuseFactor': 1}, 'LayerName': layer_config} | ||
model_hls = convert_from_keras_model( | ||
model_clone_precision_inherition, | ||
backend=backend, | ||
output_dir=output_dir, | ||
hls_config=hls_config, | ||
io_type='io_stream', # clone only happens with stream io. | ||
) | ||
assert model_hls.graph['clone_inp'].attributes['inp_cpy1'].type.precision.width == 32 | ||
assert model_hls.graph['clone_inp'].attributes['inp_cpy1'].type.precision.integer == 5 | ||
assert model_hls.graph['clone_inp'].attributes['inp_cpy2'].type.precision.width == 32 | ||
assert model_hls.graph['clone_inp'].attributes['inp_cpy2'].type.precision.integer == 5 | ||
|
||
model_hls.compile() | ||
r_hls = model_hls.predict(data) | ||
r_keras = model_clone_precision_inherition(data).numpy() | ||
|
||
assert np.allclose(r_hls, r_keras, atol=1e-5, rtol=0) | ||
|
||
|
||
if __name__ == '__main__': | ||
test_clone_precision_inherition(model_clone_precision_inherition(), data(), 'Vivado') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to set the environment variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. I have been using these lines since a while ago, but it seems they are not doing anything, at least with recent versions of tensorflows.