blob: 1a30d546c82d768369c24bbf1fa86997a3c31141 [file] [log] [blame]
Louis Verhaard7db78962020-05-25 15:05:26 +02001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
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.
16# Description:
17# Defines custom exceptions.
Tim Hallc8310b12020-06-17 14:53:11 +010018import sys
19
20from .operation import Operation
21from .tensor import Tensor
Louis Verhaard7db78962020-05-25 15:05:26 +020022
23
24class VelaError(Exception):
25 """Base class for vela exceptions"""
26
27 def __init__(self, data):
28 self.data = data
29
30 def __str__(self):
31 return repr(self.data)
32
33
34class InputFileError(VelaError):
35 """Raised when reading the input file results in errors"""
36
37 def __init__(self, file_name, msg):
Tim Hallc8310b12020-06-17 14:53:11 +010038 self.data = "Error reading input file {}: {}".format(file_name, msg)
Louis Verhaard7db78962020-05-25 15:05:26 +020039
40
41class UnsupportedFeatureError(VelaError):
42 """Raised when the input file uses non-supported features that cannot be handled"""
43
44 def __init__(self, data):
45 self.data = "The input file uses a feature that is currently not supported: {}".format(data)
46
47
48class OptionError(VelaError):
49 """Raised when an incorrect command line option is used"""
50
51 def __init__(self, option, option_value, msg):
Tim Hallc8310b12020-06-17 14:53:11 +010052 self.data = "Incorrect argument to CLI option: {} {}: {}".format(option, option_value, msg)
53
54
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020055class AllocationError(VelaError):
56 """Raised when allocation fails"""
57
58 def __init__(self, msg):
59 self.data = msg
60
61
Tim Hallc8310b12020-06-17 14:53:11 +010062def OperatorError(op, msg):
63 """Called when parsing an operator results in errors"""
64
65 assert isinstance(op, Operation)
66
67 if op.op_index is None:
68 data = "Invalid {} (name = {}) operator in the internal representation.".format(op.type, op.name)
69 else:
70 data = "Invalid {} (op_index = {}) operator in the input network.".format(op.type, op.op_index)
71
72 data += " {}\n".format(msg)
73
74 data += " Input tensors:\n"
75 for idx, tens in enumerate(op.inputs):
76 if isinstance(tens, Tensor):
77 tens_name = tens.name
78 else:
79 tens_name = "Not a Tensor"
80
81 data += " {} = {}\n".format(idx, tens_name)
82
83 data += " Output tensors:\n"
84 for idx, tens in enumerate(op.outputs):
85 if isinstance(tens, Tensor):
86 tens_name = tens.name
87 else:
88 tens_name = "Not a Tensor"
89
90 data += " {} = {}\n".format(idx, tens_name)
91
92 data = data[:-1] # remove last newline
93
94 print("Error: {}".format(data))
95 sys.exit(1)
96
97
98def TensorError(tens, msg):
99 """Called when parsing a tensor results in errors"""
100
101 assert isinstance(tens, Tensor)
102
103 data = "Invalid {} tensor. {}\n".format(tens.name, msg)
104
105 data += " Driving operators:\n"
106 for idx, op in enumerate(tens.ops):
107 if isinstance(op, Operation):
108 op_type = op.type
109 op_id = op.op_index
110 else:
111 op_type = "Not an Operation"
112 op_id = ""
113
114 data += " {} = {} ({})\n".format(idx, op_type, op_id)
115
116 data += " Consuming operators:\n"
117 for idx, op in enumerate(tens.consumer_list):
118 if isinstance(op, Operation):
119 op_type = op.type
120 op_id = op.op_index
121 else:
122 op_type = "Not an Operation"
123 op_id = ""
124
125 data += " {} = {} ({})\n".format(idx, op_type, op_id)
126
127 data = data[:-1] # remove last newline
128
129 print("Error: {}".format(data))
130 sys.exit(1)