blob: 9ec78350210b11146324a51977d202ac8004d212 [file] [log] [blame]
Kevin May43a799c2019-02-08 16:31:42 +00001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Derek Lamberti0028d1b2019-02-20 13:57:42 +00006#include "Deserializer.hpp"
Kevin May43a799c2019-02-08 16:31:42 +00007
8#include <armnn/ArmNN.hpp>
9#include <armnn/Exceptions.hpp>
10
11#include <ParserHelper.hpp>
12#include <Permute.hpp>
13#include <VerificationHelpers.hpp>
14
15#include <boost/filesystem.hpp>
16#include <boost/format.hpp>
17#include <boost/core/ignore_unused.hpp>
18#include <boost/assert.hpp>
19#include <boost/format.hpp>
20#include <boost/log/trivial.hpp>
21
22// The generated code based on the Serialize schema:
23#include <Schema_generated.h>
24
25#include <fstream>
Saoirse Stewart263829c2019-02-19 15:54:14 +000026#include <algorithm>
27#include <limits>
28#include <numeric>
Kevin May43a799c2019-02-08 16:31:42 +000029
30using armnn::ParseException;
31using namespace armnn;
Derek Lamberti0028d1b2019-02-20 13:57:42 +000032using namespace armnnSerializer;
Kevin May43a799c2019-02-08 16:31:42 +000033
Derek Lamberti0028d1b2019-02-20 13:57:42 +000034namespace armnnDeserializer
Aron Virginas-Tarfc413c02019-02-13 15:41:52 +000035{
Kevin May43a799c2019-02-08 16:31:42 +000036
Aron Virginas-Tarfc413c02019-02-13 15:41:52 +000037namespace
38{
Kevin May43a799c2019-02-08 16:31:42 +000039
40const uint32_t VIRTUAL_LAYER_ID = std::numeric_limits<uint32_t>::max();
41
Derek Lamberti0028d1b2019-02-20 13:57:42 +000042 void CheckGraph(const Deserializer::GraphPtr& graph,
Kevin May43a799c2019-02-08 16:31:42 +000043 unsigned int layersIndex,
44 const CheckLocation& location)
45{
46 if (graph->layers() == nullptr)
47 {
48 throw ParseException(
49 boost::str(
50 boost::format("%1% was called with invalid (null) graph. "
51 "Possible reason is that the graph is not yet loaded and Unpack(ed). "
52 "layers:%2% at %3%") %
53 location.m_Function %
54 layersIndex %
55 location.FileLine()));
56 }
57 else if (layersIndex >= graph->layers()->size())
58 {
59 throw ParseException(
60 boost::str(
61 boost::format("%1% was called with an invalid layers index. "
62 "layers:%2% at %3%") %
63 location.m_Function %
64 layersIndex %
65 location.FileLine()));
66 }
67}
68
Derek Lamberti0028d1b2019-02-20 13:57:42 +000069void CheckLayers(const Deserializer::GraphPtr& graph,
Kevin May43a799c2019-02-08 16:31:42 +000070 unsigned int layersIndex,
71 unsigned int layerIndex,
72 const CheckLocation& location)
73{
74 if (graph->layers() == nullptr)
75 {
76 throw ParseException(
77 boost::str(
78 boost::format("%1% was called with invalid (null) graph. "
79 "Possible reason is that the graph is not yet loaded and Unpack(ed). "
Nattapat Chaimanowong43e78642019-02-13 15:56:24 +000080 "layers:%2% at %3%") %
Kevin May43a799c2019-02-08 16:31:42 +000081 location.m_Function %
82 layersIndex %
83 location.FileLine()));
84 }
85 else if (layersIndex >= graph->layers()->size())
86 {
87 throw ParseException(
88 boost::str(
89 boost::format("%1% was called with an invalid layers index. "
Nattapat Chaimanowong43e78642019-02-13 15:56:24 +000090 "layers:%2% at %3%") %
Kevin May43a799c2019-02-08 16:31:42 +000091 location.m_Function %
92 layersIndex %
93 location.FileLine()));
94 }
95 else if (layerIndex >= graph->layers()[layersIndex].size()
96 && layerIndex != VIRTUAL_LAYER_ID)
97 {
98 throw ParseException(
99 boost::str(
100 boost::format("%1% was called with an invalid layer index. "
101 "layers:%2% layer:%3% at %4%") %
102 location.m_Function %
103 layersIndex %
104 layerIndex %
105 location.FileLine()));
106 }
107}
108
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000109void CheckTensorPtr(Deserializer::TensorRawPtr rawPtr,
Kevin May43a799c2019-02-08 16:31:42 +0000110 const CheckLocation& location)
111{
112 if (rawPtr == nullptr)
113 {
114 throw ParseException(
115 boost::str(
116 boost::format("%1% was called with a null tensor pointer. "
117 "at %2%") %
118 location.m_Function %
119 location.FileLine()));
120
121 }
122}
123
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000124void CheckConstTensorPtr(Deserializer::ConstTensorRawPtr rawPtr,
Mike Kellya0766c32019-02-19 17:22:07 +0000125 const CheckLocation& location)
126{
127 if (rawPtr == nullptr)
128 {
129 throw ParseException(boost::str(boost::format("%1% was called with a null const tensor pointer. at %2%") %
130 location.m_Function %
131 location.FileLine()));
132 }
133}
134
Kevin May43a799c2019-02-08 16:31:42 +0000135#define CHECK_TENSOR_PTR(TENSOR_PTR) \
136 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
137
Mike Kellya0766c32019-02-19 17:22:07 +0000138#define CHECK_CONST_TENSOR_PTR(TENSOR_PTR) \
139 CheckConstTensorPtr(TENSOR_PTR, CHECK_LOCATION())
140
Kevin May43a799c2019-02-08 16:31:42 +0000141#define CHECK_LAYERS(GRAPH, LAYERS_INDEX, LAYER_INDEX) \
142 CheckLayers(GRAPH, LAYERS_INDEX, LAYER_INDEX, CHECK_LOCATION())
143
144#define CHECK_GRAPH(GRAPH, LAYERS_INDEX) \
145 CheckGraph(GRAPH, LAYERS_INDEX, CHECK_LOCATION())
146}
147
Saoirse Stewart263829c2019-02-19 15:54:14 +0000148bool CheckShape(const armnn::TensorShape& actual, const std::vector<uint32_t>& expected)
149{
150 const unsigned int actualSize = actual.GetNumDimensions();
151 if (actualSize != expected.size())
152 {
153 return false;
154 }
155
156 for (unsigned int i = 0u; i < actualSize; i++)
157 {
158 if (actual[i] != static_cast<unsigned int>(expected[i]))
159 {
160 return false;
161 }
162 }
163
164 return true;
165}
166
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000167Deserializer::Deserializer()
Kevin May43a799c2019-02-08 16:31:42 +0000168: m_Network(nullptr, nullptr),
169//May require LayerType_Max to be included
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000170m_ParserFunctions(Layer_MAX+1, &Deserializer::ParseUnsupportedLayer)
Kevin May43a799c2019-02-08 16:31:42 +0000171{
172 // register supported layers
Mike Kellyaf484012019-02-20 16:53:11 +0000173 m_ParserFunctions[Layer_ActivationLayer] = &Deserializer::ParseActivation;
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000174 m_ParserFunctions[Layer_AdditionLayer] = &Deserializer::ParseAdd;
175 m_ParserFunctions[Layer_Convolution2dLayer] = &Deserializer::ParseConvolution2d;
176 m_ParserFunctions[Layer_DepthwiseConvolution2dLayer] = &Deserializer::ParseDepthwiseConvolution2d;
Sadik Armagandbb0c0c2019-02-21 09:01:41 +0000177 m_ParserFunctions[Layer_FullyConnectedLayer] = &Deserializer::ParseFullyConnected;
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000178 m_ParserFunctions[Layer_MultiplicationLayer] = &Deserializer::ParseMultiplication;
Nattapat Chaimanowong30b00202019-02-20 17:31:34 +0000179 m_ParserFunctions[Layer_PermuteLayer] = &Deserializer::ParsePermute;
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000180 m_ParserFunctions[Layer_Pooling2dLayer] = &Deserializer::ParsePooling2d;
181 m_ParserFunctions[Layer_ReshapeLayer] = &Deserializer::ParseReshape;
182 m_ParserFunctions[Layer_SoftmaxLayer] = &Deserializer::ParseSoftmax;
Kevin May43a799c2019-02-08 16:31:42 +0000183}
184
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000185Deserializer::LayerBaseRawPtr Deserializer::GetBaseLayer(const GraphPtr& graphPtr, unsigned int layerIndex)
Kevin May43a799c2019-02-08 16:31:42 +0000186{
187 auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type();
188
189 switch(layerType)
190 {
Mike Kellyaf484012019-02-20 16:53:11 +0000191 case Layer::Layer_ActivationLayer:
192 return graphPtr->layers()->Get(layerIndex)->layer_as_ActivationLayer()->base();
Kevin May43a799c2019-02-08 16:31:42 +0000193 case Layer::Layer_AdditionLayer:
194 return graphPtr->layers()->Get(layerIndex)->layer_as_AdditionLayer()->base();
Mike Kellya0766c32019-02-19 17:22:07 +0000195 case Layer::Layer_Convolution2dLayer:
196 return graphPtr->layers()->Get(layerIndex)->layer_as_Convolution2dLayer()->base();
Aron Virginas-Tarc04125f2019-02-19 16:31:08 +0000197 case Layer::Layer_DepthwiseConvolution2dLayer:
198 return graphPtr->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer()->base();
Sadik Armagandbb0c0c2019-02-21 09:01:41 +0000199 case Layer::Layer_FullyConnectedLayer:
200 return graphPtr->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer()->base();
Kevin May43a799c2019-02-08 16:31:42 +0000201 case Layer::Layer_InputLayer:
202 return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->base();
Sadik Armagan5f450272019-02-12 14:31:45 +0000203 case Layer::Layer_MultiplicationLayer:
204 return graphPtr->layers()->Get(layerIndex)->layer_as_MultiplicationLayer()->base();
Kevin May43a799c2019-02-08 16:31:42 +0000205 case Layer::Layer_OutputLayer:
206 return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->base();
Nattapat Chaimanowong30b00202019-02-20 17:31:34 +0000207 case Layer::Layer_PermuteLayer:
208 return graphPtr->layers()->Get(layerIndex)->layer_as_PermuteLayer()->base();
Saoirse Stewart3166c3e2019-02-18 15:24:53 +0000209 case Layer::Layer_Pooling2dLayer:
210 return graphPtr->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->base();
Saoirse Stewart263829c2019-02-19 15:54:14 +0000211 case Layer::Layer_ReshapeLayer:
212 return graphPtr->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->base();
Aron Virginas-Tarfc413c02019-02-13 15:41:52 +0000213 case Layer::Layer_SoftmaxLayer:
214 return graphPtr->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->base();
Kevin May43a799c2019-02-08 16:31:42 +0000215 case Layer::Layer_NONE:
216 default:
217 throw ParseException(boost::str(
218 boost::format("Layer must have a type %1%") %
219 Layer::Layer_NONE));
220 }
221}
222
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000223int32_t Deserializer::GetBindingLayerInfo(const GraphPtr& graphPtr, unsigned int layerIndex)
Kevin May43a799c2019-02-08 16:31:42 +0000224{
225 auto layerType = graphPtr->layers()->Get(layerIndex)->layer_type();
226
227 if (layerType == Layer::Layer_InputLayer)
228 {
229 return graphPtr->layers()->Get(layerIndex)->layer_as_InputLayer()->base()->layerBindingId();
230 }
231 else if ( layerType == Layer::Layer_OutputLayer )
232 {
233 return graphPtr->layers()->Get(layerIndex)->layer_as_OutputLayer()->base()->layerBindingId();
234 }
235 return 0;
236}
237
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000238armnn::DataLayout ToDataLayout(armnnSerializer::DataLayout dataLayout)
Mike Kellya0766c32019-02-19 17:22:07 +0000239{
240 switch (dataLayout)
241 {
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000242 case armnnSerializer::DataLayout::DataLayout_NHWC:
Mike Kellya0766c32019-02-19 17:22:07 +0000243 return armnn::DataLayout::NHWC;
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000244 case armnnSerializer::DataLayout::DataLayout_NCHW:
Mike Kellya0766c32019-02-19 17:22:07 +0000245 default:
246 return armnn::DataLayout::NCHW;
247 }
248}
249
Mike Kellyaf484012019-02-20 16:53:11 +0000250armnn::ActivationFunction ToActivationFunction(armnnSerializer::ActivationFunction function)
251{
252 switch (function)
253 {
254 case armnnSerializer::ActivationFunction_Sigmoid:
255 return armnn::ActivationFunction::Sigmoid;
256 case armnnSerializer::ActivationFunction_TanH:
257 return armnn::ActivationFunction::TanH;
258 case armnnSerializer::ActivationFunction_Linear:
259 return armnn::ActivationFunction::Linear;
260 case armnnSerializer::ActivationFunction_ReLu:
261 return armnn::ActivationFunction::ReLu;
262 case armnnSerializer::ActivationFunction_BoundedReLu:
263 return armnn::ActivationFunction::BoundedReLu;
264 case armnnSerializer::ActivationFunction_LeakyReLu:
265 return armnn::ActivationFunction::LeakyReLu;
266 case armnnSerializer::ActivationFunction_Abs:
267 return armnn::ActivationFunction::Abs;
268 case armnnSerializer::ActivationFunction_Sqrt:
269 return armnn::ActivationFunction::Sqrt;
270 case armnnSerializer::ActivationFunction_Square:
271 return armnn::ActivationFunction::Square;
272 default:
273 return armnn::ActivationFunction::Sigmoid;
274 }
275}
276
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000277armnn::TensorInfo ToTensorInfo(Deserializer::TensorRawPtr tensorPtr)
Kevin May43a799c2019-02-08 16:31:42 +0000278{
279 armnn::DataType type;
280 CHECK_TENSOR_PTR(tensorPtr);
281
282 switch (tensorPtr->dataType())
283 {
284 case DataType_QuantisedAsymm8:
285 type = armnn::DataType::QuantisedAsymm8;
286 break;
Mike Kellya0766c32019-02-19 17:22:07 +0000287 case DataType_Signed32:
288 type = armnn::DataType::Signed32;
289 break;
Kevin May43a799c2019-02-08 16:31:42 +0000290 case DataType_Float32:
291 type = armnn::DataType::Float32;
292 break;
293 case DataType_Float16:
294 type = armnn::DataType::Float16;
295 break;
296 case DataType_Boolean:
297 type = armnn::DataType::Boolean;
298 break;
299 default:
300 {
301 CheckLocation location = CHECK_LOCATION();
302 throw ParseException(
303 boost::str(
304 boost::format("Unsupported data type %1% = %2%. %3%") %
305 tensorPtr->dataType() %
306 EnumNameDataType(tensorPtr->dataType()) %
307 location.AsString()));
308 }
309 }
310 float quantizationScale = tensorPtr->quantizationScale();
311 int32_t quantizationOffset = tensorPtr->quantizationOffset();
312
313 auto dimensions = tensorPtr->dimensions();
314 unsigned int size = dimensions->size();
315 std::vector<unsigned int> outputDims(dimensions->begin(), dimensions->begin() + size);
316
317 // two statements (on purpose) for easier debugging:
318 armnn::TensorInfo result(size,
319 outputDims.data(),
320 type,
321 quantizationScale,
322 quantizationOffset);
323 return result;
324}
325
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000326armnn::ConstTensor ToConstTensor(Deserializer::ConstTensorRawPtr constTensorPtr)
Mike Kellya0766c32019-02-19 17:22:07 +0000327{
328 CHECK_CONST_TENSOR_PTR(constTensorPtr);
329 armnn::TensorInfo tensorInfo = ToTensorInfo(constTensorPtr->info());
330
331 switch (constTensorPtr->data_type())
332 {
333 case ConstTensorData_ByteData:
334 return armnn::ConstTensor(tensorInfo, constTensorPtr->data_as_ByteData()->data()->data());
335 case ConstTensorData_ShortData:
336 return armnn::ConstTensor(tensorInfo, constTensorPtr->data_as_ShortData()->data()->data());
337 case ConstTensorData_IntData:
338 return armnn::ConstTensor(tensorInfo, constTensorPtr->data_as_IntData()->data()->data());
339 case ConstTensorData_LongData:
340 return armnn::ConstTensor(tensorInfo, constTensorPtr->data_as_LongData()->data()->data());
341 default:
342 {
343 CheckLocation location = CHECK_LOCATION();
344 throw ParseException(
345 boost::str(boost::format("Unsupported data type %1% = %2%. %3%") %
346 constTensorPtr->data_type() %
347 EnumNameConstTensorData(constTensorPtr->data_type()) %
348 location.AsString()));
349 }
350 }
351}
352
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000353Deserializer::LayerBaseRawPtrVector Deserializer::GetGraphInputs(const GraphPtr& graphPtr)
Kevin May43a799c2019-02-08 16:31:42 +0000354{
355
356 CHECK_GRAPH(graphPtr, 0);
357 const auto& numInputs = graphPtr->inputIds()->size();
358
359 LayerBaseRawPtrVector result(numInputs);
360
361 for (unsigned int i=0; i<numInputs; ++i)
362 {
Mike Kelly8c1701a2019-02-11 17:01:27 +0000363 uint32_t inputId = graphPtr->inputIds()->Get(i);
Kevin May43a799c2019-02-08 16:31:42 +0000364 result[i] = GetBaseLayer(graphPtr, static_cast<uint32_t>(inputId));
365 }
366 return result;
367}
368
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000369Deserializer::LayerBaseRawPtrVector Deserializer::GetGraphOutputs(const GraphPtr& graphPtr)
Kevin May43a799c2019-02-08 16:31:42 +0000370{
371 CHECK_GRAPH(graphPtr, 0);
372 const auto& numOutputs = graphPtr->outputIds()->size();
Kevin May43a799c2019-02-08 16:31:42 +0000373 LayerBaseRawPtrVector result(numOutputs);
374
375 for (unsigned int i=0; i<numOutputs; ++i)
376 {
Mike Kelly8c1701a2019-02-11 17:01:27 +0000377 uint32_t outputId = graphPtr->outputIds()->Get(i);
Saoirse Stewart263829c2019-02-19 15:54:14 +0000378
Kevin May43a799c2019-02-08 16:31:42 +0000379 result[i] = GetBaseLayer(graphPtr, static_cast<uint32_t>(outputId));
380 }
381 return result;
382}
383
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000384Deserializer::TensorRawPtrVector Deserializer::GetInputs(const GraphPtr& graphPtr,
Kevin May43a799c2019-02-08 16:31:42 +0000385 unsigned int layerIndex)
386{
387 CHECK_LAYERS(graphPtr, 0, layerIndex);
388 auto layer = GetBaseLayer(graphPtr, layerIndex);
389 const auto& numInputs = layer->inputSlots()->size();
390
391 TensorRawPtrVector result(numInputs);
392
393 for (unsigned int i=0; i<numInputs; ++i)
394 {
395 auto inputId = CHECKED_NON_NEGATIVE(static_cast<int32_t>
396 (layer->inputSlots()->Get(i)->connection()->sourceLayerIndex()));
397 result[i] = GetBaseLayer(graphPtr, inputId)->outputSlots()->Get(0)->tensorInfo();
398 }
399 return result;
400}
401
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000402Deserializer::TensorRawPtrVector Deserializer::GetOutputs(const GraphPtr& graphPtr,
Kevin May43a799c2019-02-08 16:31:42 +0000403 unsigned int layerIndex)
404{
405 CHECK_LAYERS(graphPtr, 0, layerIndex);
406 auto layer = GetBaseLayer(graphPtr, layerIndex);
407 const auto& numOutputs = layer->outputSlots()->size();
408
409 TensorRawPtrVector result(numOutputs);
410
411 for (unsigned int i=0; i<numOutputs; ++i)
412 {
413 result[i] = layer->outputSlots()->Get(i)->tensorInfo();
414 }
415 return result;
416}
417
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000418void Deserializer::ParseUnsupportedLayer(unsigned int layerIndex)
Kevin May43a799c2019-02-08 16:31:42 +0000419{
420 CHECK_LAYERS(m_Graph, 0, layerIndex);
421 const auto layerName = GetBaseLayer(m_Graph, layerIndex)->layerName()->c_str();
422 throw ParseException(
423 boost::str(
424 boost::format("Layer not supported. "
425 "layerIndex: %1% "
426 "layerName: %2% / %3%") %
427 layerIndex %
428 layerName %
429 CHECK_LOCATION().AsString()));
430}
431
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000432void Deserializer::ResetParser()
Kevin May43a799c2019-02-08 16:31:42 +0000433{
434 m_Network = armnn::INetworkPtr(nullptr, nullptr);
435 m_Graph = nullptr;
436}
437
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000438IDeserializer* IDeserializer::CreateRaw()
Kevin May43a799c2019-02-08 16:31:42 +0000439{
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000440 return new Deserializer();
Kevin May43a799c2019-02-08 16:31:42 +0000441}
442
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000443IDeserializerPtr IDeserializer::Create()
Kevin May43a799c2019-02-08 16:31:42 +0000444{
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000445 return IDeserializerPtr(CreateRaw(), &IDeserializer::Destroy);
Kevin May43a799c2019-02-08 16:31:42 +0000446}
447
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000448void IDeserializer::Destroy(IDeserializer* parser)
Kevin May43a799c2019-02-08 16:31:42 +0000449{
450 delete parser;
451}
452
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000453INetworkPtr Deserializer::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
Kevin May43a799c2019-02-08 16:31:42 +0000454{
455 ResetParser();
456 m_Graph = LoadGraphFromBinary(binaryContent.data(), binaryContent.size());
457 return CreateNetworkFromGraph();
458}
459
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000460armnn::INetworkPtr Deserializer::CreateNetworkFromBinary(std::istream& binaryContent)
Kevin May43a799c2019-02-08 16:31:42 +0000461{
Derek Lamberti2b183fb2019-02-18 16:36:57 +0000462 ResetParser();
463 m_Graph = LoadGraphFromBinary(binaryContent);
464 return CreateNetworkFromGraph();
Kevin May43a799c2019-02-08 16:31:42 +0000465}
466
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000467Deserializer::GraphPtr Deserializer::LoadGraphFromBinary(const uint8_t* binaryContent, size_t len)
Kevin May43a799c2019-02-08 16:31:42 +0000468{
469 if (binaryContent == nullptr)
470 {
471 throw InvalidArgumentException(boost::str(boost::format("Invalid (null) binary content %1%") %
472 CHECK_LOCATION().AsString()));
473 }
474 flatbuffers::Verifier verifier(binaryContent, len);
475 if (verifier.VerifyBuffer<SerializedGraph>() == false)
476 {
477 throw ParseException(
478 boost::str(boost::format("Buffer doesn't conform to the expected Armnn "
479 "flatbuffers format. size:%1% %2%") %
480 len %
481 CHECK_LOCATION().AsString()));
482 }
483 return GetSerializedGraph(binaryContent);
484}
485
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000486Deserializer::GraphPtr Deserializer::LoadGraphFromBinary(std::istream& binaryContent)
Derek Lamberti2b183fb2019-02-18 16:36:57 +0000487{
488 std::string content((std::istreambuf_iterator<char>(binaryContent)), std::istreambuf_iterator<char>());
489 return GetSerializedGraph(content.data());
490}
491
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000492INetworkPtr Deserializer::CreateNetworkFromGraph()
Kevin May43a799c2019-02-08 16:31:42 +0000493{
494 m_Network = INetwork::Create();
495 BOOST_ASSERT(m_Graph != nullptr);
496 unsigned int layerIndex = 0;
497 m_GraphConnections.emplace_back(m_Graph->layers()->size());
498 for (AnyLayer const* layer : *m_Graph->layers())
499 {
500 if (layer->layer_type() != Layer_InputLayer &&
501 layer->layer_type() != Layer_OutputLayer)
502 {
503 // lookup and call the parser function
504 auto& parserFunction = m_ParserFunctions[layer->layer_type()];
505 (this->*parserFunction)(layerIndex);
506 }
507 ++layerIndex;
508 }
509
510 SetupInputLayers();
511 SetupOutputLayers();
512
513 // establish the connections from the layer outputs to the inputs of the subsequent layers
514 for (size_t connectionIndex = 0; connectionIndex < m_GraphConnections[0].size(); ++connectionIndex)
515 {
516 if (m_GraphConnections[0][connectionIndex].outputSlot != nullptr)
517 {
518 for (size_t inputSlotIdx = 0;
519 inputSlotIdx < m_GraphConnections[0][connectionIndex].inputSlots.size();
520 ++inputSlotIdx)
521 {
522 m_GraphConnections[0][connectionIndex].outputSlot->Connect(
523 *(m_GraphConnections[0][connectionIndex].inputSlots[inputSlotIdx]));
524 }
525 }
526 }
527
528 return std::move(m_Network);
529}
530
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000531BindingPointInfo Deserializer::GetNetworkInputBindingInfo(unsigned int layerIndex,
Kevin May43a799c2019-02-08 16:31:42 +0000532 const std::string& name) const
533{
534 CHECK_LAYERS(m_Graph, 0, layerIndex);
535 auto inputs = GetGraphInputs(m_Graph);
536
537 for (auto const& input : inputs)
538 {
539 if (input->layerName()->c_str() == name)
540 {
541 int bindingId = reinterpret_cast<armnn::LayerBindingId>(GetBindingLayerInfo(m_Graph, input->index()));
542 auto layerBase = GetBaseLayer(m_Graph,input->index())->outputSlots()->Get(layerIndex);
543 return std::make_pair(bindingId, ToTensorInfo(layerBase->tensorInfo()));
544 }
545 }
546 throw ParseException(
547 boost::str(
548 boost::format("No input binding found for layer:%1% / %2%") %
549 name %
550 CHECK_LOCATION().AsString()));
551}
552
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000553BindingPointInfo Deserializer::GetNetworkOutputBindingInfo(unsigned int layerIndex,
Kevin May43a799c2019-02-08 16:31:42 +0000554 const std::string& name) const
555{
556 CHECK_LAYERS(m_Graph, 0, layerIndex);
557 auto outputs = GetGraphOutputs(m_Graph);
558
559 for (auto const& output : outputs)
560 {
561 if (output->layerName()->c_str() == name)
562 {
563 int bindingId = reinterpret_cast<armnn::LayerBindingId>(GetBindingLayerInfo(m_Graph, output->index()));
564 auto layer = GetBaseLayer(m_Graph, output->index());
565 auto sourceLayerIndex = layer->inputSlots()->Get(0)->connection()->sourceLayerIndex();
566 auto sourceLayer = GetBaseLayer(m_Graph, sourceLayerIndex);
567 return std::make_pair(bindingId, ToTensorInfo(sourceLayer->outputSlots()->Get(0)->tensorInfo()));
568 }
569 }
570 throw ParseException(
571 boost::str(
572 boost::format("No output binding found for layer:%1% / %2%") %
573 name %
574 CHECK_LOCATION().AsString()));
575}
576
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000577void Deserializer::SetupInputLayers()
Kevin May43a799c2019-02-08 16:31:42 +0000578{
579 CHECK_GRAPH(m_Graph, 0);
580 auto inputs = GetGraphInputs(m_Graph);
581 for (auto const& input : inputs)
582 {
583 IConnectableLayer* layer =
Saoirse Stewart3fcef202019-02-14 14:57:37 +0000584 m_Network->AddInputLayer(GetBindingLayerInfo(m_Graph, input->index()), input->layerName()->c_str());
Kevin May43a799c2019-02-08 16:31:42 +0000585
586 auto tensorInfo = ToTensorInfo(input->outputSlots()->Get(0)->tensorInfo());
587 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
588
589 RegisterOutputSlots(input->index(), layer);
590 }
591}
592
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000593void Deserializer::SetupOutputLayers()
Kevin May43a799c2019-02-08 16:31:42 +0000594{
595 CHECK_GRAPH(m_Graph, 0);
596 auto outputs = GetGraphOutputs(m_Graph);
597 for (auto const& output : outputs)
598 {
599 IConnectableLayer* layer =
Saoirse Stewart3fcef202019-02-14 14:57:37 +0000600 m_Network->AddOutputLayer(GetBindingLayerInfo(m_Graph, output->index()), output->layerName()->c_str());
Kevin May43a799c2019-02-08 16:31:42 +0000601
602 RegisterInputSlots(output->index(), layer);
603 }
604}
605
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000606void Deserializer::RegisterOutputSlots(uint32_t layerIndex,
Kevin May43a799c2019-02-08 16:31:42 +0000607 IConnectableLayer* layer)
608{
609 CHECK_LAYERS(m_Graph, 0, layerIndex);
610 BOOST_ASSERT(layer != nullptr);
611 auto parsedLayer = GetBaseLayer(m_Graph, layerIndex);
612 if (parsedLayer->outputSlots()->size() != layer->GetNumOutputSlots())
613 {
614 throw ParseException(
615 boost::str(boost::format("The number of outputslots (%1%) does not match the number expected (%2%)"
616 " for layer index: %3% %4%") %
617 parsedLayer->outputSlots()->size() %
618 layer->GetNumOutputSlots() %
619 layerIndex %
620 CHECK_LOCATION().AsString()));
621 }
622
623 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
624 {
625 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
626 RegisterOutputSlotOfConnection(layerIndex, slot);
627 }
628}
629
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000630void Deserializer::RegisterInputSlots(uint32_t layerIndex,
Kevin May43a799c2019-02-08 16:31:42 +0000631 armnn::IConnectableLayer* layer)
632{
633 CHECK_LAYERS(m_Graph, 0, layerIndex);
634 BOOST_ASSERT(layer != nullptr);
635 auto parsedLayer = GetBaseLayer(m_Graph, layerIndex);
636 if (parsedLayer->inputSlots()->size() != layer->GetNumInputSlots())
637 {
638 throw ParseException(
639 boost::str(boost::format("The number of inputslots (%1%) does not match the number expected (%2%)"
640 " for layer index:%3% %4%") %
641 parsedLayer->inputSlots()->size() %
642 layer->GetNumInputSlots() %
643 layerIndex %
644 CHECK_LOCATION().AsString()));
645 }
646
647 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumInputSlots(); ++slotIndex)
648 {
649 armnn::IInputSlot* slot = &(layer->GetInputSlot(slotIndex));
650 uint32_t sourceLayerIndex = parsedLayer->inputSlots()->Get(slotIndex)->connection()->sourceLayerIndex();
651 RegisterInputSlotOfConnection(sourceLayerIndex, slot);
652 }
653}
654
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000655void Deserializer::RegisterInputSlotOfConnection(uint32_t connectionIndex,
Kevin May43a799c2019-02-08 16:31:42 +0000656 armnn::IInputSlot* slot)
657{
658 BOOST_ASSERT(m_GraphConnections[0].size() > connectionIndex);
659
660 Slots& slots = m_GraphConnections[0][connectionIndex];
661 slots.inputSlots.push_back(slot);
662}
663
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000664void Deserializer::RegisterOutputSlotOfConnection(uint32_t connectionIndex,
Kevin May43a799c2019-02-08 16:31:42 +0000665 armnn::IOutputSlot* slot)
666{
667 BOOST_ASSERT(m_GraphConnections[0].size() > connectionIndex);
668
669 Slots& slots = m_GraphConnections[0][connectionIndex];
670
671 // assuming there is only one producer for that tensor
672 if (slots.outputSlot != nullptr)
673 {
674 throw ParseException(boost::str(
675 boost::format("Another layer has already registered itself as the producer of "
676 "connection:%1% / %2%") %
677 connectionIndex %
678 CHECK_LOCATION().AsString()));
679 }
680
681 slots.outputSlot = slot;
682}
683
Mike Kellyaf484012019-02-20 16:53:11 +0000684void Deserializer::ParseActivation(unsigned int layerIndex)
685{
686 CHECK_LAYERS(m_Graph, 0, layerIndex);
687 auto inputs = GetInputs(m_Graph, layerIndex);
688 CHECK_LOCATION();
689 CHECK_VALID_SIZE(inputs.size(), 1);
690
691 auto outputs = GetOutputs(m_Graph, layerIndex);
692 CHECK_VALID_SIZE(outputs.size(), 1);
693
694 auto layerName = boost::str(boost::format("Activation:%1%") % layerIndex);
695
696 auto serializerLayer = m_Graph->layers()->Get(layerIndex)->layer_as_ActivationLayer();
697 auto serializerDescriptor = serializerLayer->descriptor();
698
699 armnn::ActivationDescriptor descriptor;
700 descriptor.m_Function = ToActivationFunction(serializerDescriptor->function());
701 descriptor.m_A = serializerDescriptor->a();
702 descriptor.m_B = serializerDescriptor->b();
703
704 IConnectableLayer* layer = m_Network->AddActivationLayer(descriptor,
705 layerName.c_str());
706 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
707 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
708
709 RegisterInputSlots(layerIndex, layer);
710 RegisterOutputSlots(layerIndex, layer);
711}
712
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000713void Deserializer::ParseAdd(unsigned int layerIndex)
Kevin May43a799c2019-02-08 16:31:42 +0000714{
715 CHECK_LAYERS(m_Graph, 0, layerIndex);
716 auto inputs = GetInputs(m_Graph, layerIndex);
717 CHECK_LOCATION();
718 CHECK_VALID_SIZE(inputs.size(), 2);
719
720 auto outputs = GetOutputs(m_Graph, layerIndex);
721 CHECK_VALID_SIZE(outputs.size(), 1);
722
Saoirse Stewart3166c3e2019-02-18 15:24:53 +0000723 m_layerName = boost::str(boost::format("Addition:%1%") % layerIndex);
724 IConnectableLayer* layer = m_Network->AddAdditionLayer(m_layerName.c_str());
Kevin May43a799c2019-02-08 16:31:42 +0000725
726 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
727 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
728
729 RegisterInputSlots(layerIndex, layer);
730 RegisterOutputSlots(layerIndex, layer);
731}
732
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000733void Deserializer::ParseConvolution2d(unsigned int layerIndex)
Mike Kellya0766c32019-02-19 17:22:07 +0000734{
735 CHECK_LAYERS(m_Graph, 0, layerIndex);
736 auto inputs = GetInputs(m_Graph, layerIndex);
737 CHECK_LOCATION();
738 CHECK_VALID_SIZE(inputs.size(), 1);
739
740 auto outputs = GetOutputs(m_Graph, layerIndex);
741 CHECK_VALID_SIZE(outputs.size(), 1);
742
743 auto layerName = boost::str(boost::format("Convolution2d:%1%") % layerIndex);
744
745 auto serializerLayer = m_Graph->layers()->Get(layerIndex)->layer_as_Convolution2dLayer();
746 auto serializerDescriptor = serializerLayer->descriptor();
747
748 armnn::Convolution2dDescriptor descriptor;
749 descriptor.m_PadLeft = serializerDescriptor->padLeft();
750 descriptor.m_PadRight = serializerDescriptor->padRight();
751 descriptor.m_PadTop = serializerDescriptor->padTop();
752 descriptor.m_PadBottom = serializerDescriptor->padBottom();
753 descriptor.m_StrideX = serializerDescriptor->strideX();
754 descriptor.m_StrideY = serializerDescriptor->strideY();;
755 descriptor.m_BiasEnabled = serializerDescriptor->biasEnabled();;
756 descriptor.m_DataLayout = ToDataLayout(serializerDescriptor->dataLayout());
757
758 armnn::ConstTensor weights = ToConstTensor(serializerLayer->weights());
759 armnn::ConstTensor biases;
760
761 if (descriptor.m_BiasEnabled)
762 {
763 biases = ToConstTensor(serializerLayer->biases());
764 }
765 IConnectableLayer* layer = m_Network->AddConvolution2dLayer(descriptor,
766 weights,
767 biases,
768 layerName.c_str());
769 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
770 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
771
772 RegisterInputSlots(layerIndex, layer);
773 RegisterOutputSlots(layerIndex, layer);
774}
775
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000776void Deserializer::ParseDepthwiseConvolution2d(unsigned int layerIndex)
Aron Virginas-Tarc04125f2019-02-19 16:31:08 +0000777{
778 CHECK_LAYERS(m_Graph, 0, layerIndex);
779 auto inputs = GetInputs(m_Graph, layerIndex);
780 CHECK_LOCATION();
781 CHECK_VALID_SIZE(inputs.size(), 1);
782
783 auto outputs = GetOutputs(m_Graph, layerIndex);
784 CHECK_VALID_SIZE(outputs.size(), 1);
785
786 auto layerName = boost::str(boost::format("DepthwiseConvolution2d:%1%") % layerIndex);
787
788 auto serializerLayer = m_Graph->layers()->Get(layerIndex)->layer_as_DepthwiseConvolution2dLayer();
789 auto serializerDescriptor = serializerLayer->descriptor();
790
791 armnn::DepthwiseConvolution2dDescriptor descriptor;
792 descriptor.m_PadLeft = serializerDescriptor->padLeft();
793 descriptor.m_PadRight = serializerDescriptor->padRight();
794 descriptor.m_PadTop = serializerDescriptor->padTop();
795 descriptor.m_PadBottom = serializerDescriptor->padBottom();
796 descriptor.m_StrideX = serializerDescriptor->strideX();
797 descriptor.m_StrideY = serializerDescriptor->strideY();;
798 descriptor.m_BiasEnabled = serializerDescriptor->biasEnabled();;
799 descriptor.m_DataLayout = ToDataLayout(serializerDescriptor->dataLayout());
800
801 armnn::ConstTensor weights = ToConstTensor(serializerLayer->weights());
802 armnn::ConstTensor biases;
803
804 if (descriptor.m_BiasEnabled)
805 {
806 biases = ToConstTensor(serializerLayer->biases());
807 }
808 IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(descriptor,
809 weights,
810 biases,
811 layerName.c_str());
812
813 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
814 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
815
816 RegisterInputSlots(layerIndex, layer);
817 RegisterOutputSlots(layerIndex, layer);
818}
819
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000820void Deserializer::ParseMultiplication(unsigned int layerIndex)
Sadik Armagan5f450272019-02-12 14:31:45 +0000821{
822 CHECK_LAYERS(m_Graph, 0, layerIndex);
823 auto inputs = GetInputs(m_Graph, layerIndex);
824 CHECK_LOCATION();
825 CHECK_VALID_SIZE(inputs.size(), 2);
826
827 auto outputs = GetOutputs(m_Graph, layerIndex);
828 CHECK_VALID_SIZE(outputs.size(), 1);
829
Saoirse Stewart3166c3e2019-02-18 15:24:53 +0000830 m_layerName = boost::str(boost::format("Multiplication:%1%") % layerIndex);
831 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(m_layerName.c_str());
Sadik Armagan5f450272019-02-12 14:31:45 +0000832
833 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
834 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
835
836 RegisterInputSlots(layerIndex, layer);
837 RegisterOutputSlots(layerIndex, layer);
838}
839
Sadik Armagandbb0c0c2019-02-21 09:01:41 +0000840void Deserializer::ParseFullyConnected(unsigned int layerIndex)
841{
842 CHECK_LAYERS(m_Graph, 0, layerIndex);
843 auto inputs = GetInputs(m_Graph, layerIndex);
844 CHECK_LOCATION();
845 CHECK_VALID_SIZE(inputs.size(), 1);
846
847 auto outputs = GetOutputs(m_Graph, layerIndex);
848 CHECK_VALID_SIZE(outputs.size(), 1);
849
850 auto layerName = boost::str(boost::format("FullyConnected:%1%") % layerIndex);
851
852 auto flatBufferLayer = m_Graph->layers()->Get(layerIndex)->layer_as_FullyConnectedLayer();
853 auto flatBufferDescriptor = flatBufferLayer->descriptor();
854
855 armnn::FullyConnectedDescriptor fullyConnectedDescriptor;
856 fullyConnectedDescriptor.m_BiasEnabled = flatBufferDescriptor->biasEnabled();
857 fullyConnectedDescriptor.m_TransposeWeightMatrix = flatBufferDescriptor->transposeWeightsMatrix();
858
859 armnn::ConstTensor weightsTensor = ToConstTensor(flatBufferLayer->weights());
860
861 armnn::IConnectableLayer* layer;
862 if (flatBufferDescriptor->biasEnabled())
863 {
864 armnn::ConstTensor biasTensorData = ToConstTensor(flatBufferLayer->biases());
865 layer = m_Network->AddFullyConnectedLayer(fullyConnectedDescriptor,
866 weightsTensor,
867 biasTensorData,
868 layerName.c_str());
869 }
870 else
871 {
872 layer = m_Network->AddFullyConnectedLayer(fullyConnectedDescriptor,
873 weightsTensor,
874 layerName.c_str());
875 }
876
877 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
878 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
879
880 RegisterInputSlots(layerIndex, layer);
881 RegisterOutputSlots(layerIndex, layer);
882}
883
Nattapat Chaimanowong30b00202019-02-20 17:31:34 +0000884void Deserializer::ParsePermute(unsigned int layerIndex)
885{
886 CHECK_LAYERS(m_Graph, 0, layerIndex);
887
888 auto dimsMapping =
889 m_Graph->layers()->Get(layerIndex)->layer_as_PermuteLayer()->descriptor()->dimMappings();
890
891 auto inputs = GetInputs(m_Graph, layerIndex);
892 CHECK_VALID_SIZE(inputs.size(), 1);
893
894 auto outputs = GetOutputs(m_Graph, layerIndex);
895 CHECK_VALID_SIZE(outputs.size(), 1);
896 auto outputInfo = ToTensorInfo(outputs[0]);
897
898 m_layerName = boost::str(boost::format("Permute:%1%") % layerIndex);
899 const armnn::PermuteDescriptor descriptor(armnn::PermutationVector(dimsMapping->data(), dimsMapping->Length()));
900
901 IConnectableLayer* layer = m_Network->AddPermuteLayer(descriptor, m_layerName.c_str());
902 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
903
904 RegisterInputSlots(layerIndex, layer);
905 RegisterOutputSlots(layerIndex, layer);
906}
907
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000908armnn::Pooling2dDescriptor Deserializer::GetPoolingDescriptor(Deserializer::PoolingDescriptor pooling2dDesc,
Nattapat Chaimanowong30b00202019-02-20 17:31:34 +0000909 unsigned int layerIndex)
Saoirse Stewart3166c3e2019-02-18 15:24:53 +0000910{
911 armnn::Pooling2dDescriptor desc;
912
913 switch (pooling2dDesc->poolType())
914 {
915 case PoolingAlgorithm_Average:
916 {
917 desc.m_PoolType = armnn::PoolingAlgorithm::Average;
918 m_layerName = boost::str(boost::format("AveragePool2D:%1%") % layerIndex);
919 break;
920 }
921 case PoolingAlgorithm_Max:
922 {
923 desc.m_PoolType = armnn::PoolingAlgorithm::Max;
924 m_layerName = boost::str(boost::format("MaxPool2D:%1%") % layerIndex);
925 break;
926 }
927 default:
928 {
929 BOOST_ASSERT_MSG(false, "Unsupported pooling algorithm");
930 }
931 }
932
933 switch (pooling2dDesc->outputShapeRounding())
934 {
935 case OutputShapeRounding_Floor:
936 {
937 desc.m_OutputShapeRounding = armnn::OutputShapeRounding::Floor;
938 break;
939 }
940 case OutputShapeRounding_Ceiling:
941 {
942 desc.m_OutputShapeRounding = armnn::OutputShapeRounding::Ceiling;
943 break;
944 }
945 default:
946 {
947 BOOST_ASSERT_MSG(false, "Unsupported output shape rounding");
948 }
949 }
950
951 switch (pooling2dDesc->paddingMethod())
952 {
953 case PaddingMethod_Exclude:
954 {
955 desc.m_PaddingMethod = armnn::PaddingMethod::Exclude;
956 break;
957 }
958 case PaddingMethod_IgnoreValue:
959 {
960 desc.m_PaddingMethod = armnn::PaddingMethod::IgnoreValue;
961 break;
962 }
963 default:
964 {
965 BOOST_ASSERT_MSG(false, "Unsupported padding method");
966 }
967 }
968
969 switch (pooling2dDesc->dataLayout())
970 {
971 case DataLayout_NCHW:
972 {
973 desc.m_DataLayout = armnn::DataLayout::NCHW;
974 break;
975 }
976 case DataLayout_NHWC:
977 {
978 desc.m_DataLayout = armnn::DataLayout::NHWC;
979 break;
980 }
981 default:
982 {
983 BOOST_ASSERT_MSG(false, "Unsupported data layout");
984 }
985 }
986
987 desc.m_PadRight = pooling2dDesc->padRight();
988 desc.m_PadLeft = pooling2dDesc->padLeft();
989 desc.m_PadBottom = pooling2dDesc->padBottom();
990 desc.m_PadTop = pooling2dDesc->padTop();
991 desc.m_StrideX = pooling2dDesc->strideX();
992 desc.m_StrideY = pooling2dDesc->strideY();
993 desc.m_PoolWidth = pooling2dDesc->poolWidth();
994 desc.m_PoolHeight = pooling2dDesc->poolHeight();
995
996 return desc;
997}
998
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000999void Deserializer::ParsePooling2d(unsigned int layerIndex)
Saoirse Stewart3166c3e2019-02-18 15:24:53 +00001000{
1001 CHECK_LAYERS(m_Graph, 0, layerIndex);
1002
1003 auto pooling2dDes = m_Graph->layers()->Get(layerIndex)->layer_as_Pooling2dLayer()->descriptor();
1004
1005 auto inputs = GetInputs(m_Graph, layerIndex);
1006 CHECK_VALID_SIZE(inputs.size(), 1);
1007
1008 auto outputs = GetOutputs(m_Graph, layerIndex);
1009 CHECK_VALID_SIZE(outputs.size(), 1);
1010 auto outputInfo = ToTensorInfo(outputs[0]);
1011
1012 auto pooling2dDescriptor = GetPoolingDescriptor(pooling2dDes, layerIndex);
1013
1014 IConnectableLayer* layer = m_Network->AddPooling2dLayer(pooling2dDescriptor, m_layerName.c_str());
1015 layer->GetOutputSlot(0).SetTensorInfo(outputInfo);
1016
1017 RegisterInputSlots(layerIndex, layer);
1018 RegisterOutputSlots(layerIndex, layer);
1019}
1020
Derek Lamberti0028d1b2019-02-20 13:57:42 +00001021armnn::TensorInfo Deserializer::OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo,
Saoirse Stewart263829c2019-02-19 15:54:14 +00001022 const std::vector<uint32_t>& targetDimsIn)
1023{
1024 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
1025 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
1026
1027 if (stretchDim != targetDimsIn.end())
1028 {
1029 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
1030 {
1031 throw ParseException(boost::str(
1032 boost::format("At most one component of shape can be -1 %1%") % CHECK_LOCATION().AsString()));
1033 }
1034
1035 auto targetNumElements =
1036 boost::numeric_cast<unsigned int>(
1037 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
1038
1039 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
1040 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
1041 }
1042
1043 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
1044
1045 armnn::TensorInfo reshapeInfo = inputTensorInfo;
1046 reshapeInfo.SetShape(outputShape);
1047
1048 return reshapeInfo;
1049}
1050
Derek Lamberti0028d1b2019-02-20 13:57:42 +00001051void Deserializer::ParseReshape(unsigned int layerIndex)
Saoirse Stewart263829c2019-02-19 15:54:14 +00001052{
1053 CHECK_LAYERS(m_Graph, 0, layerIndex);
1054 auto inputs = GetInputs(m_Graph, layerIndex);
1055
1056 auto outputs = GetOutputs(m_Graph, layerIndex);
1057 CHECK_VALID_SIZE(outputs.size(), 1);
1058
1059 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1060 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
1061
1062 const auto targetDims = m_Graph->layers()->Get(layerIndex)->layer_as_ReshapeLayer()->descriptor()->targetShape();
1063 std::vector<uint32_t> outputDims(targetDims->begin(), targetDims->begin() + targetDims->size());
1064
Derek Lamberti0028d1b2019-02-20 13:57:42 +00001065 armnn::TensorInfo reshapeOutputTensorInfo = Deserializer::OutputShapeOfReshape(inputTensorInfo, outputDims);
Saoirse Stewart263829c2019-02-19 15:54:14 +00001066 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
1067
1068 const std::vector<uint32_t> expectedDims(outputs[0]->dimensions()->begin(),
1069 outputs[0]->dimensions()->begin() + outputs[0]->dimensions()->size());
1070
1071 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, expectedDims))
1072 {
1073 std::stringstream ss;
1074 ss << "New shape defined in reshape parameters "
1075 << reshapeOutputTensorShape
1076 << " does not equal output shape "
1077 << actualOutputTensorInfo.GetShape()
1078 << ": "
1079 << CHECK_LOCATION().AsString();
1080 throw ParseException(ss.str());
1081 }
1082
1083 armnn::ReshapeDescriptor reshapeDesc;
1084 reshapeDesc.m_TargetShape = reshapeOutputTensorShape;
1085
1086 auto layerName = boost::str(boost::format("Reshape:%1%") % layerIndex);
1087 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1088 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
1089
1090 RegisterInputSlots(layerIndex, layer);
1091 RegisterOutputSlots(layerIndex, layer);
1092}
1093
Derek Lamberti0028d1b2019-02-20 13:57:42 +00001094void Deserializer::ParseSoftmax(unsigned int layerIndex)
Aron Virginas-Tarfc413c02019-02-13 15:41:52 +00001095{
1096 CHECK_LAYERS(m_Graph, 0, layerIndex);
1097
Derek Lamberti0028d1b2019-02-20 13:57:42 +00001098 Deserializer::TensorRawPtrVector inputs = GetInputs(m_Graph, layerIndex);
Aron Virginas-Tarfc413c02019-02-13 15:41:52 +00001099 CHECK_VALID_SIZE(inputs.size(), 1);
1100
Derek Lamberti0028d1b2019-02-20 13:57:42 +00001101 Deserializer::TensorRawPtrVector outputs = GetOutputs(m_Graph, layerIndex);
Aron Virginas-Tarfc413c02019-02-13 15:41:52 +00001102 CHECK_VALID_SIZE(outputs.size(), 1);
1103
1104 armnn::SoftmaxDescriptor descriptor;
1105 descriptor.m_Beta = m_Graph->layers()->Get(layerIndex)->layer_as_SoftmaxLayer()->descriptor()->beta();
1106
1107 const std::string layerName = boost::str(boost::format("Softmax:%1%") % layerIndex);
1108 IConnectableLayer* layer = m_Network->AddSoftmaxLayer(descriptor, layerName.c_str());
1109
1110 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
1111 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1112
1113 RegisterInputSlots(layerIndex, layer);
1114 RegisterOutputSlots(layerIndex, layer);
Kevin May43a799c2019-02-08 16:31:42 +00001115}
Aron Virginas-Tarfc413c02019-02-13 15:41:52 +00001116
Derek Lamberti0028d1b2019-02-20 13:57:42 +00001117} // namespace armnnDeserializer