aboutsummaryrefslogtreecommitdiff
path: root/tests/test_target_config.py
blob: 368d3948b723905192a8aaff6083eb7f6a37b042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Tests for the backend config module."""
from __future__ import annotations

import pytest

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 BUILTIN_SUPPORTED_PROFILE_NAMES
from mlia.target.config import get_builtin_profile_path
from mlia.target.config import get_builtin_supported_profile_names
from mlia.target.config import is_builtin_profile
from mlia.target.config import load_profile
from mlia.target.config import TargetInfo
from mlia.target.config import TargetProfile
from mlia.target.cortex_a.advisor import CortexAInferenceAdvisor
from mlia.target.cortex_a.config import CortexAConfiguration
from mlia.utils.registry import Registry


def test_builtin_supported_profile_names() -> None:
    """Test built-in profile names."""
    assert BUILTIN_SUPPORTED_PROFILE_NAMES == get_builtin_supported_profile_names()
    assert BUILTIN_SUPPORTED_PROFILE_NAMES == [
        "cortex-a",
        "ethos-u55-128",
        "ethos-u55-256",
        "ethos-u65-256",
        "ethos-u65-512",
        "tosa",
    ]
    for profile_name in BUILTIN_SUPPORTED_PROFILE_NAMES:
        assert is_builtin_profile(profile_name)
        profile_file = get_builtin_profile_path(profile_name)
        assert profile_file.is_file()


def test_builtin_profile_files() -> None:
    """Test function 'get_bulitin_profile_file'."""
    profile_file = get_builtin_profile_path("cortex-a")
    assert profile_file.is_file()

    profile_file = get_builtin_profile_path("UNKNOWN_FILE_THAT_DOES_NOT_EXIST")
    assert not profile_file.exists()


def test_load_profile() -> None:
    """Test getting profile data."""
    profile_file = get_builtin_profile_path("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.load_json_data({"target": "MySuperTarget"})
    assert profile.target == "MySuperTarget"

    profile = MyTargetProfile("")
    with pytest.raises(ValueError):
        profile.verify()


@pytest.mark.parametrize(
    ("advice", "check_system", "supported"),
    (
        (None, False, True),
        (None, True, True),
        (AdviceCategory.COMPATIBILITY, True, True),
        (AdviceCategory.OPTIMIZATION, True, False),
    ),
)
def test_target_info(
    monkeypatch: pytest.MonkeyPatch,
    advice: AdviceCategory | None,
    check_system: bool,
    supported: bool,
) -> None:
    """Test the class 'TargetInfo'."""
    info = TargetInfo(
        ["backend"],
        ["backend"],
        CortexAInferenceAdvisor,
        CortexAConfiguration,
    )

    backend_registry = Registry[BackendConfiguration]()
    backend_registry.register(
        "backend",
        BackendConfiguration(
            [AdviceCategory.COMPATIBILITY],
            [System.CURRENT],
            BackendType.BUILTIN,
        ),
    )
    monkeypatch.setattr("mlia.target.config.backend_registry", backend_registry)

    assert info.is_supported(advice, check_system) == supported
    assert bool(info.filter_supported_backends(advice, check_system)) == supported

    info = TargetInfo(
        ["unknown_backend"],
        ["unknown_backend"],
        CortexAInferenceAdvisor,
        CortexAConfiguration,
    )
    assert not info.is_supported(advice, check_system)
    assert not info.filter_supported_backends(advice, check_system)