aboutsummaryrefslogtreecommitdiff
path: root/tests/mlia/test_api.py
diff options
context:
space:
mode:
authorDiego Russo <diego.russo@arm.com>2022-05-30 13:34:14 +0100
committerDiego Russo <diego.russo@arm.com>2022-05-30 13:34:14 +0100
commit0efca3cadbad5517a59884576ddb90cfe7ac30f8 (patch)
treeabed6cb6fbf3c439fc8d947f505b6a53d5daeb1e /tests/mlia/test_api.py
parent0777092695c143c3a54680b5748287d40c914c35 (diff)
downloadmlia-0efca3cadbad5517a59884576ddb90cfe7ac30f8.tar.gz
Add MLIA codebase0.3.0-rc.1
Add MLIA codebase including sources and tests. Change-Id: Id41707559bd721edd114793618d12ccd188d8dbd
Diffstat (limited to 'tests/mlia/test_api.py')
-rw-r--r--tests/mlia/test_api.py96
1 files changed, 96 insertions, 0 deletions
diff --git a/tests/mlia/test_api.py b/tests/mlia/test_api.py
new file mode 100644
index 0000000..54d4796
--- /dev/null
+++ b/tests/mlia/test_api.py
@@ -0,0 +1,96 @@
+# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
+# SPDX-License-Identifier: Apache-2.0
+"""Tests for the API functions."""
+from pathlib import Path
+from unittest.mock import MagicMock
+
+import pytest
+
+from mlia.api import get_advice
+from mlia.core.common import AdviceCategory
+from mlia.core.context import Context
+from mlia.core.context import ExecutionContext
+
+
+def test_get_advice_no_target_provided(test_keras_model: Path) -> None:
+ """Test getting advice when no target provided."""
+ with pytest.raises(Exception, match="Target is not provided"):
+ get_advice(None, test_keras_model, "all") # type: ignore
+
+
+def test_get_advice_wrong_category(test_keras_model: Path) -> None:
+ """Test getting advice when wrong advice category provided."""
+ with pytest.raises(Exception, match="Invalid advice category unknown"):
+ get_advice("ethos-u55-256", test_keras_model, "unknown") # type: ignore
+
+
+@pytest.mark.parametrize(
+ "category, context, expected_category",
+ [
+ [
+ "all",
+ None,
+ AdviceCategory.ALL,
+ ],
+ [
+ "optimization",
+ None,
+ AdviceCategory.OPTIMIZATION,
+ ],
+ [
+ "operators",
+ None,
+ AdviceCategory.OPERATORS,
+ ],
+ [
+ "performance",
+ None,
+ AdviceCategory.PERFORMANCE,
+ ],
+ [
+ "all",
+ ExecutionContext(),
+ AdviceCategory.ALL,
+ ],
+ [
+ "all",
+ ExecutionContext(advice_category=AdviceCategory.PERFORMANCE),
+ AdviceCategory.PERFORMANCE,
+ ],
+ [
+ "all",
+ ExecutionContext(config_parameters={"param": "value"}),
+ AdviceCategory.ALL,
+ ],
+ [
+ "all",
+ ExecutionContext(event_handlers=[MagicMock()]),
+ AdviceCategory.ALL,
+ ],
+ ],
+)
+def test_get_advice(
+ monkeypatch: pytest.MonkeyPatch,
+ category: str,
+ context: ExecutionContext,
+ expected_category: AdviceCategory,
+ test_keras_model: Path,
+) -> None:
+ """Test getting advice with valid parameters."""
+ advisor_mock = MagicMock()
+ monkeypatch.setattr("mlia.api._get_advisor", MagicMock(return_value=advisor_mock))
+
+ get_advice(
+ "ethos-u55-256",
+ test_keras_model,
+ category, # type: ignore
+ context=context,
+ )
+
+ advisor_mock.run.assert_called_once()
+ context = advisor_mock.run.mock_calls[0].args[0]
+ assert isinstance(context, Context)
+ assert context.advice_category == expected_category
+
+ assert context.event_handlers is not None
+ assert context.config_parameters is not None