blob: b241db8e76862bbced77dd53afce34842fbe2538 [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):
Michael McGeagh7a6f8432020-12-02 15:29:22 +000026 self.data = f"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):
Michael McGeagh7a6f8432020-12-02 15:29:22 +000036 super().__init__(f"Reading input file '{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):
Michael McGeagh7a6f8432020-12-02 15:29:22 +000043 super().__init__(f"Input network uses a feature that is currently not supported: {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):
Michael McGeagh7a6f8432020-12-02 15:29:22 +000055 super().__init__(f"Incorrect argument to CLI option {option}={option_value}: {msg}")
Tim Hall1bd531d2020-11-01 20:59:36 +000056
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):
Michael McGeagh7a6f8432020-12-02 15:29:22 +000067 data = f"Invalid configuration of {option}={option_value}"
Tim Hall1bd531d2020-11-01 20:59:36 +000068 if option_valid_values is not None:
Michael McGeagh7a6f8432020-12-02 15:29:22 +000069 data += f" (must be {option_valid_values})"
70 super().__init__(data)
Tim Hallc8310b12020-06-17 14:53:11 +010071
72
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020073class AllocationError(VelaError):
74 """Raised when allocation fails"""
75
76 def __init__(self, msg):
Michael McGeagh7a6f8432020-12-02 15:29:22 +000077 super().__init__(f"Allocation failed: {msg}")
Jacob Bohlin0628a8c2020-08-28 13:25:14 +020078
79
Tim Hallc8310b12020-06-17 14:53:11 +010080def OperatorError(op, msg):
Tim Hall1bd531d2020-11-01 20:59:36 +000081 """
82 Raises a VelaError exception for errors encountered when parsing an Operation
83
84 :param op: Operation object that resulted in the error
85 :param msg: str object that contains a description of the specific error encountered
86 """
Tim Hallc8310b12020-06-17 14:53:11 +010087
Michael McGeagh7a6f8432020-12-02 15:29:22 +000088 def _print_tensors(tensors):
89 lines = []
90 for idx, tens in enumerate(tensors):
91 if isinstance(tens, Tensor):
92 tens_name = tens.name
93 else:
94 tens_name = "Not a Tensor"
95 lines.append(f" {idx} = {tens_name}")
96 return lines
97
Tim Hallc8310b12020-06-17 14:53:11 +010098 assert isinstance(op, Operation)
99
100 if op.op_index is None:
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000101 lines = [f"Invalid {op.type} (name = {op.name}) operator in the internal representation. {msg}"]
Tim Hallc8310b12020-06-17 14:53:11 +0100102 else:
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000103 lines = [f"Invalid {op.type} (op_index = {op.op_index}) operator in the input network. {msg}"]
Tim Hallc8310b12020-06-17 14:53:11 +0100104
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000105 lines += [" Input tensors:"]
106 lines += _print_tensors(op.inputs)
Tim Hallc8310b12020-06-17 14:53:11 +0100107
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000108 lines += [" Output tensors:"]
109 lines += _print_tensors(op.outputs)
Tim Hallc8310b12020-06-17 14:53:11 +0100110
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000111 raise VelaError("\n".join(lines))
Tim Hallc8310b12020-06-17 14:53:11 +0100112
113
114def TensorError(tens, msg):
Tim Hall1bd531d2020-11-01 20:59:36 +0000115 """
116 Raises a VelaError exception for errors encountered when parsing a Tensor
117
118 :param tens: Tensor object that resulted in the error
119 :param msg: str object that contains a description of the specific error encountered
120 """
Tim Hallc8310b12020-06-17 14:53:11 +0100121
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000122 def _print_operators(ops):
123 lines = []
124 for idx, op in enumerate(ops):
125 if isinstance(op, Operation):
126 op_type = op.type
127 op_id = f"({op.op_index})"
128 else:
129 op_type = "Not an Operation"
130 op_id = ""
131 lines.append(f" {idx} = {op_type} {op_id}")
132 return lines
133
Tim Hallc8310b12020-06-17 14:53:11 +0100134 assert isinstance(tens, Tensor)
135
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000136 lines = [f"Invalid {tens.name} tensor. {msg}"]
Tim Hallc8310b12020-06-17 14:53:11 +0100137
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000138 lines += [" Driving operators:"]
139 lines += _print_operators(tens.ops)
Tim Hallc8310b12020-06-17 14:53:11 +0100140
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000141 lines += [" Consuming operators:"]
142 lines += _print_operators(tens.consumer_list)
Tim Hallc8310b12020-06-17 14:53:11 +0100143
Michael McGeagh7a6f8432020-12-02 15:29:22 +0000144 raise VelaError("\n".join(lines))