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. |
| 63 | void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorInfo& tensor, const void* input, void* output, |
| 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 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 73 | #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] | 74 | armnn::TensorInfo GetTensorInfoForOperand(const V1_2::Operand& operand); |
| 75 | #endif |
| 76 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 77 | #ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_3 |
| 78 | armnn::TensorInfo GetTensorInfoForOperand(const V1_3::Operand& operand); |
| 79 | #endif |
| 80 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 81 | std::string GetOperandSummary(const V1_0::Operand& operand); |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 82 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 83 | #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] | 84 | std::string GetOperandSummary(const V1_2::Operand& operand); |
| 85 | #endif |
| 86 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 87 | #ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_3 |
| 88 | std::string GetOperandSummary(const V1_3::Operand& operand); |
| 89 | #endif |
| 90 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 91 | template <typename HalModel> |
| 92 | std::string GetModelSummary(const HalModel& model) |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 93 | { |
| 94 | std::stringstream result; |
| 95 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 96 | result << getMainModel(model).inputIndexes.size() << " input(s), " |
| 97 | << getMainModel(model).operations.size() << " operation(s), " |
| 98 | << getMainModel(model).outputIndexes.size() << " output(s), " |
| 99 | << getMainModel(model).operands.size() << " operand(s) " |
| 100 | << std::endl; |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 101 | |
| 102 | result << "Inputs: "; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 103 | for (uint32_t i = 0; i < getMainModel(model).inputIndexes.size(); i++) |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 104 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 105 | result << GetOperandSummary(getMainModel(model).operands[getMainModel(model).inputIndexes[i]]) << ", "; |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 106 | } |
| 107 | result << std::endl; |
| 108 | |
| 109 | result << "Operations: "; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 110 | for (uint32_t i = 0; i < getMainModel(model).operations.size(); i++) |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 111 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 112 | result << toString(getMainModel(model).operations[i].type).c_str() << ", "; |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 113 | } |
| 114 | result << std::endl; |
| 115 | |
| 116 | result << "Outputs: "; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 117 | for (uint32_t i = 0; i < getMainModel(model).outputIndexes.size(); i++) |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 118 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 119 | result << GetOperandSummary(getMainModel(model).operands[getMainModel(model).outputIndexes[i]]) << ", "; |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 120 | } |
| 121 | result << std::endl; |
| 122 | |
| 123 | return result.str(); |
| 124 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 125 | |
| 126 | void DumpTensor(const std::string& dumpDir, |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 127 | const std::string& requestName, |
| 128 | const std::string& tensorName, |
| 129 | const armnn::ConstTensor& tensor); |
| 130 | |
| 131 | void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled, |
| 132 | const std::string& dumpDir, |
| 133 | armnn::NetworkId networkId, |
| 134 | const armnn::IProfiler* profiler); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 135 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 136 | std::string ExportNetworkGraphToDotFile(const armnn::IOptimizedNetwork& optimizedNetwork, |
| 137 | const std::string& dumpDir); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 138 | |
Sadik Armagan | b302143 | 2021-01-13 15:56:51 +0000 | [diff] [blame] | 139 | std::string SerializeNetwork(const armnn::INetwork& network, const std::string& dumpDir); |
| 140 | |
| 141 | void RenameExportedFiles(const std::string& existingSerializedFileName, |
| 142 | const std::string& existingDotFileName, |
| 143 | const std::string& dumpDir, |
| 144 | const armnn::NetworkId networkId); |
| 145 | |
| 146 | void RenameFile(const std::string& existingName, |
| 147 | const std::string& extension, |
| 148 | const std::string& dumpDir, |
| 149 | const armnn::NetworkId networkId); |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 150 | |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 151 | /// Checks if a tensor info represents a dynamic tensor |
| 152 | bool IsDynamicTensor(const armnn::TensorInfo& outputInfo); |
| 153 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 154 | /// Checks for ArmNN support of dynamic tensors. |
| 155 | bool AreDynamicTensorsSupported(void); |
| 156 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 157 | std::string GetFileTimestamp(); |
| 158 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 159 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) |
| 160 | inline V1_2::OutputShape ComputeShape(const armnn::TensorInfo& info) |
| 161 | { |
| 162 | V1_2::OutputShape shape; |
| 163 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 164 | armnn::TensorShape tensorShape = info.GetShape(); |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 165 | // Android will expect scalars as a zero dimensional tensor |
| 166 | if(tensorShape.GetDimensionality() == armnn::Dimensionality::Scalar) |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 167 | { |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 168 | shape.dimensions = android::hardware::hidl_vec<uint32_t>{}; |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | android::hardware::hidl_vec<uint32_t> dimensions; |
| 173 | const unsigned int numDims = tensorShape.GetNumDimensions(); |
| 174 | dimensions.resize(numDims); |
| 175 | for (unsigned int outputIdx = 0u; outputIdx < numDims; ++outputIdx) |
| 176 | { |
| 177 | dimensions[outputIdx] = tensorShape[outputIdx]; |
| 178 | } |
| 179 | shape.dimensions = dimensions; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 182 | shape.isSufficient = true; |
| 183 | |
| 184 | return shape; |
| 185 | } |
| 186 | #endif |
| 187 | |
| 188 | void CommitPools(std::vector<::android::nn::RunTimePoolInfo>& memPools); |
| 189 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 190 | } // namespace armnn_driver |