blob: 3e106e1fd5204d8e445dfc80268efd8d5ac1adff [file] [log] [blame]
Cathal Corbett9c9d5b92022-08-17 17:30:16 +01001//
2// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +00008#include <Layer.hpp>
Cathal Corbett9c9d5b92022-08-17 17:30:16 +01009#include <armnn/Tensor.hpp>
10#include <armnn/Types.hpp>
11
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000012#include "common/include/ProfilingGuid.hpp"
13
14#include <tosa_serialization_handler.h>
Cathal Corbett9c9d5b92022-08-17 17:30:16 +010015
16using namespace armnn;
17using namespace tosa;
18
Narumol Prangnawaratad323af2023-09-29 17:00:38 +010019const std::string mainName = "main";
20
Cathal Corbett9c9d5b92022-08-17 17:30:16 +010021// Function to return Tosa datatype from input ArmNN datatype.
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010022inline DType ArmNNToDType(const DataType& type)
Cathal Corbett9c9d5b92022-08-17 17:30:16 +010023{
24 switch (type)
25 {
26 case DataType::Float16:
Matthew Sloyanda824cc2022-10-10 12:43:20 +010027 return DType_FP16;
Narumol Prangnawaratad323af2023-09-29 17:00:38 +010028 case DataType::BFloat16:
29 return DType_BF16;
Matthew Sloyanda824cc2022-10-10 12:43:20 +010030 case DataType::Float32:
31 return DType_FP32;
Cathal Corbett9c9d5b92022-08-17 17:30:16 +010032 case DataType::QAsymmU8:
33 return DType_UINT8;
34 case DataType::QSymmS8:
35 case DataType::QAsymmS8:
36 return DType_INT8;
37 case DataType::QSymmS16:
38 return DType_INT16;
39 case DataType::Signed32:
40 return DType_INT32;
41 case DataType::Signed64:
42 // No signed 64, only DType_INT48.
43 return DType_UNKNOWN;
44 case DataType::Boolean:
45 return DType_BOOL;
46 default:
47 return DType_UNKNOWN;
48 }
49}
50
51// Function to return Tosa tensor shape from input ArmNN tensor shape.
Matthew Sloyan164bf4f2022-10-28 18:02:17 +010052inline std::vector<int32_t> GetTosaTensorShape(const TensorShape& shape)
Cathal Corbett9c9d5b92022-08-17 17:30:16 +010053{
54 std::vector<int32_t> returnShape;
55 for (u_int32_t i = 0; i < shape.GetNumDimensions(); i++)
56 {
57 returnShape.push_back(static_cast<int32_t>(shape[i]));
58 }
59 return returnShape;
60}
61
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000062// Function that generates unique name using the layer type, input slot and layer guid.
63inline std::string GenerateUniqueName(const Layer& layer, uint32_t layerSlot)
64{
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000065 std::string guid = std::to_string(layer.GetGuid());
66 std::string slotAndGuid = std::to_string(layerSlot) + "_" + guid;
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000067
Cathal Corbettb30e6552022-12-07 11:50:50 +000068 switch (layer.GetType())
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000069 {
Cathal Corbettb30e6552022-12-07 11:50:50 +000070 case LayerType::Input:
71 return "input" + slotAndGuid;
72 case LayerType::Output:
73 return "output" + slotAndGuid;
74 case LayerType::Constant:
75 return "constant_" + guid;
76 default:
77 return "intermediate" + slotAndGuid;
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000078 }
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +000079}
80
Matthew Sloyanfc9d5e72022-12-08 13:38:23 +000081// Function that generates unique output name using the layer type, input slot and layer guid.
82inline std::string GenerateUniqueOutputName(const Layer& layer, uint32_t layerSlot)
83{
84 Layer& connectedLayer = layer.GetOutputSlot().GetConnection(0)->GetOwningLayer();
85
86 // Get the layer connected to the output slot, if output use that layer and id,
87 // otherwise use current layer and id.
88 if(connectedLayer.GetType() == LayerType::Output)
89 {
90 return GenerateUniqueName(connectedLayer, layerSlot);
91 }
92 else
93 {
94 return GenerateUniqueName(layer, layerSlot);
95 }
96}
97
Cathal Corbett9c9d5b92022-08-17 17:30:16 +010098// Function to return unique int as a string to ensure uniqueness between all input, output and block names.
99static int uniqueTosaMappingID = 0;
Matthew Sloyan164bf4f2022-10-28 18:02:17 +0100100inline std::string GetUniqueTosaMappingID()
Cathal Corbett9c9d5b92022-08-17 17:30:16 +0100101{
102 return std::to_string(++uniqueTosaMappingID);
103}
Cathal Corbettbd18eab2022-11-15 12:56:16 +0000104
Cathal Corbettb30e6552022-12-07 11:50:50 +0000105// Function to return Tosa DType as string.
106inline std::string TosaDTypeToString(DType tosaDType)
107{
108 switch (tosaDType)
109 {
110 case DType_UNKNOWN:
111 return "DType_UNKNOWN";
112 case DType_BOOL:
113 return "DType_BOOL";
114 case DType_UINT8:
115 return "DType_UINT8";
116 case DType_INT4:
117 return "DType_INT4";
118 case DType_INT8:
119 return "DType_INT8";
120 case DType_INT16:
121 return "DType_INT16";
122 case DType_INT32:
123 return "DType_INT32";
124 case DType_INT48:
125 return "DType_INT48";
126 case DType_FP32:
127 return "DType_FP32";
128 case DType_UINT16:
129 return "DType_UINT16";
130 case DType_FP16:
131 return "DType_FP16";
Narumol Prangnawaratad323af2023-09-29 17:00:38 +0100132 case DType_BF16:
133 return "DType_BF16";
Cathal Corbettb30e6552022-12-07 11:50:50 +0000134 }
135 return "";
136}
137
Cathal Corbettbd18eab2022-11-15 12:56:16 +0000138// Function to return Tosa Op as string.
139inline std::string TosaOpToString(Op tosaOp)
140{
141 switch (tosaOp)
142 {
143 case Op_ADD:
144 return "Op_ADD";
145 case Op_AVG_POOL2D:
146 return "Op_AVG_POOL2D";
147 case Op_MAX_POOL2D:
148 return "Op_MAX_POOL2D";
149 case Op_PAD:
150 return "Op_PAD";
151 case Op_UNKNOWN:
152 return "Op_UNKNOWN";
153 case Op_ARGMAX:
154 return "Op_ARGMAX";
155 case Op_CONV2D:
156 return "Op_CONV2D";
157 case Op_CONV3D:
158 return "Op_CONV3D";
159 case Op_DEPTHWISE_CONV2D:
160 return "Op_DEPTHWISE_CONV2D";
161 case Op_FULLY_CONNECTED:
162 return "Op_FULLY_CONNECTED";
163 case Op_MATMUL:
164 return "Op_MATMUL";
165 case Op_TRANSPOSE_CONV2D:
166 return "Op_TRANSPOSE_CONV2D";
167 case Op_CLAMP:
168 return "Op_CLAMP";
169 case Op_RESERVED:
170 return "Op_RESERVED";
171 case Op_SIGMOID:
172 return "Op_SIGMOID";
173 case Op_TANH:
174 return "Op_TANH";
175 case Op_ARITHMETIC_RIGHT_SHIFT:
176 return "Op_ARITHMETIC_RIGHT_SHIFT";
177 case Op_BITWISE_AND:
178 return "Op_BITWISE_AND";
179 case Op_BITWISE_OR:
180 return "Op_BITWISE_OR";
181 case Op_BITWISE_XOR:
182 return "Op_BITWISE_XOR";
183 case Op_INTDIV:
184 return "Op_INTDIV";
185 case Op_LOGICAL_AND:
186 return "Op_LOGICAL_AND";
187 case Op_LOGICAL_LEFT_SHIFT:
188 return "Op_LOGICAL_LEFT_SHIFT";
189 case Op_LOGICAL_RIGHT_SHIFT:
190 return "Op_LOGICAL_RIGHT_SHIFT";
191 case Op_LOGICAL_OR:
192 return "Op_LOGICAL_OR";
193 case Op_LOGICAL_XOR:
194 return "Op_LOGICAL_XOR";
195 case Op_MAXIMUM:
196 return "Op_MAXIMUM";
197 case Op_MINIMUM:
198 return "Op_MINIMUM";
199 case Op_MUL:
200 return "Op_MUL";
201 case Op_POW:
202 return "Op_POW";
203 case Op_SUB:
204 return "Op_SUB";
205 case Op_TABLE:
206 return "Op_TABLE";
207 case Op_ABS:
208 return "Op_ABS";
209 case Op_BITWISE_NOT:
210 return "Op_BITWISE_NOT";
211 case Op_CEIL:
212 return "Op_CEIL";
213 case Op_CLZ:
214 return "Op_CLZ";
215 case Op_EXP:
216 return "Op_EXP";
217 case Op_FLOOR:
218 return "Op_FLOOR";
219 case Op_LOG:
220 return "Op_LOG";
221 case Op_LOGICAL_NOT:
222 return "Op_LOGICAL_NOT";
223 case Op_NEGATE:
224 return "Op_NEGATE";
225 case Op_RECIPROCAL:
226 return "Op_RECIPROCAL";
227 case Op_RSQRT:
228 return "Op_RSQRT";
229 case Op_SELECT:
230 return "Op_SELECT";
231 case Op_EQUAL:
232 return "Op_EQUAL";
233 case Op_GREATER:
234 return "Op_GREATER";
235 case Op_GREATER_EQUAL:
236 return "Op_GREATER_EQUAL";
237 case Op_REDUCE_ANY:
238 return "Op_REDUCE_ANY";
239 case Op_REDUCE_ALL:
240 return "Op_REDUCE_ALL";
241 case Op_REDUCE_MAX:
242 return "Op_REDUCE_MAX";
243 case Op_REDUCE_MIN:
244 return "Op_REDUCE_MIN";
245 case Op_REDUCE_PRODUCT:
246 return "Op_REDUCE_PRODUCT";
247 case Op_REDUCE_SUM:
248 return "Op_REDUCE_SUM";
249 case Op_CONCAT:
250 return "Op_CONCAT";
251 case Op_RESHAPE:
252 return "Op_RESHAPE";
253 case Op_REVERSE:
254 return "Op_REVERSE";
255 case Op_SLICE:
256 return "Op_SLICE";
257 case Op_TILE:
258 return "Op_TILE";
259 case Op_TRANSPOSE:
260 return "Op_TRANSPOSE";
261 case Op_GATHER:
262 return "Op_GATHER";
263 case Op_SCATTER:
264 return "Op_SCATTER";
265 case Op_RESIZE:
266 return "Op_RESIZE";
267 case Op_CAST:
268 return "Op_CAST";
269 case Op_RESCALE:
270 return "Op_RESCALE";
271 case Op_CONST:
272 return "Op_CONST";
273 case Op_IDENTITY:
274 return "Op_IDENTITY";
275 case Op_CUSTOM:
276 return "Op_CUSTOM";
277 case Op_COND_IF:
278 return "Op_COND_IF";
279 case Op_WHILE_LOOP:
280 return "Op_WHILE_LOOP";
Narumol Prangnawaratad323af2023-09-29 17:00:38 +0100281 case Op_FFT2D:
282 return "Op_FFT2D";
283 case Op_RFFT2D:
284 return "Op_RFFT2D";
Cathal Corbettbd18eab2022-11-15 12:56:16 +0000285 }
286 return "";
287}
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000288
289inline std::vector<uint8_t> ConvertConstantTensorDataToBuffer(const std::shared_ptr<ConstTensorHandle>& tensorHandle)
290{
Teresa Charlin3fbad942022-12-15 10:35:37 +0000291 tosa_err_t error = tosa_err_t::TOSA_OK;
Matthew Sloyanc5fe6e72022-11-25 16:10:00 +0000292 std::vector<uint8_t> uint8Data;
293 auto tensorInfo = tensorHandle->GetTensorInfo();
294
295 switch (tensorInfo.GetDataType())
296 {
297 case DataType::Float32:
298 {
299 std::vector<float> data(tensorInfo.GetNumElements());
300 memcpy(data.data(), tensorHandle->Map(true), tensorInfo.GetNumBytes());
301
302 error = TosaSerializationHandler::ConvertF32toU8(data, uint8Data);
303 break;
304 }
305 case DataType::Float16:
306 {
307 std::vector<float> data(tensorInfo.GetNumElements());
308 memcpy(data.data(), tensorHandle->Map(true), tensorInfo.GetNumBytes());
309
310 error = TosaSerializationHandler::ConvertF16toU8(data, uint8Data);
311 break;
312 }
313 case DataType::QSymmS8:
314 case DataType::QAsymmS8:
315 {
316 std::vector<int8_t> data(tensorInfo.GetNumElements());
317 memcpy(data.data(), tensorHandle->Map(true), tensorInfo.GetNumBytes());
318
319 error = TosaSerializationHandler::ConvertI8toU8(data, uint8Data);
320 break;
321 }
322 case DataType::QAsymmU8:
323 {
324 memcpy(uint8Data.data(), tensorHandle->Map(true), tensorInfo.GetNumBytes());
325 break;
326 }
327 case DataType::QSymmS16:
328 {
329 std::vector<int16_t> data(tensorInfo.GetNumElements());
330 memcpy(data.data(), tensorHandle->Map(true), tensorInfo.GetNumBytes());
331
332 error = TosaSerializationHandler::ConvertI16toU8(data, uint8Data);
333 break;
334 }
335 case DataType::Signed32:
336 {
337 std::vector<int32_t> data(tensorInfo.GetNumElements());
338 memcpy(data.data(), tensorHandle->Map(true), tensorInfo.GetNumBytes());
339
340 error = TosaSerializationHandler::ConvertI32toU8(data, uint8Data);
341 break;
342 }
343 default:
344 {
345 throw armnn::Exception("SetConstantTensorData: An unsupported data type was encountered.");
346 }
347 }
348
349 if(error != tosa_err_t::TOSA_OK)
350 {
351 throw armnn::Exception("SetConstantTensorData: An error occurred when converting constant data");
352 }
353
354 tensorHandle->Unmap();
355 return uint8Data;
356}