blob: 0c024a6c9d90f9b3bf39274623d0819492f7f2f9 [file] [log] [blame]
Rickard Bolinbc6ee582022-11-04 08:24:29 +00001# SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com>
Jonas Ohlsson45e653d2021-07-26 16:13:12 +02002#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an AS IS BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Rickard Bolinbc6ee582022-11-04 08:24:29 +000016#
Jonas Ohlsson45e653d2021-07-26 16:13:12 +020017# Description:
18# The TosaSemantic class which is a collection of TOSA model semantic checks.
19from collections import defaultdict
20
21from .operation import Op
22from .tosa_mapping import optype_to_tosa_op_type
23
24
25class TosaSemantic:
26 # TODO populate this
27
28 def __init__(self):
29 # Setup the generic constraints. Note: the order matters
30 self.generic_constraints = []
31
32 # Setup specific constraints. Note: the order matters
33 self.specific_constraints = defaultdict(list)
34
35 def is_operator_semantic_valid(self, op):
36 ext_type = optype_to_tosa_op_type(op.type)
37
38 if op.type in (Op.Placeholder, Op.SubgraphInput, Op.Const):
39 return True
40
41 for constraint in self.generic_constraints + self.specific_constraints[op.type]:
42 valid, extra = constraint(op)
43 if not valid:
44 print(f"Warning: unsupported TOSA semantics for {ext_type} '{op.name}'.")
45 print(f" - {constraint.__doc__}")
46 if extra:
47 print(f" {extra}")
48 return False
49
50 return True
51
52
53def tosa_semantic_checker(nng):
54 semantic_checker = TosaSemantic()
55 for sg in nng.subgraphs:
56 for op in sg.get_all_ops():
57 op.run_on_npu = semantic_checker.is_operator_semantic_valid(op)
58 return nng