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 | import sys |
| 19 | |
| 20 | from .operation import Operation |
| 21 | from .tensor import Tensor |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 22 | |
| 23 | |
| 24 | class 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 | |
| 34 | class InputFileError(VelaError): |
| 35 | """Raised when reading the input file results in errors""" |
| 36 | |
| 37 | def __init__(self, file_name, msg): |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 38 | self.data = "Error reading input file {}: {}".format(file_name, msg) |
Louis Verhaard | 7db7896 | 2020-05-25 15:05:26 +0200 | [diff] [blame] | 39 | |
| 40 | |
| 41 | class 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 | |
| 48 | class OptionError(VelaError): |
| 49 | """Raised when an incorrect command line option is used""" |
| 50 | |
| 51 | def __init__(self, option, option_value, msg): |
Tim Hall | c8310b1 | 2020-06-17 14:53:11 +0100 | [diff] [blame] | 52 | self.data = "Incorrect argument to CLI option: {} {}: {}".format(option, option_value, msg) |
| 53 | |
| 54 | |
| 55 | def OperatorError(op, msg): |
| 56 | """Called when parsing an operator results in errors""" |
| 57 | |
| 58 | assert isinstance(op, Operation) |
| 59 | |
| 60 | if op.op_index is None: |
| 61 | data = "Invalid {} (name = {}) operator in the internal representation.".format(op.type, op.name) |
| 62 | else: |
| 63 | data = "Invalid {} (op_index = {}) operator in the input network.".format(op.type, op.op_index) |
| 64 | |
| 65 | data += " {}\n".format(msg) |
| 66 | |
| 67 | data += " Input tensors:\n" |
| 68 | for idx, tens in enumerate(op.inputs): |
| 69 | if isinstance(tens, Tensor): |
| 70 | tens_name = tens.name |
| 71 | else: |
| 72 | tens_name = "Not a Tensor" |
| 73 | |
| 74 | data += " {} = {}\n".format(idx, tens_name) |
| 75 | |
| 76 | data += " Output tensors:\n" |
| 77 | for idx, tens in enumerate(op.outputs): |
| 78 | if isinstance(tens, Tensor): |
| 79 | tens_name = tens.name |
| 80 | else: |
| 81 | tens_name = "Not a Tensor" |
| 82 | |
| 83 | data += " {} = {}\n".format(idx, tens_name) |
| 84 | |
| 85 | data = data[:-1] # remove last newline |
| 86 | |
| 87 | print("Error: {}".format(data)) |
| 88 | sys.exit(1) |
| 89 | |
| 90 | |
| 91 | def TensorError(tens, msg): |
| 92 | """Called when parsing a tensor results in errors""" |
| 93 | |
| 94 | assert isinstance(tens, Tensor) |
| 95 | |
| 96 | data = "Invalid {} tensor. {}\n".format(tens.name, msg) |
| 97 | |
| 98 | data += " Driving operators:\n" |
| 99 | for idx, op in enumerate(tens.ops): |
| 100 | if isinstance(op, Operation): |
| 101 | op_type = op.type |
| 102 | op_id = op.op_index |
| 103 | else: |
| 104 | op_type = "Not an Operation" |
| 105 | op_id = "" |
| 106 | |
| 107 | data += " {} = {} ({})\n".format(idx, op_type, op_id) |
| 108 | |
| 109 | data += " Consuming operators:\n" |
| 110 | for idx, op in enumerate(tens.consumer_list): |
| 111 | if isinstance(op, Operation): |
| 112 | op_type = op.type |
| 113 | op_id = op.op_index |
| 114 | else: |
| 115 | op_type = "Not an Operation" |
| 116 | op_id = "" |
| 117 | |
| 118 | data += " {} = {} ({})\n".format(idx, op_type, op_id) |
| 119 | |
| 120 | data = data[:-1] # remove last newline |
| 121 | |
| 122 | print("Error: {}".format(data)) |
| 123 | sys.exit(1) |