Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +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 | # 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 |
| 22 | from ethosu.vela.nn_graph import Subgraph |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 23 | from ethosu.vela.operation import Operation |
| 24 | from ethosu.vela.tensor import create_const_tensor |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame^] | 25 | from ethosu.vela.tensor import QuantizationParameters |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 26 | from ethosu.vela.tensor import Tensor |
| 27 | |
| 28 | |
| 29 | def create_arch(): |
| 30 | return architecture_features.ArchitectureFeatures( |
| 31 | vela_config=None, |
| 32 | system_config=None, |
| 33 | accelerator_config=architecture_features.Accelerator.Ethos_U55_128.value, |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 34 | override_block_config=None, |
| 35 | block_config_limit=None, |
| 36 | global_memory_clock_scale=1.0, |
| 37 | max_blockdep=0, |
Patrik Gustavsson | 90831bc | 2020-08-24 16:26:11 +0200 | [diff] [blame] | 38 | weight_estimation_scaling=1.0, |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 39 | ) |
| 40 | |
| 41 | |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame^] | 42 | def create_elemwise_op( |
| 43 | type, |
| 44 | name, |
| 45 | ifm_shape, |
| 46 | ifm2_shape, |
| 47 | ofm_shape, |
| 48 | datatype=DataType.uint8, |
| 49 | ifm_quant=QuantizationParameters(), |
| 50 | ifm2_quant=QuantizationParameters(), |
| 51 | ofm_quant=QuantizationParameters(), |
| 52 | ): |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 53 | # Creates elementwise operation with constant IFM/IFM2 |
| 54 | if datatype.size_in_bytes() == 1: |
| 55 | np_type = np.uint8 |
| 56 | elif datatype.size_in_bytes() == 2: |
| 57 | np_type = np.int16 |
| 58 | else: |
| 59 | np_type = np.int32 |
| 60 | op = Operation(type, name) |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame^] | 61 | op.add_input_tensor( |
| 62 | create_const_tensor(name + "_ifm", ifm_shape, datatype, np.zeros(ifm_shape), np_type, quantization=ifm_quant) |
| 63 | ) |
| 64 | op.add_input_tensor( |
| 65 | create_const_tensor( |
| 66 | name + "_ifm2", ifm2_shape, datatype, np.zeros(ifm2_shape), np_type, quantization=ifm2_quant |
| 67 | ) |
| 68 | ) |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 69 | ofm = Tensor(ofm_shape, datatype, name + "_ofm") |
Dwight Lidman | 8359a47 | 2020-09-28 15:53:40 +0200 | [diff] [blame^] | 70 | ofm.quantization = ofm_quant |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 71 | op.set_output_tensor(ofm) |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 72 | return op |
| 73 | |
| 74 | |
Louis Verhaard | fa2f92a | 2020-09-21 11:56:18 +0200 | [diff] [blame] | 75 | def create_op(op_type, inputs, output, attrs=dict()): |
| 76 | op = Operation(op_type, output.name + "_op") |
| 77 | op.inputs = inputs |
| 78 | op.outputs = [output] |
| 79 | op.attrs = attrs |
| 80 | return op |
| 81 | |
| 82 | |
Louis Verhaard | 0b8268a | 2020-08-05 16:11:29 +0200 | [diff] [blame] | 83 | def create_subgraph(op_list): |
| 84 | # Creates subgraph using the given list of operations |
| 85 | sg = Subgraph() |
| 86 | all_inputs = set(tens for op in op_list for tens in op.inputs) |
| 87 | # Reversing, so that the resulting subgraph has same order as op_list |
| 88 | for op in op_list[::-1]: |
| 89 | for tens in op.outputs: |
| 90 | if tens not in all_inputs and tens not in sg.output_tensors: |
| 91 | sg.output_tensors.append(tens) |
| 92 | return sg |