blob: 4665ef16a8948e2fd5944959bca5c40e70a2338e [file] [log] [blame]
telsoa015307bc12018-03-09 13:51:08 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beck93e48982018-09-05 13:05:09 +01003// SPDX-License-Identifier: MIT
telsoa015307bc12018-03-09 13:51:08 +00004//
5
6#define LOG_TAG "ArmnnDriver"
7
8#include "ModelToINetworkConverter.hpp"
Kevin May42477c12020-03-26 13:34:14 +00009#include "Utils.hpp"
telsoa015307bc12018-03-09 13:51:08 +000010
11#include <log/log.h>
Kevin May42477c12020-03-26 13:34:14 +000012#include <type_traits>
telsoa01ce3e84a2018-08-31 09:31:35 +010013
Sadik Armagan188675f2021-02-12 17:16:42 +000014#ifdef ARMNN_ANDROID_S
15#include <LegacyUtils.h>
16#endif
17
surmeh0149b9e102018-05-17 14:11:25 +010018namespace armnn_driver
19{
kevmay01bc5f7842018-08-30 12:34:39 +010020
arovir01b0717b52018-09-05 17:03:25 +010021template<typename HalPolicy>
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010022ModelToINetworkConverter<HalPolicy>::ModelToINetworkConverter(const std::vector<armnn::BackendId>& backends,
kevmay01bc5f7842018-08-30 12:34:39 +010023 const HalModel& model,
telsoa015307bc12018-03-09 13:51:08 +000024 const std::set<unsigned int>& forcedUnsupportedOperations)
Nattapat Chaimanowongd5fd9762019-04-04 13:33:10 +010025 : m_Data(backends)
telsoa015307bc12018-03-09 13:51:08 +000026 , m_Model(model)
27 , m_ForcedUnsupportedOperations(forcedUnsupportedOperations)
telsoa015307bc12018-03-09 13:51:08 +000028 , m_ConversionResult(ConversionResult::Success)
29{
30 try
31 {
32 Convert();
33 }
Derek Lambertib9cb8442019-11-28 13:34:48 +000034 catch (std::exception& e)
telsoa015307bc12018-03-09 13:51:08 +000035 {
36 m_ConversionResult = ConversionResult::UnsupportedFeature;
37 ALOGE("%s: Unexpected exception: %s", __func__, e.what());
38 assert(false);
39 }
40}
41
arovir01b0717b52018-09-05 17:03:25 +010042template<typename HalPolicy>
43void ModelToINetworkConverter<HalPolicy>::Convert()
telsoa015307bc12018-03-09 13:51:08 +000044{
Sadik Armagan44bcc022019-06-18 17:21:36 +010045 using HalModel = typename HalPolicy::Model;
46 using HalOperand = typename HalPolicy::Operand;
47 using HalOperandType = typename HalPolicy::OperandType;
Matteo Martincighe48bdff2018-09-03 13:50:50 +010048
arovir01a15dc112018-09-03 17:12:56 +010049 ALOGV("ModelToINetworkConverter::Convert(): %s", GetModelSummary<HalModel>(m_Model).c_str());
telsoa015307bc12018-03-09 13:51:08 +000050
51 // map the memory pool into shared pointers
arovir01b0717b52018-09-05 17:03:25 +010052 m_Data.m_MemPools.clear();
Sadik Armagan188675f2021-02-12 17:16:42 +000053#if !defined(ARMNN_ANDROID_S)
arovir01b0717b52018-09-05 17:03:25 +010054 if (!setRunTimePoolInfosFromHidlMemories(&m_Data.m_MemPools, m_Model.pools))
Sadik Armagan188675f2021-02-12 17:16:42 +000055#else
56 if (!setRunTimePoolInfosFromCanonicalMemories(&m_Data.m_MemPools, uncheckedConvert(m_Model.pools)))
57#endif
telsoa015307bc12018-03-09 13:51:08 +000058 {
59 Fail("%s: Setting of run time pool infos from Hidl Memories has failed.", __func__);
60 m_ConversionResult = ConversionResult::ErrorMappingPools;
61 return;
62 }
63
Sadik Armagan188675f2021-02-12 17:16:42 +000064
telsoa015307bc12018-03-09 13:51:08 +000065 uint32_t totalPoolSize = 0;
66 for (auto&& pool : m_Model.pools)
67 {
68 totalPoolSize += pool.size();
69 }
70
Finn Williamsa4983ce2020-07-23 12:55:12 +010071 using NetworkOptions = std::vector<armnn::BackendOptions>;
72 NetworkOptions networkOptions;
73 armnn::BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
74 {
75 { "InferAndValidate", true }
76 });
77
78 networkOptions.push_back(shapeInferenceMethodOption);
79
telsoa015307bc12018-03-09 13:51:08 +000080 // Create armnn::INetwork
Finn Williamsa4983ce2020-07-23 12:55:12 +010081 m_Data.m_Network = armnn::INetwork::Create(networkOptions);
telsoa015307bc12018-03-09 13:51:08 +000082
83 // add operations to it
84 // track which layer outputs each operand
Kevin May42477c12020-03-26 13:34:14 +000085 ALOGV("ModelToINetworkConverter::Convert(): m_OutputSlotForOperand");
86 m_Data.m_OutputSlotForOperand = std::vector<armnn::IOutputSlot*>(getMainModel(m_Model).operands.size(), nullptr);
telsoa015307bc12018-03-09 13:51:08 +000087 try
88 {
Kevin May42477c12020-03-26 13:34:14 +000089 ALOGV("ModelToINetworkConverter::Convert(): for getMainModel(m_Model).inputIndexes.size()");
90 for (uint32_t i = 0; i < getMainModel(m_Model).inputIndexes.size(); i++)
telsoa015307bc12018-03-09 13:51:08 +000091 {
Kevin May42477c12020-03-26 13:34:14 +000092 ALOGV("ModelToINetworkConverter::Convert(): getMainModel(m_Model).inputIndexes[i]");
telsoa015307bc12018-03-09 13:51:08 +000093 // inputs in android nn are represented by operands
Kevin May42477c12020-03-26 13:34:14 +000094 uint32_t inputIndex = getMainModel(m_Model).inputIndexes[i];
95 ALOGV("ModelToINetworkConverter::Convert(): getMainModel(m_Model).operands[inputIndex];");
96 const HalOperand& operand = getMainModel(m_Model).operands[inputIndex];
97 ALOGV("ModelToINetworkConverter::Convert(): GetTensorInfoForOperand(operand)");
Sadik Armaganb3021432021-01-13 15:56:51 +000098 const std::string layerName = "Input_" + std::to_string(i);
99 ALOGV("ModelToINetworkConverter::Convert(): m_Data.m_Network->AddInputLayer(i, layerName.c_str())");
100 armnn::IConnectableLayer* layer = m_Data.m_Network->AddInputLayer(i, layerName.c_str());
telsoa015307bc12018-03-09 13:51:08 +0000101
Kevin May42477c12020-03-26 13:34:14 +0000102 ALOGV("ModelToINetworkConverter::Convert(): layer->GetOutputSlot(0)");
telsoa015307bc12018-03-09 13:51:08 +0000103 armnn::IOutputSlot& outputSlot = layer->GetOutputSlot(0);
Kevin May42477c12020-03-26 13:34:14 +0000104 ALOGV("ModelToINetworkConverter::Convert(): outputSlot.SetTensorInfo(GetTensorInfoForOperand(operand))");
telsoa015307bc12018-03-09 13:51:08 +0000105 outputSlot.SetTensorInfo(GetTensorInfoForOperand(operand));
106
Kevin May42477c12020-03-26 13:34:14 +0000107 ALOGV("ModelToINetworkConverter::Convert(): m_Data.m_OutputSlotForOperand[inputIndex] = &outputSlot");
telsoa015307bc12018-03-09 13:51:08 +0000108 // store for later layers
arovir01b0717b52018-09-05 17:03:25 +0100109 m_Data.m_OutputSlotForOperand[inputIndex] = &outputSlot;
telsoa015307bc12018-03-09 13:51:08 +0000110 }
111 }
Sadik Armagan44bcc022019-06-18 17:21:36 +0100112 catch (UnsupportedOperand<HalOperandType>& e)
telsoa015307bc12018-03-09 13:51:08 +0000113 {
114 Fail("%s: Operand type %s not supported in ArmnnDriver", __func__, toString(e.m_type).c_str());
115 m_ConversionResult = ConversionResult::UnsupportedFeature;
116 }
117 catch (const armnn::InvalidArgumentException& e)
118 {
119 Fail("%s: Failed to convert input operand to TensorShape: %s", __func__, e.what());
120 m_ConversionResult = ConversionResult::UnsupportedFeature;
121 }
Finn Williams291a16b2020-08-19 22:54:00 +0100122 bool UnsupportedDynamicOperation = false;
Kevin May42477c12020-03-26 13:34:14 +0000123 for (uint32_t operationIdx = 0; operationIdx < getMainModel(m_Model).operations.size(); operationIdx++)
telsoa015307bc12018-03-09 13:51:08 +0000124 {
Kevin May42477c12020-03-26 13:34:14 +0000125 const auto& operation = getMainModel(m_Model).operations[operationIdx];
telsoa015307bc12018-03-09 13:51:08 +0000126
127 bool ok = true;
128 if (m_ForcedUnsupportedOperations.find(operationIdx) != m_ForcedUnsupportedOperations.end())
129 {
130 Fail("%s: Operation at index %i has been forced to be unsupported.", __func__, operationIdx);
131 ok = false;
132 }
133
134 if (ok)
135 {
136 try
137 {
arovir01b0717b52018-09-05 17:03:25 +0100138 ok = HalPolicy::ConvertOperation(operation, m_Model, m_Data);
telsoa015307bc12018-03-09 13:51:08 +0000139 }
Sadik Armagan44bcc022019-06-18 17:21:36 +0100140 catch (UnsupportedOperand<HalOperandType>& e)
telsoa015307bc12018-03-09 13:51:08 +0000141 {
142 Fail("%s: Operand type %s not supported in ArmnnDriver", __func__, toString(e.m_type).c_str());
143 ok = false;
144 }
145 catch (const armnn::InvalidArgumentException& e)
146 {
147 Fail("%s: Failed to convert operation in %s", __func__, e.what());
148 ok = false;
149 }
150 }
151
152 // Store whether this operation was successfully converted.
153 m_OperationSupported.emplace(operationIdx, ok);
154
155 // Any single operation failing will fail the entire conversion.
156 // We still need to continue and check the other ones.
157 if (!ok)
158 {
Finn Williams291a16b2020-08-19 22:54:00 +0100159 if (m_Data.m_DynamicInputsEncountered)
160 {
161 Fail("%s: The unsupported operation at index %i has dynamic inputs.", __func__, operationIdx);
162 UnsupportedDynamicOperation = true;
163 }
164
telsoa015307bc12018-03-09 13:51:08 +0000165 m_ConversionResult = ConversionResult::UnsupportedFeature;
166 }
Finn Williams291a16b2020-08-19 22:54:00 +0100167 m_Data.m_DynamicInputsEncountered = false;
telsoa015307bc12018-03-09 13:51:08 +0000168 }
Finn Williams291a16b2020-08-19 22:54:00 +0100169
170 // Due to the NNAPI partitioner not supporting partition boundaries of unknown size,
171 // any operations who's outputs connect to an unsupported operation with with dynamic inputs
172 // will cause a failure.
173
174 // The simplest solution to this problem is to not support any operations in a model containing
175 // an unsupported operation with with dynamic inputs.
176 if (UnsupportedDynamicOperation)
177 {
178 Fail("%s: Unsupported operation with dynamic inputs found. Retroactively setting all operations to unsupported",
179 __func__);
180 for (auto& operation : m_OperationSupported)
181 {
182 operation.second = false;
183 }
184 }
185
telsoa015307bc12018-03-09 13:51:08 +0000186 try
187 {
188 if (m_ConversionResult == ConversionResult::Success)
189 {
Kevin May42477c12020-03-26 13:34:14 +0000190 for (uint32_t i = 0; i < getMainModel(m_Model).outputIndexes.size(); i++)
telsoa015307bc12018-03-09 13:51:08 +0000191 {
192 // outputs in android nn are represented by operands
Kevin May42477c12020-03-26 13:34:14 +0000193 uint32_t outputIndex = getMainModel(m_Model).outputIndexes[i];
Sadik Armaganb3021432021-01-13 15:56:51 +0000194 const std::string layerName = "Output_" + std::to_string(i);
195 armnn::IConnectableLayer* layer = m_Data.m_Network->AddOutputLayer(i, layerName.c_str());
telsoa015307bc12018-03-09 13:51:08 +0000196
arovir01b0717b52018-09-05 17:03:25 +0100197 assert(m_Data.m_OutputSlotForOperand[outputIndex]);
198 m_Data.m_OutputSlotForOperand[outputIndex]->Connect(layer->GetInputSlot(0));
telsoa015307bc12018-03-09 13:51:08 +0000199 }
200 }
201 }
202 catch (const armnn::InvalidArgumentException& e)
203 {
204 Fail("%s: Failed to convert output operand to TensorShape: %s", __func__, e.what());
205 m_ConversionResult = ConversionResult::UnsupportedFeature;
206 }
207}
208
arovir01b0717b52018-09-05 17:03:25 +0100209template<typename HalPolicy>
210bool ModelToINetworkConverter<HalPolicy>::IsOperationSupported(uint32_t operationIndex) const
telsoa015307bc12018-03-09 13:51:08 +0000211{
212 std::map<uint32_t, bool>::const_iterator it = m_OperationSupported.find(operationIndex);
213 assert(it != m_OperationSupported.end());
214 return it->second;
215}
216
arovir01b0717b52018-09-05 17:03:25 +0100217///
218/// Class template specializations
219///
telsoa015307bc12018-03-09 13:51:08 +0000220
arovir01b0717b52018-09-05 17:03:25 +0100221template class ModelToINetworkConverter<hal_1_0::HalPolicy>;
222
Matteo Martincigh8b287c22018-09-07 09:25:10 +0100223#ifdef ARMNN_ANDROID_NN_V1_1
arovir01b0717b52018-09-05 17:03:25 +0100224template class ModelToINetworkConverter<hal_1_1::HalPolicy>;
kevmay01bc5f7842018-08-30 12:34:39 +0100225#endif
226
Mike Kellyb5fdf382019-06-11 16:35:25 +0100227#ifdef ARMNN_ANDROID_NN_V1_2
228template class ModelToINetworkConverter<hal_1_1::HalPolicy>;
229template class ModelToINetworkConverter<hal_1_2::HalPolicy>;
230#endif
231
Kevin May42477c12020-03-26 13:34:14 +0000232#ifdef ARMNN_ANDROID_NN_V1_3
233template class ModelToINetworkConverter<hal_1_1::HalPolicy>;
234template class ModelToINetworkConverter<hal_1_2::HalPolicy>;
235template class ModelToINetworkConverter<hal_1_3::HalPolicy>;
236#endif
237
Matteo Martincighe48bdff2018-09-03 13:50:50 +0100238} // armnn_driver