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" |
Matthew Bentham | 268509a | 2019-02-25 13:58:24 +0000 | [diff] [blame] | 10 | #include <ArmnnSchema_generated.h> |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 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 *; |
Nina Drozd | 5772878 | 2019-02-27 10:53:27 +0000 | [diff] [blame] | 22 | using NormalizationDescriptorPtr = const armnnSerializer::NormalizationDescriptor *; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 23 | using TensorRawPtrVector = std::vector<TensorRawPtr>; |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 24 | using LayerRawPtr = const armnnSerializer::LayerBase *; |
| 25 | using LayerBaseRawPtr = const armnnSerializer::LayerBase *; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 26 | using LayerBaseRawPtrVector = std::vector<LayerBaseRawPtr>; |
| 27 | |
| 28 | public: |
| 29 | |
Derek Lamberti | 2b183fb | 2019-02-18 16:36:57 +0000 | [diff] [blame] | 30 | /// Create an input network from binary file contents |
| 31 | armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent) override; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 32 | |
Derek Lamberti | 2b183fb | 2019-02-18 16:36:57 +0000 | [diff] [blame] | 33 | /// Create an input network from a binary input stream |
| 34 | armnn::INetworkPtr CreateNetworkFromBinary(std::istream& binaryContent) override; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 35 | |
| 36 | /// 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] | 37 | BindingPointInfo GetNetworkInputBindingInfo(unsigned int layerId, const std::string& name) const override; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 38 | |
| 39 | /// 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] | 40 | BindingPointInfo GetNetworkOutputBindingInfo(unsigned int layerId, const std::string& name) const override; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 41 | |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 42 | Deserializer(); |
| 43 | ~Deserializer() {} |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 44 | |
| 45 | public: |
| 46 | // testable helpers |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 47 | static GraphPtr LoadGraphFromBinary(const uint8_t* binaryContent, size_t len); |
| 48 | static TensorRawPtrVector GetInputs(const GraphPtr& graph, unsigned int layerIndex); |
| 49 | static TensorRawPtrVector GetOutputs(const GraphPtr& graph, unsigned int layerIndex); |
| 50 | static LayerBaseRawPtrVector GetGraphInputs(const GraphPtr& graphPtr); |
| 51 | static LayerBaseRawPtrVector GetGraphOutputs(const GraphPtr& graphPtr); |
| 52 | static LayerBaseRawPtr GetBaseLayer(const GraphPtr& graphPtr, unsigned int layerIndex); |
| 53 | static int32_t GetBindingLayerInfo(const GraphPtr& graphPtr, unsigned int layerIndex); |
Éanna Ó Catháin | 633f859 | 2019-02-25 16:26:29 +0000 | [diff] [blame] | 54 | static std::string GetLayerName(const GraphPtr& graph, unsigned int index); |
Nina Drozd | 5772878 | 2019-02-27 10:53:27 +0000 | [diff] [blame] | 55 | static armnn::Pooling2dDescriptor GetPoolingDescriptor(PoolingDescriptor pooling2dDescriptor, |
| 56 | unsigned int layerIndex); |
| 57 | static armnn::NormalizationDescriptor GetNormalizationDescriptor( |
| 58 | NormalizationDescriptorPtr normalizationDescriptor, unsigned int layerIndex); |
Saoirse Stewart | 263829c | 2019-02-19 15:54:14 +0000 | [diff] [blame] | 59 | static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo, |
| 60 | const std::vector<uint32_t> & targetDimsIn); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | // No copying allowed until it is wanted and properly implemented |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 64 | Deserializer(const Deserializer&) = delete; |
| 65 | Deserializer& operator=(const Deserializer&) = delete; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 66 | |
| 67 | /// Create the network from an already loaded flatbuffers graph |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 68 | armnn::INetworkPtr CreateNetworkFromGraph(GraphPtr graph); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 69 | |
| 70 | // signature for the parser functions |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 71 | using LayerParsingFunction = void(Deserializer::*)(GraphPtr graph, unsigned int layerIndex); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 72 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 73 | void ParseUnsupportedLayer(GraphPtr graph, unsigned int layerIndex); |
| 74 | void ParseActivation(GraphPtr graph, unsigned int layerIndex); |
| 75 | void ParseAdd(GraphPtr graph, unsigned int layerIndex); |
Nattapat Chaimanowong | 6b4ed98 | 2019-02-26 17:24:13 +0000 | [diff] [blame] | 76 | void ParseBatchToSpaceNd(GraphPtr graph, unsigned int layerIndex); |
ruoyan01 | 8e7fa23 | 2019-02-28 15:09:07 +0000 | [diff] [blame] | 77 | void ParseBatchNormalization(GraphPtr graph, unsigned int layerIndex); |
Conor Kennedy | 7627788 | 2019-02-26 08:29:54 +0000 | [diff] [blame] | 78 | void ParseConstant(GraphPtr graph, unsigned int layerIndex); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 79 | void ParseConvolution2d(GraphPtr graph, unsigned int layerIndex); |
| 80 | void ParseDepthwiseConvolution2d(GraphPtr graph, unsigned int layerIndex); |
Éanna Ó Catháin | 5888589 | 2019-02-27 16:16:39 +0000 | [diff] [blame] | 81 | void ParseDivision(GraphPtr graph, unsigned int layerIndex); |
Nattapat Chaimanowong | 235cea5 | 2019-02-28 16:27:30 +0000 | [diff] [blame] | 82 | void ParseEqual(GraphPtr graph, unsigned int layerIndex); |
Finn Williams | dd2ba7e | 2019-03-01 11:51:52 +0000 | [diff] [blame] | 83 | void ParseFloor(GraphPtr graph, unsigned int layerIndex); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 84 | void ParseFullyConnected(GraphPtr graph, unsigned int layerIndex); |
Conor Kennedy | 79ffdf5 | 2019-03-01 14:24:54 +0000 | [diff] [blame] | 85 | void ParseGreater(GraphPtr graph, unsigned int layerIndex); |
Aron Virginas-Tar | 0fe3245 | 2019-02-28 13:12:47 +0000 | [diff] [blame] | 86 | void ParseMinimum(GraphPtr graph, unsigned int layerIndex); |
Aron Virginas-Tar | 377351e | 2019-02-27 14:42:31 +0000 | [diff] [blame] | 87 | void ParseMaximum(GraphPtr graph, unsigned int layerIndex); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 88 | void ParseMultiplication(GraphPtr graph, unsigned int layerIndex); |
Nina Drozd | 5772878 | 2019-02-27 10:53:27 +0000 | [diff] [blame] | 89 | void ParseNormalization(GraphPtr graph, unsigned int layerIndex); |
Nattapat Chaimanowong | ebb0f9c | 2019-03-01 12:14:06 +0000 | [diff] [blame] | 90 | void ParsePad(GraphPtr graph, unsigned int layerIndex); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 91 | void ParsePermute(GraphPtr graph, unsigned int layerIndex); |
| 92 | void ParsePooling2d(GraphPtr graph, unsigned int layerIndex); |
| 93 | void ParseReshape(GraphPtr graph, unsigned int layerIndex); |
Nattapat Chaimanowong | 6522cdc | 2019-03-01 16:14:13 +0000 | [diff] [blame] | 94 | void ParseResizeBilinear(GraphPtr graph, unsigned int layerIndex); |
Sadik Armagan | 8b42a38 | 2019-03-01 14:24:49 +0000 | [diff] [blame] | 95 | void ParseRsqrt(GraphPtr graph, unsigned int layerIndex); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 96 | void ParseSoftmax(GraphPtr graph, unsigned int layerIndex); |
Nattapat Chaimanowong | 4528699 | 2019-02-26 15:53:02 +0000 | [diff] [blame] | 97 | void ParseSpaceToBatchNd(GraphPtr graph, unsigned int layerIndex); |
Conor Kennedy | da1f975 | 2019-03-01 14:37:12 +0000 | [diff] [blame^] | 98 | void ParseSubtraction(GraphPtr graph, unsigned int layerIndex); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 99 | |
| 100 | void RegisterOutputSlotOfConnection(uint32_t connectionIndex, armnn::IOutputSlot* slot); |
| 101 | void RegisterInputSlotOfConnection(uint32_t connectionIndex, armnn::IInputSlot* slot); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 102 | void RegisterInputSlots(GraphPtr graph, uint32_t layerIndex, |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 103 | armnn::IConnectableLayer* layer); |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 104 | void RegisterOutputSlots(GraphPtr graph, uint32_t layerIndex, |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 105 | armnn::IConnectableLayer* layer); |
| 106 | void ResetParser(); |
| 107 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 108 | void SetupInputLayers(GraphPtr graphPtr); |
| 109 | void SetupOutputLayers(GraphPtr graphPtr); |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 110 | |
| 111 | /// The network we're building. Gets cleared after it is passed to the user |
| 112 | armnn::INetworkPtr m_Network; |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 113 | std::vector<LayerParsingFunction> m_ParserFunctions; |
| 114 | |
Derek Lamberti | 8ddae33 | 2019-02-21 16:29:43 +0000 | [diff] [blame] | 115 | using NameToBindingInfo = std::pair<std::string, BindingPointInfo >; |
| 116 | std::vector<NameToBindingInfo> m_InputBindings; |
| 117 | std::vector<NameToBindingInfo> m_OutputBindings; |
| 118 | |
Kevin May | 43a799c | 2019-02-08 16:31:42 +0000 | [diff] [blame] | 119 | /// A mapping of an output slot to each of the input slots it should be connected to |
| 120 | /// The outputSlot is from the layer that creates this tensor as one of its outputs |
| 121 | /// The inputSlots are from the layers that use this tensor as one of their inputs |
| 122 | struct Slots |
| 123 | { |
| 124 | armnn::IOutputSlot* outputSlot; |
| 125 | std::vector<armnn::IInputSlot*> inputSlots; |
| 126 | |
| 127 | Slots() : outputSlot(nullptr) { } |
| 128 | }; |
| 129 | typedef std::vector<Slots> Connection; |
| 130 | std::vector<Connection> m_GraphConnections; |
| 131 | }; |
| 132 | |
Derek Lamberti | 0028d1b | 2019-02-20 13:57:42 +0000 | [diff] [blame] | 133 | } //namespace armnnDeserializer |