Rickard Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame] | 1 | # SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 2 | # |
| 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 Bolin | bc6ee58 | 2022-11-04 08:24:29 +0000 | [diff] [blame] | 16 | # |
Jonas Ohlsson | 45e653d | 2021-07-26 16:13:12 +0200 | [diff] [blame] | 17 | # Description: |
| 18 | # The TosaSemantic class which is a collection of TOSA model semantic checks. |
| 19 | from collections import defaultdict |
| 20 | |
| 21 | from .operation import Op |
| 22 | from .tosa_mapping import optype_to_tosa_op_type |
| 23 | |
| 24 | |
| 25 | class 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 | |
| 53 | def 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 |