aboutsummaryrefslogtreecommitdiff
path: root/ethosu/vela/tosa_model_semantic.py
diff options
context:
space:
mode:
authorJonas Ohlsson <jonas.ohlsson@arm.com>2021-07-26 16:13:12 +0200
committerJonas Ohlsson <jonas.ohlsson@arm.com>2021-07-27 11:06:27 +0200
commit45e653dbd81633b8d78215b16a9b2205e39dd8e2 (patch)
tree18b3073eac45e9e8d69a616ae96d7a3fbdef9663 /ethosu/vela/tosa_model_semantic.py
parentc2449827ec55f49b6087e3e385fb3c4f6776dc6a (diff)
downloadethos-u-vela-45e653dbd81633b8d78215b16a9b2205e39dd8e2.tar.gz
MLBEDSW-4853: Refactor supported operators
Refactor supported operators by breaking out model semantics into its own class. Model semantics checked right after model read. Signed-off-by: Jonas Ohlsson <jonas.ohlsson@arm.com> Change-Id: If442b189efcd91dda01af60b2b3adedfacdf2fad
Diffstat (limited to 'ethosu/vela/tosa_model_semantic.py')
-rw-r--r--ethosu/vela/tosa_model_semantic.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/ethosu/vela/tosa_model_semantic.py b/ethosu/vela/tosa_model_semantic.py
new file mode 100644
index 00000000..5cd186c6
--- /dev/null
+++ b/ethosu/vela/tosa_model_semantic.py
@@ -0,0 +1,57 @@
+# Copyright (C) 2021 Arm Limited or its affiliates. All rights reserved.
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed under the Apache License, Version 2.0 (the License); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an AS IS BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# Description:
+# The TosaSemantic class which is a collection of TOSA model semantic checks.
+from collections import defaultdict
+
+from .operation import Op
+from .tosa_mapping import optype_to_tosa_op_type
+
+
+class TosaSemantic:
+ # TODO populate this
+
+ def __init__(self):
+ # Setup the generic constraints. Note: the order matters
+ self.generic_constraints = []
+
+ # Setup specific constraints. Note: the order matters
+ self.specific_constraints = defaultdict(list)
+
+ def is_operator_semantic_valid(self, op):
+ ext_type = optype_to_tosa_op_type(op.type)
+
+ if op.type in (Op.Placeholder, Op.SubgraphInput, Op.Const):
+ return True
+
+ for constraint in self.generic_constraints + self.specific_constraints[op.type]:
+ valid, extra = constraint(op)
+ if not valid:
+ print(f"Warning: unsupported TOSA semantics for {ext_type} '{op.name}'.")
+ print(f" - {constraint.__doc__}")
+ if extra:
+ print(f" {extra}")
+ return False
+
+ return True
+
+
+def tosa_semantic_checker(nng):
+ semantic_checker = TosaSemantic()
+ for sg in nng.subgraphs:
+ for op in sg.get_all_ops():
+ op.run_on_npu = semantic_checker.is_operator_semantic_valid(op)
+ return nng