|
| 1 | +import pytest |
| 2 | +import hls4ml |
| 3 | +import tensorflow as tf |
| 4 | +import numpy as np |
| 5 | +from pathlib import Path |
| 6 | +from tensorflow.keras.layers import Input, Add, Average, Concatenate, Dot, Maximum, Minimum, Multiply, Subtract |
| 7 | + |
| 8 | +test_root_path = Path(__file__).parent |
| 9 | + |
| 10 | +merge_layer = [Add, Average, Maximum, Minimum, Multiply, Subtract] |
| 11 | +io_type_options = ['io_parallel', 'io_stream'] |
| 12 | +@pytest.mark.parametrize('merge_layer', merge_layer) |
| 13 | +@pytest.mark.parametrize('io_type', io_type_options) |
| 14 | +def test_merge(merge_layer, io_type): |
| 15 | + input_shape = (10, 10, 3) |
| 16 | + |
| 17 | + in1 = Input(shape=input_shape) |
| 18 | + in2 = Input(shape=input_shape) |
| 19 | + out = merge_layer()([in1, in2]) |
| 20 | + |
| 21 | + model = tf.keras.models.Model(inputs=[in1, in2], outputs=out) |
| 22 | + model.compile(optimizer='adam', loss='mse') |
| 23 | + |
| 24 | + config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,16>') |
| 25 | + output_dir = str(test_root_path / 'hls4mlprj_merge_{}_{}'.format(merge_layer.__name__.lower(), io_type)) |
| 26 | + hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config, output_dir=output_dir, io_type=io_type) |
| 27 | + hls_model.compile() |
| 28 | + |
| 29 | + X_input1 = np.random.rand(100, *input_shape) |
| 30 | + X_input2 = np.random.rand(100, *input_shape) |
| 31 | + |
| 32 | + keras_prediction = model.predict([X_input1, X_input2]) |
| 33 | + hls_prediction = hls_model.predict([X_input1, X_input2]).reshape(keras_prediction.shape) |
| 34 | + |
| 35 | + np.testing.assert_allclose(hls_prediction, keras_prediction, rtol=0, atol=0.001) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize('axes', [1]) |
| 39 | +@pytest.mark.parametrize('io_type', ['io_parallel']) # No io_stream implementation yet |
| 40 | +def test_dot(axes, io_type): |
| 41 | + input_shape = (10,) # Only 1D implemented |
| 42 | + |
| 43 | + in1 = Input(shape=input_shape) |
| 44 | + in2 = Input(shape=input_shape) |
| 45 | + out = Dot(axes=axes)([in1, in2]) |
| 46 | + |
| 47 | + model = tf.keras.models.Model(inputs=[in1, in2], outputs=out) |
| 48 | + model.compile(optimizer='adam', loss='mse') |
| 49 | + |
| 50 | + config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,16>') |
| 51 | + output_dir = str(test_root_path / 'hls4mlprj_dot_axes_{}_{}'.format(str(axes), io_type)) |
| 52 | + hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config, output_dir=output_dir, io_type=io_type) |
| 53 | + hls_model.compile() |
| 54 | + |
| 55 | + X_input1 = np.random.rand(100, *input_shape) |
| 56 | + X_input2 = np.random.rand(100, *input_shape) |
| 57 | + |
| 58 | + keras_prediction = model.predict([X_input1, X_input2]) |
| 59 | + hls_prediction = hls_model.predict([X_input1, X_input2]).reshape(keras_prediction.shape) |
| 60 | + |
| 61 | + np.testing.assert_allclose(hls_prediction, keras_prediction, rtol=0, atol=0.001) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream']) |
| 65 | +def test_concatenate1d(io_type): |
| 66 | + input_shape = (10,) |
| 67 | + |
| 68 | + in1 = Input(shape=input_shape) |
| 69 | + in2 = Input(shape=input_shape) |
| 70 | + out = Concatenate()([in1, in2]) |
| 71 | + |
| 72 | + model = tf.keras.models.Model(inputs=[in1, in2], outputs=out) |
| 73 | + model.compile(optimizer='adam', loss='mse') |
| 74 | + |
| 75 | + config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,16>') |
| 76 | + output_dir = str(test_root_path / 'hls4mlprj_concatenate1d_{}'.format(io_type)) |
| 77 | + hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config, output_dir=output_dir, io_type=io_type) |
| 78 | + hls_model.compile() |
| 79 | + |
| 80 | + X_input1 = np.random.rand(100, *input_shape) |
| 81 | + X_input2 = np.random.rand(100, *input_shape) |
| 82 | + |
| 83 | + keras_prediction = model.predict([X_input1, X_input2]) |
| 84 | + hls_prediction = hls_model.predict([X_input1, X_input2]).reshape(keras_prediction.shape) |
| 85 | + |
| 86 | + np.testing.assert_allclose(hls_prediction, keras_prediction, rtol=0, atol=0.001) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.parametrize('axis', [1, 2]) |
| 90 | +@pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream']) |
| 91 | +def test_concatenate2d(axis, io_type): |
| 92 | + input_shape = (10, 3) |
| 93 | + |
| 94 | + in1 = Input(shape=input_shape) |
| 95 | + in2 = Input(shape=input_shape) |
| 96 | + out = Concatenate(axis=axis)([in1, in2]) |
| 97 | + |
| 98 | + model = tf.keras.models.Model(inputs=[in1, in2], outputs=out) |
| 99 | + model.compile(optimizer='adam', loss='mse') |
| 100 | + |
| 101 | + config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,16>') |
| 102 | + output_dir = str(test_root_path /'hls4mlprj_concatenate2d_axis_{}_{}'.format(str(axis), io_type)) |
| 103 | + hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config, output_dir=output_dir, io_type=io_type) |
| 104 | + hls_model.compile() |
| 105 | + |
| 106 | + X_input1 = np.random.rand(100, *input_shape) |
| 107 | + X_input2 = np.random.rand(100, *input_shape) |
| 108 | + |
| 109 | + keras_prediction = model.predict([X_input1, X_input2]) |
| 110 | + hls_prediction = hls_model.predict([X_input1, X_input2]).reshape(keras_prediction.shape) |
| 111 | + |
| 112 | + np.testing.assert_allclose(hls_prediction, keras_prediction, rtol=0, atol=0.001) |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.parametrize('axis', [1, 2, 3]) |
| 116 | +@pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream']) |
| 117 | +def test_concatenate3d(axis, io_type): |
| 118 | + input_shape = (10, 10, 3) |
| 119 | + |
| 120 | + in1 = Input(shape=input_shape) |
| 121 | + in2 = Input(shape=input_shape) |
| 122 | + out = Concatenate(axis=axis)([in1, in2]) |
| 123 | + |
| 124 | + model = tf.keras.models.Model(inputs=[in1, in2], outputs=out) |
| 125 | + model.compile(optimizer='adam', loss='mse') |
| 126 | + |
| 127 | + config = hls4ml.utils.config_from_keras_model(model, default_precision='ap_fixed<32,16>') |
| 128 | + output_dir = str(test_root_path /'hls4mlprj_concatenate3d_axis_{}_{}'.format(str(axis), io_type)) |
| 129 | + hls_model = hls4ml.converters.convert_from_keras_model(model, hls_config=config, output_dir=output_dir, io_type=io_type) |
| 130 | + hls_model.compile() |
| 131 | + |
| 132 | + X_input1 = np.random.rand(100, *input_shape) |
| 133 | + X_input2 = np.random.rand(100, *input_shape) |
| 134 | + |
| 135 | + keras_prediction = model.predict([X_input1, X_input2]) |
| 136 | + hls_prediction = hls_model.predict([X_input1, X_input2]).reshape(keras_prediction.shape) |
| 137 | + |
| 138 | + np.testing.assert_allclose(hls_prediction, keras_prediction, rtol=0, atol=0.001) |
0 commit comments