blob: 81be984cbd08268d3829b6c8140d5ae6c278db7b [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
Mike Kellyde547162023-03-08 10:08:20 +00002// Copyright © 2017-2021,2023 Arm Ltd and Contributors. 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>
Sadik Armagan188675f2021-02-12 17:16:42 +000012#include <Utils.h>
telsoa015307bc12018-03-09 13:51:08 +000013
Mike Kellyde547162023-03-08 10:08:20 +000014#include <fmt/format.h>
15
telsoa015307bc12018-03-09 13:51:08 +000016#include <vector>
17#include <string>
Matteo Martincighe48bdff2018-09-03 13:50:50 +010018#include <fstream>
19#include <iomanip>
telsoa015307bc12018-03-09 13:51:08 +000020
Matthew Bentham912b3622019-05-03 15:49:14 +010021namespace V1_0 = ::android::hardware::neuralnetworks::V1_0;
Kevin May42477c12020-03-26 13:34:14 +000022namespace V1_1 = ::android::hardware::neuralnetworks::V1_1;
Matthew Bentham912b3622019-05-03 15:49:14 +010023
Kevin May42477c12020-03-26 13:34:14 +000024#if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3)
Mike Kellyb5fdf382019-06-11 16:35:25 +010025namespace V1_2 = ::android::hardware::neuralnetworks::V1_2;
26#endif
27
Kevin May42477c12020-03-26 13:34:14 +000028#ifdef ARMNN_ANDROID_NN_V1_3
29namespace V1_3 = ::android::hardware::neuralnetworks::V1_3;
30#endif
31
telsoa015307bc12018-03-09 13:51:08 +000032namespace armnn_driver
33{
34
Kevin Mayec1e5b82020-02-26 17:00:39 +000035#ifdef ARMNN_ANDROID_R
36using DataLocation = ::android::nn::hal::DataLocation;
37#endif
38
Kevin May42477c12020-03-26 13:34:14 +000039inline const V1_0::Model& getMainModel(const V1_0::Model& model) { return model; }
40inline const V1_1::Model& getMainModel(const V1_1::Model& model) { return model; }
41
42#if defined (ARMNN_ANDROID_NN_V1_2) || defined (ARMNN_ANDROID_NN_V1_3)
43inline const V1_2::Model& getMainModel(const V1_2::Model& model) { return model; }
44#endif
45
46#ifdef ARMNN_ANDROID_NN_V1_3
47inline const V1_3::Subgraph& getMainModel(const V1_3::Model& model) { return model.main; }
48#endif
49
telsoa015307bc12018-03-09 13:51:08 +000050extern const armnn::PermutationVector g_DontPermute;
51
Mike Kellyb5fdf382019-06-11 16:35:25 +010052template <typename OperandType>
telsoa015307bc12018-03-09 13:51:08 +000053class UnsupportedOperand: public std::runtime_error
54{
55public:
Mike Kellyb5fdf382019-06-11 16:35:25 +010056 UnsupportedOperand(const OperandType type)
telsoa015307bc12018-03-09 13:51:08 +000057 : std::runtime_error("Operand type is unsupported")
58 , m_type(type)
59 {}
60
Mike Kellyb5fdf382019-06-11 16:35:25 +010061 OperandType m_type;
telsoa015307bc12018-03-09 13:51:08 +000062};
63
64/// Swizzles tensor data in @a input according to the dimension mappings.
Jan Eilersa71c0632021-04-12 13:12:19 +010065void SwizzleAndroidNn4dTensorToArmNn(armnn::TensorInfo& tensor, const void* input, void* output,
telsoa015307bc12018-03-09 13:51:08 +000066 const armnn::PermutationVector& mappings);
67
68/// Returns a pointer to a specific location in a pool
Sadik Armagan188675f2021-02-12 17:16:42 +000069void* GetMemoryFromPool(V1_0::DataLocation location,
telsoa015307bc12018-03-09 13:51:08 +000070 const std::vector<android::nn::RunTimePoolInfo>& memPools);
71
72/// Can throw UnsupportedOperand
Matthew Bentham912b3622019-05-03 15:49:14 +010073armnn::TensorInfo GetTensorInfoForOperand(const V1_0::Operand& operand);
telsoa015307bc12018-03-09 13:51:08 +000074
Teresa Charlind3381d52021-06-02 18:35:16 +010075std::string GetOperandSummary(const V1_0::Operand& operand);
76
77// Returns true for any quantized data type, false for the rest.
78bool isQuantizedOperand(const V1_0::OperandType& operandType);
79
Kevin May42477c12020-03-26 13:34:14 +000080#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 +010081armnn::TensorInfo GetTensorInfoForOperand(const V1_2::Operand& operand);
Teresa Charlind3381d52021-06-02 18:35:16 +010082
83std::string GetOperandSummary(const V1_2::Operand& operand);
84
85bool isQuantizedOperand(const V1_2::OperandType& operandType);
Mike Kellyb5fdf382019-06-11 16:35:25 +010086#endif
87
Kevin May42477c12020-03-26 13:34:14 +000088#ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_3
89armnn::TensorInfo GetTensorInfoForOperand(const V1_3::Operand& operand);
Kevin May42477c12020-03-26 13:34:14 +000090
Kevin May42477c12020-03-26 13:34:14 +000091std::string GetOperandSummary(const V1_3::Operand& operand);
Teresa Charlind3381d52021-06-02 18:35:16 +010092
93bool isQuantizedOperand(const V1_3::OperandType& operandType);
Kevin May42477c12020-03-26 13:34:14 +000094#endif
95
Matteo Martincighe48bdff2018-09-03 13:50:50 +010096template <typename HalModel>
97std::string GetModelSummary(const HalModel& model)
kevmay01bc5f7842018-08-30 12:34:39 +010098{
99 std::stringstream result;
100
Kevin May42477c12020-03-26 13:34:14 +0000101 result << getMainModel(model).inputIndexes.size() << " input(s), "
102 << getMainModel(model).operations.size() << " operation(s), "
103 << getMainModel(model).outputIndexes.size() << " output(s), "
104 << getMainModel(model).operands.size() << " operand(s) "
105 << std::endl;
kevmay01bc5f7842018-08-30 12:34:39 +0100106
107 result << "Inputs: ";
Kevin May42477c12020-03-26 13:34:14 +0000108 for (uint32_t i = 0; i < getMainModel(model).inputIndexes.size(); i++)
kevmay01bc5f7842018-08-30 12:34:39 +0100109 {
Kevin May42477c12020-03-26 13:34:14 +0000110 result << GetOperandSummary(getMainModel(model).operands[getMainModel(model).inputIndexes[i]]) << ", ";
kevmay01bc5f7842018-08-30 12:34:39 +0100111 }
112 result << std::endl;
113
114 result << "Operations: ";
Kevin May42477c12020-03-26 13:34:14 +0000115 for (uint32_t i = 0; i < getMainModel(model).operations.size(); i++)
kevmay01bc5f7842018-08-30 12:34:39 +0100116 {
Kevin May42477c12020-03-26 13:34:14 +0000117 result << toString(getMainModel(model).operations[i].type).c_str() << ", ";
kevmay01bc5f7842018-08-30 12:34:39 +0100118 }
119 result << std::endl;
120
121 result << "Outputs: ";
Kevin May42477c12020-03-26 13:34:14 +0000122 for (uint32_t i = 0; i < getMainModel(model).outputIndexes.size(); i++)
kevmay01bc5f7842018-08-30 12:34:39 +0100123 {
Kevin May42477c12020-03-26 13:34:14 +0000124 result << GetOperandSummary(getMainModel(model).operands[getMainModel(model).outputIndexes[i]]) << ", ";
kevmay01bc5f7842018-08-30 12:34:39 +0100125 }
126 result << std::endl;
127
128 return result.str();
129}
telsoa015307bc12018-03-09 13:51:08 +0000130
Mike Kelly37c3e502021-11-09 15:43:37 +0000131template <typename TensorType>
telsoa015307bc12018-03-09 13:51:08 +0000132void DumpTensor(const std::string& dumpDir,
telsoa01ce3e84a2018-08-31 09:31:35 +0100133 const std::string& requestName,
134 const std::string& tensorName,
Mike Kelly37c3e502021-11-09 15:43:37 +0000135 const TensorType& tensor);
telsoa01ce3e84a2018-08-31 09:31:35 +0100136
137void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled,
138 const std::string& dumpDir,
139 armnn::NetworkId networkId,
140 const armnn::IProfiler* profiler);
telsoa015307bc12018-03-09 13:51:08 +0000141
Jim Flynn829ad302019-12-13 14:43:24 +0000142std::string ExportNetworkGraphToDotFile(const armnn::IOptimizedNetwork& optimizedNetwork,
143 const std::string& dumpDir);
telsoa01ce3e84a2018-08-31 09:31:35 +0100144
Sadik Armagan0a2dfab2021-10-06 16:41:44 +0100145std::string SerializeNetwork(const armnn::INetwork& network,
146 const std::string& dumpDir,
147 std::vector<uint8_t>& dataCacheData,
148 bool dataCachingActive = true);
Sadik Armaganb3021432021-01-13 15:56:51 +0000149
150void RenameExportedFiles(const std::string& existingSerializedFileName,
151 const std::string& existingDotFileName,
152 const std::string& dumpDir,
153 const armnn::NetworkId networkId);
154
155void RenameFile(const std::string& existingName,
156 const std::string& extension,
157 const std::string& dumpDir,
158 const armnn::NetworkId networkId);
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100159
Aron Virginas-Tar573a8fa2019-07-23 14:01:37 +0100160/// Checks if a tensor info represents a dynamic tensor
161bool IsDynamicTensor(const armnn::TensorInfo& outputInfo);
162
Finn Williamsa4983ce2020-07-23 12:55:12 +0100163/// Checks for ArmNN support of dynamic tensors.
164bool AreDynamicTensorsSupported(void);
165
Jim Flynn829ad302019-12-13 14:43:24 +0000166std::string GetFileTimestamp();
167
Kevin May42477c12020-03-26 13:34:14 +0000168#if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3)
169inline V1_2::OutputShape ComputeShape(const armnn::TensorInfo& info)
170{
171 V1_2::OutputShape shape;
172
Kevin May42477c12020-03-26 13:34:14 +0000173 armnn::TensorShape tensorShape = info.GetShape();
Finn Williamsfc884b42020-06-11 17:35:44 +0100174 // Android will expect scalars as a zero dimensional tensor
175 if(tensorShape.GetDimensionality() == armnn::Dimensionality::Scalar)
Kevin May42477c12020-03-26 13:34:14 +0000176 {
Finn Williamsfc884b42020-06-11 17:35:44 +0100177 shape.dimensions = android::hardware::hidl_vec<uint32_t>{};
178 }
179 else
180 {
181 android::hardware::hidl_vec<uint32_t> dimensions;
182 const unsigned int numDims = tensorShape.GetNumDimensions();
183 dimensions.resize(numDims);
184 for (unsigned int outputIdx = 0u; outputIdx < numDims; ++outputIdx)
185 {
186 dimensions[outputIdx] = tensorShape[outputIdx];
187 }
188 shape.dimensions = dimensions;
Kevin May42477c12020-03-26 13:34:14 +0000189 }
190
Kevin May42477c12020-03-26 13:34:14 +0000191 shape.isSufficient = true;
192
193 return shape;
194}
195#endif
196
197void CommitPools(std::vector<::android::nn::RunTimePoolInfo>& memPools);
198
Mike Kellyde547162023-03-08 10:08:20 +0000199template <typename ErrorStatus, typename Request>
200ErrorStatus ValidateRequestArgument(const Request& request,
201 const armnn::TensorInfo& tensorInfo,
202 const V1_0::RequestArgument& requestArgument,
203 std::string descString);
Matthew Bentham912b3622019-05-03 15:49:14 +0100204} // namespace armnn_driver