aboutsummaryrefslogtreecommitdiff
path: root/tests/test_target_config.py
blob: c6235a5828327178949eb92736b6cc8a2622b23a (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
# 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

from pathlib import Path

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 copy_profile_file_to_output_dir
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_copy_profile_file_to_output_dir(tmp_path: Path) -> None:
    """Test if the profile file is copied into the output directory."""
    test_target_profile_name = "ethos-u55-128"
    test_file_path = Path(f"{tmp_path}/{test_target_profile_name}.toml")

    copy_profile_file_to_output_dir(test_target_profile_name, tmp_path)
    assert Path.is_file(test_file_path)


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(
    ("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_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