aboutsummaryrefslogtreecommitdiff
path: root/tests/test_cli_options.py
blob: c02ef89f32bb37abad60fad3a8bc61586d94ebcb (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Tests for module options."""
from __future__ import annotations

import argparse
from contextlib import ExitStack as does_not_raise
from typing import Any

import pytest

from mlia.cli.options import get_output_format
from mlia.cli.options import get_target_profile_opts
from mlia.cli.options import parse_optimization_parameters
from mlia.core.typing import OutputFormat


@pytest.mark.parametrize(
    "pruning, clustering, pruning_target, clustering_target, expected_error,"
    "expected_result",
    [
        [
            False,
            False,
            None,
            None,
            does_not_raise(),
            [
                {
                    "optimization_type": "pruning",
                    "optimization_target": 0.5,
                    "layers_to_optimize": None,
                }
            ],
        ],
        [
            True,
            False,
            None,
            None,
            does_not_raise(),
            [
                {
                    "optimization_type": "pruning",
                    "optimization_target": 0.5,
                    "layers_to_optimize": None,
                }
            ],
        ],
        [
            False,
            True,
            None,
            None,
            does_not_raise(),
            [
                {
                    "optimization_type": "clustering",
                    "optimization_target": 32,
                    "layers_to_optimize": None,
                }
            ],
        ],
        [
            True,
            True,
            None,
            None,
            does_not_raise(),
            [
                {
                    "optimization_type": "pruning",
                    "optimization_target": 0.5,
                    "layers_to_optimize": None,
                },
                {
                    "optimization_type": "clustering",
                    "optimization_target": 32,
                    "layers_to_optimize": None,
                },
            ],
        ],
        [
            False,
            False,
            0.4,
            None,
            does_not_raise(),
            [
                {
                    "optimization_type": "pruning",
                    "optimization_target": 0.4,
                    "layers_to_optimize": None,
                }
            ],
        ],
        [
            False,
            False,
            None,
            32,
            pytest.raises(
                argparse.ArgumentError,
                match="To enable clustering optimization you need to include "
                "the `--clustering` flag in your command.",
            ),
            None,
        ],
        [
            False,
            True,
            None,
            32.2,
            does_not_raise(),
            [
                {
                    "optimization_type": "clustering",
                    "optimization_target": 32.2,
                    "layers_to_optimize": None,
                }
            ],
        ],
    ],
)
def test_parse_optimization_parameters(
    pruning: bool,
    clustering: bool,
    pruning_target: float | None,
    clustering_target: int | None,
    expected_error: Any,
    expected_result: Any,
) -> None:
    """Test function parse_optimization_parameters."""
    with expected_error:
        result = parse_optimization_parameters(
            pruning, clustering, pruning_target, clustering_target
        )
        assert result == expected_result


@pytest.mark.parametrize(
    "args, expected_opts",
    [
        [
            {},
            [],
        ],
        [
            {"target_profile": "profile"},
            ["--target-profile", "profile"],
        ],
        [
            # for the default profile empty list should be returned
            {"target": "ethos-u55-256"},
            [],
        ],
    ],
)
def test_get_target_opts(args: dict | None, expected_opts: list[str]) -> None:
    """Test getting target options."""
    assert get_target_profile_opts(args) == expected_opts


@pytest.mark.parametrize(
    "args, expected_output_format",
    [
        [
            {},
            "plain_text",
        ],
        [
            {"json": True},
            "json",
        ],
        [
            {"json": False},
            "plain_text",
        ],
    ],
)
def test_get_output_format(args: dict, expected_output_format: OutputFormat) -> None:
    """Test get_output_format function."""
    arguments = argparse.Namespace(**args)
    output_format = get_output_format(arguments)
    assert output_format == expected_output_format