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