diff options
author | Benjamin Klimczak <benjamin.klimczak@arm.com> | 2023-01-11 12:32:02 +0000 |
---|---|---|
committer | Benjamin Klimczak <benjamin.klimczak@arm.com> | 2023-02-08 15:23:29 +0000 |
commit | a4fb8c72f15146c95df16c25e75f03344e9814fd (patch) | |
tree | ce6d9cf39951a0c85d2773d436cc5010ecf78a8f /tests | |
parent | 09ecc5c8acb758e8def33155feb746a34dd7b560 (diff) | |
download | mlia-a4fb8c72f15146c95df16c25e75f03344e9814fd.tar.gz |
MLIA-591 Create interface for target profiles
New class 'TargetProfile' is used to load and verify target profiles.
Change-Id: I76373a923e2e5f55c4e95860635afe9fc5627a5d
Diffstat (limited to 'tests')
-rw-r--r-- | tests/conftest.py | 2 | ||||
-rw-r--r-- | tests/test_backend_vela_compat.py | 4 | ||||
-rw-r--r-- | tests/test_backend_vela_compiler.py | 8 | ||||
-rw-r--r-- | tests/test_backend_vela_performance.py | 10 | ||||
-rw-r--r-- | tests/test_cli_commands.py | 2 | ||||
-rw-r--r-- | tests/test_target_config.py | 63 | ||||
-rw-r--r-- | tests/test_target_cortex_a_reporters.py | 4 | ||||
-rw-r--r-- | tests/test_target_ethos_u_config.py | 42 | ||||
-rw-r--r-- | tests/test_target_ethos_u_data_analysis.py | 8 | ||||
-rw-r--r-- | tests/test_target_ethos_u_data_collection.py | 8 | ||||
-rw-r--r-- | tests/test_target_ethos_u_reporters.py | 2 | ||||
-rw-r--r-- | tests/test_target_tosa_reporters.py | 2 | ||||
-rw-r--r-- | tests/test_utils_filesystem.py | 27 |
13 files changed, 92 insertions, 90 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index b698a73..67549e7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -157,7 +157,7 @@ def fixture_test_models_path( save_tflite_model(tflite_model, tflite_model_path) tflite_vela_model = tmp_path / "test_model_vela.tflite" - device = EthosUConfiguration("ethos-u55-256") + device = EthosUConfiguration.load_profile("ethos-u55-256") optimize_model(tflite_model_path, device.compiler_options, tflite_vela_model) tf.saved_model.save(keras_model, str(tmp_path / "tf_model_test_model")) diff --git a/tests/test_backend_vela_compat.py b/tests/test_backend_vela_compat.py index a2e7f90..4653d7d 100644 --- a/tests/test_backend_vela_compat.py +++ b/tests/test_backend_vela_compat.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. +# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates. # SPDX-License-Identifier: Apache-2.0 """Tests for module vela/compat.""" from pathlib import Path @@ -55,7 +55,7 @@ from mlia.utils.filesystem import working_directory ) def test_operators(test_models_path: Path, model: str, expected_ops: Operators) -> None: """Test operators function.""" - device = EthosUConfiguration("ethos-u55-256") + device = EthosUConfiguration.load_profile("ethos-u55-256") operators = supported_operators(test_models_path / model, device.compiler_options) for expected, actual in zip(expected_ops.ops, operators.ops): diff --git a/tests/test_backend_vela_compiler.py b/tests/test_backend_vela_compiler.py index 20121d6..2d937ea 100644 --- a/tests/test_backend_vela_compiler.py +++ b/tests/test_backend_vela_compiler.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. +# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates. # SPDX-License-Identifier: Apache-2.0 """Tests for module vela/compiler.""" from pathlib import Path @@ -146,7 +146,9 @@ def test_vela_compiler_with_parameters(test_resources_path: Path) -> None: def test_compile_model(test_tflite_model: Path) -> None: """Test model optimization.""" - compiler = VelaCompiler(EthosUConfiguration("ethos-u55-256").compiler_options) + compiler = VelaCompiler( + EthosUConfiguration.load_profile("ethos-u55-256").compiler_options + ) optimized_model = compiler.compile_model(test_tflite_model) assert isinstance(optimized_model, OptimizedModel) @@ -156,7 +158,7 @@ def test_optimize_model(tmp_path: Path, test_tflite_model: Path) -> None: """Test model optimization and saving into file.""" tmp_file = tmp_path / "temp.tflite" - device = EthosUConfiguration("ethos-u55-256") + device = EthosUConfiguration.load_profile("ethos-u55-256") optimize_model(test_tflite_model, device.compiler_options, tmp_file.absolute()) assert tmp_file.is_file() diff --git a/tests/test_backend_vela_performance.py b/tests/test_backend_vela_performance.py index 34c11ab..569de61 100644 --- a/tests/test_backend_vela_performance.py +++ b/tests/test_backend_vela_performance.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. +# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates. # SPDX-License-Identifier: Apache-2.0 """Tests for module vela/performance.""" from pathlib import Path @@ -14,7 +14,7 @@ from mlia.target.ethos_u.config import EthosUConfiguration def test_estimate_performance(test_tflite_model: Path) -> None: """Test getting performance estimations.""" - device = EthosUConfiguration("ethos-u55-256") + device = EthosUConfiguration.load_profile("ethos-u55-256") perf_metrics = estimate_performance(test_tflite_model, device.compiler_options) assert isinstance(perf_metrics, PerformanceMetrics) @@ -24,7 +24,7 @@ def test_estimate_performance_already_optimized( tmp_path: Path, test_tflite_model: Path ) -> None: """Test that performance estimation should fail for already optimized model.""" - device = EthosUConfiguration("ethos-u55-256") + device = EthosUConfiguration.load_profile("ethos-u55-256") optimized_model_path = tmp_path / "optimized_model.tflite" @@ -41,7 +41,7 @@ def test_read_invalid_model(test_tflite_invalid_model: Path) -> None: with pytest.raises( Exception, match=f"Unable to read model {test_tflite_invalid_model}" ): - device = EthosUConfiguration("ethos-u55-256") + device = EthosUConfiguration.load_profile("ethos-u55-256") estimate_performance(test_tflite_invalid_model, device.compiler_options) @@ -58,7 +58,7 @@ def test_compile_invalid_model( with pytest.raises( Exception, match="Model could not be optimized with Vela compiler" ): - device = EthosUConfiguration("ethos-u55-256") + device = EthosUConfiguration.load_profile("ethos-u55-256") optimize_model(test_tflite_model, device.compiler_options, model_path) assert not model_path.exists() diff --git a/tests/test_cli_commands.py b/tests/test_cli_commands.py index b65d90e..61cc5a6 100644 --- a/tests/test_cli_commands.py +++ b/tests/test_cli_commands.py @@ -79,7 +79,7 @@ def test_opt_valid_optimization_target( def mock_performance_estimation(monkeypatch: pytest.MonkeyPatch) -> None: """Mock performance estimation.""" metrics = PerformanceMetrics( - EthosUConfiguration("ethos-u55-256"), + EthosUConfiguration.load_profile("ethos-u55-256"), NPUCycles(1, 2, 3, 4, 5, 6), MemoryUsage(1, 2, 3, 4, 5), ) diff --git a/tests/test_target_config.py b/tests/test_target_config.py index 48f0a58..26f524e 100644 --- a/tests/test_target_config.py +++ b/tests/test_target_config.py @@ -9,15 +9,68 @@ from mlia.backend.config import BackendConfiguration from mlia.backend.config import BackendType from mlia.backend.config import System from mlia.core.common import AdviceCategory -from mlia.target.config import IPConfiguration +from mlia.target.config import get_builtin_supported_profile_names +from mlia.target.config import get_profile_file +from mlia.target.config import load_profile from mlia.target.config import TargetInfo +from mlia.target.config import TargetProfile from mlia.utils.registry import Registry -def test_ip_config() -> None: - """Test the class 'IPConfiguration'.""" - cfg = IPConfiguration("AnyTarget") - assert cfg.target == "AnyTarget" +def test_get_builtin_supported_profile_names() -> None: + """Test profile names getter.""" + assert get_builtin_supported_profile_names() == [ + "cortex-a", + "ethos-u55-128", + "ethos-u55-256", + "ethos-u65-256", + "ethos-u65-512", + "tosa", + ] + + +def test_get_profile_file() -> None: + """Test function 'get_profile_file'.""" + profile_file = get_profile_file("cortex-a") + assert profile_file.is_file() + assert profile_file == get_profile_file(profile_file) + + with pytest.raises(Exception): + get_profile_file("UNKNOWN") + with pytest.raises(Exception): + get_profile_file("") + + +def test_load_profile() -> None: + """Test getting profile data.""" + profile_file = get_profile_file("ethos-u55-256") + assert load_profile(profile_file) == { + "target": "ethos-u55", + "mac": 256, + "memory_mode": "Shared_Sram", + "system_config": "Ethos_U55_High_End_Embedded", + } + + with pytest.raises(Exception, match=r"No such file or directory: 'unknown'"): + load_profile("unknown") + + +def test_target_profile() -> None: + """Test the class 'TargetProfile'.""" + + class MyTargetProfile(TargetProfile): + """Test class deriving from TargetProfile.""" + + def verify(self) -> None: + super().verify() + assert self.target + + profile = MyTargetProfile("AnyTarget") + assert profile.target == "AnyTarget" + + profile = MyTargetProfile("") + with pytest.raises(ValueError): + profile.verify() @pytest.mark.parametrize( diff --git a/tests/test_target_cortex_a_reporters.py b/tests/test_target_cortex_a_reporters.py index 4b39aa1..c32ef7b 100644 --- a/tests/test_target_cortex_a_reporters.py +++ b/tests/test_target_cortex_a_reporters.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. +# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates. # SPDX-License-Identifier: Apache-2.0 """Tests for Cortex-A reporters.""" from typing import Any @@ -18,7 +18,7 @@ from mlia.target.cortex_a.reporters import report_device def test_report_device() -> None: """Test function report_device().""" - report = report_device(CortexAConfiguration("cortex-a")) + report = report_device(CortexAConfiguration.load_profile("cortex-a")) assert report.to_plain_text() diff --git a/tests/test_target_ethos_u_config.py b/tests/test_target_ethos_u_config.py index 08a20ff..7f13b26 100644 --- a/tests/test_target_ethos_u_config.py +++ b/tests/test_target_ethos_u_config.py @@ -5,14 +5,11 @@ from __future__ import annotations from contextlib import ExitStack as does_not_raise from typing import Any -from unittest.mock import MagicMock import pytest from mlia.backend.vela.compiler import VelaCompilerOptions from mlia.target.ethos_u.config import EthosUConfiguration -from mlia.target.ethos_u.config import get_target -from mlia.utils.filesystem import get_vela_config def test_compiler_options_default_init() -> None: @@ -33,48 +30,28 @@ def test_compiler_options_default_init() -> None: def test_ethosu_target() -> None: """Test Ethos-U target configuration init.""" - default_config = EthosUConfiguration("ethos-u55-256") + default_config = EthosUConfiguration.load_profile("ethos-u55-256") assert default_config.target == "ethos-u55" assert default_config.mac == 256 assert default_config.compiler_options is not None -def test_get_target() -> None: - """Test function get_target.""" - with pytest.raises(Exception, match="No target profile given"): - get_target(None) # type: ignore - - with pytest.raises(Exception, match=r"File not found:*"): - get_target("unknown") - - u65_device = get_target("ethos-u65-512") - - assert isinstance(u65_device, EthosUConfiguration) - assert u65_device.target == "ethos-u65" - assert u65_device.mac == 512 - assert u65_device.compiler_options.accelerator_config == "ethos-u65-512" - assert u65_device.compiler_options.memory_mode == "Dedicated_Sram" - assert u65_device.compiler_options.config_files == str(get_vela_config()) - - @pytest.mark.parametrize( "profile_data, expected_error", [ [ {}, pytest.raises( - Exception, - match="Mandatory fields missing from target profile: " - r"\['mac', 'memory_mode', 'system_config', 'target'\]", + KeyError, + match=r"'target'", ), ], [ {"target": "ethos-u65", "mac": 512}, pytest.raises( - Exception, - match="Mandatory fields missing from target profile: " - r"\['memory_mode', 'system_config'\]", + KeyError, + match=r"'system_config'", ), ], [ @@ -114,12 +91,9 @@ def test_get_target() -> None: ], ) def test_ethosu_configuration( - monkeypatch: pytest.MonkeyPatch, profile_data: dict[str, Any], expected_error: Any + profile_data: dict[str, Any], expected_error: Any ) -> None: """Test creating Ethos-U configuration.""" - monkeypatch.setattr( - "mlia.target.ethos_u.config.get_profile", MagicMock(return_value=profile_data) - ) - with expected_error: - EthosUConfiguration("target") + cfg = EthosUConfiguration(**profile_data) + cfg.verify() diff --git a/tests/test_target_ethos_u_data_analysis.py b/tests/test_target_ethos_u_data_analysis.py index bac27ad..e919f5d 100644 --- a/tests/test_target_ethos_u_data_analysis.py +++ b/tests/test_target_ethos_u_data_analysis.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. +# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates. # SPDX-License-Identifier: Apache-2.0 """Tests for Ethos-U data analysis module.""" from __future__ import annotations @@ -84,7 +84,7 @@ def test_perf_metrics_diff() -> None: [ OptimizationPerformanceMetrics( PerformanceMetrics( - EthosUConfiguration("ethos-u55-256"), + EthosUConfiguration.load_profile("ethos-u55-256"), NPUCycles(1, 2, 3, 4, 5, 6), # memory metrics are in kilobytes MemoryUsage(*[i * 1024 for i in range(1, 6)]), # type: ignore @@ -95,7 +95,7 @@ def test_perf_metrics_diff() -> None: OptimizationSettings("pruning", 0.5, None), ], PerformanceMetrics( - EthosUConfiguration("ethos-u55-256"), + EthosUConfiguration.load_profile("ethos-u55-256"), NPUCycles(1, 2, 3, 4, 5, 6), # memory metrics are in kilobytes MemoryUsage( @@ -127,7 +127,7 @@ def test_perf_metrics_diff() -> None: [ OptimizationPerformanceMetrics( PerformanceMetrics( - EthosUConfiguration("ethos-u55-256"), + EthosUConfiguration.load_profile("ethos-u55-256"), NPUCycles(1, 2, 3, 4, 5, 6), # memory metrics are in kilobytes MemoryUsage(*[i * 1024 for i in range(1, 6)]), # type: ignore diff --git a/tests/test_target_ethos_u_data_collection.py b/tests/test_target_ethos_u_data_collection.py index 2cf7482..829d2a7 100644 --- a/tests/test_target_ethos_u_data_collection.py +++ b/tests/test_target_ethos_u_data_collection.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates. +# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates. # SPDX-License-Identifier: Apache-2.0 """Tests for the data collection module for Ethos-U.""" from pathlib import Path @@ -50,7 +50,7 @@ def test_operator_compatibility_collector( sample_context: Context, test_tflite_model: Path ) -> None: """Test operator compatibility data collector.""" - device = EthosUConfiguration("ethos-u55-256") + device = EthosUConfiguration.load_profile("ethos-u55-256") collector = EthosUOperatorCompatibility(test_tflite_model, device) collector.set_context(sample_context) @@ -63,7 +63,7 @@ def test_performance_collector( monkeypatch: pytest.MonkeyPatch, sample_context: Context, test_tflite_model: Path ) -> None: """Test performance data collector.""" - device = EthosUConfiguration("ethos-u55-256") + device = EthosUConfiguration.load_profile("ethos-u55-256") mock_performance_estimation(monkeypatch, device) @@ -81,7 +81,7 @@ def test_optimization_performance_collector( test_tflite_model: Path, ) -> None: """Test optimization performance data collector.""" - device = EthosUConfiguration("ethos-u55-256") + device = EthosUConfiguration.load_profile("ethos-u55-256") mock_performance_estimation(monkeypatch, device) collector = EthosUOptimizationPerformance( diff --git a/tests/test_target_ethos_u_reporters.py b/tests/test_target_ethos_u_reporters.py index bc764a0..0c5764e 100644 --- a/tests/test_target_ethos_u_reporters.py +++ b/tests/test_target_ethos_u_reporters.py @@ -118,7 +118,7 @@ def test_report_operators( "device, expected_plain_text, expected_json_dict", [ [ - EthosUConfiguration("ethos-u55-256"), + EthosUConfiguration.load_profile("ethos-u55-256"), """Device information: Target ethos-u55 MAC 256 diff --git a/tests/test_target_tosa_reporters.py b/tests/test_target_tosa_reporters.py index 59da270..43d2a56 100644 --- a/tests/test_target_tosa_reporters.py +++ b/tests/test_target_tosa_reporters.py @@ -18,7 +18,7 @@ from mlia.target.tosa.reporters import tosa_formatters def test_tosa_report_device() -> None: """Test function report_device().""" - report = report_device(TOSAConfiguration("tosa")) + report = report_device(TOSAConfiguration.load_profile("tosa")) assert report.to_plain_text() diff --git a/tests/test_utils_filesystem.py b/tests/test_utils_filesystem.py index 954f9e3..d0a6e6f 100644 --- a/tests/test_utils_filesystem.py +++ b/tests/test_utils_filesystem.py @@ -9,10 +9,8 @@ import pytest from mlia.utils.filesystem import all_files_exist from mlia.utils.filesystem import all_paths_valid from mlia.utils.filesystem import copy_all -from mlia.utils.filesystem import get_builtin_supported_profile_names from mlia.utils.filesystem import get_mlia_resources from mlia.utils.filesystem import get_mlia_target_profiles_dir -from mlia.utils.filesystem import get_profile from mlia.utils.filesystem import get_vela_config from mlia.utils.filesystem import sha256 from mlia.utils.filesystem import temp_directory @@ -36,31 +34,6 @@ def test_get_mlia_target_profiles() -> None: assert get_mlia_target_profiles_dir().is_dir() -def test_get_builtin_supported_profile_names() -> None: - """Test profile names getter.""" - assert get_builtin_supported_profile_names() == [ - "cortex-a", - "ethos-u55-128", - "ethos-u55-256", - "ethos-u65-256", - "ethos-u65-512", - "tosa", - ] - - -def test_get_profile() -> None: - """Test getting profile data.""" - assert get_profile("ethos-u55-256") == { - "target": "ethos-u55", - "mac": 256, - "memory_mode": "Shared_Sram", - "system_config": "Ethos_U55_High_End_Embedded", - } - - with pytest.raises(Exception, match=r"File not found:*"): - get_profile("unknown") - - @pytest.mark.parametrize("raise_exception", [True, False]) def test_temp_file(raise_exception: bool) -> None: """Test temp_file context manager.""" |