Skip to content
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
merged 8 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 4 additions & 0 deletions hls4ml/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from hls4ml import converters, report, utils # noqa: F401

try:
Expand All @@ -22,3 +24,5 @@ def reseed(newseed):
torch.manual_seed(newseed)
except ImportError:
print('\nPyTorch seed not set')

os.environ['RANDOM_SEED'] = f'{newseed}'
Copy link
Contributor

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?

Copy link
Contributor Author

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.

20 changes: 11 additions & 9 deletions hls4ml/backends/fpga/passes/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,19 @@ def initialize(self):
class CloneFunctionTemplate(FunctionCallTemplate):
def __init__(self):
super().__init__(Clone, include_header=clone_include_list)
self.template = None # to be filled once number of clones known

def format(self, node):
params = self._default_function_params(node)
for i, _output in enumerate(node.outputs):
params['output' + str(i + 1)] = node.variables[node.outputs[i]].name

if self.template is None:
self.template = (
'nnet::clone_stream<{input_t}, {output_t}, {size}>({input}, '
+ ', '.join(['{output' + str(i + 1) + '}' for i in range(len(node.outputs))])
+ ');'
)
template = (
'nnet::clone_stream<{input_t}, {output_t}, {size}>({input}, '
+ ', '.join(['{output' + str(i + 1) + '}' for i in range(len(node.outputs))])
+ ');'
)

return self.template.format(**params)
return template.format(**params)


def register_clone(backend):
Expand Down Expand Up @@ -79,13 +77,17 @@ def transform(self, model, node):
attrs = {'size': np.prod(out_var.shape)}
idx = layer.inputs.index(output)
layer.inputs[idx] = output + '_cpy' + str(i)
clone_layer = model.make_node(

clone_layer: Clone = model.make_node(
Clone,
'clone_' + node.name,
attrs,
[output],
[output + '_cpy' + str(i + 1) for i in range(len(output_map[output]))],
)
for i in range(len(output_map[output])):
key = output + '_cpy' + str(i + 1)
clone_layer.attributes[key].type = node.attributes['result_t']
jmitrevs marked this conversation as resolved.
Show resolved Hide resolved
model.insert_node(clone_layer)
transformed = True

Expand Down
93 changes: 93 additions & 0 deletions test/pytest/test_stream_clone.py
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')
Loading