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 ParseScatterNd(
size_t subgraphIndex,
size_t operatorIndex);
184 void ParseShape(
size_t subgraphIndex,
size_t operatorIndex);
185 void ParseSin(
size_t subgraphIndex,
size_t operatorIndex);
186 void ParseSlice(
size_t subgraphIndex,
size_t operatorIndex);
187 void ParseSoftmax(
size_t subgraphIndex,
size_t operatorIndex);
188 void ParseSqrt(
size_t subgraphIndex,
size_t operatorIndex);
189 void ParseSpaceToBatchND(
size_t subgraphIndex,
size_t operatorIndex);
190 void ParseSpaceToDepth(
size_t subgraphIndex,
size_t operatorIndex);
191 void ParseSplit(
size_t subgraphIndex,
size_t operatorIndex);
192 void ParseSplitV(
size_t subgraphIndex,
size_t operatorIndex);
193 void ParseSqueeze(
size_t subgraphIndex,
size_t operatorIndex);
194 void ParseSquare(
size_t subgraphIndex,
size_t operatorIndex);
195 void ParseSquaredDifference(
size_t subgraphIndex,
size_t operatorIndex);
196 void ParseStridedSlice(
size_t subgraphIndex,
size_t operatorIndex);
197 void ParseSub(
size_t subgraphIndex,
size_t operatorIndex);
198 void ParseSum(
size_t subgraphIndex,
size_t operatorIndex);
199 void ParseTanH(
size_t subgraphIndex,
size_t operatorIndex);
200 void ParseTile(
size_t subgraphIndex,
size_t operatorIndex);
201 void ParseTranspose(
size_t subgraphIndex,
size_t operatorIndex);
202 void ParseTransposeConv(
size_t subgraphIndex,
size_t operatorIndex);
203 void ParseUnidirectionalSequenceLSTM(
size_t subgraphIndex,
size_t operatorIndex);
204 void ParseUnpack(
size_t subgraphIndex,
size_t operatorIndex);
206 void RegisterProducerOfTensor(
size_t subgraphIndex,
size_t tensorIndex,
armnn::IOutputSlot* slot);
207 void RegisterConsumerOfTensor(
size_t subgraphIndex,
size_t tensorIndex,
armnn::IInputSlot* slot);
208 void RegisterInputSlots(
size_t subgraphIndex,
209 size_t operatorIndex,
211 const std::vector<unsigned int>& tensorIndexes,
212 unsigned int startingSlotIndex = 0);
213 void RegisterOutputSlots(
size_t subgraphIndex,
214 size_t operatorIndex,
216 const std::vector<unsigned int>& tensorIndexes);
218 void SetupInputLayerTensorInfos(
size_t subgraphIndex);
219 void SetupConstantLayerTensorInfos(
size_t subgraphIndex);
221 void SetupInputLayers(
size_t subgraphIndex);
222 void SetupOutputLayers(
size_t subgraphIndex);
223 void SetupConstantLayers(
size_t subgraphIndex);
227 void AddBroadcastReshapeLayer(
size_t subgraphIndex,
228 size_t operatorIndex,
233 unsigned int outputSlot,
234 std::string reshapeLayerName,
239 unsigned int outputSlot,
240 tflite::ActivationFunctionType activationType);
247 struct SupportedDataStorage
251 SupportedDataStorage(std::unique_ptr<
float[]>&& data);
252 SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data);
253 SupportedDataStorage(std::unique_ptr<int8_t[]>&& data);
254 SupportedDataStorage(std::unique_ptr<int32_t[]>&& data);
258 std::unique_ptr<float[]> m_FloatData;
259 std::unique_ptr<uint8_t[]> m_Uint8Data;
260 std::unique_ptr<int8_t[]> m_Int8Data;
261 std::unique_ptr<int32_t[]> m_Int32Data;
264 bool ShouldConstantTensorBeCreated(
unsigned int tensorIndex);
275 std::pair<armnn::ConstTensor, SupportedDataStorage>
280 std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
286 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
292 std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
297 size_t operatorIndex,
301 size_t operatorIndex,
304 std::vector<int> inputs);
307 size_t operatorIndex,
310 std::vector<armnn::TensorShape> inputShapes = {});
319 std::vector<OperatorParsingFunction> m_ParserFunctions;
320 std::unordered_map<std::string, OperatorParsingFunction> m_CustomParserFunctions;
328 std::vector<armnn::IInputSlot*> inputSlots;
330 TensorSlots() : outputSlot(nullptr) { }
332 typedef std::vector<TensorSlots> TensorConnections;
335 std::vector<TensorConnections> m_SubgraphConnections;
339 std::vector<std::vector<unsigned int>> m_OverriddenOutputShapes;
341 std::vector<unsigned int> m_ConstantsToDequantize;
342 std::vector<unsigned int> m_ConstantsToBeCreated;
343 std::map<size_t, armnn::TensorInfo> m_TensorInfos;