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> |
Michael Butler | efd0f26 | 2021-01-11 13:21:44 -0800 | [diff] [blame] | 11 | #include <LegacyHalUtils.h> |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 12 | #include <NeuralNetworks.h> |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame] | 13 | #include "NamespaceAdaptor.hpp" |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 14 | |
| 15 | #include <vector> |
| 16 | #include <string> |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 17 | #include <fstream> |
| 18 | #include <iomanip> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 19 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 20 | namespace V1_0 = ::android::hardware::neuralnetworks::V1_0; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 21 | namespace V1_1 = ::android::hardware::neuralnetworks::V1_1; |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 22 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 23 | #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] | 24 | namespace V1_2 = ::android::hardware::neuralnetworks::V1_2; |
| 25 | #endif |
| 26 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 27 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 28 | namespace V1_3 = ::android::hardware::neuralnetworks::V1_3; |
| 29 | #endif |
| 30 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 31 | namespace armnn_driver |
| 32 | { |
| 33 | |
Kevin DuBois | c0945c7 | 2020-11-20 16:57:09 -0800 | [diff] [blame] | 34 | #ifdef ARMNN_ANDROID_S |
| 35 | using DataLocation = ::android::nn::DataLocation; |
| 36 | #elif ARMNN_ANDROID_R |
Kevin May | ec1e5b8 | 2020-02-26 17:00:39 +0000 | [diff] [blame] | 37 | using DataLocation = ::android::nn::hal::DataLocation; |
| 38 | #endif |
| 39 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 40 | inline const V1_0::Model& getMainModel(const V1_0::Model& model) { return model; } |
| 41 | inline const V1_1::Model& getMainModel(const V1_1::Model& model) { return model; } |
| 42 | |
| 43 | #if defined (ARMNN_ANDROID_NN_V1_2) || defined (ARMNN_ANDROID_NN_V1_3) |
| 44 | inline const V1_2::Model& getMainModel(const V1_2::Model& model) { return model; } |
| 45 | #endif |
| 46 | |
| 47 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 48 | inline const V1_3::Subgraph& getMainModel(const V1_3::Model& model) { return model.main; } |
| 49 | #endif |
| 50 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 51 | extern const armnn::PermutationVector g_DontPermute; |
| 52 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 53 | template <typename OperandType> |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 54 | class UnsupportedOperand: public std::runtime_error |
| 55 | { |
| 56 | public: |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 57 | UnsupportedOperand(const OperandType type) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 58 | : std::runtime_error("Operand type is unsupported") |
| 59 | , m_type(type) |
| 60 | {} |
| 61 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 62 | OperandType m_type; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /// Swizzles tensor data in @a input according to the dimension mappings. |
| 66 | void SwizzleAndroidNn4dTensorToArmNn(const armnn::TensorInfo& tensor, const void* input, void* output, |
| 67 | const armnn::PermutationVector& mappings); |
| 68 | |
| 69 | /// Returns a pointer to a specific location in a pool |
Kevin DuBois | 30c34ae | 2020-08-26 13:53:41 -0700 | [diff] [blame] | 70 | void* GetMemoryFromPool(V1_0::DataLocation location, |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 71 | const std::vector<android::nn::RunTimePoolInfo>& memPools); |
| 72 | |
| 73 | /// Can throw UnsupportedOperand |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 74 | armnn::TensorInfo GetTensorInfoForOperand(const V1_0::Operand& operand); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 75 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 76 | #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] | 77 | armnn::TensorInfo GetTensorInfoForOperand(const V1_2::Operand& operand); |
| 78 | #endif |
| 79 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 80 | #ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_3 |
| 81 | armnn::TensorInfo GetTensorInfoForOperand(const V1_3::Operand& operand); |
| 82 | #endif |
| 83 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 84 | std::string GetOperandSummary(const V1_0::Operand& operand); |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 85 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 86 | #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] | 87 | std::string GetOperandSummary(const V1_2::Operand& operand); |
| 88 | #endif |
| 89 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 90 | #ifdef ARMNN_ANDROID_NN_V1_3 // Using ::android::hardware::neuralnetworks::V1_3 |
| 91 | std::string GetOperandSummary(const V1_3::Operand& operand); |
| 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 | |
| 129 | void DumpTensor(const std::string& dumpDir, |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 130 | const std::string& requestName, |
| 131 | const std::string& tensorName, |
| 132 | const armnn::ConstTensor& tensor); |
| 133 | |
| 134 | void DumpJsonProfilingIfRequired(bool gpuProfilingEnabled, |
| 135 | const std::string& dumpDir, |
| 136 | armnn::NetworkId networkId, |
| 137 | const armnn::IProfiler* profiler); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 138 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 139 | std::string ExportNetworkGraphToDotFile(const armnn::IOptimizedNetwork& optimizedNetwork, |
| 140 | const std::string& dumpDir); |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 141 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 142 | 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] | 143 | |
Aron Virginas-Tar | 573a8fa | 2019-07-23 14:01:37 +0100 | [diff] [blame] | 144 | /// Checks if a tensor info represents a dynamic tensor |
| 145 | bool IsDynamicTensor(const armnn::TensorInfo& outputInfo); |
| 146 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 147 | /// Checks for ArmNN support of dynamic tensors. |
| 148 | bool AreDynamicTensorsSupported(void); |
| 149 | |
Jim Flynn | 829ad30 | 2019-12-13 14:43:24 +0000 | [diff] [blame] | 150 | std::string GetFileTimestamp(); |
| 151 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 152 | #if defined(ARMNN_ANDROID_NN_V1_2) || defined(ARMNN_ANDROID_NN_V1_3) |
| 153 | inline V1_2::OutputShape ComputeShape(const armnn::TensorInfo& info) |
| 154 | { |
| 155 | V1_2::OutputShape shape; |
| 156 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 157 | armnn::TensorShape tensorShape = info.GetShape(); |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 158 | // Android will expect scalars as a zero dimensional tensor |
| 159 | if(tensorShape.GetDimensionality() == armnn::Dimensionality::Scalar) |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 160 | { |
Finn Williams | fc884b4 | 2020-06-11 17:35:44 +0100 | [diff] [blame] | 161 | shape.dimensions = android::hardware::hidl_vec<uint32_t>{}; |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | android::hardware::hidl_vec<uint32_t> dimensions; |
| 166 | const unsigned int numDims = tensorShape.GetNumDimensions(); |
| 167 | dimensions.resize(numDims); |
| 168 | for (unsigned int outputIdx = 0u; outputIdx < numDims; ++outputIdx) |
| 169 | { |
| 170 | dimensions[outputIdx] = tensorShape[outputIdx]; |
| 171 | } |
| 172 | shape.dimensions = dimensions; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 175 | shape.isSufficient = true; |
| 176 | |
| 177 | return shape; |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | void CommitPools(std::vector<::android::nn::RunTimePoolInfo>& memPools); |
| 182 | |
Matthew Bentham | 912b362 | 2019-05-03 15:49:14 +0100 | [diff] [blame] | 183 | } // namespace armnn_driver |