12 #include <schema_generated.h>
14 #include <unordered_map>
17 #include <tensorflow/lite/version.h>
19 #if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 3)
20 #define ARMNN_POST_TFLITE_2_3
30 using ModelPtr = std::unique_ptr<tflite::ModelT>;
53 const std::string& name)
const;
58 const std::string& name)
const;
91 const std::vector<int32_t>& targetDimsIn);
106 using OperatorParsingFunction = void(
TfLiteParserImpl::*)(
size_t subgraphIndex,
size_t operatorIndex);
108 void ParseCustomOperator(
size_t subgraphIndex,
size_t operatorIndex);
109 void ParseUnsupportedOperator(
size_t subgraphIndex,
size_t operatorIndex);
111 void ParseAbs(
size_t subgraphIndex,
size_t operatorIndex);
113 void ParseAdd(
size_t subgraphIndex,
size_t operatorIndex);
115 void ParseArgMin(
size_t subgraphIndex,
size_t operatorIndex);
116 void ParseArgMax(
size_t subgraphIndex,
size_t operatorIndex);
117 void ParseAveragePool2D(
size_t subgraphIndex,
size_t operatorIndex);
118 void ParseBatchMatMul(
size_t subgraphIndex,
size_t operatorIndex);
119 void ParseBatchToSpaceND(
size_t subgraphIndex,
size_t operatorIndex);
120 void ParseBroadcastTo(
size_t subgraphIndex,
size_t operatorIndex);
121 void ParseCast(
size_t subgraphIndex,
size_t operatorIndex);
122 void ParseCeil(
size_t subgraphIndex,
size_t operatorIndex);
124 void ParseConcatenation(
size_t subgraphIndex,
size_t operatorIndex);
125 void ParseConv2D(
size_t subgraphIndex,
size_t operatorIndex);
127 #if defined(ARMNN_POST_TFLITE_2_4)
128 void ParseConv3D(
size_t subgraphIndex,
size_t operatorIndex);
130 void ParseDepthToSpace(
size_t subgraphIndex,
size_t operatorIndex);
131 void ParseDepthwiseConv2D(
size_t subgraphIndex,
size_t operatorIndex);
132 void ParseDequantize(
size_t subgraphIndex,
size_t operatorIndex);
133 void ParseDetectionPostProcess(
size_t subgraphIndex,
size_t operatorIndex);
134 void ParseDiv(
size_t subgraphIndex,
size_t operatorIndex);
135 void ParseElementwiseUnary(
size_t subgraphIndex,
size_t operatorIndex,
armnn::UnaryOperation unaryOperation);
136 void ParseElu(
size_t subgraphIndex,
size_t operatorIndex);
137 void ParseEqual(
size_t subgraphIndex,
size_t operatorIndex);
138 void ParseExp(
size_t subgraphIndex,
size_t operatorIndex);
139 void ParseExpandDims(
size_t subgraphIndex,
size_t operatorIndex);
140 void ParseFloorDiv(
size_t subgraphIndex,
size_t operatorIndex);
141 void ParseFullyConnected(
size_t subgraphIndex,
size_t operatorIndex);
142 void ParseGather(
size_t subgraphIndex,
size_t operatorIndex);
143 void ParseGatherNd(
size_t subgraphIndex,
size_t operatorIndex);
144 void ParseGelu(
size_t subgraphIndex,
size_t operatorIndex);
145 void ParseGreater(
size_t subgraphIndex,
size_t operatorIndex);
146 void ParseGreaterOrEqual(
size_t subgraphIndex,
size_t operatorIndex);
147 void ParseHardSwish(
size_t subgraphIndex,
size_t operatorIndex);
148 void ParseLeakyRelu(
size_t subgraphIndex,
size_t operatorIndex);
149 void ParseLess(
size_t subgraphIndex,
size_t operatorIndex);
150 void ParseLessOrEqual(
size_t subgraphIndex,
size_t operatorIndex);
151 void ParseLog(
size_t subgraphIndex,
size_t operatorIndex);
152 void ParseLocalResponseNormalization(
size_t subgraphIndex,
size_t operatorIndex);
153 void ParseLogicalNot(
size_t subgraphIndex,
size_t operatorIndex);
154 void ParseLogistic(
size_t subgraphIndex,
size_t operatorIndex);
155 void ParseLogSoftmax(
size_t subgraphIndex,
size_t operatorIndex);
156 void ParseL2Normalization(
size_t subgraphIndex,
size_t operatorIndex);
157 void ParseMaxPool2D(
size_t subgraphIndex,
size_t operatorIndex);
158 void ParseMaximum(
size_t subgraphIndex,
size_t operatorIndex);
159 void ParseMean(
size_t subgraphIndex,
size_t operatorIndex);
160 void ParseMinimum(
size_t subgraphIndex,
size_t operatorIndex);
161 void ParseMirrorPad(
size_t subgraphIndex,
size_t operatorIndex);
162 void ParseMul(
size_t subgraphIndex,
size_t operatorIndex);
163 void ParseNeg(
size_t subgraphIndex,
size_t operatorIndex);
164 void ParseNotEqual(
size_t subgraphIndex,
size_t operatorIndex);
165 void ParsePack(
size_t subgraphIndex,
size_t operatorIndex);
166 void ParsePad(
size_t subgraphIndex,
size_t operatorIndex);
168 void ParsePower(
size_t subgraphIndex,
size_t operatorIndex);
169 void ParsePrelu(
size_t subgraphIndex,
size_t operatorIndex);
170 void ParseQuantize(
size_t subgraphIndex,
size_t operatorIndex);
172 void ParseReduceMax(
size_t subgraphIndex,
size_t operatorIndex);
173 void ParseReduceMin(
size_t subgraphIndex,
size_t operatorIndex);
174 void ParseReduceProd(
size_t subgraphIndex,
size_t operatorIndex);
175 void ParseRelu(
size_t subgraphIndex,
size_t operatorIndex);
176 void ParseRelu6(
size_t subgraphIndex,
size_t operatorIndex);
177 void ParseReshape(
size_t subgraphIndex,
size_t operatorIndex);
178 void ParseResize(
size_t subgraphIndex,
size_t operatorIndex,
armnn::ResizeMethod resizeMethod);
179 void ParseResizeBilinear(
size_t subgraphIndex,
size_t operatorIndex);
180 void ParseResizeNearestNeighbor(
size_t subgraphIndex,
size_t operatorIndex);
181 void ParseReverseV2(
size_t subgraphIndex,
size_t operatorIndex);
182 void ParseRsqrt(
size_t subgraphIndex,
size_t operatorIndex);
183 void ParseShape(
size_t subgraphIndex,
size_t operatorIndex);
184 void ParseSin(
size_t subgraphIndex,
size_t operatorIndex);
185 void ParseSlice(
size_t subgraphIndex,
size_t operatorIndex);
186 void ParseSoftmax(
size_t subgraphIndex,
size_t operatorIndex);
187 void ParseSqrt(
size_t subgraphIndex,
size_t operatorIndex);
188 void ParseSpaceToBatchND(
size_t subgraphIndex,
size_t operatorIndex);
189 void ParseSpaceToDepth(
size_t subgraphIndex,
size_t operatorIndex);
190 void ParseSplit(
size_t subgraphIndex,
size_t operatorIndex);
191 void ParseSplitV(
size_t subgraphIndex,
size_t operatorIndex);
192 void ParseSqueeze(
size_t subgraphIndex,
size_t operatorIndex);
193 void ParseSquare(
size_t subgraphIndex,
size_t operatorIndex);
194 void ParseSquaredDifference(
size_t subgraphIndex,
size_t operatorIndex);
195 void ParseStridedSlice(
size_t subgraphIndex,
size_t operatorIndex);
196 void ParseSub(
size_t subgraphIndex,
size_t operatorIndex);
197 void ParseSum(
size_t subgraphIndex,
size_t operatorIndex);
198 void ParseTanH(
size_t subgraphIndex,
size_t operatorIndex);
199 void ParseTile(
size_t subgraphIndex,
size_t operatorIndex);
200 void ParseTranspose(
size_t subgraphIndex,
size_t operatorIndex);
201 void ParseTransposeConv(
size_t subgraphIndex,
size_t operatorIndex);
202 void ParseUnidirectionalSequenceLSTM(
size_t subgraphIndex,
size_t operatorIndex);
203 void ParseUnpack(
size_t subgraphIndex,
size_t operatorIndex);
205 void RegisterProducerOfTensor(
size_t subgraphIndex,
size_t tensorIndex,
armnn::IOutputSlot* slot);
206 void RegisterConsumerOfTensor(
size_t subgraphIndex,
size_t tensorIndex,
armnn::IInputSlot* slot);
207 void RegisterInputSlots(
size_t subgraphIndex,
208 size_t operatorIndex,
210 const std::vector<unsigned int>& tensorIndexes,
211 unsigned int startingSlotIndex = 0);
212 void RegisterOutputSlots(
size_t subgraphIndex,
213 size_t operatorIndex,
215 const std::vector<unsigned int>& tensorIndexes);
217 void SetupInputLayerTensorInfos(
size_t subgraphIndex);
218 void SetupConstantLayerTensorInfos(
size_t subgraphIndex);
220 void SetupInputLayers(
size_t subgraphIndex);
221 void SetupOutputLayers(
size_t subgraphIndex);
222 void SetupConstantLayers(
size_t subgraphIndex);
226 void AddBroadcastReshapeLayer(
size_t subgraphIndex,
227 size_t operatorIndex,
232 unsigned int outputSlot,
233 std::string reshapeLayerName,
238 unsigned int outputSlot,
239 tflite::ActivationFunctionType activationType);
246 struct SupportedDataStorage
250 SupportedDataStorage(std::unique_ptr<
float[]>&& data);
251 SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data);
252 SupportedDataStorage(std::unique_ptr<int8_t[]>&& data);
253 SupportedDataStorage(std::unique_ptr<int32_t[]>&& data);
257 std::unique_ptr<float[]> m_FloatData;
258 std::unique_ptr<uint8_t[]> m_Uint8Data;
259 std::unique_ptr<int8_t[]> m_Int8Data;
260 std::unique_ptr<int32_t[]> m_Int32Data;
263 bool ShouldConstantTensorBeCreated(
unsigned int tensorIndex);
274 std::pair<armnn::ConstTensor, SupportedDataStorage>
279 std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
285 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
291 std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
296 size_t operatorIndex,
300 size_t operatorIndex,
303 std::vector<int> inputs);
306 size_t operatorIndex,
309 std::vector<armnn::TensorShape> inputShapes = {});
318 std::vector<OperatorParsingFunction> m_ParserFunctions;
319 std::unordered_map<std::string, OperatorParsingFunction> m_CustomParserFunctions;
327 std::vector<armnn::IInputSlot*> inputSlots;
329 TensorSlots() : outputSlot(nullptr) { }
331 typedef std::vector<TensorSlots> TensorConnections;
334 std::vector<TensorConnections> m_SubgraphConnections;
338 std::vector<std::vector<unsigned int>> m_OverriddenOutputShapes;
340 std::vector<unsigned int> m_ConstantsToDequantize;
341 std::vector<unsigned int> m_ConstantsToBeCreated;
342 std::map<size_t, armnn::TensorInfo> m_TensorInfos;