telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 1 | // |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. 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 | #define LOG_TAG "ArmnnDriver" |
| 7 | |
| 8 | #include "ModelToINetworkConverter.hpp" |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 9 | #include "Utils.hpp" |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 10 | |
| 11 | #include <log/log.h> |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 12 | #include <type_traits> |
telsoa01 | ce3e84a | 2018-08-31 09:31:35 +0100 | [diff] [blame] | 13 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 14 | #ifdef ARMNN_ANDROID_S |
| 15 | #include <LegacyUtils.h> |
| 16 | #endif |
| 17 | |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 18 | namespace armnn_driver |
| 19 | { |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 20 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 21 | template<typename HalPolicy> |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 22 | ModelToINetworkConverter<HalPolicy>::ModelToINetworkConverter(const std::vector<armnn::BackendId>& backends, |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 23 | const HalModel& model, |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 24 | const std::set<unsigned int>& forcedUnsupportedOperations) |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 25 | : m_Data(backends) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 26 | , m_Model(model) |
| 27 | , m_ForcedUnsupportedOperations(forcedUnsupportedOperations) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 28 | , m_ConversionResult(ConversionResult::Success) |
| 29 | { |
| 30 | try |
| 31 | { |
| 32 | Convert(); |
| 33 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 34 | catch (std::exception& e) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 35 | { |
| 36 | m_ConversionResult = ConversionResult::UnsupportedFeature; |
| 37 | ALOGE("%s: Unexpected exception: %s", __func__, e.what()); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 41 | template<typename HalPolicy> |
| 42 | void ModelToINetworkConverter<HalPolicy>::Convert() |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 43 | { |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 44 | using HalModel = typename HalPolicy::Model; |
| 45 | using HalOperand = typename HalPolicy::Operand; |
| 46 | using HalOperandType = typename HalPolicy::OperandType; |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 47 | |
arovir01 | a15dc11 | 2018-09-03 17:12:56 +0100 | [diff] [blame] | 48 | ALOGV("ModelToINetworkConverter::Convert(): %s", GetModelSummary<HalModel>(m_Model).c_str()); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 49 | |
| 50 | // map the memory pool into shared pointers |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 51 | m_Data.m_MemPools.clear(); |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 52 | #if !defined(ARMNN_ANDROID_S) |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 53 | if (!setRunTimePoolInfosFromHidlMemories(&m_Data.m_MemPools, m_Model.pools)) |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 54 | #else |
| 55 | if (!setRunTimePoolInfosFromCanonicalMemories(&m_Data.m_MemPools, uncheckedConvert(m_Model.pools))) |
| 56 | #endif |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 57 | { |
| 58 | Fail("%s: Setting of run time pool infos from Hidl Memories has failed.", __func__); |
| 59 | m_ConversionResult = ConversionResult::ErrorMappingPools; |
| 60 | return; |
| 61 | } |
| 62 | |
Sadik Armagan | 188675f | 2021-02-12 17:16:42 +0000 | [diff] [blame] | 63 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 64 | uint32_t totalPoolSize = 0; |
| 65 | for (auto&& pool : m_Model.pools) |
| 66 | { |
| 67 | totalPoolSize += pool.size(); |
| 68 | } |
| 69 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 70 | using NetworkOptions = std::vector<armnn::BackendOptions>; |
| 71 | NetworkOptions networkOptions; |
| 72 | armnn::BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod", |
| 73 | { |
| 74 | { "InferAndValidate", true } |
| 75 | }); |
| 76 | |
| 77 | networkOptions.push_back(shapeInferenceMethodOption); |
| 78 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 79 | // Create armnn::INetwork |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 80 | m_Data.m_Network = armnn::INetwork::Create(networkOptions); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 81 | |
| 82 | // add operations to it |
| 83 | // track which layer outputs each operand |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 84 | ALOGV("ModelToINetworkConverter::Convert(): m_OutputSlotForOperand"); |
| 85 | m_Data.m_OutputSlotForOperand = std::vector<armnn::IOutputSlot*>(getMainModel(m_Model).operands.size(), nullptr); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 86 | try |
| 87 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 88 | ALOGV("ModelToINetworkConverter::Convert(): for getMainModel(m_Model).inputIndexes.size()"); |
| 89 | for (uint32_t i = 0; i < getMainModel(m_Model).inputIndexes.size(); i++) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 90 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 91 | ALOGV("ModelToINetworkConverter::Convert(): getMainModel(m_Model).inputIndexes[i]"); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 92 | // inputs in android nn are represented by operands |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 93 | uint32_t inputIndex = getMainModel(m_Model).inputIndexes[i]; |
| 94 | ALOGV("ModelToINetworkConverter::Convert(): getMainModel(m_Model).operands[inputIndex];"); |
| 95 | const HalOperand& operand = getMainModel(m_Model).operands[inputIndex]; |
| 96 | ALOGV("ModelToINetworkConverter::Convert(): GetTensorInfoForOperand(operand)"); |
Sadik Armagan | b302143 | 2021-01-13 15:56:51 +0000 | [diff] [blame] | 97 | const std::string layerName = "Input_" + std::to_string(i); |
| 98 | ALOGV("ModelToINetworkConverter::Convert(): m_Data.m_Network->AddInputLayer(i, layerName.c_str())"); |
| 99 | armnn::IConnectableLayer* layer = m_Data.m_Network->AddInputLayer(i, layerName.c_str()); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 100 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 101 | ALOGV("ModelToINetworkConverter::Convert(): layer->GetOutputSlot(0)"); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 102 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 103 | ALOGV("ModelToINetworkConverter::Convert(): outputSlot.SetTensorInfo(GetTensorInfoForOperand(operand))"); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 104 | outputSlot.SetTensorInfo(GetTensorInfoForOperand(operand)); |
| 105 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 106 | ALOGV("ModelToINetworkConverter::Convert(): m_Data.m_OutputSlotForOperand[inputIndex] = &outputSlot"); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 107 | // store for later layers |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 108 | m_Data.m_OutputSlotForOperand[inputIndex] = &outputSlot; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 111 | catch (UnsupportedOperand<HalOperandType>& e) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 112 | { |
| 113 | Fail("%s: Operand type %s not supported in ArmnnDriver", __func__, toString(e.m_type).c_str()); |
| 114 | m_ConversionResult = ConversionResult::UnsupportedFeature; |
| 115 | } |
| 116 | catch (const armnn::InvalidArgumentException& e) |
| 117 | { |
| 118 | Fail("%s: Failed to convert input operand to TensorShape: %s", __func__, e.what()); |
| 119 | m_ConversionResult = ConversionResult::UnsupportedFeature; |
| 120 | } |
Finn Williams | 291a16b | 2020-08-19 22:54:00 +0100 | [diff] [blame] | 121 | bool UnsupportedDynamicOperation = false; |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 122 | for (uint32_t operationIdx = 0; operationIdx < getMainModel(m_Model).operations.size(); operationIdx++) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 123 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 124 | const auto& operation = getMainModel(m_Model).operations[operationIdx]; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 125 | |
| 126 | bool ok = true; |
| 127 | if (m_ForcedUnsupportedOperations.find(operationIdx) != m_ForcedUnsupportedOperations.end()) |
| 128 | { |
| 129 | Fail("%s: Operation at index %i has been forced to be unsupported.", __func__, operationIdx); |
| 130 | ok = false; |
| 131 | } |
| 132 | |
| 133 | if (ok) |
| 134 | { |
| 135 | try |
| 136 | { |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 137 | ok = HalPolicy::ConvertOperation(operation, m_Model, m_Data); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 138 | } |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 139 | catch (UnsupportedOperand<HalOperandType>& e) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 140 | { |
| 141 | Fail("%s: Operand type %s not supported in ArmnnDriver", __func__, toString(e.m_type).c_str()); |
| 142 | ok = false; |
| 143 | } |
| 144 | catch (const armnn::InvalidArgumentException& e) |
| 145 | { |
| 146 | Fail("%s: Failed to convert operation in %s", __func__, e.what()); |
| 147 | ok = false; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // Store whether this operation was successfully converted. |
| 152 | m_OperationSupported.emplace(operationIdx, ok); |
| 153 | |
| 154 | // Any single operation failing will fail the entire conversion. |
| 155 | // We still need to continue and check the other ones. |
| 156 | if (!ok) |
| 157 | { |
Finn Williams | 291a16b | 2020-08-19 22:54:00 +0100 | [diff] [blame] | 158 | if (m_Data.m_DynamicInputsEncountered) |
| 159 | { |
| 160 | Fail("%s: The unsupported operation at index %i has dynamic inputs.", __func__, operationIdx); |
| 161 | UnsupportedDynamicOperation = true; |
| 162 | } |
| 163 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 164 | m_ConversionResult = ConversionResult::UnsupportedFeature; |
| 165 | } |
Finn Williams | 291a16b | 2020-08-19 22:54:00 +0100 | [diff] [blame] | 166 | m_Data.m_DynamicInputsEncountered = false; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 167 | } |
Finn Williams | 291a16b | 2020-08-19 22:54:00 +0100 | [diff] [blame] | 168 | |
| 169 | // Due to the NNAPI partitioner not supporting partition boundaries of unknown size, |
| 170 | // any operations who's outputs connect to an unsupported operation with with dynamic inputs |
| 171 | // will cause a failure. |
| 172 | |
| 173 | // The simplest solution to this problem is to not support any operations in a model containing |
| 174 | // an unsupported operation with with dynamic inputs. |
| 175 | if (UnsupportedDynamicOperation) |
| 176 | { |
| 177 | Fail("%s: Unsupported operation with dynamic inputs found. Retroactively setting all operations to unsupported", |
| 178 | __func__); |
| 179 | for (auto& operation : m_OperationSupported) |
| 180 | { |
| 181 | operation.second = false; |
| 182 | } |
| 183 | } |
| 184 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 185 | try |
| 186 | { |
| 187 | if (m_ConversionResult == ConversionResult::Success) |
| 188 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 189 | for (uint32_t i = 0; i < getMainModel(m_Model).outputIndexes.size(); i++) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 190 | { |
| 191 | // outputs in android nn are represented by operands |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 192 | uint32_t outputIndex = getMainModel(m_Model).outputIndexes[i]; |
Sadik Armagan | b302143 | 2021-01-13 15:56:51 +0000 | [diff] [blame] | 193 | const std::string layerName = "Output_" + std::to_string(i); |
| 194 | armnn::IConnectableLayer* layer = m_Data.m_Network->AddOutputLayer(i, layerName.c_str()); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 195 | |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 196 | if (!m_Data.m_OutputSlotForOperand[outputIndex]) |
| 197 | { |
| 198 | Fail("%s: OutputSlot %i does not exist", __func__, outputIndex); |
| 199 | m_ConversionResult = ConversionResult::UnsupportedFeature; |
| 200 | break; |
| 201 | } |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 202 | m_Data.m_OutputSlotForOperand[outputIndex]->Connect(layer->GetInputSlot(0)); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
| 206 | catch (const armnn::InvalidArgumentException& e) |
| 207 | { |
| 208 | Fail("%s: Failed to convert output operand to TensorShape: %s", __func__, e.what()); |
| 209 | m_ConversionResult = ConversionResult::UnsupportedFeature; |
| 210 | } |
| 211 | } |
| 212 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 213 | template<typename HalPolicy> |
| 214 | bool ModelToINetworkConverter<HalPolicy>::IsOperationSupported(uint32_t operationIndex) const |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 215 | { |
| 216 | std::map<uint32_t, bool>::const_iterator it = m_OperationSupported.find(operationIndex); |
Mike Kelly | e2d611e | 2021-10-14 12:35:58 +0100 | [diff] [blame] | 217 | if (it == m_OperationSupported.end()) |
| 218 | { |
| 219 | return Fail("%s: Unrecognised Operation Index: %i", __func__, operationIndex); |
| 220 | } |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 221 | return it->second; |
| 222 | } |
| 223 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 224 | /// |
| 225 | /// Class template specializations |
| 226 | /// |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 227 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 228 | template class ModelToINetworkConverter<hal_1_0::HalPolicy>; |
| 229 | |
Matteo Martincigh | 8b287c2 | 2018-09-07 09:25:10 +0100 | [diff] [blame] | 230 | #ifdef ARMNN_ANDROID_NN_V1_1 |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 231 | template class ModelToINetworkConverter<hal_1_1::HalPolicy>; |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 232 | #endif |
| 233 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 234 | #ifdef ARMNN_ANDROID_NN_V1_2 |
| 235 | template class ModelToINetworkConverter<hal_1_1::HalPolicy>; |
| 236 | template class ModelToINetworkConverter<hal_1_2::HalPolicy>; |
| 237 | #endif |
| 238 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 239 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 240 | template class ModelToINetworkConverter<hal_1_1::HalPolicy>; |
| 241 | template class ModelToINetworkConverter<hal_1_2::HalPolicy>; |
| 242 | template class ModelToINetworkConverter<hal_1_3::HalPolicy>; |
| 243 | #endif |
| 244 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 245 | } // armnn_driver |