blob: c4d89f7e8da834896ce6b027fa1f5f7201571cb9 [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#pragma once
telsoa015307bc12018-03-09 13:51:08 +00007#include <armnn/ArmNN.hpp>
arovir01b0717b52018-09-05 17:03:25 +01008
telsoa015307bc12018-03-09 13:51:08 +00009#include <CpuExecutor.h>
arovir01b0717b52018-09-05 17:03:25 +010010#include <HalInterfaces.h>
11#include <NeuralNetworks.h>
telsoa015307bc12018-03-09 13:51:08 +000012
13#include <vector>
14#include <string>
Matteo Martincighe48bdff2018-09-03 13:50:50 +010015#include <fstream>
16#include <iomanip>
telsoa015307bc12018-03-09 13:51:08 +000017
Matthew Bentham912b3622019-05-03 15:49:14 +010018namespace V1_0 = ::android::hardware::neuralnetworks::V1_0;
Kevin May42477c12020-03-26 13:34:14 +000019namespace V1_1 = ::android::hardware::neuralnetworks::V1_1;
Matthew Bentham912b3622019-05-03 15:49:14 +010020
Kevin May42477c12020-03-26 13:34:14 +000021#if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3)
Mike Kellyb5fdf382019-06-11 16:35:25 +010022namespace V1_2 = ::android::hardware::neuralnetworks::V1_2;
23#endif
24
Kevin May42477c12020-03-26 13:34:14 +000025#ifdef ARMNN_ANDROID_NN_V1_3
26namespace V1_3 = ::android::hardware::neuralnetworks::V1_3;
27#endif
28
telsoa015307bc12018-03-09 13:51:08 +000029namespace armnn_driver
30{
31
Kevin Mayec1e5b82020-02-26 17:00:39 +000032#ifdef ARMNN_ANDROID_R
33using DataLocation = ::android::nn::hal::DataLocation;
34#endif
35
Kevin May42477c12020-03-26 13:34:14 +000036inline const V1_0::Model& getMainModel(const V1_0::Model& model) { return model; }
37inline const V1_1::Model& getMainModel(const V1_1::Model& model) { return model; }
38
39#if defined (ARMNN_ANDROID_NN_V1_2) || defined (ARMNN_ANDROID_NN_V1_3)
40inline const V1_2::Model& getMainModel(const V1_2::Model& model) { return model; }
41#endif
42
43#ifdef ARMNN_ANDROID_NN_V1_3
44inline const V1_3::Subgraph& getMainModel(const V1_3::Model& model) { return model.main; }
45#endif
46
telsoa015307bc12018-03-09 13:51:08 +000047extern const armnn::PermutationVector g_DontPermute;
48
Mike Kellyb5fdf382019-06-11 16:35:25 +010049template <typename OperandType>
telsoa015307bc12018-03-09 13:51:08 +000050class UnsupportedOperand: public std::runtime_error
51{
52public:
Mike Kellyb5fdf382019-06-11 16:35:25 +010053 UnsupportedOperand(const OperandType type)
telsoa015307bc12018-03-09 13:51:08 +000054 : std::runtime_error("Operand type is unsupported")
55 , m_type(type)
56 {}
57
Mike Kellyb5fdf382019-06-11 16:35:25 +010058 OperandType m_type;
telsoa015307bc12018-03-09 13:51:08 +000059};
60
61/// Swizzles tensor data in @a input according to the dimension mappings.
62void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorInfo& tensor, const void* input, void* output,
63 const armnn::PermutationVector& mappings);
64
65/// Returns a pointer to a specific location in a pool
66void* GetMemoryFromPool(DataLocation location,
67 const std::vector<android::nn::RunTimePoolInfo>& memPools);
68
69/// Can throw UnsupportedOperand
Matthew Bentham912b3622019-05-03 15:49:14 +010070armnn::TensorInfo GetTensorInfoForOperand(const V1_0::Operand& operand);
telsoa015307bc12018-03-09 13:51:08 +000071
Kevin May42477c12020-03-26 13:34:14 +000072#if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) // Using ::android::hardware::neuralnetworks::V1_2
Mike Kellyb5fdf382019-06-11 16:35:25 +010073armnn::TensorInfo GetTensorInfoForOperand(const V1_2::Operand& operand);
74#endif
75
Kevin May42477c12020-03-26 13:34:14 +000076#ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_3
77armnn::TensorInfo GetTensorInfoForOperand(const V1_3::Operand& operand);
78#endif
79
Matthew Bentham912b3622019-05-03 15:49:14 +010080std::string GetOperandSummary(const V1_0::Operand& operand);
kevmay01bc5f7842018-08-30 12:34:39 +010081
Kevin May42477c12020-03-26 13:34:14 +000082#if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) // Using ::android::hardware::neuralnetworks::V1_2
Mike Kellyb5fdf382019-06-11 16:35:25 +010083std::string GetOperandSummary(const V1_2::Operand& operand);
84#endif
85
Kevin May42477c12020-03-26 13:34:14 +000086#ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_3
87std::string GetOperandSummary(const V1_3::Operand& operand);
88#endif
89
Matteo Martincighe48bdff2018-09-03 13:50:50 +010090template <typename HalModel>
91std::string GetModelSummary(const HalModel& model)
kevmay01bc5f7842018-08-30 12:34:39 +010092{
93 std::stringstream result;
94
Kevin May42477c12020-03-26 13:34:14 +000095 result << getMainModel(model).inputIndexes.size() << " input(s), "
96 << getMainModel(model).operations.size() << " operation(s), "
97 << getMainModel(model).outputIndexes.size() << " output(s), "
98 << getMainModel(model).operands.size() << " operand(s) "
99 << std::endl;
kevmay01bc5f7842018-08-30 12:34:39 +0100100
101 result << "Inputs: ";
Kevin May42477c12020-03-26 13:34:14 +0000102 for (uint32_t i = 0; i < getMainModel(model).inputIndexes.size(); i++)
kevmay01bc5f7842018-08-30 12:34:39 +0100103 {
Kevin May42477c12020-03-26 13:34:14 +0000104 result << GetOperandSummary(getMainModel(model).operands[getMainModel(model).inputIndexes[i]]) << ", ";
kevmay01bc5f7842018-08-30 12:34:39 +0100105 }
106 result << std::endl;
107
108 result << "Operations: ";
Kevin May42477c12020-03-26 13:34:14 +0000109 for (uint32_t i = 0; i < getMainModel(model).operations.size(); i++)
kevmay01bc5f7842018-08-30 12:34:39 +0100110 {
Kevin May42477c12020-03-26 13:34:14 +0000111 result << toString(getMainModel(model).operations[i].type).c_str() << ", ";
kevmay01bc5f7842018-08-30 12:34:39 +0100112 }
113 result << std::endl;
114
115 result << "Outputs: ";
Kevin May42477c12020-03-26 13:34:14 +0000116 for (uint32_t i = 0; i < getMainModel(model).outputIndexes.size(); i++)
kevmay01bc5f7842018-08-30 12:34:39 +0100117 {
Kevin May42477c12020-03-26 13:34:14 +0000118 result << GetOperandSummary(getMainModel(model).operands[getMainModel(model).outputIndexes[i]]) << ", ";
kevmay01bc5f7842018-08-30 12:34:39 +0100119 }
120 result << std::endl;
121
122 return result.str();
123}
telsoa015307bc12018-03-09 13:51:08 +0000124
125void DumpTensor(const std::string& dumpDir,
telsoa01ce3e84a2018-08-31 09:31:35 +0100126 const std::string& requestName,
127 const std::string& tensorName,
128 const armnn::ConstTensor& tensor);
129
130void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled,
131 const std::string& dumpDir,
132 armnn::NetworkId networkId,
133 const armnn::IProfiler* profiler);
telsoa015307bc12018-03-09 13:51:08 +0000134
Jim Flynn829ad302019-12-13 14:43:24 +0000135std::string ExportNetworkGraphToDotFile(const armnn::IOptimizedNetwork& optimizedNetwork,
136 const std::string& dumpDir);
telsoa01ce3e84a2018-08-31 09:31:35 +0100137
Sadik Armaganb3021432021-01-13 15:56:51 +0000138std::string SerializeNetwork(const armnn::INetwork& network, const std::string& dumpDir);
139
140void RenameExportedFiles(const std::string& existingSerializedFileName,
141 const std::string& existingDotFileName,
142 const std::string& dumpDir,
143 const armnn::NetworkId networkId);
144
145void RenameFile(const std::string& existingName,
146 const std::string& extension,
147 const std::string& dumpDir,
148 const armnn::NetworkId networkId);
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100149
Aron Virginas-Tar573a8fa2019-07-23 14:01:37 +0100150/// Checks if a tensor info represents a dynamic tensor
151bool IsDynamicTensor(const armnn::TensorInfo& outputInfo);
152
Finn Williamsa4983ce2020-07-23 12:55:12 +0100153/// Checks for ArmNN support of dynamic tensors.
154bool AreDynamicTensorsSupported(void);
155
Jim Flynn829ad302019-12-13 14:43:24 +0000156std::string GetFileTimestamp();
157
Kevin May42477c12020-03-26 13:34:14 +0000158#if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3)
159inline V1_2::OutputShape ComputeShape(const armnn::TensorInfo& info)
160{
161 V1_2::OutputShape shape;
162
Kevin May42477c12020-03-26 13:34:14 +0000163 armnn::TensorShape tensorShape = info.GetShape();
Finn Williamsfc884b42020-06-11 17:35:44 +0100164 // Android will expect scalars as a zero dimensional tensor
165 if(tensorShape.GetDimensionality() == armnn::Dimensionality::Scalar)
Kevin May42477c12020-03-26 13:34:14 +0000166 {
Finn Williamsfc884b42020-06-11 17:35:44 +0100167 shape.dimensions = android::hardware::hidl_vec<uint32_t>{};
168 }
169 else
170 {
171 android::hardware::hidl_vec<uint32_t> dimensions;
172 const unsigned int numDims = tensorShape.GetNumDimensions();
173 dimensions.resize(numDims);
174 for (unsigned int outputIdx = 0u; outputIdx < numDims; ++outputIdx)
175 {
176 dimensions[outputIdx] = tensorShape[outputIdx];
177 }
178 shape.dimensions = dimensions;
Kevin May42477c12020-03-26 13:34:14 +0000179 }
180
Kevin May42477c12020-03-26 13:34:14 +0000181 shape.isSufficient = true;
182
183 return shape;
184}
185#endif
186
187void CommitPools(std::vector<::android::nn::RunTimePoolInfo>& memPools);
188
Matthew Bentham912b3622019-05-03 15:49:14 +0100189} // namespace armnn_driver