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 | #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 | |
surmeh01 | 49b9e10 | 2018-05-17 14:11:25 +0100 | [diff] [blame] | 14 | namespace armnn_driver |
| 15 | { |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 16 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 17 | template<typename HalPolicy> |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 18 | ModelToINetworkConverter<HalPolicy>::ModelToINetworkConverter(const std::vector<armnn::BackendId>& backends, |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 19 | const HalModel& model, |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 20 | const std::set<unsigned int>& forcedUnsupportedOperations) |
Nattapat Chaimanowong | d5fd976 | 2019-04-04 13:33:10 +0100 | [diff] [blame] | 21 | : m_Data(backends) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 22 | , m_Model(model) |
| 23 | , m_ForcedUnsupportedOperations(forcedUnsupportedOperations) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 24 | , m_ConversionResult(ConversionResult::Success) |
| 25 | { |
| 26 | try |
| 27 | { |
| 28 | Convert(); |
| 29 | } |
Derek Lamberti | b9cb844 | 2019-11-28 13:34:48 +0000 | [diff] [blame] | 30 | catch (std::exception& e) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 31 | { |
| 32 | m_ConversionResult = ConversionResult::UnsupportedFeature; |
| 33 | ALOGE("%s: Unexpected exception: %s", __func__, e.what()); |
| 34 | assert(false); |
| 35 | } |
| 36 | } |
| 37 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 38 | template<typename HalPolicy> |
| 39 | void ModelToINetworkConverter<HalPolicy>::Convert() |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 40 | { |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 41 | using HalModel = typename HalPolicy::Model; |
| 42 | using HalOperand = typename HalPolicy::Operand; |
| 43 | using HalOperandType = typename HalPolicy::OperandType; |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 44 | |
arovir01 | a15dc11 | 2018-09-03 17:12:56 +0100 | [diff] [blame] | 45 | ALOGV("ModelToINetworkConverter::Convert(): %s", GetModelSummary<HalModel>(m_Model).c_str()); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 46 | |
| 47 | // map the memory pool into shared pointers |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 48 | m_Data.m_MemPools.clear(); |
| 49 | if (!setRunTimePoolInfosFromHidlMemories(&m_Data.m_MemPools, m_Model.pools)) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 50 | { |
| 51 | Fail("%s: Setting of run time pool infos from Hidl Memories has failed.", __func__); |
| 52 | m_ConversionResult = ConversionResult::ErrorMappingPools; |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | uint32_t totalPoolSize = 0; |
| 57 | for (auto&& pool : m_Model.pools) |
| 58 | { |
| 59 | totalPoolSize += pool.size(); |
| 60 | } |
| 61 | |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 62 | using NetworkOptions = std::vector<armnn::BackendOptions>; |
| 63 | NetworkOptions networkOptions; |
| 64 | armnn::BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod", |
| 65 | { |
| 66 | { "InferAndValidate", true } |
| 67 | }); |
| 68 | |
| 69 | networkOptions.push_back(shapeInferenceMethodOption); |
| 70 | |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 71 | // Create armnn::INetwork |
Finn Williams | a4983ce | 2020-07-23 12:55:12 +0100 | [diff] [blame] | 72 | m_Data.m_Network = armnn::INetwork::Create(networkOptions); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 73 | |
| 74 | // add operations to it |
| 75 | // track which layer outputs each operand |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 76 | ALOGV("ModelToINetworkConverter::Convert(): m_OutputSlotForOperand"); |
| 77 | 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] | 78 | try |
| 79 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 80 | ALOGV("ModelToINetworkConverter::Convert(): for getMainModel(m_Model).inputIndexes.size()"); |
| 81 | for (uint32_t i = 0; i < getMainModel(m_Model).inputIndexes.size(); i++) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 82 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 83 | ALOGV("ModelToINetworkConverter::Convert(): getMainModel(m_Model).inputIndexes[i]"); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 84 | // inputs in android nn are represented by operands |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 85 | uint32_t inputIndex = getMainModel(m_Model).inputIndexes[i]; |
| 86 | ALOGV("ModelToINetworkConverter::Convert(): getMainModel(m_Model).operands[inputIndex];"); |
| 87 | const HalOperand& operand = getMainModel(m_Model).operands[inputIndex]; |
| 88 | ALOGV("ModelToINetworkConverter::Convert(): GetTensorInfoForOperand(operand)"); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 89 | const armnn::TensorInfo& tensor = GetTensorInfoForOperand(operand); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 90 | ALOGV("ModelToINetworkConverter::Convert(): m_Data.m_Network->AddInputLayer(i)"); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 91 | armnn::IConnectableLayer* layer = m_Data.m_Network->AddInputLayer(i); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 92 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 93 | ALOGV("ModelToINetworkConverter::Convert(): layer->GetOutputSlot(0)"); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 94 | armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0); |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 95 | ALOGV("ModelToINetworkConverter::Convert(): outputSlot.SetTensorInfo(GetTensorInfoForOperand(operand))"); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 96 | outputSlot.SetTensorInfo(GetTensorInfoForOperand(operand)); |
| 97 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 98 | ALOGV("ModelToINetworkConverter::Convert(): m_Data.m_OutputSlotForOperand[inputIndex] = &outputSlot"); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 99 | // store for later layers |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 100 | m_Data.m_OutputSlotForOperand[inputIndex] = &outputSlot; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 103 | catch (UnsupportedOperand<HalOperandType>& e) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 104 | { |
| 105 | Fail("%s: Operand type %s not supported in ArmnnDriver", __func__, toString(e.m_type).c_str()); |
| 106 | m_ConversionResult = ConversionResult::UnsupportedFeature; |
| 107 | } |
| 108 | catch (const armnn::InvalidArgumentException& e) |
| 109 | { |
| 110 | Fail("%s: Failed to convert input operand to TensorShape: %s", __func__, e.what()); |
| 111 | m_ConversionResult = ConversionResult::UnsupportedFeature; |
| 112 | } |
| 113 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 114 | for (uint32_t operationIdx = 0; operationIdx < getMainModel(m_Model).operations.size(); operationIdx++) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 115 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 116 | const auto& operation = getMainModel(m_Model).operations[operationIdx]; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 117 | |
| 118 | bool ok = true; |
| 119 | if (m_ForcedUnsupportedOperations.find(operationIdx) != m_ForcedUnsupportedOperations.end()) |
| 120 | { |
| 121 | Fail("%s: Operation at index %i has been forced to be unsupported.", __func__, operationIdx); |
| 122 | ok = false; |
| 123 | } |
| 124 | |
| 125 | if (ok) |
| 126 | { |
| 127 | try |
| 128 | { |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 129 | ok = HalPolicy::ConvertOperation(operation, m_Model, m_Data); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 130 | } |
Sadik Armagan | 44bcc02 | 2019-06-18 17:21:36 +0100 | [diff] [blame] | 131 | catch (UnsupportedOperand<HalOperandType>& e) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 132 | { |
| 133 | Fail("%s: Operand type %s not supported in ArmnnDriver", __func__, toString(e.m_type).c_str()); |
| 134 | ok = false; |
| 135 | } |
| 136 | catch (const armnn::InvalidArgumentException& e) |
| 137 | { |
| 138 | Fail("%s: Failed to convert operation in %s", __func__, e.what()); |
| 139 | ok = false; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Store whether this operation was successfully converted. |
| 144 | m_OperationSupported.emplace(operationIdx, ok); |
| 145 | |
| 146 | // Any single operation failing will fail the entire conversion. |
| 147 | // We still need to continue and check the other ones. |
| 148 | if (!ok) |
| 149 | { |
| 150 | m_ConversionResult = ConversionResult::UnsupportedFeature; |
| 151 | } |
| 152 | } |
| 153 | try |
| 154 | { |
| 155 | if (m_ConversionResult == ConversionResult::Success) |
| 156 | { |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 157 | for (uint32_t i = 0; i < getMainModel(m_Model).outputIndexes.size(); i++) |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 158 | { |
| 159 | // outputs in android nn are represented by operands |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 160 | uint32_t outputIndex = getMainModel(m_Model).outputIndexes[i]; |
| 161 | const HalOperand& operand = getMainModel(m_Model).operands[outputIndex]; |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 162 | const armnn::TensorInfo& tensor = GetTensorInfoForOperand(operand); |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 163 | armnn::IConnectableLayer* layer = m_Data.m_Network->AddOutputLayer(i); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 164 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 165 | assert(m_Data.m_OutputSlotForOperand[outputIndex]); |
| 166 | m_Data.m_OutputSlotForOperand[outputIndex]->Connect(layer->GetInputSlot(0)); |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | } |
| 170 | catch (const armnn::InvalidArgumentException& e) |
| 171 | { |
| 172 | Fail("%s: Failed to convert output operand to TensorShape: %s", __func__, e.what()); |
| 173 | m_ConversionResult = ConversionResult::UnsupportedFeature; |
| 174 | } |
| 175 | } |
| 176 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 177 | template<typename HalPolicy> |
| 178 | bool ModelToINetworkConverter<HalPolicy>::IsOperationSupported(uint32_t operationIndex) const |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 179 | { |
| 180 | std::map<uint32_t, bool>::const_iterator it = m_OperationSupported.find(operationIndex); |
| 181 | assert(it != m_OperationSupported.end()); |
| 182 | return it->second; |
| 183 | } |
| 184 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 185 | /// |
| 186 | /// Class template specializations |
| 187 | /// |
telsoa01 | 5307bc1 | 2018-03-09 13:51:08 +0000 | [diff] [blame] | 188 | |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 189 | template class ModelToINetworkConverter<hal_1_0::HalPolicy>; |
| 190 | |
Matteo Martincigh | 8b287c2 | 2018-09-07 09:25:10 +0100 | [diff] [blame] | 191 | #ifdef ARMNN_ANDROID_NN_V1_1 |
arovir01 | b0717b5 | 2018-09-05 17:03:25 +0100 | [diff] [blame] | 192 | template class ModelToINetworkConverter<hal_1_1::HalPolicy>; |
kevmay01 | bc5f784 | 2018-08-30 12:34:39 +0100 | [diff] [blame] | 193 | #endif |
| 194 | |
Mike Kelly | b5fdf38 | 2019-06-11 16:35:25 +0100 | [diff] [blame] | 195 | #ifdef ARMNN_ANDROID_NN_V1_2 |
| 196 | template class ModelToINetworkConverter<hal_1_1::HalPolicy>; |
| 197 | template class ModelToINetworkConverter<hal_1_2::HalPolicy>; |
| 198 | #endif |
| 199 | |
Kevin May | 42477c1 | 2020-03-26 13:34:14 +0000 | [diff] [blame] | 200 | #ifdef ARMNN_ANDROID_NN_V1_3 |
| 201 | template class ModelToINetworkConverter<hal_1_1::HalPolicy>; |
| 202 | template class ModelToINetworkConverter<hal_1_2::HalPolicy>; |
| 203 | template class ModelToINetworkConverter<hal_1_3::HalPolicy>; |
| 204 | #endif |
| 205 | |
Matteo Martincigh | e48bdff | 2018-09-03 13:50:50 +0100 | [diff] [blame] | 206 | } // armnn_driver |