Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 1 | # 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 Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 18 | from .operation import Operation |
| 19 | from .tensor import Tensor |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 20 | |
| 21 | |
| 22 | class VelaError(Exception): |
| 23 | """Base class for vela exceptions""" |
| 24 | |
| 25 | def __init__(self, data): |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 26 | self.data = "Error: " + data |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 27 | |
| 28 | def __str__(self): |
| 29 | return repr(self.data) |
| 30 | |
| 31 | |
| 32 | class InputFileError(VelaError): |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 33 | """Raised when reading an input file results in errors""" |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 34 | |
| 35 | def __init__(self, file_name, msg): |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 36 | self.data = "Reading input file {}: {}".format(file_name, msg) |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 37 | |
| 38 | |
| 39 | class UnsupportedFeatureError(VelaError): |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 40 | """Raised when the input network uses non-supported features that cannot be handled""" |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 41 | |
| 42 | def __init__(self, data): |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 43 | self.data = "Input network uses a feature that is currently not supported: {}".format(data) |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 44 | |
| 45 | |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 46 | class 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 Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 53 | |
| 54 | def __init__(self, option, option_value, msg): |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 55 | self.data = "Incorrect argument to CLI option: {} = {}: {}".format(option, option_value, msg) |
| 56 | |
| 57 | |
| 58 | class 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 Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 72 | |
| 73 | |
Jacob Bohlin | 0628a8c | 2020-08-28 13:25:14 +0200 | [diff] [blame] | 74 | class AllocationError(VelaError): |
| 75 | """Raised when allocation fails""" |
| 76 | |
| 77 | def __init__(self, msg): |
| 78 | self.data = msg |
| 79 | |
| 80 | |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 81 | def OperatorError(op, msg): |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 82 | """ |
| 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 Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 88 | |
| 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 Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 118 | raise VelaError(data) |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 119 | |
| 120 | |
| 121 | def TensorError(tens, msg): |
Tim Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 122 | """ |
| 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 Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 128 | |
| 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 Hall | 1bd531d | 2020-11-01 20:59:36 +0000 | [diff] [blame] | 157 | raise VelaError(data) |