forked from fastmachinelearning/hls4ml
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fastmachinelearning:main' into main
- Loading branch information
Showing
5 changed files
with
72 additions
and
13 deletions.
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
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,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' |
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,33 @@ | ||
from glob import glob | ||
from pathlib import Path | ||
|
||
import keras | ||
import numpy as np | ||
import pytest | ||
|
||
import hls4ml | ||
|
||
test_root_path = Path(__file__).parent | ||
test_root_path = Path('/tmp/trash') | ||
|
||
|
||
@pytest.mark.parametrize('k', [0, 1]) | ||
@pytest.mark.parametrize('i', [4, 8, 10]) | ||
@pytest.mark.parametrize('f', [-2, 0, 2, 7, 14]) | ||
def test_weight_writer(k, i, f): | ||
k, b, i = k, k + i + f, k + i | ||
w = np.array([[np.float32(2.0**-f)]]) | ||
u = '' if k else 'u' | ||
dtype = f'{u}fixed<{b}, {i}>' | ||
hls_config = {'LayerName': {'dense': {'Precision': {'weight': dtype}}}} | ||
model = keras.Sequential([keras.layers.Dense(1, input_shape=(1,), name='dense')]) | ||
model.layers[0].kernel.assign(keras.backend.constant(w)) | ||
output_dir = str(test_root_path / f'hls4ml_prj_test_weight_writer_{dtype}') | ||
model_hls = hls4ml.converters.convert_from_keras_model(model, hls_config=hls_config, output_dir=output_dir) | ||
model_hls.write() | ||
w_paths = glob(str(Path(output_dir) / 'firmware/weights/w*.txt')) | ||
print(w_paths[0]) | ||
assert len(w_paths) == 1 | ||
w_loaded = np.loadtxt(w_paths[0], delimiter=',').reshape(1, 1) | ||
print(f'{w[0,0]:.14}', f'{w_loaded[0,0]:.14}') | ||
assert np.all(w == w_loaded) |