blob: c548f849cc3f181f45a12a151f8bf7e9fdd7ce69 [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
telsoa015307bc12018-03-09 13:51:08 +00004//
5
6#define LOG_TAG "ArmnnDriver"
7
8#include "Utils.hpp"
Jim Flynnf2e175c2019-12-12 15:11:30 +00009#include "Half.hpp"
telsoa015307bc12018-03-09 13:51:08 +000010
Matteo Martincigh00d6ed12019-11-28 17:13:24 +000011#include <armnnUtils/Permute.hpp>
12
Derek Lambertid00ad912020-01-22 15:55:16 +000013#include <armnn/Utils.hpp>
14
telsoa015307bc12018-03-09 13:51:08 +000015#include <cassert>
Jim Flynn829ad302019-12-13 14:43:24 +000016#include <cerrno>
telsoa015307bc12018-03-09 13:51:08 +000017#include <cinttypes>
Jim Flynn829ad302019-12-13 14:43:24 +000018#include <sstream>
19#include <cstdio>
20#include <time.h>
21
22
telsoa015307bc12018-03-09 13:51:08 +000023
24using namespace android;
telsoa01ce3e84a2018-08-31 09:31:35 +010025using namespace android::hardware;
telsoa015307bc12018-03-09 13:51:08 +000026using namespace android::hidl::memory::V1_0;
27
28namespace armnn_driver
29{
30const armnn::PermutationVector g_DontPermute{};
31
32namespace
33{
34
telsoa015307bc12018-03-09 13:51:08 +000035void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorShape& inTensorShape, const void* input,
Matteo Martincighbf19d2a2019-11-29 11:46:50 +000036 void* output, size_t dataTypeSize, const armnn::PermutationVector& mappings)
telsoa015307bc12018-03-09 13:51:08 +000037{
Matteo Martincighbf19d2a2019-11-29 11:46:50 +000038 assert(inTensorShape.GetNumDimensions() == 4U);
telsoa015307bc12018-03-09 13:51:08 +000039
Matteo Martincighbf19d2a2019-11-29 11:46:50 +000040 armnnUtils::Permute(armnnUtils::Permuted(inTensorShape, mappings), mappings, input, output, dataTypeSize);
telsoa015307bc12018-03-09 13:51:08 +000041}
42
43} // anonymous namespace
44
45void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorInfo& tensor, const void* input, void* output,
46 const armnn::PermutationVector& mappings)
47{
48 assert(tensor.GetNumDimensions() == 4U);
49
Matteo Martincighbf19d2a2019-11-29 11:46:50 +000050 armnn::DataType dataType = tensor.GetDataType();
51 switch (dataType)
telsoa015307bc12018-03-09 13:51:08 +000052 {
Mike Kelly3c673942019-07-25 09:26:06 +010053 case armnn::DataType::Float16:
telsoa015307bc12018-03-09 13:51:08 +000054 case armnn::DataType::Float32:
Derek Lamberti1a38cda2020-01-10 17:28:20 +000055 case armnn::DataType::QAsymmU8:
Derek Lambertid00ad912020-01-22 15:55:16 +000056 case armnn::DataType::QSymmS8:
Matteo Martincighbf19d2a2019-11-29 11:46:50 +000057 SwizzleAndroidNn4dTensorToArmNn(tensor.GetShape(), input, output, armnn::GetDataTypeSize(dataType), mappings);
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +000058 break;
telsoa015307bc12018-03-09 13:51:08 +000059 default:
60 ALOGW("Unknown armnn::DataType for swizzling");
61 assert(0);
62 }
63}
64
65void* GetMemoryFromPool(DataLocation location, const std::vector<android::nn::RunTimePoolInfo>& memPools)
66{
67 // find the location within the pool
68 assert(location.poolIndex < memPools.size());
69
surmeh01deb3bdb2018-07-05 12:06:04 +010070 const android::nn::RunTimePoolInfo& memPool = memPools[location.poolIndex];
71
surmeh01deb3bdb2018-07-05 12:06:04 +010072 uint8_t* memPoolBuffer = memPool.getBuffer();
surmeh01deb3bdb2018-07-05 12:06:04 +010073
74 uint8_t* memory = memPoolBuffer + location.offset;
telsoa015307bc12018-03-09 13:51:08 +000075
76 return memory;
77}
78
Matthew Bentham912b3622019-05-03 15:49:14 +010079armnn::TensorInfo GetTensorInfoForOperand(const V1_0::Operand& operand)
telsoa015307bc12018-03-09 13:51:08 +000080{
81 armnn::DataType type;
82
83 switch (operand.type)
84 {
Matthew Bentham912b3622019-05-03 15:49:14 +010085 case V1_0::OperandType::TENSOR_FLOAT32:
telsoa015307bc12018-03-09 13:51:08 +000086 type = armnn::DataType::Float32;
87 break;
Matthew Bentham912b3622019-05-03 15:49:14 +010088 case V1_0::OperandType::TENSOR_QUANT8_ASYMM:
Derek Lamberti1a38cda2020-01-10 17:28:20 +000089 type = armnn::DataType::QAsymmU8;
telsoa015307bc12018-03-09 13:51:08 +000090 break;
Matthew Bentham912b3622019-05-03 15:49:14 +010091 case V1_0::OperandType::TENSOR_INT32:
telsoa015307bc12018-03-09 13:51:08 +000092 type = armnn::DataType::Signed32;
93 break;
94 default:
Mike Kellyb5fdf382019-06-11 16:35:25 +010095 throw UnsupportedOperand<V1_0::OperandType>(operand.type);
telsoa015307bc12018-03-09 13:51:08 +000096 }
97
98 armnn::TensorInfo ret(operand.dimensions.size(), operand.dimensions.data(), type);
99
100 ret.SetQuantizationScale(operand.scale);
101 ret.SetQuantizationOffset(operand.zeroPoint);
102
103 return ret;
104}
105
Mike Kellyb5fdf382019-06-11 16:35:25 +0100106#ifdef ARMNN_ANDROID_NN_V1_2 // Using ::android::hardware::neuralnetworks::V1_2
107
108armnn::TensorInfo GetTensorInfoForOperand(const V1_2::Operand& operand)
109{
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000110 using namespace armnn;
Derek Lambertid00ad912020-01-22 15:55:16 +0000111 bool perChannel = false;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100112
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000113 DataType type;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100114 switch (operand.type)
115 {
Sadik Armagan793a70c2020-03-19 13:54:04 +0000116 case V1_2::OperandType::TENSOR_BOOL8:
117 type = armnn::DataType::Boolean;
118 break;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100119 case V1_2::OperandType::TENSOR_FLOAT32:
120 type = armnn::DataType::Float32;
121 break;
Mike Kelly3c673942019-07-25 09:26:06 +0100122 case V1_2::OperandType::TENSOR_FLOAT16:
123 type = armnn::DataType::Float16;
124 break;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100125 case V1_2::OperandType::TENSOR_QUANT8_ASYMM:
Derek Lamberti1a38cda2020-01-10 17:28:20 +0000126 type = armnn::DataType::QAsymmU8;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100127 break;
Derek Lambertid00ad912020-01-22 15:55:16 +0000128 case V1_2::OperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL:
129 perChannel=true;
130 ARMNN_FALLTHROUGH;
Mike Kelly0e2e31b2019-11-19 09:16:00 +0000131 case V1_2::OperandType::TENSOR_QUANT8_SYMM:
FinnWilliamsArm624fe9f2019-12-06 17:12:42 +0000132 type = armnn::DataType::QSymmS8;
Mike Kelly0e2e31b2019-11-19 09:16:00 +0000133 break;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100134 case V1_2::OperandType::TENSOR_QUANT16_SYMM:
Derek Lamberti1a38cda2020-01-10 17:28:20 +0000135 type = armnn::DataType::QSymmS16;
Mike Kellyb5fdf382019-06-11 16:35:25 +0100136 break;
137 case V1_2::OperandType::TENSOR_INT32:
138 type = armnn::DataType::Signed32;
139 break;
140 default:
141 throw UnsupportedOperand<V1_2::OperandType>(operand.type);
142 }
143
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000144 TensorInfo ret(operand.dimensions.size(), operand.dimensions.data(), type);
Derek Lambertid00ad912020-01-22 15:55:16 +0000145 if (perChannel)
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000146 {
147 // ExtraParams is expected to be of type channelQuant
148 BOOST_ASSERT(operand.extraParams.getDiscriminator() ==
149 V1_2::Operand::ExtraParams::hidl_discriminator::channelQuant);
Mike Kellyb5fdf382019-06-11 16:35:25 +0100150
Aron Virginas-Tar9f0693b2019-11-06 14:32:30 +0000151 auto perAxisQuantParams = operand.extraParams.channelQuant();
152
153 ret.SetQuantizationScales(perAxisQuantParams.scales);
154 ret.SetQuantizationDim(MakeOptional<unsigned int>(perAxisQuantParams.channelDim));
155 }
156 else
157 {
158 ret.SetQuantizationScale(operand.scale);
159 ret.SetQuantizationOffset(operand.zeroPoint);
160 }
Mike Kellyb5fdf382019-06-11 16:35:25 +0100161
162 return ret;
163}
164
165#endif
166
Matthew Bentham912b3622019-05-03 15:49:14 +0100167std::string GetOperandSummary(const V1_0::Operand& operand)
telsoa015307bc12018-03-09 13:51:08 +0000168{
169 return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " +
170 toString(operand.type);
171}
172
Mike Kellyb5fdf382019-06-11 16:35:25 +0100173#ifdef ARMNN_ANDROID_NN_V1_2 // Using ::android::hardware::neuralnetworks::V1_2
174
175std::string GetOperandSummary(const V1_2::Operand& operand)
176{
177 return android::hardware::details::arrayToString(operand.dimensions, operand.dimensions.size()) + " " +
178 toString(operand.type);
179}
180
181#endif
182
telsoa015307bc12018-03-09 13:51:08 +0000183using DumpElementFunction = void (*)(const armnn::ConstTensor& tensor,
184 unsigned int elementIndex,
185 std::ofstream& fileStream);
186
187namespace
188{
189template <typename ElementType, typename PrintableType = ElementType>
190void DumpTensorElement(const armnn::ConstTensor& tensor, unsigned int elementIndex, std::ofstream& fileStream)
191{
192 const ElementType* elements = reinterpret_cast<const ElementType*>(tensor.GetMemoryArea());
193 fileStream << static_cast<PrintableType>(elements[elementIndex]) << ",";
194}
195
196constexpr const char* MemoryLayoutString(const armnn::ConstTensor& tensor)
197{
198 const char* str = "";
199
200 switch (tensor.GetNumDimensions())
201 {
202 case 4: { str = "(BHWC) "; break; }
203 case 3: { str = "(HWC) "; break; }
204 case 2: { str = "(HW) "; break; }
205 default: { str = ""; break; }
206 }
207
208 return str;
209}
210} // namespace
211
212void DumpTensor(const std::string& dumpDir,
213 const std::string& requestName,
214 const std::string& tensorName,
215 const armnn::ConstTensor& tensor)
216{
217 // The dump directory must exist in advance.
218 const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.dump") % dumpDir % requestName % tensorName);
219
220 std::ofstream fileStream;
221 fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc);
222
223 if (!fileStream.good())
224 {
225 ALOGW("Could not open file %s for writing", fileName.c_str());
226 return;
227 }
228
229 DumpElementFunction dumpElementFunction = nullptr;
230
231 switch (tensor.GetDataType())
232 {
233 case armnn::DataType::Float32:
234 {
235 dumpElementFunction = &DumpTensorElement<float>;
236 break;
237 }
Derek Lamberti1a38cda2020-01-10 17:28:20 +0000238 case armnn::DataType::QAsymmU8:
telsoa015307bc12018-03-09 13:51:08 +0000239 {
240 dumpElementFunction = &DumpTensorElement<uint8_t, uint32_t>;
241 break;
242 }
243 case armnn::DataType::Signed32:
244 {
245 dumpElementFunction = &DumpTensorElement<int32_t>;
246 break;
247 }
Jim Flynnf2e175c2019-12-12 15:11:30 +0000248 case armnn::DataType::Float16:
249 {
250 dumpElementFunction = &DumpTensorElement<armnn::Half>;
251 break;
252 }
telsoa015307bc12018-03-09 13:51:08 +0000253 default:
254 {
255 dumpElementFunction = nullptr;
256 }
257 }
258
259 if (dumpElementFunction != nullptr)
260 {
261 const unsigned int numDimensions = tensor.GetNumDimensions();
262
263 const unsigned int batch = (numDimensions == 4) ? tensor.GetShape()[numDimensions - 4] : 1;
264
265 const unsigned int height = (numDimensions >= 3)
266 ? tensor.GetShape()[numDimensions - 3]
267 : (numDimensions >= 2) ? tensor.GetShape()[numDimensions - 2] : 1;
268
269 const unsigned int width = (numDimensions >= 3)
270 ? tensor.GetShape()[numDimensions - 2]
271 : (numDimensions >= 1) ? tensor.GetShape()[numDimensions - 1] : 0;
272
273 const unsigned int channels = (numDimensions >= 3) ? tensor.GetShape()[numDimensions - 1] : 1;
274
275 fileStream << "# Number of elements " << tensor.GetNumElements() << std::endl;
276 fileStream << "# Dimensions " << MemoryLayoutString(tensor);
277 fileStream << "[" << tensor.GetShape()[0];
278 for (unsigned int d = 1; d < numDimensions; d++)
279 {
280 fileStream << "," << tensor.GetShape()[d];
281 }
282 fileStream << "]" << std::endl;
283
284 for (unsigned int e = 0, b = 0; b < batch; ++b)
285 {
286 if (numDimensions >= 4)
287 {
288 fileStream << "# Batch " << b << std::endl;
289 }
290 for (unsigned int c = 0; c < channels; c++)
291 {
292 if (numDimensions >= 3)
293 {
294 fileStream << "# Channel " << c << std::endl;
295 }
296 for (unsigned int h = 0; h < height; h++)
297 {
298 for (unsigned int w = 0; w < width; w++, e += channels)
299 {
300 (*dumpElementFunction)(tensor, e, fileStream);
301 }
302 fileStream << std::endl;
303 }
304 e -= channels - 1;
305 if (c < channels)
306 {
307 e -= ((height * width) - 1) * channels;
308 }
309 }
310 fileStream << std::endl;
311 }
312 fileStream << std::endl;
313 }
314 else
315 {
316 fileStream << "Cannot dump tensor elements: Unsupported data type "
317 << static_cast<unsigned int>(tensor.GetDataType()) << std::endl;
318 }
319
320 if (!fileStream.good())
321 {
322 ALOGW("An error occurred when writing to file %s", fileName.c_str());
323 }
324}
325
telsoa01ce3e84a2018-08-31 09:31:35 +0100326void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled,
327 const std::string& dumpDir,
328 armnn::NetworkId networkId,
329 const armnn::IProfiler* profiler)
330{
331 // Check if profiling is required.
332 if (!gpuProfilingEnabled)
333 {
334 return;
335 }
336
337 // The dump directory must exist in advance.
338 if (dumpDir.empty())
339 {
340 return;
341 }
342
343 BOOST_ASSERT(profiler);
344
345 // Set the name of the output profiling file.
346 const std::string fileName = boost::str(boost::format("%1%/%2%_%3%.json")
347 % dumpDir
348 % std::to_string(networkId)
349 % "profiling");
350
351 // Open the ouput file for writing.
352 std::ofstream fileStream;
353 fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc);
354
355 if (!fileStream.good())
356 {
357 ALOGW("Could not open file %s for writing", fileName.c_str());
358 return;
359 }
360
361 // Write the profiling info to a JSON file.
362 profiler->Print(fileStream);
363}
364
Jim Flynn829ad302019-12-13 14:43:24 +0000365std::string ExportNetworkGraphToDotFile(const armnn::IOptimizedNetwork& optimizedNetwork,
366 const std::string& dumpDir)
367{
368 std::string fileName;
369 // The dump directory must exist in advance.
370 if (dumpDir.empty())
371 {
372 return fileName;
373 }
374
375 std::string timestamp = GetFileTimestamp();
376 if (timestamp.empty())
377 {
378 return fileName;
379 }
380
381 // Set the name of the output .dot file.
382 fileName = boost::str(boost::format("%1%/%2%_networkgraph.dot")
383 % dumpDir
384 % timestamp);
385
386 ALOGV("Exporting the optimized network graph to file: %s", fileName.c_str());
387
388 // Write the network graph to a dot file.
389 std::ofstream fileStream;
390 fileStream.open(fileName, std::ofstream::out | std::ofstream::trunc);
391
392 if (!fileStream.good())
393 {
394 ALOGW("Could not open file %s for writing", fileName.c_str());
395 return fileName;
396 }
397
398 if (optimizedNetwork.SerializeToDot(fileStream) != armnn::Status::Success)
399 {
400 ALOGW("An error occurred when writing to file %s", fileName.c_str());
401 }
402 return fileName;
403}
404
Aron Virginas-Tar573a8fa2019-07-23 14:01:37 +0100405bool IsDynamicTensor(const armnn::TensorInfo& outputInfo)
406{
407 // Dynamic tensors have at least one 0-sized dimension
408 return outputInfo.GetNumElements() == 0u;
409}
410
Jim Flynn829ad302019-12-13 14:43:24 +0000411std::string GetFileTimestamp()
412{
413 // used to get a timestamp to name diagnostic files (the ArmNN serialized graph
414 // and getSupportedOperations.txt files)
415 timespec ts;
416 int iRet = clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
417 std::stringstream ss;
418 if (iRet == 0)
419 {
420 ss << std::to_string(ts.tv_sec) << "_" << std::to_string(ts.tv_nsec);
421 }
422 else
423 {
424 ALOGW("clock_gettime failed with errno %s : %s", std::to_string(errno).c_str(), std::strerror(errno));
425 }
426 return ss.str();
427}
428
429void RenameGraphDotFile(const std::string& oldName, const std::string& dumpDir, const armnn::NetworkId networkId)
430{
431 if (dumpDir.empty())
432 {
433 return;
434 }
435 if (oldName.empty())
436 {
437 return;
438 }
439 const std::string newFileName = boost::str(boost::format("%1%/%2%_networkgraph.dot")
440 % dumpDir
441 % std::to_string(networkId));
442 int iRet = rename(oldName.c_str(), newFileName.c_str());
443 if (iRet != 0)
444 {
445 std::stringstream ss;
446 ss << "rename of [" << oldName << "] to [" << newFileName << "] failed with errno " << std::to_string(errno)
447 << " : " << std::strerror(errno);
448 ALOGW(ss.str().c_str());
449 }
450}
451
452
453
telsoa015307bc12018-03-09 13:51:08 +0000454} // namespace armnn_driver