blob: 2a635d0e842237a48d85764a4eecb9d9042de004 [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 +010018from .operation import Operation
19from .tensor import Tensor
Louis Verhaard7db78962020-05-25 15:05:26 +020020
21
22class VelaError(Exception):
23 """Base class for vela exceptions"""
24
25 def __init__(self, data):
Tim Hall1bd531d2020-11-01 20:59:36 +000026 self.data = "Error: " + data
Louis Verhaard7db78962020-05-25 15:05:26 +020027
28 def __str__(self):
29 return repr(self.data)
30
31
32class InputFileError(VelaError):
Tim Hall1bd531d2020-11-01 20:59:36 +000033 """Raised when reading an input file results in errors"""
Louis Verhaard7db78962020-05-25 15:05:26 +020034
35 def __init__(self, file_name, msg):
Tim Hall1bd531d2020-11-01 20:59:36 +000036 self.data = "Reading input file {}: {}".format(file_name, msg)
Louis Verhaard7db78962020-05-25 15:05:26 +020037
38
39class UnsupportedFeatureError(VelaError):
Tim Hall1bd531d2020-11-01 20:59:36 +000040 """Raised when the input network uses non-supported features that cannot be handled"""
Louis Verhaard7db78962020-05-25 15:05:26 +020041
42 def __init__(self, data):
Tim Hall1bd531d2020-11-01 20:59:36 +000043 self.data = "Input network uses a feature that is currently not supported: {}".format(data)
Louis Verhaard7db78962020-05-25 15:05:26 +020044
45
Tim Hall1bd531d2020-11-01 20:59:36 +000046class CliOptionError(VelaError):
47 """Raised for errors encountered with a command line option
48
49 :param option: str object that contains the name of the command line option
50 :param option_value: the command line option that resulted in the error
51 :param msg: str object that contains a description of the specific error encountered
52 """
Louis Verhaard7db78962020-05-25 15:05:26 +020053
54 def __init__(self, option, option_value, msg):
Tim Hall1bd531d2020-11-01 20:59:36 +000055 self.data = "Incorrect argument to CLI option: {} = {}: {}".format(option, option_value, msg)
56
57
58class ConfigOptionError(VelaError):
59 """Raised for errors encountered with a configuration option
60
61 :param option: str object that contains the name of the configuration option
62 :param option_value: the configuration option that resulted in the error
63 :param option_valid_values (optional): str object that contains the valid configuration option values
64 """
65
66 def __init__(self, option, option_value, option_valid_values=None):
67 self.data = "Invalid configuration of {} = {}".format(option, option_value)
68 if option_valid_values is not None:
69 self.data += " (must be {}).".format(option_valid_values)
70 else:
71 self.data += "."
Tim Hallc8310b12020-06-17 14:53:11 +010072
73
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020074class AllocationError(VelaError):
75 """Raised when allocation fails"""
76
77 def __init__(self, msg):
78 self.data = msg
79
80
Tim Hallc8310b12020-06-17 14:53:11 +010081def OperatorError(op, msg):
Tim Hall1bd531d2020-11-01 20:59:36 +000082 """
83 Raises a VelaError exception for errors encountered when parsing an Operation
84
85 :param op: Operation object that resulted in the error
86 :param msg: str object that contains a description of the specific error encountered
87 """
Tim Hallc8310b12020-06-17 14:53:11 +010088
89 assert isinstance(op, Operation)
90
91 if op.op_index is None:
92 data = "Invalid {} (name = {}) operator in the internal representation.".format(op.type, op.name)
93 else:
94 data = "Invalid {} (op_index = {}) operator in the input network.".format(op.type, op.op_index)
95
96 data += " {}\n".format(msg)
97
98 data += " Input tensors:\n"
99 for idx, tens in enumerate(op.inputs):
100 if isinstance(tens, Tensor):
101 tens_name = tens.name
102 else:
103 tens_name = "Not a Tensor"
104
105 data += " {} = {}\n".format(idx, tens_name)
106
107 data += " Output tensors:\n"
108 for idx, tens in enumerate(op.outputs):
109 if isinstance(tens, Tensor):
110 tens_name = tens.name
111 else:
112 tens_name = "Not a Tensor"
113
114 data += " {} = {}\n".format(idx, tens_name)
115
116 data = data[:-1] # remove last newline
117
Tim Hall1bd531d2020-11-01 20:59:36 +0000118 raise VelaError(data)
Tim Hallc8310b12020-06-17 14:53:11 +0100119
120
121def TensorError(tens, msg):
Tim Hall1bd531d2020-11-01 20:59:36 +0000122 """
123 Raises a VelaError exception for errors encountered when parsing a Tensor
124
125 :param tens: Tensor object that resulted in the error
126 :param msg: str object that contains a description of the specific error encountered
127 """
Tim Hallc8310b12020-06-17 14:53:11 +0100128
129 assert isinstance(tens, Tensor)
130
131 data = "Invalid {} tensor. {}\n".format(tens.name, msg)
132
133 data += " Driving operators:\n"
134 for idx, op in enumerate(tens.ops):
135 if isinstance(op, Operation):
136 op_type = op.type
137 op_id = op.op_index
138 else:
139 op_type = "Not an Operation"
140 op_id = ""
141
142 data += " {} = {} ({})\n".format(idx, op_type, op_id)
143
144 data += " Consuming operators:\n"
145 for idx, op in enumerate(tens.consumer_list):
146 if isinstance(op, Operation):
147 op_type = op.type
148 op_id = op.op_index
149 else:
150 op_type = "Not an Operation"
151 op_id = ""
152
153 data += " {} = {} ({})\n".format(idx, op_type, op_id)
154
155 data = data[:-1] # remove last newline
156
Tim Hall1bd531d2020-11-01 20:59:36 +0000157 raise VelaError(data)