1/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#ifndef TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_
16#define TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_
17
18// These functions transform codes and data structures that are defined in the
19// flatbuffer serialization format into in-memory values that are used by the
20// runtime API and interpreter.
21
22#include <cstddef>
23#include <new>
24#include <type_traits>
25
26#include "tensorflow/lite/c/common.h"
27#include "tensorflow/lite/core/api/error_reporter.h"
28#include "tensorflow/lite/schema/schema_generated.h"
29
30namespace tflite {
31
32// Interface class for builtin data allocations.
33class BuiltinDataAllocator {
34 public:
35 virtual void* Allocate(size_t size, size_t alignment_hint) = 0;
36 virtual void Deallocate(void* data) = 0;
37
38 // Allocate a structure, but make sure it is a POD structure that doesn't
39 // require constructors to run. The reason we do this, is that Interpreter's C
40 // extension part will take ownership so destructors will not be run during
41 // deallocation.
42 template <typename T>
43 T* AllocatePOD() {
44 // TODO(b/154346074): Change this to is_trivially_destructible when all
45 // platform targets support that properly.
46 static_assert(std::is_pod<T>::value, "Builtin data structure must be POD.");
47 void* allocated_memory = this->Allocate(sizeof(T), alignof(T));
48 return new (allocated_memory) T();
49 }
50
51 virtual ~BuiltinDataAllocator() {}
52};
53
54// Parse the appropriate data out of the op.
55//
56// This handles builtin data explicitly as there are flatbuffer schemas.
57// If it returns kTfLiteOk, it passes the data out with `builtin_data`. The
58// calling function has to pass in an allocator object, and this allocator
59// will be called to reserve space for the output data. If the calling
60// function's allocator reserves memory on the heap, then it's the calling
61// function's responsibility to free it.
62// If it returns kTfLiteError, `builtin_data` will be `nullptr`.
63TfLiteStatus ParseOpData(const Operator* op, BuiltinOperator op_type,
64 ErrorReporter* error_reporter,
65 BuiltinDataAllocator* allocator, void** builtin_data);
66
67// Converts the tensor data type used in the flat buffer to the representation
68// used by the runtime.
69TfLiteStatus ConvertTensorType(TensorType tensor_type, TfLiteType* type,
70 ErrorReporter* error_reporter);
71
72TfLiteStatus ParseAbs(const Operator* op, ErrorReporter* error_reporter,
73 BuiltinDataAllocator* allocator, void** builtin_data);
74
75TfLiteStatus ParseAdd(const Operator* op, ErrorReporter* error_reporter,
76 BuiltinDataAllocator* allocator, void** builtin_data);
77
78TfLiteStatus ParseAddN(const Operator* op, ErrorReporter* error_reporter,
79 BuiltinDataAllocator* allocator, void** builtin_data);
80
81TfLiteStatus ParseArgMax(const Operator* op, ErrorReporter* error_reporter,
82 BuiltinDataAllocator* allocator, void** builtin_data);
83
84TfLiteStatus ParseArgMin(const Operator* op, ErrorReporter* error_reporter,
85 BuiltinDataAllocator* allocator, void** builtin_data);
86
87TfLiteStatus ParseAssignVariable(const Operator* op,
88 ErrorReporter* error_reporter,
89 BuiltinDataAllocator* allocator,
90 void** builtin_data);
91
92TfLiteStatus ParseBatchMatMul(const Operator* op, ErrorReporter* error_reporter,
93 BuiltinDataAllocator* allocator,
94 void** builtin_data);
95
96TfLiteStatus ParseBatchToSpaceNd(const Operator* op,
97 ErrorReporter* error_reporter,
98 BuiltinDataAllocator* allocator,
99 void** builtin_data);
100
101TfLiteStatus ParseBroadcastArgs(const Operator* op,
102 ErrorReporter* error_reporter,
103 BuiltinDataAllocator* allocator,
104 void** builtin_data);
105
106TfLiteStatus ParseBroadcastTo(const Operator* op, ErrorReporter* error_reporter,
107 BuiltinDataAllocator* allocator,
108 void** builtin_data);
109
110TfLiteStatus ParseCallOnce(const Operator* op, ErrorReporter* error_reporter,
111 BuiltinDataAllocator* allocator,
112 void** builtin_data);
113
114TfLiteStatus ParseCeil(const Operator* op, ErrorReporter* error_reporter,
115 BuiltinDataAllocator* allocator, void** builtin_data);
116
117TfLiteStatus ParseCast(const Operator* op, ErrorReporter* error_reporter,
118 BuiltinDataAllocator* allocator, void** builtin_data);
119
120TfLiteStatus ParseConcatenation(const Operator* op,
121 ErrorReporter* error_reporter,
122 BuiltinDataAllocator* allocator,
123 void** builtin_data);
124
125TfLiteStatus ParseConv2D(const Operator* op, ErrorReporter* error_reporter,
126 BuiltinDataAllocator* allocator, void** builtin_data);
127
128TfLiteStatus ParseCos(const Operator* op, ErrorReporter* error_reporter,
129 BuiltinDataAllocator* allocator, void** builtin_data);
130
131TfLiteStatus ParseCumsum(const Operator* op, ErrorReporter* error_reporter,
132 BuiltinDataAllocator* allocator, void** builtin_data);
133
134TfLiteStatus ParseDepthToSpace(const Operator* op,
135 ErrorReporter* error_reporter,
136 BuiltinDataAllocator* allocator,
137 void** builtin_data);
138
139TfLiteStatus ParseDepthwiseConv2D(const Operator* op,
140 ErrorReporter* error_reporter,
141 BuiltinDataAllocator* allocator,
142 void** builtin_data);
143
144TfLiteStatus ParseDequantize(const Operator* op, ErrorReporter* error_reporter,
145 BuiltinDataAllocator* allocator,
146 void** builtin_data);
147
148TfLiteStatus ParseDiv(const Operator* op, ErrorReporter* error_reporter,
149 BuiltinDataAllocator* allocator, void** builtin_data);
150
151TfLiteStatus ParseElu(const Operator* op, ErrorReporter* error_reporter,
152 BuiltinDataAllocator* allocator, void** builtin_data);
153
154TfLiteStatus ParseEqual(const Operator* op, ErrorReporter* error_reporter,
155 BuiltinDataAllocator* allocator, void** builtin_data);
156
157TfLiteStatus ParseExp(const Operator* op, ErrorReporter* error_reporter,
158 BuiltinDataAllocator* allocator, void** builtin_data);
159
160TfLiteStatus ParseExpandDims(const Operator* op, ErrorReporter* error_reporter,
161 BuiltinDataAllocator* allocator,
162 void** builtin_data);
163
164TfLiteStatus ParseFill(const Operator* op, ErrorReporter* error_reporter,
165 BuiltinDataAllocator* allocator, void** builtin_data);
166
167TfLiteStatus ParseFloor(const Operator* op, ErrorReporter* error_reporter,
168 BuiltinDataAllocator* allocator, void** builtin_data);
169
170TfLiteStatus ParseFloorDiv(const Operator* op, ErrorReporter* error_reporter,
171 BuiltinDataAllocator* allocator,
172 void** builtin_data);
173
174TfLiteStatus ParseFloorMod(const Operator* op, ErrorReporter* error_reporter,
175 BuiltinDataAllocator* allocator,
176 void** builtin_data);
177
178TfLiteStatus ParseFullyConnected(const Operator* op,
179 ErrorReporter* error_reporter,
180 BuiltinDataAllocator* allocator,
181 void** builtin_data);
182
183TfLiteStatus ParseGather(const Operator* op, ErrorReporter* error_reporter,
184 BuiltinDataAllocator* allocator, void** builtin_data);
185
186TfLiteStatus ParseGatherNd(const Operator* op, ErrorReporter* error_reporter,
187 BuiltinDataAllocator* allocator,
188 void** builtin_data);
189
190TfLiteStatus ParseGreater(const Operator* op, ErrorReporter* error_reporter,
191 BuiltinDataAllocator* allocator, void** builtin_data);
192
193TfLiteStatus ParseGreaterEqual(const Operator* op,
194 ErrorReporter* error_reporter,
195 BuiltinDataAllocator* allocator,
196 void** builtin_data);
197
198TfLiteStatus ParseHardSwish(const Operator* op, ErrorReporter* error_reporter,
199 BuiltinDataAllocator* allocator,
200 void** builtin_data);
201
202TfLiteStatus ParseIf(const Operator* op, ErrorReporter* error_reporter,
203 BuiltinDataAllocator* allocator, void** builtin_data);
204
205TfLiteStatus ParseL2Normalization(const Operator* op,
206 ErrorReporter* error_reporter,
207 BuiltinDataAllocator* allocator,
208 void** builtin_data);
209
210TfLiteStatus ParseLeakyRelu(const Operator* op, ErrorReporter* error_reporter,
211 BuiltinDataAllocator* allocator,
212 void** builtin_data);
213
214TfLiteStatus ParseLess(const Operator* op, ErrorReporter* error_reporter,
215 BuiltinDataAllocator* allocator, void** builtin_data);
216
217TfLiteStatus ParseLessEqual(const Operator* op, ErrorReporter* error_reporter,
218 BuiltinDataAllocator* allocator,
219 void** builtin_data);
220
221TfLiteStatus ParseLog(const Operator* op, ErrorReporter* error_reporter,
222 BuiltinDataAllocator* allocator, void** builtin_data);
223
224TfLiteStatus ParseLogicalAnd(const Operator* op, ErrorReporter* error_reporter,
225 BuiltinDataAllocator* allocator,
226 void** builtin_data);
227
228TfLiteStatus ParseLogicalNot(const Operator* op, ErrorReporter* error_reporter,
229 BuiltinDataAllocator* allocator,
230 void** builtin_data);
231
232TfLiteStatus ParseLogicalOr(const Operator* op, ErrorReporter* error_reporter,
233 BuiltinDataAllocator* allocator,
234 void** builtin_data);
235
236TfLiteStatus ParseLogistic(const Operator* op, ErrorReporter* error_reporter,
237 BuiltinDataAllocator* allocator,
238 void** builtin_data);
239
240TfLiteStatus ParseLogSoftmax(const Operator* op, ErrorReporter* error_reporter,
241 BuiltinDataAllocator* allocator,
242 void** builtin_data);
243
244TfLiteStatus ParseLSTM(const Operator* op, ErrorReporter* error_reporter,
245 BuiltinDataAllocator* allocator, void** builtin_data);
246
247TfLiteStatus ParseMaximum(const Operator* op, ErrorReporter* error_reporter,
248 BuiltinDataAllocator* allocator, void** builtin_data);
249
250TfLiteStatus ParseMinimum(const Operator* op, ErrorReporter* error_reporter,
251 BuiltinDataAllocator* allocator, void** builtin_data);
252
253TfLiteStatus ParseMirrorPad(const Operator* op, ErrorReporter* error_reporter,
254 BuiltinDataAllocator* allocator,
255 void** builtin_data);
256
257TfLiteStatus ParseMul(const Operator* op, ErrorReporter* error_reporter,
258 BuiltinDataAllocator* allocator, void** builtin_data);
259
260TfLiteStatus ParseNeg(const Operator* op, ErrorReporter* error_reporter,
261 BuiltinDataAllocator* allocator, void** builtin_data);
262
263TfLiteStatus ParseNotEqual(const Operator* op, ErrorReporter* error_reporter,
264 BuiltinDataAllocator* allocator,
265 void** builtin_data);
266
267TfLiteStatus ParsePack(const Operator* op, ErrorReporter* error_reporter,
268 BuiltinDataAllocator* allocator, void** builtin_data);
269
270TfLiteStatus ParsePad(const Operator* op, ErrorReporter* error_reporter,
271 BuiltinDataAllocator* allocator, void** builtin_data);
272
273TfLiteStatus ParsePadV2(const Operator* op, ErrorReporter* error_reporter,
274 BuiltinDataAllocator* allocator, void** builtin_data);
275
276TfLiteStatus ParsePool(const Operator* op, ErrorReporter* error_reporter,
277 BuiltinDataAllocator* allocator, void** builtin_data);
278
279TfLiteStatus ParsePow(const Operator* op, ErrorReporter* error_reporter,
280 BuiltinDataAllocator* allocator, void** builtin_data);
281
282TfLiteStatus ParsePrelu(const Operator* op, ErrorReporter* error_reporter,
283 BuiltinDataAllocator* allocator, void** builtin_data);
284
285TfLiteStatus ParseQuantize(const Operator* op, ErrorReporter* error_reporter,
286 BuiltinDataAllocator* allocator,
287 void** builtin_data);
288
289TfLiteStatus ParseReadVariable(const Operator* op,
290 ErrorReporter* error_reporter,
291 BuiltinDataAllocator* allocator,
292 void** builtin_data);
293
294TfLiteStatus ParseReducer(const Operator* op, ErrorReporter* error_reporter,
295 BuiltinDataAllocator* allocator, void** builtin_data);
296
297TfLiteStatus ParseRelu(const Operator* op, ErrorReporter* error_reporter,
298 BuiltinDataAllocator* allocator, void** builtin_data);
299
300TfLiteStatus ParseRelu6(const Operator* op, ErrorReporter* error_reporter,
301 BuiltinDataAllocator* allocator, void** builtin_data);
302
303TfLiteStatus ParseReshape(const Operator* op, ErrorReporter* error_reporter,
304 BuiltinDataAllocator* allocator, void** builtin_data);
305
306TfLiteStatus ParseResizeBilinear(const Operator* op,
307 ErrorReporter* error_reporter,
308 BuiltinDataAllocator* allocator,
309 void** builtin_data);
310
311TfLiteStatus ParseResizeNearestNeighbor(const Operator* op,
312 ErrorReporter* error_reporter,
313 BuiltinDataAllocator* allocator,
314 void** builtin_data);
315
316TfLiteStatus ParseRound(const Operator* op, ErrorReporter* error_reporter,
317 BuiltinDataAllocator* allocator, void** builtin_data);
318
319TfLiteStatus ParseRsqrt(const Operator* op, ErrorReporter* error_reporter,
320 BuiltinDataAllocator* allocator, void** builtin_data);
321
322TfLiteStatus ParseSelectV2(const Operator* op, ErrorReporter* error_reporter,
323 BuiltinDataAllocator* allocator,
324 void** builtin_data);
325
326TfLiteStatus ParseShape(const Operator* op, ErrorReporter* error_reporter,
327 BuiltinDataAllocator* allocator, void** builtin_data);
328
329TfLiteStatus ParseSin(const Operator* op, ErrorReporter* error_reporter,
330 BuiltinDataAllocator* allocator, void** builtin_data);
331
332TfLiteStatus ParseSlice(const Operator* op, ErrorReporter* error_reporter,
333 BuiltinDataAllocator* allocator, void** builtin_data);
334
335TfLiteStatus ParseSoftmax(const Operator* op, ErrorReporter* error_reporter,
336 BuiltinDataAllocator* allocator, void** builtin_data);
337
338TfLiteStatus ParseSpaceToBatchNd(const Operator* op,
339 ErrorReporter* error_reporter,
340 BuiltinDataAllocator* allocator,
341 void** builtin_data);
342
343TfLiteStatus ParseSpaceToDepth(const Operator* op,
344 ErrorReporter* error_reporter,
345 BuiltinDataAllocator* allocator,
346 void** builtin_data);
347
348TfLiteStatus ParseSplit(const Operator* op, ErrorReporter* error_reporter,
349 BuiltinDataAllocator* allocator, void** builtin_data);
350
351TfLiteStatus ParseSplitV(const Operator* op, ErrorReporter* error_reporter,
352 BuiltinDataAllocator* allocator, void** builtin_data);
353
354TfLiteStatus ParseSqueeze(const Operator* op, ErrorReporter* error_reporter,
355 BuiltinDataAllocator* allocator, void** builtin_data);
356
357TfLiteStatus ParseSqrt(const Operator* op, ErrorReporter* error_reporter,
358 BuiltinDataAllocator* allocator, void** builtin_data);
359
360TfLiteStatus ParseSquare(const Operator* op, ErrorReporter* error_reporter,
361 BuiltinDataAllocator* allocator, void** builtin_data);
362
363TfLiteStatus ParseSquaredDifference(const Operator* op,
364 ErrorReporter* error_reporter,
365 BuiltinDataAllocator* allocator,
366 void** builtin_data);
367
368TfLiteStatus ParseStridedSlice(const Operator* op,
369 ErrorReporter* error_reporter,
370 BuiltinDataAllocator* allocator,
371 void** builtin_data);
372
373TfLiteStatus ParseSub(const Operator* op, ErrorReporter* error_reporter,
374 BuiltinDataAllocator* allocator, void** builtin_data);
375
376TfLiteStatus ParseSvdf(const Operator* op, ErrorReporter* error_reporter,
377 BuiltinDataAllocator* allocator, void** builtin_data);
378
379TfLiteStatus ParseTanh(const Operator* op, ErrorReporter* error_reporter,
380 BuiltinDataAllocator* allocator, void** builtin_data);
381
382TfLiteStatus ParseTranspose(const Operator* op, ErrorReporter* error_reporter,
383 BuiltinDataAllocator* allocator,
384 void** builtin_data);
385
386TfLiteStatus ParseTransposeConv(const Operator* op,
387 ErrorReporter* error_reporter,
388 BuiltinDataAllocator* allocator,
389 void** builtin_data);
390
391TfLiteStatus ParseUnpack(const Operator* op, ErrorReporter* error_reporter,
392 BuiltinDataAllocator* allocator, void** builtin_data);
393
394TfLiteStatus ParseUnidirectionalSequenceLSTM(const Operator* op,
395 ErrorReporter* error_reporter,
396 BuiltinDataAllocator* allocator,
397 void** builtin_data);
398
399TfLiteStatus ParseVarHandle(const Operator* op, ErrorReporter* error_reporter,
400 BuiltinDataAllocator* allocator,
401 void** builtin_data);
402
403TfLiteStatus ParseWhile(const Operator* op, ErrorReporter* error_reporter,
404 BuiltinDataAllocator* allocator, void** builtin_data);
405
406TfLiteStatus ParseZerosLike(const Operator* op, ErrorReporter* error_reporter,
407 BuiltinDataAllocator* allocator,
408 void** builtin_data);
409
410} // namespace tflite
411
412#endif // TENSORFLOW_LITE_CORE_API_FLATBUFFER_CONVERSIONS_H_
413