Skip to content

Let repack_stream optimizer inheirt original precision #907

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 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions hls4ml/backends/fpga/passes/repack_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def transform(self, model, node):

# Insert new Repack node instead of Reshape
repack_layer = model.make_node(Repack, 'repack_' + node.name, attrs, node.inputs.copy())
# As result_t attribute is not honored by type conversion, set it manually here
repack_layer.attributes[repack_layer.name].type = node.attributes[node.name].type
model.replace_node(node, repack_layer)

return True
27 changes: 27 additions & 0 deletions test/pytest/test_repack_precision.py
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'