aboutsummaryrefslogtreecommitdiff
path: root/tests/test_target_cortex_a_advice_generation.py
blob: 916bdc126ffbeb05e9d7f508079ebe9042b0a6e0 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Tests for advice generation."""
from __future__ import annotations

import pytest

from mlia.backend.armnn_tflite_delegate.compat import (
    ARMNN_TFLITE_DELEGATE,
)
from mlia.core.advice_generation import Advice
from mlia.core.common import AdviceCategory
from mlia.core.common import DataItem
from mlia.core.context import ExecutionContext
from mlia.nn.tensorflow.tflite_graph import TFL_ACTIVATION_FUNCTION
from mlia.target.common.reporters import ModelHasCustomOperators
from mlia.target.common.reporters import ModelIsNotTFLiteCompatible
from mlia.target.common.reporters import TFLiteCompatibilityCheckFailed
from mlia.target.cortex_a.advice_generation import CortexAAdviceProducer
from mlia.target.cortex_a.config import CortexAConfiguration
from mlia.target.cortex_a.data_analysis import ModelIsCortexACompatible
from mlia.target.cortex_a.data_analysis import ModelIsNotCortexACompatible

VERSION = CortexAConfiguration.load_profile("cortex-a").armnn_tflite_delegate_version
BACKEND_INFO = (
    f"{ARMNN_TFLITE_DELEGATE['backend']} " f"{ARMNN_TFLITE_DELEGATE['ops'][VERSION]}"
)


@pytest.mark.parametrize(
    "input_data, advice_category, expected_advice",
    [
        [
            ModelIsNotCortexACompatible(BACKEND_INFO, {"UNSUPPORTED_OP"}, {}),
            {AdviceCategory.COMPATIBILITY},
            [
                Advice(
                    [
                        "The following operators are not supported by "
                        f"{BACKEND_INFO} and will fall back to the TensorFlow "
                        "Lite runtime:",
                        " - UNSUPPORTED_OP",
                    ]
                ),
                Advice(
                    [
                        "Please, refer to the full table of operators above "
                        "for more information.",
                        CortexAAdviceProducer.cortex_a_disclaimer,
                    ]
                ),
            ],
        ],
        [
            ModelIsNotCortexACompatible(
                BACKEND_INFO,
                {"UNSUPPORTED_OP"},
                {
                    "CONV_2D": ModelIsNotCortexACompatible.ActivationFunctionSupport(
                        used_unsupported={TFL_ACTIVATION_FUNCTION.SIGN_BIT.name},
                        supported={"RELU"},
                    )
                },
            ),
            {AdviceCategory.COMPATIBILITY},
            [
                Advice(
                    [
                        "The following operators are not supported by "
                        f"{BACKEND_INFO} and will fall back to the TensorFlow "
                        "Lite runtime:",
                        " - UNSUPPORTED_OP",
                    ]
                ),
                Advice(
                    [
                        "The fused activation functions of the following "
                        f"operators are not supported by {BACKEND_INFO}. "
                        "Please consider using one of the supported activation "
                        "functions instead:",
                        " - CONV_2D\n"
                        "   - Used unsupported: {'SIGN_BIT'}\n"
                        "   - Supported: {'RELU'}",
                    ]
                ),
                Advice(
                    [
                        "Please, refer to the full table of operators above "
                        "for more information.",
                        CortexAAdviceProducer.cortex_a_disclaimer,
                    ]
                ),
            ],
        ],
        [
            ModelIsCortexACompatible(BACKEND_INFO),
            {AdviceCategory.COMPATIBILITY},
            [
                Advice(
                    [
                        f"Model is fully compatible with {BACKEND_INFO} for Cortex-A.",
                        CortexAAdviceProducer.cortex_a_disclaimer,
                    ]
                )
            ],
        ],
        [
            ModelIsNotTFLiteCompatible(
                flex_ops=["flex_op1", "flex_op2"],
                custom_ops=["custom_op1", "custom_op2"],
            ),
            {AdviceCategory.COMPATIBILITY},
            [
                Advice(
                    [
                        "The following operators are not natively "
                        "supported by TensorFlow Lite: flex_op1, flex_op2.",
                        "Using select TensorFlow operators in TensorFlow Lite model "
                        "requires special initialization of TFLiteConverter and "
                        "TensorFlow Lite run-time.",
                        "Please refer to the TensorFlow documentation for "
                        "more details: "
                        "https://www.tensorflow.org/lite/guide/ops_select",
                        "Note, such models are not supported by "
                        "the ML Inference Advisor.",
                    ]
                ),
                Advice(
                    [
                        "The following operators appear to be custom and not natively "
                        "supported by TensorFlow Lite: custom_op1, custom_op2.",
                        "Using custom operators in TensorFlow Lite model "
                        "requires special initialization of TFLiteConverter and "
                        "TensorFlow Lite run-time.",
                        "Please refer to the TensorFlow documentation for "
                        "more details: "
                        "https://www.tensorflow.org/lite/guide/ops_custom",
                        "Note, such models are not supported by "
                        "the ML Inference Advisor.",
                    ]
                ),
            ],
        ],
        [
            ModelIsNotTFLiteCompatible(),
            {AdviceCategory.COMPATIBILITY},
            [
                Advice(
                    [
                        "Model could not be converted into TensorFlow Lite format.",
                        "Please refer to the table for more details.",
                    ]
                ),
            ],
        ],
        [
            ModelHasCustomOperators(),
            {AdviceCategory.COMPATIBILITY},
            [
                Advice(
                    [
                        "Models with custom operators require special initialization "
                        "and currently are not supported by the ML Inference Advisor.",
                    ]
                ),
            ],
        ],
        [
            TFLiteCompatibilityCheckFailed(),
            {AdviceCategory.COMPATIBILITY},
            [
                Advice(
                    [
                        "Model could not be converted into TensorFlow Lite format.",
                        "Please refer to the table for more details.",
                    ]
                ),
            ],
        ],
    ],
)
def test_cortex_a_advice_producer(
    tmpdir: str,
    input_data: DataItem,
    advice_category: set[AdviceCategory],
    expected_advice: list[Advice],
) -> None:
    """Test Cortex-A advice producer."""
    producer = CortexAAdviceProducer()

    context = ExecutionContext(
        advice_category=advice_category,
        output_dir=tmpdir,
    )

    producer.set_context(context)
    producer.produce_advice(input_data)

    assert producer.get_advice() == expected_advice