blob: ee407b6ec58232495f707d0f759812a19169aaa1 [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
Dwight Lidman8359a472020-09-28 15:53:40 +020025from ethosu.vela.tensor import QuantizationParameters
Louis Verhaard0b8268a2020-08-05 16:11:29 +020026from ethosu.vela.tensor import Tensor
27
28
29def create_arch():
Louis Verhaard52078302020-11-18 13:35:06 +010030 return architecture_features.create_default_arch(architecture_features.Accelerator.Ethos_U55_128)
Louis Verhaard0b8268a2020-08-05 16:11:29 +020031
32
Michael McGeagh65fd9982020-10-20 11:49:28 +010033def default_quant_params():
34 qp = QuantizationParameters()
35 qp.scale_f32 = np.float32(1)
36 qp.zero_point = 0
37 return qp
38
39
Dwight Lidman8359a472020-09-28 15:53:40 +020040def create_elemwise_op(
Michael McGeagh65fd9982020-10-20 11:49:28 +010041 op_type,
Dwight Lidman8359a472020-09-28 15:53:40 +020042 name,
43 ifm_shape,
44 ifm2_shape,
45 ofm_shape,
46 datatype=DataType.uint8,
Michael McGeagh65fd9982020-10-20 11:49:28 +010047 ifm_quant=default_quant_params(),
48 ifm2_quant=default_quant_params(),
49 ofm_quant=default_quant_params(),
Dwight Lidman8359a472020-09-28 15:53:40 +020050):
Louis Verhaard0b8268a2020-08-05 16:11:29 +020051 # Creates elementwise operation with constant IFM/IFM2
52 if datatype.size_in_bytes() == 1:
53 np_type = np.uint8
54 elif datatype.size_in_bytes() == 2:
55 np_type = np.int16
56 else:
57 np_type = np.int32
Michael McGeagh65fd9982020-10-20 11:49:28 +010058 op = Operation(op_type, name)
Dwight Lidman8359a472020-09-28 15:53:40 +020059 op.add_input_tensor(
60 create_const_tensor(name + "_ifm", ifm_shape, datatype, np.zeros(ifm_shape), np_type, quantization=ifm_quant)
61 )
Michael McGeagh65fd9982020-10-20 11:49:28 +010062 if ifm2_shape is not None:
63 op.add_input_tensor(
64 create_const_tensor(
65 name + "_ifm2", ifm2_shape, datatype, np.zeros(ifm2_shape), np_type, quantization=ifm2_quant
66 )
Dwight Lidman8359a472020-09-28 15:53:40 +020067 )
Louis Verhaard0b8268a2020-08-05 16:11:29 +020068 ofm = Tensor(ofm_shape, datatype, name + "_ofm")
Dwight Lidman8359a472020-09-28 15:53:40 +020069 ofm.quantization = ofm_quant
Louis Verhaard0b8268a2020-08-05 16:11:29 +020070 op.set_output_tensor(ofm)
Louis Verhaard0b8268a2020-08-05 16:11:29 +020071 return op
72
73
Dwight Lidmanc7187432020-11-16 17:40:46 +010074def create_op_with_quant_tensors(
75 op_type, ifm_shape, ofm_shape, weights_shape=None, bias_shape=None, datatype=DataType.uint8
76):
Michael McGeagh1f951fc2020-10-14 09:30:02 +010077 ifm = Tensor(ifm_shape, datatype, "in")
Michael McGeagh65fd9982020-10-20 11:49:28 +010078 ifm.quantization = default_quant_params()
Michael McGeagh1f951fc2020-10-14 09:30:02 +010079 ofm = Tensor(ofm_shape, datatype, "out")
Michael McGeagh65fd9982020-10-20 11:49:28 +010080 ofm.quantization = default_quant_params()
Michael McGeagh1f951fc2020-10-14 09:30:02 +010081 op = Operation(op_type, "op")
82 op.add_input_tensor(ifm)
83 op.set_output_tensor(ofm)
84 # Optional weight tensor
85 if weights_shape is not None:
86 if datatype.size_in_bytes() == 1:
87 np_type = np.uint8
88 elif datatype.size_in_bytes() == 2:
89 np_type = np.int16
90 else:
91 np_type = np.int32
Michael McGeagh65fd9982020-10-20 11:49:28 +010092 qp = default_quant_params()
Michael McGeagh1f951fc2020-10-14 09:30:02 +010093 qp.zero_point = np.zeros(weights_shape)
94 weights = create_const_tensor(
95 "weights", weights_shape, datatype, np.zeros(weights_shape), np_type, quantization=qp
96 )
97 op.add_input_tensor(weights)
Dwight Lidmanc7187432020-11-16 17:40:46 +010098 # Optional bias tensor
99 if bias_shape is not None:
100 qp = default_quant_params()
101 qp.zero_point = np.zeros(bias_shape)
102 bias = create_const_tensor("bias", bias_shape, DataType.int32, np.zeros(bias_shape), np.int32, quantization=qp)
103 op.add_input_tensor(bias)
Michael McGeagh1f951fc2020-10-14 09:30:02 +0100104 return op
105
106
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200107def create_op(op_type, inputs, output, attrs=dict()):
108 op = Operation(op_type, output.name + "_op")
109 op.inputs = inputs
110 op.outputs = [output]
111 op.attrs = attrs
112 return op
113
114
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200115def create_subgraph(op_list):
116 # Creates subgraph using the given list of operations
117 sg = Subgraph()
118 all_inputs = set(tens for op in op_list for tens in op.inputs)
119 # Reversing, so that the resulting subgraph has same order as op_list
120 for op in op_list[::-1]:
121 for tens in op.outputs:
122 if tens not in all_inputs and tens not in sg.output_tensors:
123 sg.output_tensors.append(tens)
124 return sg