telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 1 | // |
Mike Kelly | de54716 | 2023-03-08 10:08:20 +0000 | [diff] [blame] | 2 | // Copyright © 2017-2021,2023 Arm Ltd and Contributors. All rights reserved. |
David Beck | 93e4898 | 2018-09-05 13:05:09 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 4 | // |
| 5 | |
| 6 | #pragma once |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 7 | #include <armnn/ArmNN.hpp> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 8 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 9 | #include <CpuExecutor.h> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 10 | #include <HalInterfaces.h> |
| 11 | #include <NeuralNetworks.h> |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 12 | #include <Utils.h> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 13 | |
Mike Kelly | de54716 | 2023-03-08 10:08:20 +0000 | [diff] [blame] | 14 | #include <fmt/format.h> |
| 15 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 16 | #include <vector> |
| 17 | #include <string> |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 18 | #include <fstream> |
| 19 | #include <iomanip> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 20 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 21 | namespace V1_0 = ::android::hardware::neuralnetworks::V1_0; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 22 | namespace V1_1 = ::android::hardware::neuralnetworks::V1_1; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 23 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 24 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 25 | namespace V1_2 = ::android::hardware::neuralnetworks::V1_2; |
| 26 | #endif |
| 27 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 28 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 29 | namespace V1_3 = ::android::hardware::neuralnetworks::V1_3; |
| 30 | #endif |
| 31 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 32 | namespace armnn_driver |
| 33 | { |
| 34 | |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 35 | #ifdef ARMNN_ANDROID_R |
| 36 | using DataLocation = ::android::nn::hal::DataLocation; |
| 37 | #endif |
| 38 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 39 | inline const V1_0::Model& getMainModel(const V1_0::Model& model) { return model; } |
| 40 | inline 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) |
| 43 | inline const V1_2::Model& getMainModel(const V1_2::Model& model) { return model; } |
| 44 | #endif |
| 45 | |
| 46 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 47 | inline const V1_3::Subgraph& getMainModel(const V1_3::Model& model) { return model.main; } |
| 48 | #endif |
| 49 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 50 | extern const armnn::PermutationVector g_DontPermute; |
| 51 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 52 | template <typename OperandType> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 53 | class UnsupportedOperand: public std::runtime_error |
| 54 | { |
| 55 | public: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 56 | UnsupportedOperand(const OperandType type) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 57 | : std::runtime_error("Operand type is unsupported") |
| 58 | , m_type(type) |
| 59 | {} |
| 60 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 61 | OperandType m_type; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /// Swizzles tensor data in @a input according to the dimension mappings. |
Jan Eilers | a71c063 | 2021-04-12 13:12:19 +0100 | [diff] [blame] | 65 | void SwizzleAndroidNn4dTensorToArmNn(armnn::TensorInfo& tensor, const void* input, void* output, |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 66 | const armnn::PermutationVector& mappings); |
| 67 | |
| 68 | /// Returns a pointer to a specific location in a pool |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 69 | void* GetMemoryFromPool(V1_0::DataLocation location, |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 70 | const std::vector<android::nn::RunTimePoolInfo>& memPools); |
| 71 | |
| 72 | /// Can throw UnsupportedOperand |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 73 | armnn::TensorInfo GetTensorInfoForOperand(const V1_0::Operand& operand); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 74 | |
Teresa Charlin | d3381d5 | 2021-06-02 18:35:16 +0100 | [diff] [blame] | 75 | std::string GetOperandSummary(const V1_0::Operand& operand); |
| 76 | |
| 77 | // Returns true for any quantized data type, false for the rest. |
| 78 | bool isQuantizedOperand(const V1_0::OperandType& operandType); |
| 79 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 80 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) // Using ::android::hardware::neuralnetworks::V1_2 |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 81 | armnn::TensorInfo GetTensorInfoForOperand(const V1_2::Operand& operand); |
Teresa Charlin | d3381d5 | 2021-06-02 18:35:16 +0100 | [diff] [blame] | 82 | |
| 83 | std::string GetOperandSummary(const V1_2::Operand& operand); |
| 84 | |
| 85 | bool isQuantizedOperand(const V1_2::OperandType& operandType); |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 86 | #endif |
| 87 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 88 | #ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_3 |
| 89 | armnn::TensorInfo GetTensorInfoForOperand(const V1_3::Operand& operand); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 90 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 91 | std::string GetOperandSummary(const V1_3::Operand& operand); |
Teresa Charlin | d3381d5 | 2021-06-02 18:35:16 +0100 | [diff] [blame] | 92 | |
| 93 | bool isQuantizedOperand(const V1_3::OperandType& operandType); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 94 | #endif |
| 95 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 96 | template <typename HalModel> |
| 97 | std::string GetModelSummary(const HalModel& model) |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 98 | { |
| 99 | std::stringstream result; |
| 100 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 101 | 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; |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 106 | |
| 107 | result << "Inputs: "; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 108 | for (uint32_t i = 0; i < getMainModel(model).inputIndexes.size(); i++) |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 109 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 110 | result << GetOperandSummary(getMainModel(model).operands[getMainModel(model).inputIndexes[i]]) << ", "; |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 111 | } |
| 112 | result << std::endl; |
| 113 | |
| 114 | result << "Operations: "; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 115 | for (uint32_t i = 0; i < getMainModel(model).operations.size(); i++) |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 116 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 117 | result << toString(getMainModel(model).operations[i].type).c_str() << ", "; |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 118 | } |
| 119 | result << std::endl; |
| 120 | |
| 121 | result << "Outputs: "; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 122 | for (uint32_t i = 0; i < getMainModel(model).outputIndexes.size(); i++) |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 123 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 124 | result << GetOperandSummary(getMainModel(model).operands[getMainModel(model).outputIndexes[i]]) << ", "; |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 125 | } |
| 126 | result << std::endl; |
| 127 | |
| 128 | return result.str(); |
| 129 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 130 | |
Mike Kelly | 37c3e50 | 2021-11-09 15:43:37 +0000 | [diff] [blame] | 131 | template <typename TensorType> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 132 | void DumpTensor(const std::string& dumpDir, |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 133 | const std::string& requestName, |
| 134 | const std::string& tensorName, |
Mike Kelly | 37c3e50 | 2021-11-09 15:43:37 +0000 | [diff] [blame] | 135 | const TensorType& tensor); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 136 | |
| 137 | void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled, |
| 138 | const std::string& dumpDir, |
| 139 | armnn::NetworkId networkId, |
| 140 | const armnn::IProfiler* profiler); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 141 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 142 | std::string ExportNetworkGraphToDotFile(const armnn::IOptimizedNetwork& optimizedNetwork, |
| 143 | const std::string& dumpDir); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 144 | |
Sadik Armagan | 0a2dfab | 2021-10-06 16:41:44 +0100 | [diff] [blame] | 145 | std::string SerializeNetwork(const armnn::INetwork& network, |
| 146 | const std::string& dumpDir, |
| 147 | std::vector<uint8_t>& dataCacheData, |
| 148 | bool dataCachingActive = true); |
Sadik Armagan | b302143 | 2021-01-13 15:56:51 +0000 | [diff] [blame] | 149 | |
| 150 | void RenameExportedFiles(const std::string& existingSerializedFileName, |
| 151 | const std::string& existingDotFileName, |
| 152 | const std::string& dumpDir, |
| 153 | const armnn::NetworkId networkId); |
| 154 | |
| 155 | void RenameFile(const std::string& existingName, |
| 156 | const std::string& extension, |
| 157 | const std::string& dumpDir, |
| 158 | const armnn::NetworkId networkId); |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 159 | |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 160 | /// Checks if a tensor info represents a dynamic tensor |
| 161 | bool IsDynamicTensor(const armnn::TensorInfo& outputInfo); |
| 162 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 163 | /// Checks for ArmNN support of dynamic tensors. |
| 164 | bool AreDynamicTensorsSupported(void); |
| 165 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 166 | std::string GetFileTimestamp(); |
| 167 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 168 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) |
| 169 | inline V1_2::OutputShape ComputeShape(const armnn::TensorInfo& info) |
| 170 | { |
| 171 | V1_2::OutputShape shape; |
| 172 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 173 | armnn::TensorShape tensorShape = info.GetShape(); |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 174 | // Android will expect scalars as a zero dimensional tensor |
| 175 | if(tensorShape.GetDimensionality() == armnn::Dimensionality::Scalar) |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 176 | { |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 177 | 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 May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 191 | shape.isSufficient = true; |
| 192 | |
| 193 | return shape; |
| 194 | } |
| 195 | #endif |
| 196 | |
| 197 | void CommitPools(std::vector<::android::nn::RunTimePoolInfo>& memPools); |
| 198 | |
Mike Kelly | de54716 | 2023-03-08 10:08:20 +0000 | [diff] [blame] | 199 | template <typename ErrorStatus, typename Request> |
| 200 | ErrorStatus ValidateRequestArgument(const Request& request, |
| 201 | const armnn::TensorInfo& tensorInfo, |
| 202 | const V1_0::RequestArgument& requestArgument, |
| 203 | std::string descString); |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 204 | } // namespace armnn_driver |