-
Notifications
You must be signed in to change notification settings - Fork 462
Transpose2d, Concatenate2d, and up to 3 Clones for io_stream #402
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,39 @@ | ||
from hls4ml.model.optimizer import OptimizerPass | ||
from hls4ml.model.types import FixedPrecisionType | ||
|
||
def get_concat_type(itype1, itype2): | ||
newwidth = max(itype1.width, itype2.width) | ||
newint = max(itype1.integer, itype2.integer) | ||
if (itype1.signed ^ itype2.signed): # XOR | ||
newint += 1 | ||
newwidth += 1 | ||
newrmode = itype1.rounding_mode if itype1.rounding_mode is not None else itype2.rounding_mode | ||
newsmode = itype1.saturation_mode if itype1.saturation_mode is not None else itype2.saturation_mode | ||
newsbits = itype1.saturation_bits if itype1.saturation_bits is not None else itype2.saturation_bits | ||
|
||
newtype = FixedPrecisionType(newwidth, newint, itype1.signed or itype2.signed, | ||
newrmode, newsmode, newsbits) | ||
return newtype | ||
|
||
class SetPrecisionConcat(OptimizerPass): | ||
def match(self, node): | ||
if node.__class__.__name__ == 'Concatenate': | ||
otype = node.get_output_variable().type.precision | ||
itype1 = node.get_input_variable(node.inputs[0]).type.precision | ||
itype2 = node.get_input_variable(node.inputs[1]).type.precision | ||
if isinstance(otype, FixedPrecisionType) and otype != get_concat_type(itype1, itype2): | ||
return True | ||
return False | ||
|
||
def transform(self, model, node): | ||
""" | ||
Set concat output precision | ||
""" | ||
otype = node.get_output_variable().type.precision | ||
itype1 = node.get_input_variable(node.inputs[0]).type.precision | ||
itype2 = node.get_input_variable(node.inputs[1]).type.precision | ||
newtype = get_concat_type(itype1, itype2) | ||
print("Found {} in the model, optimizing {} to {}...".format(node.name, otype, newtype)) | ||
node.get_output_variable().type.precision = newtype | ||
|
||
return True |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,48 @@ | ||
import pytest | ||
import hls4ml | ||
import numpy as np | ||
from tensorflow.keras.models import model_from_json, Model | ||
from tensorflow.keras.layers import Input, Permute, Concatenate, Activation | ||
import yaml | ||
|
||
@pytest.fixture(scope='module') | ||
def data(): | ||
X = np.random.rand(100, 2, 3) | ||
return X | ||
|
||
@pytest.fixture(scope='module') | ||
def keras_model(): | ||
inp = Input(shape=(2, 3), name='input_1') | ||
x = Permute((2, 1))(inp) | ||
y = Concatenate(axis=1)([x, x]) | ||
x = Activation('relu', name='relu')(x) | ||
out = Concatenate(axis=1)([x, y]) | ||
model = Model(inputs=inp, outputs=out) | ||
return model | ||
|
||
@pytest.fixture | ||
@pytest.mark.parametrize('io_type', ['io_parallel', | ||
'io_stream']) | ||
def hls_model(keras_model, io_type): | ||
hls_config = hls4ml.utils.config_from_keras_model(keras_model, | ||
default_precision='ap_fixed<16,3,AP_RND_CONV,AP_SAT>', | ||
granularity='name') | ||
hls_config['LayerName']['relu']['Precision'] = 'ap_ufixed<17,3>' | ||
hls_model = hls4ml.converters.convert_from_keras_model(keras_model, | ||
hls_config=hls_config, | ||
io_type=io_type, | ||
output_dir='hls4mlprj_transpose_{}'.format(io_type)) | ||
|
||
hls_model.compile() | ||
return hls_model | ||
|
||
@pytest.mark.parametrize('io_type', ['io_parallel', | ||
'io_stream']) | ||
def test_accuracy(data, keras_model, hls_model): | ||
X = data | ||
model = keras_model | ||
# model under test predictions and accuracy | ||
y_keras = model.predict(X) | ||
y_hls4ml = hls_model.predict(X).reshape(y_keras.shape) | ||
# "accuracy" of hls4ml predictions vs keras | ||
np.testing.assert_allclose(y_keras, y_hls4ml, rtol=0, atol=1e-04, verbose=True) |
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.
Uh oh!
There was an error while loading. Please reload this page.