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> |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 13 | #include <unordered_map> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
| 16 | namespace armnnTfLiteParser |
| 17 | { |
| 18 | |
| 19 | class TfLiteParser : public ITfLiteParser |
| 20 | { |
| 21 | public: |
| 22 | // Shorthands for TfLite types |
| 23 | using ModelPtr = std::unique_ptr<tflite::ModelT>; |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 24 | using SubgraphPtr = std::unique_ptr<tflite::SubGraphT>; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 25 | using OperatorPtr = std::unique_ptr<tflite::OperatorT>; |
| 26 | using OperatorCodePtr = std::unique_ptr<tflite::OperatorCodeT>; |
| 27 | using TensorPtr = std::unique_ptr<tflite::TensorT>; |
| 28 | using TensorRawPtr = const tflite::TensorT *; |
| 29 | using TensorRawPtrVector = std::vector<TensorRawPtr>; |
| 30 | using TensorIdRawPtr = std::pair<size_t, TensorRawPtr>; |
| 31 | using TensorIdRawPtrVector = std::vector<TensorIdRawPtr>; |
| 32 | using BufferPtr = std::unique_ptr<tflite::BufferT>; |
| 33 | using BufferRawPtr = const tflite::BufferT *; |
| 34 | |
| 35 | public: |
| 36 | /// Create the network from a flatbuffers binary file on disk |
| 37 | virtual armnn::INetworkPtr CreateNetworkFromBinaryFile(const char* graphFile) override; |
| 38 | |
| 39 | /// Create the network from a flatbuffers binary |
| 40 | virtual armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent) override; |
| 41 | |
| 42 | |
| 43 | /// Retrieve binding info (layer id and tensor info) for the network input identified by |
| 44 | /// the given layer name and subgraph id |
| 45 | virtual BindingPointInfo GetNetworkInputBindingInfo(size_t subgraphId, |
| 46 | const std::string& name) const override; |
| 47 | |
| 48 | /// Retrieve binding info (layer id and tensor info) for the network output identified by |
| 49 | /// the given layer name and subgraph id |
| 50 | virtual BindingPointInfo GetNetworkOutputBindingInfo(size_t subgraphId, |
| 51 | const std::string& name) const override; |
| 52 | |
| 53 | /// Return the number of subgraphs in the parsed model |
| 54 | virtual size_t GetSubgraphCount() const override; |
| 55 | |
| 56 | /// Return the input tensor names for a given subgraph |
| 57 | virtual std::vector<std::string> GetSubgraphInputTensorNames(size_t subgraphId) const override; |
| 58 | |
| 59 | /// Return the output tensor names for a given subgraph |
| 60 | virtual std::vector<std::string> GetSubgraphOutputTensorNames(size_t subgraphId) const override; |
| 61 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 62 | TfLiteParser(const armnn::Optional<ITfLiteParser::TfLiteParserOptions>& options = armnn::EmptyOptional()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 63 | virtual ~TfLiteParser() {} |
| 64 | |
| 65 | public: |
| 66 | // testable helpers |
| 67 | static ModelPtr LoadModelFromFile(const char * fileName); |
| 68 | static ModelPtr LoadModelFromBinary(const uint8_t * binaryContent, size_t len); |
| 69 | static TensorRawPtrVector GetInputs(const ModelPtr & model, size_t subgraphIndex, size_t operatorIndex); |
| 70 | static TensorRawPtrVector GetOutputs(const ModelPtr & model, size_t subgraphIndex, size_t operatorIndex); |
| 71 | static TensorIdRawPtrVector GetSubgraphInputs(const ModelPtr & model, size_t subgraphIndex); |
| 72 | static TensorIdRawPtrVector GetSubgraphOutputs(const ModelPtr & model, size_t subgraphIndex); |
| 73 | static std::vector<int32_t>& GetInputTensorIds(const ModelPtr& model, size_t subgraphIndex, size_t operatorIndex); |
| 74 | static std::vector<int32_t>& GetOutputTensorIds(const ModelPtr& model, size_t subgraphIndex, size_t operatorIndex); |
| 75 | |
| 76 | static BufferRawPtr GetBuffer(const ModelPtr& model, size_t bufferIndex); |
| 77 | static armnn::TensorInfo OutputShapeOfSqueeze(const std::vector<uint32_t> & squeezeDims, |
| 78 | const armnn::TensorInfo & inputTensorInfo); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 79 | static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo, |
| 80 | const std::vector<int32_t> & targetDimsIn); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | // No copying allowed until it is wanted and properly implemented |
| 84 | TfLiteParser(const TfLiteParser &) = delete; |
| 85 | TfLiteParser & operator=(const TfLiteParser &) = delete; |
| 86 | |
| 87 | /// Create the network from an already loaded flatbuffers model |
| 88 | armnn::INetworkPtr CreateNetworkFromModel(); |
| 89 | |
| 90 | // signature for the parser functions |
| 91 | using OperatorParsingFunction = void(TfLiteParser::*)(size_t subgraphIndex, size_t operatorIndex); |
| 92 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 93 | void ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 94 | void ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex); |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 95 | |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 96 | void ParseActivation(size_t subgraphIndex, size_t operatorIndex, armnn::ActivationFunction activationType); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 97 | void ParseAdd(size_t subgraphIndex, size_t operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 98 | void ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | db947e2 | 2019-02-08 18:52:21 -0200 | [diff] [blame] | 99 | void ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 100 | void ParseConcatenation(size_t subgraphIndex, size_t operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 101 | void ParseConv2D(size_t subgraphIndex, size_t operatorIndex); |
| 102 | void ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex); |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 103 | void ParseDequantize(size_t subgraphIndex, size_t operatorIndex); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 104 | void ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 105 | void ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex); |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 106 | void ParseLogistic(size_t subgraphIndex, size_t operatorIndex); |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 107 | void ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 108 | void ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | b8d805e | 2019-02-12 22:57:13 -0200 | [diff] [blame] | 109 | void ParseMaximum(size_t subgraphIndex, size_t operatorIndex); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 110 | void ParseMean(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | 8f6d7a7 | 2019-02-12 22:58:18 -0200 | [diff] [blame] | 111 | void ParseMinimum(size_t subgraphIndex, size_t operatorIndex); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 112 | void ParseMul(size_t subgraphIndex, size_t operatorIndex); |
Matthew Jackson | bcca1f4 | 2019-07-16 11:39:21 +0100 | [diff] [blame] | 113 | void ParsePack(size_t subgraphIndex, size_t operatorIndex); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 114 | void ParsePad(size_t subgraphIndex, size_t operatorIndex); |
| 115 | void ParsePool(size_t subgraphIndex, size_t operatorIndex, armnn::PoolingAlgorithm algorithm); |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 116 | void ParseQuantize(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 117 | void ParseRelu(size_t subgraphIndex, size_t operatorIndex); |
| 118 | void ParseRelu6(size_t subgraphIndex, size_t operatorIndex); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 119 | void ParseReshape(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 120 | void ParseResize(size_t subgraphIndex, size_t operatorIndex, armnn::ResizeMethod resizeMethod); |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 121 | void ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 122 | void ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex); |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 123 | void ParseSlice(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 124 | void ParseSoftmax(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | baded14 | 2019-02-08 19:02:48 -0200 | [diff] [blame] | 125 | void ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 126 | void ParseSplit(size_t subgraphIndex, size_t operatorIndex); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 127 | void ParseSqueeze(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | 451d95b | 2019-02-12 22:59:22 -0200 | [diff] [blame] | 128 | void ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex); |
Bruno Goncalves | bbeae26 | 2019-02-07 18:37:39 -0200 | [diff] [blame] | 129 | void ParseSub(size_t subgraphIndex, size_t operatorIndex); |
Nina Drozd | 9985176 | 2019-04-09 09:37:38 +0100 | [diff] [blame] | 130 | void ParseTanH(size_t subgraphIndex, size_t operatorIndex); |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 131 | void ParseTranspose(size_t subgraphIndex, size_t operatorIndex); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 132 | void ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 133 | void ParseUnpack(size_t subgraphIndex, size_t operatorIndex); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 134 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 135 | void RegisterProducerOfTensor(size_t subgraphIndex, size_t tensorIndex, armnn::IOutputSlot* slot); |
| 136 | void RegisterConsumerOfTensor(size_t subgraphIndex, size_t tensorIndex, armnn::IInputSlot* slot); |
| 137 | void RegisterInputSlots(size_t subgraphIndex, |
| 138 | size_t operatorIndex, |
| 139 | armnn::IConnectableLayer* layer, |
| 140 | const std::vector<unsigned int>& tensorIndexes); |
| 141 | void RegisterOutputSlots(size_t subgraphIndex, |
| 142 | size_t operatorIndex, |
| 143 | armnn::IConnectableLayer* layer, |
| 144 | const std::vector<unsigned int>& tensorIndexes); |
| 145 | |
| 146 | void SetupInputLayers(size_t subgraphIndex); |
| 147 | void SetupOutputLayers(size_t subgraphIndex); |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 148 | void SetupConstantLayers(size_t subgraphIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 149 | |
| 150 | void ResetParser(); |
| 151 | |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 152 | void AddBroadcastReshapeLayer(size_t subgraphIndex, |
| 153 | size_t operatorIndex, |
| 154 | armnn::IConnectableLayer* layer); |
| 155 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 156 | /// Attach an activation layer to the one passed as a parameter |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 157 | armnn::IConnectableLayer* AddFusedActivationLayer(armnn::IConnectableLayer* layer, |
| 158 | unsigned int outputSlot, |
| 159 | tflite::ActivationFunctionType activationType); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 160 | |
| 161 | // SupportedDataStorage's purpose is to hold data till we pass over to the network. |
| 162 | // We don't care about the content, and we want a single datatype to simplify the code. |
| 163 | struct SupportedDataStorage |
| 164 | { |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 165 | public: |
| 166 | // Convenience constructors |
| 167 | SupportedDataStorage(std::unique_ptr<float[]>&& data); |
| 168 | SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data); |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 169 | SupportedDataStorage(std::unique_ptr<int8_t[]>&& data); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 170 | SupportedDataStorage(std::unique_ptr<int32_t[]>&& data); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 171 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 172 | private: |
| 173 | // Pointers to the data buffers |
| 174 | std::unique_ptr<float[]> m_FloatData; |
| 175 | std::unique_ptr<uint8_t[]> m_Uint8Data; |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 176 | std::unique_ptr<int8_t[]> m_Int8Data; |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 177 | std::unique_ptr<int32_t[]> m_Int32Data; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 178 | }; |
| 179 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 180 | |
| 181 | template<typename T> |
| 182 | std::pair<armnn::ConstTensor, TfLiteParser::SupportedDataStorage> |
| 183 | CreateConstTensorAndStoreData(TfLiteParser::BufferRawPtr bufferPtr, |
| 184 | TfLiteParser::TensorRawPtr tensorPtr, |
| 185 | armnn::TensorInfo& tensorInfo, |
| 186 | armnn::Optional<armnn::PermutationVector&> permutationVector); |
| 187 | |
| 188 | std::pair<armnn::ConstTensor, SupportedDataStorage> |
| 189 | CreateConstTensor(TensorRawPtr tensorPtr, |
| 190 | armnn::TensorInfo& tensorInfo, |
| 191 | armnn::Optional<armnn::PermutationVector&> permutationVector); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 192 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 193 | // Settings for configuring the TfLiteParser |
| 194 | armnn::Optional<ITfLiteParser::TfLiteParserOptions> m_Options; |
| 195 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 196 | /// The network we're building. Gets cleared after it is passed to the user |
| 197 | armnn::INetworkPtr m_Network; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 198 | ModelPtr m_Model; |
| 199 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 200 | std::vector<OperatorParsingFunction> m_ParserFunctions; |
| 201 | std::unordered_map<std::string, OperatorParsingFunction> m_CustomParserFunctions; |
| 202 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 203 | /// A mapping of an output slot to each of the input slots it should be connected to |
| 204 | /// The outputSlot is from the layer that creates this tensor as one of its ouputs |
| 205 | /// The inputSlots are from the layers that use this tensor as one of their inputs |
| 206 | struct TensorSlots |
| 207 | { |
| 208 | armnn::IOutputSlot* outputSlot; |
| 209 | std::vector<armnn::IInputSlot*> inputSlots; |
| 210 | |
| 211 | TensorSlots() : outputSlot(nullptr) { } |
| 212 | }; |
| 213 | typedef std::vector<TensorSlots> TensorConnections; |
| 214 | /// Connections for tensors in each subgraph |
| 215 | /// The first index is the subgraph ID, the second index is the tensor ID |
| 216 | std::vector<TensorConnections> m_SubgraphConnections; |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 217 | |
| 218 | /// This is used in case that the model does not speciry the output. |
| 219 | /// The shape can be calculated from the options. |
| 220 | std::vector<std::vector<unsigned int>> m_OverridenOutputShapes; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | } |