ArmNN
 21.08
TfLiteParser.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #pragma once
6 
7 #include "armnn/INetwork.hpp"
9 #include "armnn/Types.hpp"
10 
11 #include <schema_generated.h>
12 #include <functional>
13 #include <unordered_map>
14 #include <vector>
15 
16 namespace armnnTfLiteParser
17 {
18 
20 {
21 public:
22  // Shorthands for TfLite types
23  using ModelPtr = std::unique_ptr<tflite::ModelT>;
24  using SubgraphPtr = std::unique_ptr<tflite::SubGraphT>;
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  armnn::INetworkPtr CreateNetworkFromBinaryFile(const char* graphFile);
38 
39  /// Create the network from a flatbuffers binary
40  armnn::INetworkPtr CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent);
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
46  const std::string& name) const;
47 
48  /// Retrieve binding info (layer id and tensor info) for the network output identified by
49  /// the given layer name and subgraph id
51  const std::string& name) const;
52 
53  /// Return the number of subgraphs in the parsed model
54  size_t GetSubgraphCount() const;
55 
56  /// Return the input tensor names for a given subgraph
57  std::vector<std::string> GetSubgraphInputTensorNames(size_t subgraphId) const;
58 
59  /// Return the output tensor names for a given subgraph
60  std::vector<std::string> GetSubgraphOutputTensorNames(size_t subgraphId) const;
61 
63  ~TfLiteParserImpl() = default;
64 
65 public:
66  // testable helpers
67  armnn::INetworkPtr CreateNetworkFromBinaryAsDynamic(const std::vector<uint8_t>& binaryContent);
68 
69  armnn::INetworkPtr LoadModel(std::unique_ptr<tflite::ModelT> model);
70 
71  static ModelPtr LoadModelFromFile(const char* fileName);
72  static ModelPtr LoadModelFromBinary(const uint8_t* binaryContent, size_t len);
73  static TensorRawPtrVector GetInputs(const ModelPtr& model, size_t subgraphIndex, size_t operatorIndex);
74  static TensorRawPtrVector GetOutputs(const ModelPtr& model, size_t subgraphIndex, size_t operatorIndex);
75  static TensorIdRawPtrVector GetSubgraphInputs(const ModelPtr& model, size_t subgraphIndex);
76  static TensorIdRawPtrVector GetSubgraphOutputs(const ModelPtr& model, size_t subgraphIndex);
77  static std::vector<int32_t>& GetInputTensorIds(const ModelPtr& model, size_t subgraphIndex, size_t operatorIndex);
78  static std::vector<int32_t>& GetOutputTensorIds(const ModelPtr& model, size_t subgraphIndex, size_t operatorIndex);
79 
80  static BufferRawPtr GetBuffer(const ModelPtr& model, size_t bufferIndex);
81  static armnn::TensorInfo OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims,
82  const armnn::TensorInfo& inputTensorInfo);
83  static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo,
84  const std::vector<int32_t>& targetDimsIn);
85 
86  /// Retrieve version in X.Y.Z form
87  static const std::string GetVersion();
88 
89 private:
90 
91  // No copying allowed until it is wanted and properly implemented
92  TfLiteParserImpl(const TfLiteParserImpl &) = delete;
93  TfLiteParserImpl & operator=(const TfLiteParserImpl &) = delete;
94 
95  /// Create the network from an already loaded flatbuffers model
96  armnn::INetworkPtr CreateNetworkFromModel();
97 
98  // signature for the parser functions
99  using OperatorParsingFunction = void(TfLiteParserImpl::*)(size_t subgraphIndex, size_t operatorIndex);
100 
101  void ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex);
102  void ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex);
103 
104  void ParseAbs(size_t subgraphIndex, size_t operatorIndex);
105  void ParseActivation(size_t subgraphIndex, size_t operatorIndex, armnn::ActivationFunction activationType);
106  void ParseAdd(size_t subgraphIndex, size_t operatorIndex);
107  void ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, armnn::ArgMinMaxFunction argMinMaxFunction);
108  void ParseArgMin(size_t subgraphIndex, size_t operatorIndex);
109  void ParseArgMax(size_t subgraphIndex, size_t operatorIndex);
110  void ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex);
111  void ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex);
112  void ParseCast(size_t subgraphIndex, size_t operatorIndex);
113  void ParseComparison(size_t subgraphIndex, size_t operatorIndex, armnn::ComparisonOperation comparisonOperation);
114  void ParseConcatenation(size_t subgraphIndex, size_t operatorIndex);
115  void ParseConv2D(size_t subgraphIndex, size_t operatorIndex);
116  void ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex);
117  void ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex);
118  void ParseDequantize(size_t subgraphIndex, size_t operatorIndex);
119  void ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex);
120  void ParseDiv(size_t subgraphIndex, size_t operatorIndex);
121  void ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, armnn::UnaryOperation unaryOperation);
122  void ParseElu(size_t subgraphIndex, size_t operatorIndex);
123  void ParseEqual(size_t subgraphIndex, size_t operatorIndex);
124  void ParseExp(size_t subgraphIndex, size_t operatorIndex);
125  void ParseExpandDims(size_t subgraphIndex, size_t operatorIndex);
126  void ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex);
127  void ParseGather(size_t subgraphIndex, size_t operatorIndex);
128  void ParseGreater(size_t subgraphIndex, size_t operatorIndex);
129  void ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex);
130  void ParseHardSwish(size_t subgraphIndex, size_t operatorIndex);
131  void ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex);
132  void ParseLess(size_t subgraphIndex, size_t operatorIndex);
133  void ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex);
134  void ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex);
135  void ParseLogistic(size_t subgraphIndex, size_t operatorIndex);
136  void ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex);
137  void ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex);
138  void ParseMaximum(size_t subgraphIndex, size_t operatorIndex);
139  void ParseMean(size_t subgraphIndex, size_t operatorIndex);
140  void ParseMinimum(size_t subgraphIndex, size_t operatorIndex);
141  void ParseMul(size_t subgraphIndex, size_t operatorIndex);
142  void ParseNeg(size_t subgraphIndex, size_t operatorIndex);
143  void ParseNotEqual(size_t subgraphIndex, size_t operatorIndex);
144  void ParsePack(size_t subgraphIndex, size_t operatorIndex);
145  void ParsePad(size_t subgraphIndex, size_t operatorIndex);
146  void ParsePool(size_t subgraphIndex, size_t operatorIndex, armnn::PoolingAlgorithm algorithm);
147  void ParsePrelu(size_t subgraphIndex, size_t operatorIndex);
148  void ParseQuantize(size_t subgraphIndex, size_t operatorIndex);
149  void ParseReduce(size_t subgraphIndex, size_t operatorIndex, armnn::ReduceOperation reduceOperation);
150  void ParseReduceMax(size_t subgraphIndex, size_t operatorIndex);
151  void ParseReduceMin(size_t subgraphIndex, size_t operatorIndex);
152  void ParseRelu(size_t subgraphIndex, size_t operatorIndex);
153  void ParseRelu6(size_t subgraphIndex, size_t operatorIndex);
154  void ParseReshape(size_t subgraphIndex, size_t operatorIndex);
155  void ParseResize(size_t subgraphIndex, size_t operatorIndex, armnn::ResizeMethod resizeMethod);
156  void ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex);
157  void ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex);
158  void ParseRsqrt(size_t subgraphIndex, size_t operatorIndex);
159  void ParseShape(size_t subgraphIndex, size_t operatorIndex);
160  void ParseSlice(size_t subgraphIndex, size_t operatorIndex);
161  void ParseSoftmax(size_t subgraphIndex, size_t operatorIndex);
162  void ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex);
163  void ParseSplit(size_t subgraphIndex, size_t operatorIndex);
164  void ParseSplitV(size_t subgraphIndex, size_t operatorIndex);
165  void ParseSqueeze(size_t subgraphIndex, size_t operatorIndex);
166  void ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex);
167  void ParseSub(size_t subgraphIndex, size_t operatorIndex);
168  void ParseSum(size_t subgraphIndex, size_t operatorIndex);
169  void ParseTanH(size_t subgraphIndex, size_t operatorIndex);
170  void ParseTranspose(size_t subgraphIndex, size_t operatorIndex);
171  void ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex);
172  void ParseUnpack(size_t subgraphIndex, size_t operatorIndex);
173 
174  void RegisterProducerOfTensor(size_t subgraphIndex, size_t tensorIndex, armnn::IOutputSlot* slot);
175  void RegisterConsumerOfTensor(size_t subgraphIndex, size_t tensorIndex, armnn::IInputSlot* slot);
176  void RegisterInputSlots(size_t subgraphIndex,
177  size_t operatorIndex,
179  const std::vector<unsigned int>& tensorIndexes,
180  unsigned int startingSlotIndex = 0);
181  void RegisterOutputSlots(size_t subgraphIndex,
182  size_t operatorIndex,
184  const std::vector<unsigned int>& tensorIndexes);
185 
186  void SetupInputLayers(size_t subgraphIndex);
187  void SetupOutputLayers(size_t subgraphIndex);
188  void SetupConstantLayers(size_t subgraphIndex);
189 
190  void ResetParser();
191 
192  void AddBroadcastReshapeLayer(size_t subgraphIndex,
193  size_t operatorIndex,
194  armnn::IConnectableLayer* layer);
195 
196  /// Attach an activation layer to the one passed as a parameter
197  armnn::IConnectableLayer* AddFusedActivationLayer(armnn::IConnectableLayer* layer,
198  unsigned int outputSlot,
199  tflite::ActivationFunctionType activationType);
200 
201  // SupportedDataStorage's purpose is to hold data till we pass over to the network.
202  // We don't care about the content, and we want a single datatype to simplify the code.
203  struct SupportedDataStorage
204  {
205  public:
206  // Convenience constructors
207  SupportedDataStorage(std::unique_ptr<float[]>&& data);
208  SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data);
209  SupportedDataStorage(std::unique_ptr<int8_t[]>&& data);
210  SupportedDataStorage(std::unique_ptr<int32_t[]>&& data);
211 
212  private:
213  // Pointers to the data buffers
214  std::unique_ptr<float[]> m_FloatData;
215  std::unique_ptr<uint8_t[]> m_Uint8Data;
216  std::unique_ptr<int8_t[]> m_Int8Data;
217  std::unique_ptr<int32_t[]> m_Int32Data;
218  };
219 
220  bool IsConstTensor(TensorRawPtr tensorPtr);
221  armnn::ConstTensor CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
222  armnn::TensorInfo& tensorInfo);
223  std::pair<armnn::ConstTensor, SupportedDataStorage>
224  CreateConstTensorPermuted(TensorRawPtr tensorPtr,
225  armnn::TensorInfo& tensorInfo,
227 
228  template<typename T>
229  std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
230  CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
232  armnn::TensorInfo& tensorInfo,
234 
235  // Settings for configuring the TfLiteParser
237 
238  /// The network we're building. Gets cleared after it is passed to the user
239  armnn::INetworkPtr m_Network;
240  ModelPtr m_Model;
241 
242  std::vector<OperatorParsingFunction> m_ParserFunctions;
243  std::unordered_map<std::string, OperatorParsingFunction> m_CustomParserFunctions;
244 
245  /// A mapping of an output slot to each of the input slots it should be connected to
246  /// The outputSlot is from the layer that creates this tensor as one of its ouputs
247  /// The inputSlots are from the layers that use this tensor as one of their inputs
248  struct TensorSlots
249  {
250  armnn::IOutputSlot* outputSlot;
251  std::vector<armnn::IInputSlot*> inputSlots;
252 
253  TensorSlots() : outputSlot(nullptr) { }
254  };
255  typedef std::vector<TensorSlots> TensorConnections;
256  /// Connections for tensors in each subgraph
257  /// The first index is the subgraph ID, the second index is the tensor ID
258  std::vector<TensorConnections> m_SubgraphConnections;
259 
260  /// This is used in case that the model does not speciry the output.
261  /// The shape can be calculated from the options.
262  std::vector<std::vector<unsigned int>> m_OverridenOutputShapes;
263 };
264 
265 }
std::unique_ptr< tflite::TensorT > TensorPtr
std::unique_ptr< tflite::ModelT > ModelPtr
static TensorIdRawPtrVector GetSubgraphOutputs(const ModelPtr &model, size_t subgraphIndex)
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
const tflite::TensorT * TensorRawPtr
const tflite::BufferT * BufferRawPtr
BindingPointInfo GetNetworkOutputBindingInfo(size_t subgraphId, const std::string &name) const
Retrieve binding info (layer id and tensor info) for the network output identified by the given layer...
std::vector< std::string > GetSubgraphOutputTensorNames(size_t subgraphId) const
Return the output tensor names for a given subgraph.
std::unique_ptr< tflite::OperatorT > OperatorPtr
static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo &inputTensorInfo, const std::vector< int32_t > &targetDimsIn)
PoolingAlgorithm
Definition: Types.hpp:116
TfLiteParserImpl(const armnn::Optional< ITfLiteParser::TfLiteParserOptions > &options=armnn::EmptyOptional())
std::unique_ptr< tflite::BufferT > BufferPtr
armnn::INetworkPtr CreateNetworkFromBinary(const std::vector< uint8_t > &binaryContent)
Create the network from a flatbuffers binary.
static BufferRawPtr GetBuffer(const ModelPtr &model, size_t bufferIndex)
armnn::INetworkPtr CreateNetworkFromBinaryFile(const char *graphFile)
Create the network from a flatbuffers binary file on disk.
std::unique_ptr< tflite::OperatorCodeT > OperatorCodePtr
ComparisonOperation
Definition: Types.hpp:88
ReduceOperation
Definition: Types.hpp:123
BindingPointInfo GetNetworkInputBindingInfo(size_t subgraphId, const std::string &name) const
Retrieve binding info (layer id and tensor info) for the network input identified by the given layer ...
static ModelPtr LoadModelFromBinary(const uint8_t *binaryContent, size_t len)
std::vector< TensorIdRawPtr > TensorIdRawPtrVector
static std::vector< int32_t > & GetInputTensorIds(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
An output connection slot for a layer.
Definition: INetwork.hpp:37
static const std::string GetVersion()
Retrieve version in X.Y.Z form.
static ModelPtr LoadModelFromFile(const char *fileName)
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
std::vector< TensorRawPtr > TensorRawPtrVector
size_t GetSubgraphCount() const
Return the number of subgraphs in the parsed model.
armnn::INetworkPtr LoadModel(std::unique_ptr< tflite::ModelT > model)
std::pair< size_t, TensorRawPtr > TensorIdRawPtr
std::unique_ptr< tflite::SubGraphT > SubgraphPtr
static TensorIdRawPtrVector GetSubgraphInputs(const ModelPtr &model, size_t subgraphIndex)
static TensorRawPtrVector GetInputs(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
static TensorRawPtrVector GetOutputs(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32
static std::vector< int32_t > & GetOutputTensorIds(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
ArgMinMaxFunction
Definition: Types.hpp:82
ResizeMethod
Definition: Types.hpp:131
UnaryOperation
Definition: Types.hpp:104
armnn::BindingPointInfo BindingPointInfo
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:172
armnn::INetworkPtr CreateNetworkFromBinaryAsDynamic(const std::vector< uint8_t > &binaryContent)
static armnn::TensorInfo OutputShapeOfSqueeze(std::vector< uint32_t > squeezeDims, const armnn::TensorInfo &inputTensorInfo)
std::vector< std::string > GetSubgraphInputTensorNames(size_t subgraphId) const
Return the input tensor names for a given subgraph.
An input connection slot for a layer.
Definition: INetwork.hpp:24
ActivationFunction
Definition: Types.hpp:66