blob: adb874a0cfa2017e83768990b8fcb2aff1840ea0 [file] [log] [blame]
Louis Verhaard0b8268a2020-08-05 16:11:29 +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# Utilities used in vela unit tests
18import numpy as np
19
20from ethosu.vela import architecture_features
21from ethosu.vela.data_type import DataType
22from ethosu.vela.nn_graph import Subgraph
Louis Verhaard0b8268a2020-08-05 16:11:29 +020023from ethosu.vela.operation import Operation
24from ethosu.vela.tensor import create_const_tensor
Louis Verhaard0b8268a2020-08-05 16:11:29 +020025from ethosu.vela.tensor import Tensor
26
27
28def create_arch():
29 return architecture_features.ArchitectureFeatures(
30 vela_config=None,
31 system_config=None,
32 accelerator_config=architecture_features.Accelerator.Ethos_U55_128.value,
Louis Verhaard0b8268a2020-08-05 16:11:29 +020033 override_block_config=None,
34 block_config_limit=None,
35 global_memory_clock_scale=1.0,
36 max_blockdep=0,
Patrik Gustavsson90831bc2020-08-24 16:26:11 +020037 weight_estimation_scaling=1.0,
Louis Verhaard0b8268a2020-08-05 16:11:29 +020038 )
39
40
41def create_elemwise_op(type, name, ifm_shape, ifm2_shape, ofm_shape, datatype=DataType.uint8):
42 # Creates elementwise operation with constant IFM/IFM2
43 if datatype.size_in_bytes() == 1:
44 np_type = np.uint8
45 elif datatype.size_in_bytes() == 2:
46 np_type = np.int16
47 else:
48 np_type = np.int32
49 op = Operation(type, name)
50 op.add_input_tensor(create_const_tensor(name + "_ifm", ifm_shape, datatype, np.zeros(ifm_shape), np_type))
51 op.add_input_tensor(create_const_tensor(name + "_ifm2", ifm2_shape, datatype, np.zeros(ifm2_shape), np_type))
52 ofm = Tensor(ofm_shape, datatype, name + "_ofm")
53 op.set_output_tensor(ofm)
Louis Verhaard0b8268a2020-08-05 16:11:29 +020054 return op
55
56
Louis Verhaardfa2f92a2020-09-21 11:56:18 +020057def create_op(op_type, inputs, output, attrs=dict()):
58 op = Operation(op_type, output.name + "_op")
59 op.inputs = inputs
60 op.outputs = [output]
61 op.attrs = attrs
62 return op
63
64
Louis Verhaard0b8268a2020-08-05 16:11:29 +020065def create_subgraph(op_list):
66 # Creates subgraph using the given list of operations
67 sg = Subgraph()
68 all_inputs = set(tens for op in op_list for tens in op.inputs)
69 # Reversing, so that the resulting subgraph has same order as op_list
70 for op in op_list[::-1]:
71 for tens in op.outputs:
72 if tens not in all_inputs and tens not in sg.output_tensors:
73 sg.output_tensors.append(tens)
74 return sg