diff options
author | Benjamin Klimczak <benjamin.klimczak@arm.com> | 2023-03-20 18:07:54 +0000 |
---|---|---|
committer | Benjamin Klimczak <benjamin.klimczak@arm.com> | 2023-10-11 15:42:55 +0100 |
commit | 62768232c5fe4ed6b87136c336b65e13d030e9d4 (patch) | |
tree | 847c36a2f7e092982bc1d7a66d0bf601447c8d20 /tests/test_nn_rewrite_core_graph_edit_record.py | |
parent | 446c379c92e15ad8f24ed0db853dd0fc9c271151 (diff) | |
download | mlia-62768232c5fe4ed6b87136c336b65e13d030e9d4.tar.gz |
MLIA-843 Add unit tests for module mlia.nn.rewrite
Note: The unit tests mostly call the main functions from the respective
modules only.
Change-Id: Ib2ce5c53d0c3eb222b8b8be42fba33ac8e007574
Signed-off-by: Benjamin Klimczak <benjamin.klimczak@arm.com>
Diffstat (limited to 'tests/test_nn_rewrite_core_graph_edit_record.py')
-rw-r--r-- | tests/test_nn_rewrite_core_graph_edit_record.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/test_nn_rewrite_core_graph_edit_record.py b/tests/test_nn_rewrite_core_graph_edit_record.py new file mode 100644 index 0000000..39aeef5 --- /dev/null +++ b/tests/test_nn_rewrite_core_graph_edit_record.py @@ -0,0 +1,52 @@ +# SPDX-FileCopyrightText: Copyright 2023, Arm Limited and/or its affiliates. +# SPDX-License-Identifier: Apache-2.0 +"""Tests for module mlia.nn.rewrite.graph_edit.record.""" +from pathlib import Path + +import pytest +import tensorflow as tf + +from mlia.nn.rewrite.core.graph_edit.record import record_model +from mlia.nn.rewrite.core.utils.numpy_tfrecord import NumpyTFReader + + +@pytest.mark.parametrize("batch_size", (None, 1, 2)) +def test_record_model( + test_tflite_model: Path, + tmp_path: Path, + test_tfrecord: Path, + batch_size: int, +) -> None: + """Test the function record_model().""" + output_file = tmp_path / "out.tfrecord" + record_model( + input_filename=str(test_tfrecord), + model_filename=str(test_tflite_model), + output_filename=str(output_file), + batch_size=batch_size, + ) + assert output_file.is_file() + + def data_matches_outputs(name: str, tensor: tf.Tensor, model_outputs: list) -> bool: + """Check that the name and the tensor match any of the model outputs.""" + for model_output in model_outputs: + if model_output["name"] == name: + # If the name is a match, tensor shape and type have to match! + tensor_shape = tensor.shape.as_list() + tensor_type = tensor.dtype.as_numpy_dtype + return all( + ( + tensor_shape == model_output["shape"].tolist(), + tensor_type == model_output["dtype"], + ) + ) + return False + + # Now load model and the data and make sure that the written data matches + # any of the model outputs + interpreter = tf.lite.Interpreter(str(test_tflite_model)) + model_outputs = interpreter.get_output_details() + dataset = NumpyTFReader(str(output_file)) + for data in dataset: + for name, tensor in data.items(): + assert data_matches_outputs(name, tensor, model_outputs) |