erik.andersson@arm.com | 460c689 | 2021-02-24 14:38:09 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020-2021 Arm Limited or its affiliates. All rights reserved. |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 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 | # Utilities used in vela unit tests |
| 18 | import numpy as np |
| 19 | |
| 20 | from ethosu.vela import architecture_features |
| 21 | from ethosu.vela.data_type import DataType |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 22 | from ethosu.vela.nn_graph import Graph |
| 23 | from ethosu.vela.nn_graph import PassPlacement |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 24 | from ethosu.vela.nn_graph import Subgraph |
Dwight Lidman | 0dd21c7 | 2020-11-24 13:45:50 +0100 | [diff] [blame] | 25 | from ethosu.vela.operation import Op |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 26 | from ethosu.vela.operation import Operation |
| 27 | from ethosu.vela.tensor import create_const_tensor |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 28 | from ethosu.vela.tensor import QuantizationParameters |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 29 | from ethosu.vela.tensor import Tensor |
| 30 | |
| 31 | |
| 32 | def create_arch(): |
Louis Verhaard | 5207830 | 2020-11-18 13:35:06 +0100 | [diff] [blame] | 33 | return architecture_features.create_default_arch(architecture_features.Accelerator.Ethos_U55_128) |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 34 | |
| 35 | |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 36 | def default_quant_params(): |
| 37 | qp = QuantizationParameters() |
| 38 | qp.scale_f32 = np.float32(1) |
| 39 | qp.zero_point = 0 |
| 40 | return qp |
| 41 | |
| 42 | |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 43 | def create_elemwise_op( |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 44 | op_type, |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 45 | name, |
| 46 | ifm_shape, |
| 47 | ifm2_shape, |
| 48 | ofm_shape, |
| 49 | datatype=DataType.uint8, |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 50 | ifm_quant=default_quant_params(), |
| 51 | ifm2_quant=default_quant_params(), |
| 52 | ofm_quant=default_quant_params(), |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 53 | ): |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 54 | # Creates elementwise operation with constant IFM/IFM2 |
| 55 | if datatype.size_in_bytes() == 1: |
| 56 | np_type = np.uint8 |
| 57 | elif datatype.size_in_bytes() == 2: |
| 58 | np_type = np.int16 |
| 59 | else: |
| 60 | np_type = np.int32 |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 61 | op = Operation(op_type, name) |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 62 | op.add_input_tensor( |
| 63 | create_const_tensor(name + "_ifm", ifm_shape, datatype, np.zeros(ifm_shape), np_type, quantization=ifm_quant) |
| 64 | ) |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 65 | if ifm2_shape is not None: |
| 66 | op.add_input_tensor( |
| 67 | create_const_tensor( |
| 68 | name + "_ifm2", ifm2_shape, datatype, np.zeros(ifm2_shape), np_type, quantization=ifm2_quant |
| 69 | ) |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 70 | ) |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 71 | ofm = Tensor(ofm_shape, datatype, name + "_ofm") |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame] | 72 | ofm.quantization = ofm_quant |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 73 | op.set_output_tensor(ofm) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 74 | op.set_ifm_ofm_shapes() |
| 75 | |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 76 | return op |
| 77 | |
| 78 | |
Dwight Lidman | c718743 | 2020-11-16 17:40:46 +0100 | [diff] [blame] | 79 | def create_op_with_quant_tensors( |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 80 | op_type, ifm_shape, ofm_shape, weights_shape=None, bias_shape=None, datatype=DataType.uint8, set_ifm_ofm_shapes=True |
Dwight Lidman | c718743 | 2020-11-16 17:40:46 +0100 | [diff] [blame] | 81 | ): |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 82 | ifm = Tensor(ifm_shape, datatype, "in") |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 83 | ifm.quantization = default_quant_params() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 84 | ofm = Tensor(ofm_shape, datatype, "out") |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 85 | ofm.quantization = default_quant_params() |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 86 | op = Operation(op_type, "op") |
| 87 | op.add_input_tensor(ifm) |
| 88 | op.set_output_tensor(ofm) |
| 89 | # Optional weight tensor |
| 90 | if weights_shape is not None: |
| 91 | if datatype.size_in_bytes() == 1: |
| 92 | np_type = np.uint8 |
| 93 | elif datatype.size_in_bytes() == 2: |
| 94 | np_type = np.int16 |
| 95 | else: |
| 96 | np_type = np.int32 |
Michael McGeagh | 65fd998 | 2020-10-20 11:49:28 +0100 | [diff] [blame] | 97 | qp = default_quant_params() |
Dwight Lidman | 0dd21c7 | 2020-11-24 13:45:50 +0100 | [diff] [blame] | 98 | if op.type is not Op.FullyConnected: |
| 99 | qp.zero_point = np.zeros(weights_shape) |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 100 | weights = create_const_tensor( |
| 101 | "weights", weights_shape, datatype, np.zeros(weights_shape), np_type, quantization=qp |
| 102 | ) |
| 103 | op.add_input_tensor(weights) |
Dwight Lidman | c718743 | 2020-11-16 17:40:46 +0100 | [diff] [blame] | 104 | # Optional bias tensor |
| 105 | if bias_shape is not None: |
| 106 | qp = default_quant_params() |
Dwight Lidman | 0dd21c7 | 2020-11-24 13:45:50 +0100 | [diff] [blame] | 107 | if op.type is not Op.FullyConnected: |
| 108 | qp.zero_point = np.zeros(bias_shape) |
Dwight Lidman | c718743 | 2020-11-16 17:40:46 +0100 | [diff] [blame] | 109 | bias = create_const_tensor("bias", bias_shape, DataType.int32, np.zeros(bias_shape), np.int32, quantization=qp) |
| 110 | op.add_input_tensor(bias) |
Patrik Gustavsson | 2349d42 | 2020-12-01 16:02:29 +0100 | [diff] [blame] | 111 | |
patrik.gustavsson | eeb8515 | 2020-12-21 17:10:40 +0000 | [diff] [blame] | 112 | if set_ifm_ofm_shapes: |
| 113 | op.set_ifm_ofm_shapes() |
| 114 | |
Michael McGeagh | 1f951fc | 2020-10-14 09:30:02 +0100 | [diff] [blame] | 115 | return op |
| 116 | |
| 117 | |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 118 | def create_op(op_type, inputs, output, attrs=None, set_ifm_ofm_shapes=True): |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 119 | op = Operation(op_type, output.name + "_op") |
Louis Verhaard | ae2d553 | 2020-12-11 17:19:54 +0100 | [diff] [blame] | 120 | for input in inputs: |
| 121 | op.add_input_tensor(input) |
| 122 | op.set_output_tensor(output) |
Michael McGeagh | 6f72526 | 2020-12-03 15:21:36 +0000 | [diff] [blame] | 123 | if attrs is not None: |
| 124 | op.attrs = attrs |
Patrik Gustavsson | 3a26920 | 2021-01-21 08:28:55 +0100 | [diff] [blame] | 125 | if set_ifm_ofm_shapes: |
| 126 | op.set_ifm_ofm_shapes() |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 127 | return op |
| 128 | |
| 129 | |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 130 | def create_subgraph(op_list): |
| 131 | # Creates subgraph using the given list of operations |
| 132 | sg = Subgraph() |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 133 | sg.placement = PassPlacement.Npu |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 134 | all_inputs = set(tens for op in op_list for tens in op.inputs) |
| 135 | # Reversing, so that the resulting subgraph has same order as op_list |
| 136 | for op in op_list[::-1]: |
| 137 | for tens in op.outputs: |
| 138 | if tens not in all_inputs and tens not in sg.output_tensors: |
| 139 | sg.output_tensors.append(tens) |
| 140 | return sg |
Louis Verhaard | c822d62 | 2021-03-11 14:59:06 +0100 | [diff] [blame] | 141 | |
| 142 | |
| 143 | def create_graph(op_list): |
| 144 | # Creates subgraph using the given list of operations |
| 145 | nng = Graph() |
| 146 | sg = create_subgraph(op_list) |
| 147 | nng.subgraphs.append(sg) |
| 148 | return nng |