aboutsummaryrefslogtreecommitdiff
path: root/tests/mlia/test_devices_ethosu_config.py
blob: 49c999a97fe5016af9ee7c2fe97d2725cd1c9a39 (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
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Tests for config module."""
from contextlib import ExitStack as does_not_raise
from typing import Any
from typing import Dict
from unittest.mock import MagicMock

import pytest

from mlia.devices.ethosu.config import EthosUConfiguration
from mlia.devices.ethosu.config import get_target
from mlia.tools.vela_wrapper import VelaCompilerOptions
from mlia.utils.filesystem import get_vela_config


def test_compiler_options_default_init() -> None:
    """Test compiler options default init."""
    opts = VelaCompilerOptions()

    assert opts.config_files is None
    assert opts.system_config == "internal-default"
    assert opts.memory_mode == "internal-default"
    assert opts.accelerator_config is None
    assert opts.max_block_dependency == 3
    assert opts.arena_cache_size is None
    assert opts.tensor_allocator == "HillClimb"
    assert opts.cpu_tensor_alignment == 16
    assert opts.optimization_strategy == "Performance"
    assert opts.output_dir is None


def test_ethosu_target() -> None:
    """Test Ethos-U target configuration init."""
    default_config = EthosUConfiguration("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="Unable to find target profile unknown"):
        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'\]",
            ),
        ],
        [
            {"target": "ethos-u65", "mac": 512},
            pytest.raises(
                Exception,
                match="Mandatory fields missing from target profile: "
                r"\['memory_mode', 'system_config'\]",
            ),
        ],
        [
            {
                "target": "ethos-u65",
                "mac": 2,
                "system_config": "Ethos_U65_Embedded",
                "memory_mode": "Shared_Sram",
            },
            pytest.raises(
                Exception,
                match=r"Mac value for selected device should be in \[256, 512\]",
            ),
        ],
        [
            {
                "target": "ethos-u55",
                "mac": 1,
                "system_config": "Ethos_U55_High_End_Embedded",
                "memory_mode": "Shared_Sram",
            },
            pytest.raises(
                Exception,
                match="Mac value for selected device should be "
                r"in \[32, 64, 128, 256\]",
            ),
        ],
        [
            {
                "target": "ethos-u65",
                "mac": 512,
                "system_config": "Ethos_U65_Embedded",
                "memory_mode": "Shared_Sram",
            },
            does_not_raise(),
        ],
    ],
)
def test_ethosu_configuration(
    monkeypatch: pytest.MonkeyPatch, profile_data: Dict[str, Any], expected_error: Any
) -> None:
    """Test creating Ethos-U configuration."""
    monkeypatch.setattr(
        "mlia.devices.ethosu.config.get_profile", MagicMock(return_value=profile_data)
    )

    with expected_error:
        EthosUConfiguration("target")