diff --git a/hls4ml/backends/fpga/passes/repack_stream.py b/hls4ml/backends/fpga/passes/repack_stream.py index a502f0ab20..2408ec5ebe 100644 --- a/hls4ml/backends/fpga/passes/repack_stream.py +++ b/hls4ml/backends/fpga/passes/repack_stream.py @@ -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 diff --git a/test/pytest/test_repack_precision.py b/test/pytest/test_repack_precision.py new file mode 100644 index 0000000000..9ac2fd97f9 --- /dev/null +++ b/test/pytest/test_repack_precision.py @@ -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'