aboutsummaryrefslogtreecommitdiff
path: root/tests/aiet/test_run_vela_script.py
blob: 971856eb434e7635c916b4b169968eac64543772 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
# pylint: disable=redefined-outer-name,no-self-use
"""Module for testing run_vela.py script."""
from pathlib import Path
from typing import Any
from typing import List

import pytest
from click.testing import CliRunner

from aiet.cli.common import MiddlewareExitCode
from aiet.resources.tools.vela.check_model import get_model_from_file
from aiet.resources.tools.vela.check_model import is_vela_optimised
from aiet.resources.tools.vela.run_vela import run_vela


@pytest.fixture(scope="session")
def vela_config_path(test_tools_path: Path) -> Path:
    """Return test systems path in a pytest fixture."""
    return test_tools_path / "vela" / "vela.ini"


@pytest.fixture(
    params=[
        ["ethos-u65-256", "Ethos_U65_High_End", "U65_Shared_Sram"],
        ["ethos-u55-32", "Ethos_U55_High_End_Embedded", "U55_Shared_Sram"],
    ]
)
def ethos_config(request: Any) -> Any:
    """Fixture to provide different configuration for Ethos-U optimization with Vela."""
    return request.param


# pylint: disable=too-many-arguments
def generate_args(
    input_: Path,
    output: Path,
    cfg: Path,
    acc_config: str,
    system_config: str,
    memory_mode: str,
) -> List[str]:
    """Generate arguments that can be passed to script 'run_vela'."""
    return [
        "-i",
        str(input_),
        "-o",
        str(output),
        "--config",
        str(cfg),
        "--accelerator-config",
        acc_config,
        "--system-config",
        system_config,
        "--memory-mode",
        memory_mode,
        "--optimise",
        "Performance",
    ]


def check_run_vela(
    cli_runner: CliRunner, args: List, expected_success: bool, output_file: Path
) -> None:
    """Run Vela with the given arguments and check the result."""
    result = cli_runner.invoke(run_vela, args)
    success = result.exit_code == MiddlewareExitCode.SUCCESS
    assert success == expected_success
    if success:
        model = get_model_from_file(output_file)
        assert is_vela_optimised(model)


def run_vela_script(
    cli_runner: CliRunner,
    input_model_file: Path,
    output_model_file: Path,
    vela_config: Path,
    expected_success: bool,
    acc_config: str,
    system_config: str,
    memory_mode: str,
) -> None:
    """Run the command 'run_vela' on the command line."""
    args = generate_args(
        input_model_file,
        output_model_file,
        vela_config,
        acc_config,
        system_config,
        memory_mode,
    )
    check_run_vela(cli_runner, args, expected_success, output_model_file)


class TestRunVelaCli:
    """Test the command-line execution of the run_vela command."""

    def test_non_optimised_model(
        self,
        cli_runner: CliRunner,
        non_optimised_input_model_file: Path,
        tmp_path: Path,
        vela_config_path: Path,
        ethos_config: List,
    ) -> None:
        """Verify Vela is run correctly on an unoptimised model."""
        run_vela_script(
            cli_runner,
            non_optimised_input_model_file,
            tmp_path / "test.tflite",
            vela_config_path,
            True,
            *ethos_config,
        )

    def test_optimised_model(
        self,
        cli_runner: CliRunner,
        optimised_input_model_file: Path,
        tmp_path: Path,
        vela_config_path: Path,
        ethos_config: List,
    ) -> None:
        """Verify Vela is run correctly on an already optimised model."""
        run_vela_script(
            cli_runner,
            optimised_input_model_file,
            tmp_path / "test.tflite",
            vela_config_path,
            True,
            *ethos_config,
        )

    def test_invalid_model(
        self,
        cli_runner: CliRunner,
        invalid_input_model_file: Path,
        tmp_path: Path,
        vela_config_path: Path,
        ethos_config: List,
    ) -> None:
        """Verify an error is raised when the input model is not valid."""
        run_vela_script(
            cli_runner,
            invalid_input_model_file,
            tmp_path / "test.tflite",
            vela_config_path,
            False,
            *ethos_config,
        )