aboutsummaryrefslogtreecommitdiff
path: root/tests/mlia/test_cli_helpers.py
blob: 2c528852b9be82b21ef8c86c53db1e5483668c80 (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
# SPDX-FileCopyrightText: Copyright 2022, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Tests for the helper classes."""
from typing import Any
from typing import Dict
from typing import List

import pytest

from mlia.cli.helpers import CLIActionResolver
from mlia.nn.tensorflow.optimizations.select import OptimizationSettings


class TestCliActionResolver:
    """Test cli action resolver."""

    @staticmethod
    @pytest.mark.parametrize(
        "args, params, expected_result",
        [
            [
                {},
                {"opt_settings": "some_setting"},
                [],
            ],
            [
                {},
                {},
                [
                    "Note: you will need a Keras model for that.",
                    "For example: mlia optimization --optimization-type "
                    "pruning,clustering --optimization-target 0.5,32 "
                    "/path/to/keras_model",
                    "For more info: mlia optimization --help",
                ],
            ],
            [
                {"model": "model.h5"},
                {},
                [
                    "For example: mlia optimization --optimization-type "
                    "pruning,clustering --optimization-target 0.5,32 model.h5",
                    "For more info: mlia optimization --help",
                ],
            ],
            [
                {"model": "model.h5"},
                {"opt_settings": [OptimizationSettings("pruning", 0.5, None)]},
                [
                    "For more info: mlia optimization --help",
                    "Optimization command: "
                    "mlia optimization --optimization-type pruning "
                    "--optimization-target 0.5 model.h5",
                ],
            ],
            [
                {"model": "model.h5", "target_profile": "target_profile"},
                {"opt_settings": [OptimizationSettings("pruning", 0.5, None)]},
                [
                    "For more info: mlia optimization --help",
                    "Optimization command: "
                    "mlia optimization --optimization-type pruning "
                    "--optimization-target 0.5 "
                    "--target-profile target_profile model.h5",
                ],
            ],
        ],
    )
    def test_apply_optimizations(
        args: Dict[str, Any],
        params: Dict[str, Any],
        expected_result: List[str],
    ) -> None:
        """Test action resolving for applying optimizations."""
        resolver = CLIActionResolver(args)
        assert resolver.apply_optimizations(**params) == expected_result

    @staticmethod
    def test_supported_operators_info() -> None:
        """Test supported operators info."""
        resolver = CLIActionResolver({})
        assert resolver.supported_operators_info() == [
            "For guidance on supported operators, run: mlia operators "
            "--supported-ops-report",
        ]

    @staticmethod
    def test_operator_compatibility_details() -> None:
        """Test operator compatibility details info."""
        resolver = CLIActionResolver({})
        assert resolver.operator_compatibility_details() == [
            "For more details, run: mlia operators --help"
        ]

    @staticmethod
    def test_optimization_details() -> None:
        """Test optimization details info."""
        resolver = CLIActionResolver({})
        assert resolver.optimization_details() == [
            "For more info, see: mlia optimization --help"
        ]

    @staticmethod
    @pytest.mark.parametrize(
        "args, expected_result",
        [
            [
                {},
                [],
            ],
            [
                {"model": "model.tflite"},
                [
                    "Check the estimated performance by running the "
                    "following command: ",
                    "mlia performance model.tflite",
                ],
            ],
            [
                {"model": "model.tflite", "target_profile": "target_profile"},
                [
                    "Check the estimated performance by running the "
                    "following command: ",
                    "mlia performance --target-profile target_profile model.tflite",
                ],
            ],
        ],
    )
    def test_check_performance(
        args: Dict[str, Any], expected_result: List[str]
    ) -> None:
        """Test check performance info."""
        resolver = CLIActionResolver(args)
        assert resolver.check_performance() == expected_result

    @staticmethod
    @pytest.mark.parametrize(
        "args, expected_result",
        [
            [
                {},
                [],
            ],
            [
                {"model": "model.tflite"},
                [
                    "Try running the following command to verify that:",
                    "mlia operators model.tflite",
                ],
            ],
            [
                {"model": "model.tflite", "target_profile": "target_profile"},
                [
                    "Try running the following command to verify that:",
                    "mlia operators --target-profile target_profile model.tflite",
                ],
            ],
        ],
    )
    def test_check_operator_compatibility(
        args: Dict[str, Any], expected_result: List[str]
    ) -> None:
        """Test checking operator compatibility info."""
        resolver = CLIActionResolver(args)
        assert resolver.check_operator_compatibility() == expected_result