aboutsummaryrefslogtreecommitdiff
path: root/src/mlia/target/cortex_a/reporters.py
blob: 65d7906abcf8106c0c1728740db16005800893ec (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
# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Reports module."""
from __future__ import annotations

from typing import Any
from typing import Callable
from typing import cast

from mlia.core.advice_generation import Advice
from mlia.core.reporters import report_advice
from mlia.core.reporting import Cell
from mlia.core.reporting import Column
from mlia.core.reporting import Format
from mlia.core.reporting import NestedReport
from mlia.core.reporting import Report
from mlia.core.reporting import ReportItem
from mlia.core.reporting import Table
from mlia.nn.tensorflow.tflite_compat import TFLiteCompatibilityInfo
from mlia.target.cortex_a.config import CortexAConfiguration
from mlia.target.cortex_a.operators import CortexACompatibilityInfo
from mlia.utils.console import style_improvement
from mlia.utils.types import is_list_of


def report_target(target_config: CortexAConfiguration) -> Report:
    """Generate report for the target."""
    return NestedReport(
        "Target information",
        "target",
        [
            ReportItem("Target", alias="target", value=target_config.target),
        ],
    )


def report_tflite_compatiblity(compat_info: TFLiteCompatibilityInfo) -> Report:
    """Generate report for the TensorFlow Lite compatibility information."""
    if compat_info.conversion_errors:
        return Table(
            [
                Column("#", only_for=["plain_text"]),
                Column("Operator", alias="operator"),
                Column(
                    "Operator location",
                    alias="operator_location",
                    fmt=Format(wrap_width=25),
                ),
                Column("Error code", alias="error_code"),
                Column(
                    "Error message", alias="error_message", fmt=Format(wrap_width=25)
                ),
            ],
            [
                (
                    index + 1,
                    err.operator,
                    ", ".join(err.location),
                    err.code.name,
                    err.message,
                )
                for index, err in enumerate(compat_info.conversion_errors)
            ],
            name="TensorFlow Lite conversion errors",
            alias="tensorflow_lite_conversion_errors",
        )

    return Table(
        columns=[
            Column("Reason", alias="reason"),
            Column(
                "Exception details",
                alias="exception_details",
                fmt=Format(wrap_width=40),
            ),
        ],
        rows=[
            (
                "TensorFlow Lite compatibility check failed with exception",
                str(compat_info.conversion_exception),
            ),
        ],
        name="TensorFlow Lite compatibility errors",
        alias="tflite_compatibility",
    )


def report_cortex_a_operators(op_compat: CortexACompatibilityInfo) -> Report:
    """Generate report for the operators."""
    return Table(
        [
            Column("#", only_for=["plain_text"]),
            Column(
                "Operator location",
                alias="operator_location",
                fmt=Format(wrap_width=30),
            ),
            Column("Operator name", alias="operator_name", fmt=Format(wrap_width=20)),
            Column(
                "Arm NN TFLite Delegate compatibility",
                alias="cortex_a_compatible",
                fmt=Format(wrap_width=40),
            ),
        ],
        [
            (
                index + 1,
                op.location,
                op.full_name,
                Cell(
                    op_compat.get_support_type(op),
                    Format(
                        wrap_width=30,
                        style=style_improvement(op_compat.is_op_compatible(op)),
                        str_fmt=lambda v: cast(str, v.value),
                    ),
                ),
            )
            for index, op in enumerate(op_compat.operators)
        ],
        name="Operators",
        alias="operators",
    )


def cortex_a_formatters(data: Any) -> Callable[[Any], Report]:
    """Find appropriate formatter for the provided data."""
    if is_list_of(data, Advice):
        return report_advice

    if isinstance(data, CortexAConfiguration):
        return report_target

    if isinstance(data, TFLiteCompatibilityInfo):
        return report_tflite_compatiblity

    if isinstance(data, CortexACompatibilityInfo):
        return report_cortex_a_operators

    raise Exception(f"Unable to find appropriate formatter for {data}.")