telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | #pragma once |
| 6 | |
| 7 | #include "armnn/INetwork.hpp" |
| 8 | #include "armnnTfLiteParser/ITfLiteParser.hpp" |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 9 | #include "armnn/Types.hpp" |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 10 | |
| 11 | #include <schema_generated.h> |
| 12 | #include <functional> |
| 13 | #include <vector> |
| 14 | |
| 15 | namespace armnnTfLiteParser |
| 16 | { |
| 17 | |
| 18 | class TfLiteParser : public ITfLiteParser |
| 19 | { |
| 20 | public: |
| 21 | // Shorthands for TfLite types |
| 22 | using ModelPtr = std::unique_ptr<tflite::ModelT>; |
| 23 | using SubGraphPtr = std::unique_ptr<tflite::SubGraphT>; |
| 24 | using OperatorPtr = std::unique_ptr<tflite::OperatorT>; |
| 25 | using OperatorCodePtr = std::unique_ptr<tflite::OperatorCodeT>; |
| 26 | using TensorPtr = std::unique_ptr<tflite::TensorT>; |
| 27 | using TensorRawPtr = const tflite::TensorT *; |
| 28 | using TensorRawPtrVector = std::vector<TensorRawPtr>; |
| 29 | using TensorIdRawPtr = std::pair<size_t, TensorRawPtr>; |
| 30 | using TensorIdRawPtrVector = std::vector<TensorIdRawPtr>; |
| 31 | using BufferPtr = std::unique_ptr<tflite::BufferT>; |
| 32 | using BufferRawPtr = const tflite::BufferT *; |
| 33 | |
| 34 | public: |
| 35 | /// Create the network from a flatbuffers binary file on disk |
| 36 | virtual armnn::INetworkPtr CreateNetworkFromBinaryFile(const char* graphFile) override; |
| 37 | |
| 38 | /// Create the network from a flatbuffers binary |
| 39 | virtual armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent) override; |
| 40 | |
| 41 | |
| 42 | /// Retrieve binding info (layer id and tensor info) for the network input identified by |
| 43 | /// the given layer name and subgraph id |
| 44 | virtual BindingPointInfo GetNetworkInputBindingInfo(size_t subgraphId, |
| 45 | const std::string& name) const override; |
| 46 | |
| 47 | /// Retrieve binding info (layer id and tensor info) for the network output identified by |
| 48 | /// the given layer name and subgraph id |
| 49 | virtual BindingPointInfo GetNetworkOutputBindingInfo(size_t subgraphId, |
| 50 | const std::string& name) const override; |
| 51 | |
| 52 | /// Return the number of subgraphs in the parsed model |
| 53 | virtual size_t GetSubgraphCount() const override; |
| 54 | |
| 55 | /// Return the input tensor names for a given subgraph |
| 56 | virtual std::vector<std::string> GetSubgraphInputTensorNames(size_t subgraphId) const override; |
| 57 | |
| 58 | /// Return the output tensor names for a given subgraph |
| 59 | virtual std::vector<std::string> GetSubgraphOutputTensorNames(size_t subgraphId) const override; |
| 60 | |
| 61 | TfLiteParser(); |
| 62 | virtual ~TfLiteParser() {} |
| 63 | |
| 64 | public: |
| 65 | // testable helpers |
| 66 | static ModelPtr LoadModelFromFile(const char * fileName); |
| 67 | static ModelPtr LoadModelFromBinary(const uint8_t * binaryContent, size_t len); |
| 68 | static TensorRawPtrVector GetInputs(const ModelPtr & model, size_t subgraphIndex, size_t operatorIndex); |
| 69 | static TensorRawPtrVector GetOutputs(const ModelPtr & model, size_t subgraphIndex, size_t operatorIndex); |
| 70 | static TensorIdRawPtrVector GetSubgraphInputs(const ModelPtr & model, size_t subgraphIndex); |
| 71 | static TensorIdRawPtrVector GetSubgraphOutputs(const ModelPtr & model, size_t subgraphIndex); |
| 72 | static std::vector<int32_t>& GetInputTensorIds(const ModelPtr& model, size_t subgraphIndex, size_t operatorIndex); |
| 73 | static std::vector<int32_t>& GetOutputTensorIds(const ModelPtr& model, size_t subgraphIndex, size_t operatorIndex); |
| 74 | |
| 75 | static BufferRawPtr GetBuffer(const ModelPtr& model, size_t bufferIndex); |
| 76 | static armnn::TensorInfo OutputShapeOfSqueeze(const std::vector<uint32_t> & squeezeDims, |
| 77 | const armnn::TensorInfo & inputTensorInfo); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 78 | static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo, |
| 79 | const std::vector<int32_t> & targetDimsIn); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 80 | |
| 81 | private: |
| 82 | // No copying allowed until it is wanted and properly implemented |
| 83 | TfLiteParser(const TfLiteParser &) = delete; |
| 84 | TfLiteParser & operator=(const TfLiteParser &) = delete; |
| 85 | |
| 86 | /// Create the network from an already loaded flatbuffers model |
| 87 | armnn::INetworkPtr CreateNetworkFromModel(); |
| 88 | |
| 89 | // signature for the parser functions |
| 90 | using OperatorParsingFunction = void(TfLiteParser::*)(size_t subgraphIndex, size_t operatorIndex); |
| 91 | |
| 92 | void ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex); |
| 93 | void ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 94 | void ParseConcatenation(size_t subgraphIndex, size_t operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 95 | void ParseConv2D(size_t subgraphIndex, size_t operatorIndex); |
| 96 | void ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 97 | void ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 98 | void ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 99 | void ParseRelu(size_t subgraphIndex, size_t operatorIndex); |
| 100 | void ParseRelu6(size_t subgraphIndex, size_t operatorIndex); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 101 | void ParseReshape(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 102 | void ParseSoftmax(size_t subgraphIndex, size_t operatorIndex); |
| 103 | void ParseSqueeze(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 104 | void ParseAdd(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 105 | void ParseMul(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | 2235cee | 2018-12-19 12:51:45 -0200 | [diff] [blame] | 106 | void ParseMean(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | 6c2355b | 2018-12-19 12:52:01 -0200 | [diff] [blame] | 107 | void ParsePad(size_t subgraphIndex, size_t operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 108 | |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 109 | void ParsePool(size_t subgraphIndex, size_t operatorIndex, armnn::PoolingAlgorithm algorithm); |
| 110 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 111 | void RegisterProducerOfTensor(size_t subgraphIndex, size_t tensorIndex, armnn::IOutputSlot* slot); |
| 112 | void RegisterConsumerOfTensor(size_t subgraphIndex, size_t tensorIndex, armnn::IInputSlot* slot); |
| 113 | void RegisterInputSlots(size_t subgraphIndex, |
| 114 | size_t operatorIndex, |
| 115 | armnn::IConnectableLayer* layer, |
| 116 | const std::vector<unsigned int>& tensorIndexes); |
| 117 | void RegisterOutputSlots(size_t subgraphIndex, |
| 118 | size_t operatorIndex, |
| 119 | armnn::IConnectableLayer* layer, |
| 120 | const std::vector<unsigned int>& tensorIndexes); |
| 121 | |
| 122 | void SetupInputLayers(size_t subgraphIndex); |
| 123 | void SetupOutputLayers(size_t subgraphIndex); |
| 124 | |
| 125 | void ResetParser(); |
| 126 | |
| 127 | /// Attach an activation layer to the one passed as a parameter |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 128 | armnn::IConnectableLayer* AddFusedActivationLayer(armnn::IConnectableLayer* layer, |
| 129 | unsigned int outputSlot, |
| 130 | tflite::ActivationFunctionType activationType); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 131 | |
| 132 | // SupportedDataStorage's purpose is to hold data till we pass over to the network. |
| 133 | // We don't care about the content, and we want a single datatype to simplify the code. |
| 134 | struct SupportedDataStorage |
| 135 | { |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 136 | public: |
| 137 | // Convenience constructors |
| 138 | SupportedDataStorage(std::unique_ptr<float[]>&& data); |
| 139 | SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data); |
| 140 | SupportedDataStorage(std::unique_ptr<int32_t[]>&& data); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 141 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 142 | private: |
| 143 | // Pointers to the data buffers |
| 144 | std::unique_ptr<float[]> m_FloatData; |
| 145 | std::unique_ptr<uint8_t[]> m_Uint8Data; |
| 146 | std::unique_ptr<int32_t[]> m_Int32Data; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 147 | }; |
| 148 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 149 | |
| 150 | template<typename T> |
| 151 | std::pair<armnn::ConstTensor, TfLiteParser::SupportedDataStorage> |
| 152 | CreateConstTensorAndStoreData(TfLiteParser::BufferRawPtr bufferPtr, |
| 153 | TfLiteParser::TensorRawPtr tensorPtr, |
| 154 | armnn::TensorInfo& tensorInfo, |
| 155 | armnn::Optional<armnn::PermutationVector&> permutationVector); |
| 156 | |
| 157 | std::pair<armnn::ConstTensor, SupportedDataStorage> |
| 158 | CreateConstTensor(TensorRawPtr tensorPtr, |
| 159 | armnn::TensorInfo& tensorInfo, |
| 160 | armnn::Optional<armnn::PermutationVector&> permutationVector); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 161 | |
| 162 | /// The network we're building. Gets cleared after it is passed to the user |
| 163 | armnn::INetworkPtr m_Network; |
| 164 | std::vector<OperatorParsingFunction> m_ParserFunctions; |
| 165 | ModelPtr m_Model; |
| 166 | |
| 167 | /// A mapping of an output slot to each of the input slots it should be connected to |
| 168 | /// The outputSlot is from the layer that creates this tensor as one of its ouputs |
| 169 | /// The inputSlots are from the layers that use this tensor as one of their inputs |
| 170 | struct TensorSlots |
| 171 | { |
| 172 | armnn::IOutputSlot* outputSlot; |
| 173 | std::vector<armnn::IInputSlot*> inputSlots; |
| 174 | |
| 175 | TensorSlots() : outputSlot(nullptr) { } |
| 176 | }; |
| 177 | typedef std::vector<TensorSlots> TensorConnections; |
| 178 | /// Connections for tensors in each subgraph |
| 179 | /// The first index is the subgraph ID, the second index is the tensor ID |
| 180 | std::vector<TensorConnections> m_SubgraphConnections; |
| 181 | }; |
| 182 | |
| 183 | } |