blob: 4b2938b94c169a7a5a9373f9a4fda566676ca868 [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
Dwight Lidman0dd21c72020-11-24 13:45:50 +010023from ethosu.vela.operation import Op
Louis Verhaard0b8268a2020-08-05 16:11:29 +020024from ethosu.vela.operation import Operation
25from ethosu.vela.tensor import create_const_tensor
Dwight Lidman8359a472020-09-28 15:53:40 +020026from ethosu.vela.tensor import QuantizationParameters
Louis Verhaard0b8268a2020-08-05 16:11:29 +020027from ethosu.vela.tensor import Tensor
28
29
30def create_arch():
Louis Verhaard52078302020-11-18 13:35:06 +010031 return architecture_features.create_default_arch(architecture_features.Accelerator.Ethos_U55_128)
Louis Verhaard0b8268a2020-08-05 16:11:29 +020032
33
Michael McGeagh65fd9982020-10-20 11:49:28 +010034def default_quant_params():
35 qp = QuantizationParameters()
36 qp.scale_f32 = np.float32(1)
37 qp.zero_point = 0
38 return qp
39
40
Dwight Lidman8359a472020-09-28 15:53:40 +020041def create_elemwise_op(
Michael McGeagh65fd9982020-10-20 11:49:28 +010042 op_type,
Dwight Lidman8359a472020-09-28 15:53:40 +020043 name,
44 ifm_shape,
45 ifm2_shape,
46 ofm_shape,
47 datatype=DataType.uint8,
Michael McGeagh65fd9982020-10-20 11:49:28 +010048 ifm_quant=default_quant_params(),
49 ifm2_quant=default_quant_params(),
50 ofm_quant=default_quant_params(),
Dwight Lidman8359a472020-09-28 15:53:40 +020051):
Louis Verhaard0b8268a2020-08-05 16:11:29 +020052 # Creates elementwise operation with constant IFM/IFM2
53 if datatype.size_in_bytes() == 1:
54 np_type = np.uint8
55 elif datatype.size_in_bytes() == 2:
56 np_type = np.int16
57 else:
58 np_type = np.int32
Michael McGeagh65fd9982020-10-20 11:49:28 +010059 op = Operation(op_type, name)
Dwight Lidman8359a472020-09-28 15:53:40 +020060 op.add_input_tensor(
61 create_const_tensor(name + "_ifm", ifm_shape, datatype, np.zeros(ifm_shape), np_type, quantization=ifm_quant)
62 )
Michael McGeagh65fd9982020-10-20 11:49:28 +010063 if ifm2_shape is not None:
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 )
Dwight Lidman8359a472020-09-28 15:53:40 +020068 )
Louis Verhaard0b8268a2020-08-05 16:11:29 +020069 ofm = Tensor(ofm_shape, datatype, name + "_ofm")
Dwight Lidman8359a472020-09-28 15:53:40 +020070 ofm.quantization = ofm_quant
Louis Verhaard0b8268a2020-08-05 16:11:29 +020071 op.set_output_tensor(ofm)
Louis Verhaard0b8268a2020-08-05 16:11:29 +020072 return op
73
74
Dwight Lidmanc7187432020-11-16 17:40:46 +010075def create_op_with_quant_tensors(
76 op_type, ifm_shape, ofm_shape, weights_shape=None, bias_shape=None, datatype=DataType.uint8
77):
Michael McGeagh1f951fc2020-10-14 09:30:02 +010078 ifm = Tensor(ifm_shape, datatype, "in")
Michael McGeagh65fd9982020-10-20 11:49:28 +010079 ifm.quantization = default_quant_params()
Michael McGeagh1f951fc2020-10-14 09:30:02 +010080 ofm = Tensor(ofm_shape, datatype, "out")
Michael McGeagh65fd9982020-10-20 11:49:28 +010081 ofm.quantization = default_quant_params()
Michael McGeagh1f951fc2020-10-14 09:30:02 +010082 op = Operation(op_type, "op")
83 op.add_input_tensor(ifm)
84 op.set_output_tensor(ofm)
85 # Optional weight tensor
86 if weights_shape is not None:
87 if datatype.size_in_bytes() == 1:
88 np_type = np.uint8
89 elif datatype.size_in_bytes() == 2:
90 np_type = np.int16
91 else:
92 np_type = np.int32
Michael McGeagh65fd9982020-10-20 11:49:28 +010093 qp = default_quant_params()
Dwight Lidman0dd21c72020-11-24 13:45:50 +010094 if op.type is not Op.FullyConnected:
95 qp.zero_point = np.zeros(weights_shape)
Michael McGeagh1f951fc2020-10-14 09:30:02 +010096 weights = create_const_tensor(
97 "weights", weights_shape, datatype, np.zeros(weights_shape), np_type, quantization=qp
98 )
99 op.add_input_tensor(weights)
Dwight Lidmanc7187432020-11-16 17:40:46 +0100100 # Optional bias tensor
101 if bias_shape is not None:
102 qp = default_quant_params()
Dwight Lidman0dd21c72020-11-24 13:45:50 +0100103 if op.type is not Op.FullyConnected:
104 qp.zero_point = np.zeros(bias_shape)
Dwight Lidmanc7187432020-11-16 17:40:46 +0100105 bias = create_const_tensor("bias", bias_shape, DataType.int32, np.zeros(bias_shape), np.int32, quantization=qp)
106 op.add_input_tensor(bias)
Michael McGeagh1f951fc2020-10-14 09:30:02 +0100107 return op
108
109
Louis Verhaardfa2f92a2020-09-21 11:56:18 +0200110def create_op(op_type, inputs, output, attrs=dict()):
111 op = Operation(op_type, output.name + "_op")
112 op.inputs = inputs
113 op.outputs = [output]
114 op.attrs = attrs
115 return op
116
117
Louis Verhaard0b8268a2020-08-05 16:11:29 +0200118def create_subgraph(op_list):
119 # Creates subgraph using the given list of operations
120 sg = Subgraph()
121 all_inputs = set(tens for op in op_list for tens in op.inputs)
122 # Reversing, so that the resulting subgraph has same order as op_list
123 for op in op_list[::-1]:
124 for tens in op.outputs:
125 if tens not in all_inputs and tens not in sg.output_tensors:
126 sg.output_tensors.append(tens)
127 return sg