|
| 1 | +""" Test that nested models in Keras is properly parsed and expanded by the optimizers. |
| 2 | +""" |
| 3 | + |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import pytest |
| 8 | +from tensorflow.keras.layers import Dense, Input |
| 9 | +from tensorflow.keras.models import Model, Sequential |
| 10 | + |
| 11 | +import hls4ml |
| 12 | + |
| 13 | +test_root_path = Path(__file__).parent |
| 14 | + |
| 15 | + |
| 16 | +def make_nested_model(input_shape): |
| 17 | + """ |
| 18 | + This model will have the following architecture: |
| 19 | + Functional (fun_model) |
| 20 | + Dense (fun_first_dense) |
| 21 | + Sequential (seq_sub) |
| 22 | + Dense |
| 23 | + Dense |
| 24 | + Dense (fun_middle_dense) |
| 25 | + Functional (fun_sub) |
| 26 | + Dense |
| 27 | + Dense |
| 28 | + Dense (fun_last_dense) |
| 29 | + """ |
| 30 | + seq_sub = Sequential(name='seq_sub') |
| 31 | + seq_sub.add(Dense(5, activation='linear', input_shape=(5,), name='seq_sub_dense_1')) |
| 32 | + seq_sub.add(Dense(3, activation='linear', name='seq_sub_dense_2')) |
| 33 | + |
| 34 | + fun_input = Input(shape=(8,), name='fun_input') |
| 35 | + fun_x = Dense(7, activation='linear', name='fun_sub_dense_1')(fun_input) |
| 36 | + fun_x = Dense(6, activation='linear', name='fun_sub_dense_2')(fun_x) |
| 37 | + fun_sub = Model(inputs=fun_input, outputs=fun_x, name='fun_sub') |
| 38 | + |
| 39 | + input = Input(shape=input_shape, name='model_input') |
| 40 | + x = Dense(5, activation='linear', name='fun_first_dense')(input) |
| 41 | + x = seq_sub(x) |
| 42 | + x = Dense(8, activation='linear', name='fun_middle_dense')(x) |
| 43 | + x = fun_sub(x) |
| 44 | + x = Dense(4, activation='linear', name='fun_last_dense')(x) |
| 45 | + fun_model = Model(inputs=input, outputs=x, name='fun_model') |
| 46 | + |
| 47 | + return fun_model |
| 48 | + |
| 49 | + |
| 50 | +def make_sub_nested_model(input_shape): |
| 51 | + """ |
| 52 | + The following abomination will create this hierarchy: |
| 53 | + Sequential |
| 54 | + Dense (first_dense) |
| 55 | + Functional (fun_model) |
| 56 | + Dense (fun_first_dense) |
| 57 | + Sequential (fun_model_seq_sub) |
| 58 | + Dense |
| 59 | + Dense |
| 60 | + Dense (fun_middle_dense) |
| 61 | + Functional (fun_model_fun_sub) |
| 62 | + Dense |
| 63 | + Dense |
| 64 | + Dense (fun_last_dense) |
| 65 | + Dense (middle_dense) |
| 66 | + Sequential (seq_model) |
| 67 | + Dense |
| 68 | + Functional (seq_model_fun_sub) |
| 69 | + Dense |
| 70 | + Dense |
| 71 | + Dense |
| 72 | + Sequential (seq_model_seq_sub) |
| 73 | + Dense |
| 74 | + Dense |
| 75 | + Dense |
| 76 | + Dense (last_dense) |
| 77 | + """ |
| 78 | + fun_model_seq_sub = Sequential(name='fun_model_seq_sub') |
| 79 | + fun_model_seq_sub.add(Dense(5, activation='linear', input_shape=(5,), name='fun_seq_sub_dense_1')) |
| 80 | + fun_model_seq_sub.add(Dense(3, activation='linear', name='fun_seq_sub_dense_2')) |
| 81 | + |
| 82 | + fun_fun_input = Input(shape=(8,), name='fun_fun_input') |
| 83 | + fun_fun_x = Dense(7, activation='linear', name='fun_fun_sub_dense_1')(fun_fun_input) |
| 84 | + fun_fun_x = Dense(6, activation='linear', name='fun_fun_sub_dense_2')(fun_fun_x) |
| 85 | + fun_model_fun_sub = Model(inputs=fun_fun_input, outputs=fun_fun_x, name='fun_model_fun_sub') |
| 86 | + |
| 87 | + fun_input = Input(shape=(10,), name='fun_input') |
| 88 | + fun_x = Dense(5, activation='linear', name='fun_first_dense')(fun_input) |
| 89 | + fun_x = fun_model_seq_sub(fun_x) |
| 90 | + fun_x = Dense(8, activation='linear', name='fun_middle_dense')(fun_x) |
| 91 | + fun_x = fun_model_fun_sub(fun_x) |
| 92 | + fun_x = Dense(4, activation='linear', name='fun_last_dense')(fun_x) |
| 93 | + fun_model = Model(inputs=fun_input, outputs=fun_x, name='fun_model') |
| 94 | + |
| 95 | + seq_fun_input = Input(shape=(2,), name='seq_fun_input') |
| 96 | + seq_fun_x = Dense(9, activation='linear', name='seq_fun_sub_dense_1')(seq_fun_input) |
| 97 | + seq_fun_x = Dense(3, activation='linear', name='seq_fun_sub_dense_2')(seq_fun_x) |
| 98 | + seq_model_fun_sub = Model(inputs=seq_fun_input, outputs=seq_fun_x, name='seq_model_fun_sub') |
| 99 | + |
| 100 | + seq_model_seq_sub = Sequential(name='seq_model_seq_sub') |
| 101 | + seq_model_seq_sub.add(Dense(5, activation='linear', input_shape=(2,), name='seq_seq_sub_dense_1')) |
| 102 | + seq_model_seq_sub.add(Dense(7, activation='linear', name='seq_seq_sub_dense_2')) |
| 103 | + |
| 104 | + seq_model = Sequential(name='seq_model') |
| 105 | + seq_model.add(Dense(2, activation='linear', input_shape=(6,), name='seq_first_dense')) |
| 106 | + seq_model.add(seq_model_fun_sub) |
| 107 | + seq_model.add(Dense(2, activation='linear', name='seq_middle_dense')) |
| 108 | + seq_model.add(seq_model_seq_sub) |
| 109 | + seq_model.add(Dense(2, activation='linear', name='seq_last_dense')) |
| 110 | + |
| 111 | + model = Sequential() |
| 112 | + model.add(Dense(10, activation='linear', input_shape=input_shape, name='first_dense')) |
| 113 | + model.add(fun_model) |
| 114 | + model.add(Dense(6, activation='linear', name='middle_dense')) |
| 115 | + model.add(seq_model) |
| 116 | + model.add(Dense(4, activation='linear', name='last_dense')) |
| 117 | + |
| 118 | + return model |
| 119 | + |
| 120 | + |
| 121 | +def randX(batch_size, N): |
| 122 | + return np.random.rand(batch_size, N) |
| 123 | + |
| 124 | + |
| 125 | +@pytest.fixture(scope='module') |
| 126 | +def randX_20_15(): |
| 127 | + return randX(20, 15) |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize('backend', ['Vivado', 'Quartus']) |
| 131 | +@pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream']) |
| 132 | +def test_nested_model(randX_20_15, backend, io_type): |
| 133 | + n_in = 15 |
| 134 | + input_shape = (n_in,) |
| 135 | + keras_model = make_nested_model(input_shape) |
| 136 | + keras_model.compile(optimizer='adam', loss='mae') |
| 137 | + |
| 138 | + config = hls4ml.utils.config_from_keras_model(keras_model, default_precision='fixed<24,12>') |
| 139 | + prj_name = f'hls4mlprj_nested_model_{backend}_{io_type}' |
| 140 | + output_dir = str(test_root_path / prj_name) |
| 141 | + hls_model = hls4ml.converters.convert_from_keras_model( |
| 142 | + keras_model, hls_config=config, output_dir=output_dir, io_type=io_type, backend=backend |
| 143 | + ) |
| 144 | + hls_model.compile() |
| 145 | + |
| 146 | + X = randX_20_15 |
| 147 | + y_keras = keras_model.predict(X) |
| 148 | + y_hls4ml = hls_model.predict(X) |
| 149 | + |
| 150 | + np.testing.assert_allclose(y_keras.ravel(), y_hls4ml.ravel(), rtol=1e-2, atol=0.02) |
| 151 | + |
| 152 | + |
| 153 | +@pytest.mark.parametrize('backend', ['Vivado', 'Quartus']) |
| 154 | +@pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream']) |
| 155 | +def test_sub_nested_model(randX_20_15, backend, io_type): |
| 156 | + n_in = 15 |
| 157 | + input_shape = (n_in,) |
| 158 | + keras_model = make_sub_nested_model(input_shape) |
| 159 | + keras_model.compile(optimizer='adam', loss='mae') |
| 160 | + |
| 161 | + config = hls4ml.utils.config_from_keras_model(keras_model, default_precision='fixed<24,12>') |
| 162 | + prj_name = f'hls4mlprj_sub_nested_model_{backend}_{io_type}' |
| 163 | + output_dir = str(test_root_path / prj_name) |
| 164 | + hls_model = hls4ml.converters.convert_from_keras_model( |
| 165 | + keras_model, hls_config=config, output_dir=output_dir, io_type=io_type, backend=backend |
| 166 | + ) |
| 167 | + hls_model.compile() |
| 168 | + |
| 169 | + X = randX_20_15 |
| 170 | + y_keras = keras_model.predict(X) |
| 171 | + y_hls4ml = hls_model.predict(X) |
| 172 | + |
| 173 | + np.testing.assert_allclose(y_keras.ravel(), y_hls4ml.ravel(), rtol=1e-2, atol=0.02) |
0 commit comments