Skip to content

Commit

Permalink
add test for multiout test 2
Browse files Browse the repository at this point in the history
  • Loading branch information
calad0i committed Oct 29, 2024
1 parent 38cf8ed commit 42766b5
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions test/pytest/test_multiout_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,40 @@ def model():
return model


@pytest.fixture(scope='module')
def model2():
in1 = keras.layers.Input(shape=(24, 8))
in2 = keras.layers.Input(shape=(16))
out1 = keras.layers.Conv1D(1, 3)(in1)
out1 = keras.layers.Flatten()(out1)
out2 = keras.layers.Dense(16, activation='relu')(out1)
out2 = keras.layers.Add()([out2, in2])
out3 = keras.layers.Dense(2)(out1)
model = keras.models.Model(inputs=[in1, in2], outputs=[out1, out2, out3])
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.fixture(scope='module')
def data2():
X1 = np.random.normal(0, 1, (1000, 24, 8))
X2 = np.random.normal(0, 1, (1000, 16))
X1 = np.clip(X1, -16, 15)
X2 = np.clip(X2, -16, 15)
return X1, X2


@pytest.mark.parametrize('backend', ['Vivado', 'Quartus', 'Vitis'])
@pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream'])
def test_multi_clone(model, data, backend: str, io_type: str):
def test_multi_output_nn(model, data, backend: str, io_type: str):
output_dir = str(test_root_path / f'hls4mlprj_multiout_network_{backend}_{io_type}')
hls_config = {'Model': {'Precision': 'fixed<32,5>', 'ReuseFactor': 1}}
layer_config = {
'dense1': {'Precision': {'result': 'fixed<35,5>'}},
'dense2': {'Precision': {'result': 'fixed<40,5>'}},
'dense1_linear': {'Precision': {'result': 'fixed<35,5>'}},
'dense2_linear': {'Precision': {'result': 'fixed<40,5>'}},
}
hls_config['LayerName'] = layer_config
model_hls = convert_from_keras_model(
model, backend=backend, output_dir=output_dir, hls_config=hls_config, io_type=io_type
)
Expand All @@ -50,3 +65,24 @@ def test_multi_clone(model, data, backend: str, io_type: str):

assert np.allclose(r_hls[0], r_keras[0], atol=1e-5, rtol=0)
assert np.allclose(r_hls[1], r_keras[1], atol=1e-5, rtol=0)


@pytest.mark.parametrize('backend', ['Vivado', 'Quartus', 'Vitis'])
@pytest.mark.parametrize('io_type', ['io_parallel', 'io_stream'])
@pytest.mark.parametrize('strategy', ['latency', 'resource'])
def test_multi_output_nn_2(model2, data2, backend: str, io_type: str, strategy: str):
"""Cover corner case where a flatten layer is cloned multiple times, and used as model output"""
output_dir = str(test_root_path / f'hls4mlprj_multiout_network_2_{backend}_{io_type}_{strategy}')
hls_config = {'Model': {'Precision': 'fixed<32,5>', 'ReuseFactor': 1}, 'Strategy': strategy}

model_hls = convert_from_keras_model(
model2, backend=backend, output_dir=output_dir, hls_config=hls_config, io_type=io_type
)

model_hls.compile()
r_hls = model_hls.predict(data2)
r_keras = model2.predict(data2, verbose=0, batch_size=1000)

assert np.allclose(r_hls[0], r_keras[0], atol=1e-5, rtol=0)
assert np.allclose(r_hls[1], r_keras[1], atol=1e-5, rtol=0)
assert np.allclose(r_hls[2], r_keras[2], atol=1e-5, rtol=0)

0 comments on commit 42766b5

Please sign in to comment.