blob: 3161618501091bdcac202c7bd505ce1c9d541d98 (
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
|
# SPDX-FileCopyrightText: Copyright 2022-2023, Arm Limited and/or its affiliates.
# SPDX-License-Identifier: Apache-2.0
"""Cortex-A data analysis module."""
from __future__ import annotations
from collections import defaultdict
from dataclasses import dataclass
from dataclasses import field
from functools import singledispatchmethod
from mlia.core.common import DataItem
from mlia.core.data_analysis import Fact
from mlia.core.data_analysis import FactExtractor
from mlia.nn.tensorflow.tflite_compat import TFLiteCompatibilityInfo
from mlia.target.common.reporters import analyze_tflite_compatibility_common
from mlia.target.cortex_a.operators import CortexACompatibilityInfo
class CortexADataAnalyzer(FactExtractor):
"""Cortex-A data analyzer."""
@singledispatchmethod
def analyze_data(self, data_item: DataItem) -> None: # type: ignore
"""Analyse the data."""
@analyze_data.register
def analyze_operator_compatibility(
self, data_item: CortexACompatibilityInfo
) -> None:
"""Analyse operator compatibility information."""
if data_item.is_cortex_a_compatible:
self.add_fact(ModelIsCortexACompatible(data_item.backend_info))
else:
unsupported_ops = set()
activation_func_support: defaultdict[
str, ModelIsNotCortexACompatible.ActivationFunctionSupport
] = defaultdict(ModelIsNotCortexACompatible.ActivationFunctionSupport)
for oper in data_item.operators:
support_type = data_item.get_support_type(oper)
if support_type == data_item.SupportType.OP_NOT_SUPPORTED:
unsupported_ops.add(oper.full_name)
elif support_type == data_item.SupportType.ACTIVATION_NOT_SUPPORTED:
# Add used but unsupported actication functions
activation_func_support[oper.full_name].used_unsupported.add(
oper.activation_func.name
)
# Add supported activation functions
activation_func_support[oper.full_name].supported.update(
data_item.supported_activation_functions(oper)
)
assert (
unsupported_ops or activation_func_support or not data_item.operators
), (
"The model is marked as not compatible with Cortex-A but there "
"are no unsupported ops activation functions listed."
)
self.add_fact(
ModelIsNotCortexACompatible(
data_item.backend_info, unsupported_ops, activation_func_support
)
)
@analyze_data.register
def analyze_tflite_compatibility(self, data_item: TFLiteCompatibilityInfo) -> None:
"""Analyze TensorFlow Lite compatibility information."""
analyze_tflite_compatibility_common(self, data_item)
@dataclass
class CortexACompatibility(Fact):
"""Base class for Cortex-A compatibility providing backend info."""
backend_info: str
@dataclass
class ModelIsCortexACompatible(CortexACompatibility):
"""Model is completely compatible with Cortex-A."""
@dataclass
class ModelIsNotCortexACompatible(CortexACompatibility):
"""Model is not compatible with Cortex-A."""
@dataclass
class ActivationFunctionSupport:
"""Activation function support per operator."""
used_unsupported: set[str] = field(default_factory=set)
supported: set[str] = field(default_factory=set)
unsupported_ops: set[str]
activation_func_support: dict[str, ActivationFunctionSupport]
|