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