blob: 94318e406288f237d2e8cc6aecdda8adc7caf008 [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
6#pragma once
7
8#include "armnn/INetwork.hpp"
Derek Lamberti0028d1b2019-02-20 13:57:42 +00009#include "armnnDeserializer/IDeserializer.hpp"
Matthew Bentham268509a2019-02-25 13:58:24 +000010#include <ArmnnSchema_generated.h>
Kevin May43a799c2019-02-08 16:31:42 +000011
Derek Lamberti0028d1b2019-02-20 13:57:42 +000012namespace armnnDeserializer
Kevin May43a799c2019-02-08 16:31:42 +000013{
Derek Lamberti0028d1b2019-02-20 13:57:42 +000014class Deserializer : public IDeserializer
Kevin May43a799c2019-02-08 16:31:42 +000015{
16public:
17 // Shorthands for deserializer types
Derek Lamberti0028d1b2019-02-20 13:57:42 +000018 using ConstTensorRawPtr = const armnnSerializer::ConstTensor *;
19 using GraphPtr = const armnnSerializer::SerializedGraph *;
20 using TensorRawPtr = const armnnSerializer::TensorInfo *;
21 using PoolingDescriptor = const armnnSerializer::Pooling2dDescriptor *;
Kevin May43a799c2019-02-08 16:31:42 +000022 using TensorRawPtrVector = std::vector<TensorRawPtr>;
Derek Lamberti0028d1b2019-02-20 13:57:42 +000023 using LayerRawPtr = const armnnSerializer::LayerBase *;
24 using LayerBaseRawPtr = const armnnSerializer::LayerBase *;
Kevin May43a799c2019-02-08 16:31:42 +000025 using LayerBaseRawPtrVector = std::vector<LayerBaseRawPtr>;
26
27public:
28
Derek Lamberti2b183fb2019-02-18 16:36:57 +000029 /// Create an input network from binary file contents
30 armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent) override;
Kevin May43a799c2019-02-08 16:31:42 +000031
Derek Lamberti2b183fb2019-02-18 16:36:57 +000032 /// Create an input network from a binary input stream
33 armnn::INetworkPtr CreateNetworkFromBinary(std::istream& binaryContent) override;
Kevin May43a799c2019-02-08 16:31:42 +000034
35 /// Retrieve binding info (layer id and tensor info) for the network input identified by the given layer name
Derek Lamberti2b183fb2019-02-18 16:36:57 +000036 BindingPointInfo GetNetworkInputBindingInfo(unsigned int layerId, const std::string& name) const override;
Kevin May43a799c2019-02-08 16:31:42 +000037
38 /// Retrieve binding info (layer id and tensor info) for the network output identified by the given layer name
Derek Lamberti2b183fb2019-02-18 16:36:57 +000039 BindingPointInfo GetNetworkOutputBindingInfo(unsigned int layerId, const std::string& name) const override;
Kevin May43a799c2019-02-08 16:31:42 +000040
Derek Lamberti0028d1b2019-02-20 13:57:42 +000041 Deserializer();
42 ~Deserializer() {}
Kevin May43a799c2019-02-08 16:31:42 +000043
44public:
45 // testable helpers
Kevin May43a799c2019-02-08 16:31:42 +000046 static GraphPtr LoadGraphFromBinary(const uint8_t* binaryContent, size_t len);
47 static TensorRawPtrVector GetInputs(const GraphPtr& graph, unsigned int layerIndex);
48 static TensorRawPtrVector GetOutputs(const GraphPtr& graph, unsigned int layerIndex);
49 static LayerBaseRawPtrVector GetGraphInputs(const GraphPtr& graphPtr);
50 static LayerBaseRawPtrVector GetGraphOutputs(const GraphPtr& graphPtr);
51 static LayerBaseRawPtr GetBaseLayer(const GraphPtr& graphPtr, unsigned int layerIndex);
52 static int32_t GetBindingLayerInfo(const GraphPtr& graphPtr, unsigned int layerIndex);
Éanna Ó Catháin633f8592019-02-25 16:26:29 +000053 static std::string GetLayerName(const GraphPtr& graph, unsigned int index);
Saoirse Stewart3166c3e2019-02-18 15:24:53 +000054 armnn::Pooling2dDescriptor GetPoolingDescriptor(PoolingDescriptor pooling2dDescriptor,
55 unsigned int layerIndex);
Saoirse Stewart263829c2019-02-19 15:54:14 +000056 static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo,
57 const std::vector<uint32_t> & targetDimsIn);
Kevin May43a799c2019-02-08 16:31:42 +000058
59private:
60 // No copying allowed until it is wanted and properly implemented
Derek Lamberti0028d1b2019-02-20 13:57:42 +000061 Deserializer(const Deserializer&) = delete;
62 Deserializer& operator=(const Deserializer&) = delete;
Kevin May43a799c2019-02-08 16:31:42 +000063
64 /// Create the network from an already loaded flatbuffers graph
Derek Lamberti8ddae332019-02-21 16:29:43 +000065 armnn::INetworkPtr CreateNetworkFromGraph(GraphPtr graph);
Kevin May43a799c2019-02-08 16:31:42 +000066
67 // signature for the parser functions
Derek Lamberti8ddae332019-02-21 16:29:43 +000068 using LayerParsingFunction = void(Deserializer::*)(GraphPtr graph, unsigned int layerIndex);
Kevin May43a799c2019-02-08 16:31:42 +000069
Derek Lamberti8ddae332019-02-21 16:29:43 +000070 void ParseUnsupportedLayer(GraphPtr graph, unsigned int layerIndex);
71 void ParseActivation(GraphPtr graph, unsigned int layerIndex);
72 void ParseAdd(GraphPtr graph, unsigned int layerIndex);
73 void ParseConvolution2d(GraphPtr graph, unsigned int layerIndex);
74 void ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex);
75 void ParseFullyConnected(GraphPtr graph, unsigned int layerIndex);
76 void ParseMultiplication(GraphPtr graph, unsigned int layerIndex);
77 void ParsePermute(GraphPtr graph, unsigned int layerIndex);
78 void ParsePooling2d(GraphPtr graph, unsigned int layerIndex);
79 void ParseReshape(GraphPtr graph, unsigned int layerIndex);
80 void ParseSoftmax(GraphPtr graph, unsigned int layerIndex);
Kevin May43a799c2019-02-08 16:31:42 +000081
82 void RegisterOutputSlotOfConnection(uint32_t connectionIndex, armnn::IOutputSlot* slot);
83 void RegisterInputSlotOfConnection(uint32_t connectionIndex, armnn::IInputSlot* slot);
Derek Lamberti8ddae332019-02-21 16:29:43 +000084 void RegisterInputSlots(GraphPtr graph, uint32_t layerIndex,
Kevin May43a799c2019-02-08 16:31:42 +000085 armnn::IConnectableLayer* layer);
Derek Lamberti8ddae332019-02-21 16:29:43 +000086 void RegisterOutputSlots(GraphPtr graph, uint32_t layerIndex,
Kevin May43a799c2019-02-08 16:31:42 +000087 armnn::IConnectableLayer* layer);
88 void ResetParser();
89
Derek Lamberti8ddae332019-02-21 16:29:43 +000090 void SetupInputLayers(GraphPtr graphPtr);
91 void SetupOutputLayers(GraphPtr graphPtr);
Kevin May43a799c2019-02-08 16:31:42 +000092
93 /// The network we're building. Gets cleared after it is passed to the user
94 armnn::INetworkPtr m_Network;
Kevin May43a799c2019-02-08 16:31:42 +000095 std::vector<LayerParsingFunction> m_ParserFunctions;
96
Derek Lamberti8ddae332019-02-21 16:29:43 +000097 using NameToBindingInfo = std::pair<std::string, BindingPointInfo >;
98 std::vector<NameToBindingInfo> m_InputBindings;
99 std::vector<NameToBindingInfo> m_OutputBindings;
100
Kevin May43a799c2019-02-08 16:31:42 +0000101 /// A mapping of an output slot to each of the input slots it should be connected to
102 /// The outputSlot is from the layer that creates this tensor as one of its outputs
103 /// The inputSlots are from the layers that use this tensor as one of their inputs
104 struct Slots
105 {
106 armnn::IOutputSlot* outputSlot;
107 std::vector<armnn::IInputSlot*> inputSlots;
108
109 Slots() : outputSlot(nullptr) { }
110 };
111 typedef std::vector<Slots> Connection;
112 std::vector<Connection> m_GraphConnections;
113};
114
Derek Lamberti0028d1b2019-02-20 13:57:42 +0000115} //namespace armnnDeserializer