Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 1 | // |
| 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 Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 9 | #include "armnnDeserializer/IDeserializer.hpp" |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 10 | #include <Schema_generated.h> |
| 11 | |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 12 | namespace armnnDeserializer |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 13 | { |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 14 | class Deserializer : public IDeserializer |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 15 | { |
| 16 | public: |
| 17 | // Shorthands for deserializer types |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 18 | using ConstTensorRawPtr = const armnnSerializer::ConstTensor *; |
| 19 | using GraphPtr = const armnnSerializer::SerializedGraph *; |
| 20 | using TensorRawPtr = const armnnSerializer::TensorInfo *; |
| 21 | using PoolingDescriptor = const armnnSerializer::Pooling2dDescriptor *; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 22 | using TensorRawPtrVector = std::vector<TensorRawPtr>; |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 23 | using LayerRawPtr = const armnnSerializer::LayerBase *; |
| 24 | using LayerBaseRawPtr = const armnnSerializer::LayerBase *; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 25 | using LayerBaseRawPtrVector = std::vector<LayerBaseRawPtr>; |
| 26 | |
| 27 | public: |
| 28 | |
Derek Lamberti | 2b183fb | 2019-02-18 16:36:57 +0000 | [diff] [blame] | 29 | /// Create an input network from binary file contents |
| 30 | armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent) override; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 31 | |
Derek Lamberti | 2b183fb | 2019-02-18 16:36:57 +0000 | [diff] [blame] | 32 | /// Create an input network from a binary input stream |
| 33 | armnn::INetworkPtr CreateNetworkFromBinary(std::istream& binaryContent) override; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 34 | |
| 35 | /// Retrieve binding info (layer id and tensor info) for the network input identified by the given layer name |
Derek Lamberti | 2b183fb | 2019-02-18 16:36:57 +0000 | [diff] [blame] | 36 | BindingPointInfo GetNetworkInputBindingInfo(unsigned int layerId, const std::string& name) const override; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 37 | |
| 38 | /// Retrieve binding info (layer id and tensor info) for the network output identified by the given layer name |
Derek Lamberti | 2b183fb | 2019-02-18 16:36:57 +0000 | [diff] [blame] | 39 | BindingPointInfo GetNetworkOutputBindingInfo(unsigned int layerId, const std::string& name) const override; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 40 | |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 41 | Deserializer(); |
| 42 | ~Deserializer() {} |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 43 | |
| 44 | public: |
| 45 | // testable helpers |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 46 | 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); |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 53 | armnn::Pooling2dDescriptor GetPoolingDescriptor(PoolingDescriptor pooling2dDescriptor, |
| 54 | unsigned int layerIndex); |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 55 | static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo, |
| 56 | const std::vector<uint32_t> & targetDimsIn); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | // No copying allowed until it is wanted and properly implemented |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 60 | Deserializer(const Deserializer&) = delete; |
| 61 | Deserializer& operator=(const Deserializer&) = delete; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 62 | |
| 63 | /// Create the network from an already loaded flatbuffers graph |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 64 | armnn::INetworkPtr CreateNetworkFromGraph(GraphPtr graph); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 65 | |
| 66 | // signature for the parser functions |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 67 | using LayerParsingFunction = void(Deserializer::*)(GraphPtr graph, unsigned int layerIndex); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 68 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 69 | void ParseUnsupportedLayer(GraphPtr graph, unsigned int layerIndex); |
| 70 | void ParseActivation(GraphPtr graph, unsigned int layerIndex); |
| 71 | void ParseAdd(GraphPtr graph, unsigned int layerIndex); |
| 72 | void ParseConvolution2d(GraphPtr graph, unsigned int layerIndex); |
| 73 | void ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex); |
| 74 | void ParseFullyConnected(GraphPtr graph, unsigned int layerIndex); |
| 75 | void ParseMultiplication(GraphPtr graph, unsigned int layerIndex); |
| 76 | void ParsePermute(GraphPtr graph, unsigned int layerIndex); |
| 77 | void ParsePooling2d(GraphPtr graph, unsigned int layerIndex); |
| 78 | void ParseReshape(GraphPtr graph, unsigned int layerIndex); |
| 79 | void ParseSoftmax(GraphPtr graph, unsigned int layerIndex); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 80 | |
| 81 | void RegisterOutputSlotOfConnection(uint32_t connectionIndex, armnn::IOutputSlot* slot); |
| 82 | void RegisterInputSlotOfConnection(uint32_t connectionIndex, armnn::IInputSlot* slot); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 83 | void RegisterInputSlots(GraphPtr graph, uint32_t layerIndex, |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 84 | armnn::IConnectableLayer* layer); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 85 | void RegisterOutputSlots(GraphPtr graph, uint32_t layerIndex, |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 86 | armnn::IConnectableLayer* layer); |
| 87 | void ResetParser(); |
| 88 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 89 | void SetupInputLayers(GraphPtr graphPtr); |
| 90 | void SetupOutputLayers(GraphPtr graphPtr); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 91 | |
| 92 | /// The network we're building. Gets cleared after it is passed to the user |
| 93 | armnn::INetworkPtr m_Network; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 94 | std::vector<LayerParsingFunction> m_ParserFunctions; |
Saoirse Stewart | 3166c3e | 2019-02-18 15:24:53 +0000 | [diff] [blame] | 95 | std::string m_layerName; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 96 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 97 | using NameToBindingInfo = std::pair<std::string, BindingPointInfo >; |
| 98 | std::vector<NameToBindingInfo> m_InputBindings; |
| 99 | std::vector<NameToBindingInfo> m_OutputBindings; |
| 100 | |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 101 | /// 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 Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 115 | } //namespace armnnDeserializer |