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