|
1 |
| -import pytest |
2 |
| -import hls4ml |
3 |
| -import numpy as np |
4 | 1 | from pathlib import Path
|
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import pytest |
| 5 | +from tensorflow.keras.layers import Embedding, Input |
5 | 6 | from tensorflow.keras.models import Model
|
6 |
| -from tensorflow.keras.layers import Input, Embedding |
| 7 | + |
| 8 | +import hls4ml |
7 | 9 |
|
8 | 10 | test_root_path = Path(__file__).parent
|
9 | 11 |
|
| 12 | + |
10 | 13 | @pytest.fixture(scope='module')
|
11 | 14 | def data():
|
12 | 15 | X = np.random.randint(10, size=(32, 100))
|
13 | 16 | return X
|
14 | 17 |
|
| 18 | + |
15 | 19 | @pytest.fixture(scope='module')
|
16 | 20 | def keras_model():
|
17 | 21 | inputs = Input(shape=(100,), name='embedding_input')
|
18 | 22 | embedding = Embedding(13, 8, input_length=100, name='embedding')(inputs)
|
19 | 23 | model = Model(inputs=inputs, outputs=embedding)
|
20 | 24 | return model
|
21 | 25 |
|
| 26 | + |
22 | 27 | @pytest.fixture
|
23 | 28 | @pytest.mark.parametrize('backend', ['Vivado', 'Quartus'])
|
24 | 29 | @pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream'])
|
25 | 30 | def hls_model(keras_model, backend, io_type):
|
26 |
| - hls_config = hls4ml.utils.config_from_keras_model(keras_model, |
27 |
| - default_precision='ap_fixed<16,6>', |
28 |
| - granularity='name') |
| 31 | + hls_config = hls4ml.utils.config_from_keras_model(keras_model, default_precision='ap_fixed<16,6>', granularity='name') |
29 | 32 | hls_config['LayerName']['embedding_input']['Precision']['result'] = 'ap_uint<4>'
|
30 | 33 | out_dir = str(test_root_path / 'hls4mlprj_embed_{}_{}').format(backend, io_type)
|
31 |
| - hls_model = hls4ml.converters.convert_from_keras_model(keras_model, |
32 |
| - backend=backend, |
33 |
| - hls_config=hls_config, |
34 |
| - io_type=io_type, |
35 |
| - output_dir=out_dir) |
| 34 | + hls_model = hls4ml.converters.convert_from_keras_model( |
| 35 | + keras_model, backend=backend, hls_config=hls_config, io_type=io_type, output_dir=out_dir |
| 36 | + ) |
36 | 37 |
|
37 | 38 | hls_model.compile()
|
38 | 39 | return hls_model
|
39 | 40 |
|
| 41 | + |
40 | 42 | @pytest.mark.parametrize('backend', ['Vivado', 'Quartus'])
|
41 | 43 | @pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream'])
|
42 | 44 | def test_embedding_accuracy(data, keras_model, hls_model):
|
43 | 45 | X = data
|
44 | 46 | model = keras_model
|
45 | 47 | # model under test predictions and accuracy
|
46 | 48 | y_keras = model.predict(X)
|
47 |
| - y_hls4ml = hls_model.predict(X.astype(np.float)).reshape(y_keras.shape) |
| 49 | + y_hls4ml = hls_model.predict(X.astype(float)).reshape(y_keras.shape) |
48 | 50 | # "accuracy" of hls4ml predictions vs keras
|
49 | 51 | np.testing.assert_allclose(y_keras, y_hls4ml, rtol=0, atol=1e-03, verbose=True)
|
0 commit comments