1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_
16 #define TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_
17 
18 #include <stdint.h>
19 
20 #include "tensorflow/lite/c/common.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif  // __cplusplus
25 
26 // TfLiteReshapeParams can't have dynamic data so we fix the maximum possible
27 // number of dimensions.
28 #define TFLITE_RESHAPE_PARAMS_MAX_DIMENSION_COUNT 8
29 
30 // TODO(aselle): Consider using "if this then that" for testing.
31 
32 // Useful placeholder to put in otherwise empty structs to avoid size warnings.
33 typedef struct {
34   char dummy;
35 } EmptyStructPlaceholder;
36 
37 // IMPORTANT: All new members of structs must be added at the end to ensure
38 // backwards compatibility.
39 
40 // Possible padding types (for convolutions)
41 typedef enum {
42   kTfLitePaddingUnknown = 0,
43   kTfLitePaddingSame,
44   kTfLitePaddingValid,
45 } TfLitePadding;
46 
47 typedef enum {
48   kTfLiteMirrorPaddingUnknown = 0,
49   kTfLiteMirrorPaddingReflect,
50   kTfLiteMirrorPaddingSymmetric,
51 } TfLiteMirrorPaddingMode;
52 
53 // TODO(b/130259536): We should move this out of builtin_op_data.
54 typedef struct {
55   int width;
56   int height;
57   int width_offset;
58   int height_offset;
59 } TfLitePaddingValues;
60 
61 typedef struct {
62   TfLiteMirrorPaddingMode mode;
63 } TfLiteMirrorPaddingParams;
64 
65 // Possible fused activation functions.
66 typedef enum {
67   kTfLiteActNone = 0,
68   kTfLiteActRelu,
69   kTfLiteActReluN1To1,  // min(max(-1, x), 1)
70   kTfLiteActRelu6,      // min(max(0, x), 6)
71   kTfLiteActTanh,
72   kTfLiteActSignBit,
73   kTfLiteActSigmoid,
74 } TfLiteFusedActivation;
75 
76 typedef struct {
77   // Parameters for CONV_2D version 1.
78   TfLitePadding padding;
79   int stride_width;
80   int stride_height;
81   TfLiteFusedActivation activation;
82 
83   // Parameters for CONV_2D version 2.
84   // Note: Version 2 supports dilation values not equal to 1.
85   int dilation_width_factor;
86   int dilation_height_factor;
87 } TfLiteConvParams;
88 
89 typedef struct {
90   TfLitePadding padding;
91   int stride_width;
92   int stride_height;
93   int stride_depth;
94   int dilation_width_factor;
95   int dilation_height_factor;
96   int dilation_depth_factor;
97   TfLiteFusedActivation activation;
98 } TfLiteConv3DParams;
99 
100 typedef struct {
101   TfLitePadding padding;
102   int stride_width;
103   int stride_height;
104   int filter_width;
105   int filter_height;
106   TfLiteFusedActivation activation;
107   struct {
108     TfLitePaddingValues padding;
109   } computed;
110 } TfLitePoolParams;
111 
112 typedef struct {
113   // Parameters for DepthwiseConv version 1 or above.
114   TfLitePadding padding;
115   int stride_width;
116   int stride_height;
117   // `depth_multiplier` is redundant. It's used by CPU kernels in
118   // TensorFlow 2.0 or below, but ignored in versions above.
119   //
120   // The information can be deduced from the shape of input and the shape of
121   // weights. Since the TFLiteConverter toolchain doesn't support partially
122   // specified shapes, relying on `depth_multiplier` stops us from supporting
123   // graphs with dynamic shape tensors.
124   //
125   // Note: Some of the delegates (e.g. NNAPI, GPU) are still relying on this
126   // field.
127   int depth_multiplier;
128   TfLiteFusedActivation activation;
129   // Parameters for DepthwiseConv version 2 or above.
130   int dilation_width_factor;
131   int dilation_height_factor;
132 } TfLiteDepthwiseConvParams;
133 
134 typedef struct {
135   int rank;
136   TfLiteFusedActivation activation;
137 
138   // Parameter for SVDF version 4.
139   bool asymmetric_quantize_inputs;
140 } TfLiteSVDFParams;
141 
142 typedef struct {
143   TfLiteFusedActivation activation;
144 
145   // Parameter for RNN version 3.
146   bool asymmetric_quantize_inputs;
147 } TfLiteRNNParams;
148 
149 typedef struct {
150   bool time_major;
151   TfLiteFusedActivation activation;
152 
153   // Parameter for Sequence RNN version 3.
154   bool asymmetric_quantize_inputs;
155 } TfLiteSequenceRNNParams;
156 
157 typedef struct {
158   bool time_major;
159   TfLiteFusedActivation activation;
160   bool merge_outputs;
161 
162   // Parameter for Bidirectional RNN verison 3.
163   bool asymmetric_quantize_inputs;
164 } TfLiteBidirectionalSequenceRNNParams;
165 
166 typedef enum {
167   kTfLiteFullyConnectedWeightsFormatDefault = 0,
168   kTfLiteFullyConnectedWeightsFormatShuffled4x16Int8 = 1,
169 } TfLiteFullyConnectedWeightsFormat;
170 
171 typedef struct {
172   // Parameters for FullyConnected version 1 or above.
173   TfLiteFusedActivation activation;
174 
175   // Parameters for FullyConnected version 2 or above.
176   TfLiteFullyConnectedWeightsFormat weights_format;
177 
178   // Parameters for FullyConnected version 5 or above.
179   // If set to true, then the number of dimensions in the input and the output
180   // tensors are the same. Furthermore, all but the last dimension of the input
181   // and output shapes will be equal.
182   bool keep_num_dims;
183 
184   // Parameters for FullyConnected version 7 or above.
185   // If set to true and the weights are quantized, then non constant inputs
186   // are quantized at evaluation time with asymmetric quantization.
187   bool asymmetric_quantize_inputs;
188 } TfLiteFullyConnectedParams;
189 
190 typedef enum {
191   kTfLiteLshProjectionUnknown = 0,
192   kTfLiteLshProjectionSparse = 1,
193   kTfLiteLshProjectionDense = 2,
194 } TfLiteLSHProjectionType;
195 
196 typedef struct {
197   TfLiteLSHProjectionType type;
198 } TfLiteLSHProjectionParams;
199 
200 typedef struct {
201   float beta;
202 } TfLiteSoftmaxParams;
203 
204 typedef struct {
205   int axis;
206   TfLiteFusedActivation activation;
207 } TfLiteConcatenationParams;
208 
209 typedef struct {
210   TfLiteFusedActivation activation;
211   // Parameter added for the version 4.
212   bool pot_scale_int16;
213 } TfLiteAddParams;
214 
215 typedef struct {
216   EmptyStructPlaceholder placeholder;
217 } TfLiteSpaceToBatchNDParams;
218 
219 typedef struct {
220   EmptyStructPlaceholder placeholder;
221 } TfLiteBatchToSpaceNDParams;
222 
223 typedef struct {
224   bool adj_x;
225   bool adj_y;
226   // Parameters for BatchMatMul version 4 or above.
227   // If set to true and the weights are quantized, then non constant inputs
228   // are quantized at evaluation time with asymmetric quantization.
229   bool asymmetric_quantize_inputs;
230 } TfLiteBatchMatMulParams;
231 
232 typedef struct {
233   TfLiteFusedActivation activation;
234 } TfLiteMulParams;
235 
236 typedef struct {
237   TfLiteFusedActivation activation;
238   // Parameter added for the version 5.
239   bool pot_scale_int16;
240 } TfLiteSubParams;
241 
242 typedef struct {
243   TfLiteFusedActivation activation;
244 } TfLiteDivParams;
245 
246 typedef struct {
247   TfLiteFusedActivation activation;
248 } TfLiteL2NormParams;
249 
250 typedef struct {
251   int radius;
252   float bias;
253   float alpha;
254   float beta;
255 } TfLiteLocalResponseNormParams;
256 
257 typedef enum {
258   kTfLiteLSTMFullKernel = 0,
259   kTfLiteLSTMBasicKernel
260 } TfLiteLSTMKernelType;
261 
262 typedef struct {
263   // Parameters for LSTM version 1.
264   TfLiteFusedActivation activation;
265   float cell_clip;
266   float proj_clip;
267 
268   // Parameters for LSTM version 2.
269   // kTfLiteLSTMBasicKernel is only supported in version 2 or above.
270   TfLiteLSTMKernelType kernel_type;
271 
272   // Parameters for LSTM version 4.
273   bool asymmetric_quantize_inputs;
274 } TfLiteLSTMParams;
275 
276 typedef struct {
277   // Parameters needed for the underlying LSTM.
278   TfLiteFusedActivation activation;
279   float cell_clip;
280   float proj_clip;
281 
282   // If set to true then the first dimension is time, otherwise batch.
283   bool time_major;
284 
285   // Parameter for unidirectional sequence RNN version 3.
286   bool asymmetric_quantize_inputs;
287 } TfLiteUnidirectionalSequenceLSTMParams;
288 
289 typedef struct {
290   // Parameters supported by version 1:
291   // Parameters inherited for the LSTM kernel.
292   TfLiteFusedActivation activation;
293   float cell_clip;
294   float proj_clip;
295 
296   // If true, store the outputs of both directions in the first output.
297   bool merge_outputs;
298 
299   // Parameters supported by version 2:
300   // If set to true then the first dimension is time, otherwise batch.
301   bool time_major;
302 
303   // Parameters supported by version 4:
304   // If set to true, then hybrid ops use asymmetric quantization for inputs.
305   bool asymmetric_quantize_inputs;
306 } TfLiteBidirectionalSequenceLSTMParams;
307 
308 typedef struct {
309   bool align_corners;
310   // half_pixel_centers assumes pixels are of half the actual dimensions, and
311   // yields more accurate resizes. Corresponds to the same argument for the
312   // original TensorFlow op in TF2.0.
313   bool half_pixel_centers;
314 } TfLiteResizeBilinearParams;
315 
316 typedef struct {
317   bool align_corners;
318   bool half_pixel_centers;
319 } TfLiteResizeNearestNeighborParams;
320 
321 typedef struct {
322   EmptyStructPlaceholder placeholder;
323 } TfLitePadParams;
324 
325 typedef struct {
326   EmptyStructPlaceholder placeholder;
327 } TfLitePadV2Params;
328 
329 typedef struct {
330   // These fields are only used in old models for backward compatibility.
331   // In the current implementation, we use the 2nd input of the op as the shape,
332   // and these fields are unused.
333   int shape[TFLITE_RESHAPE_PARAMS_MAX_DIMENSION_COUNT];
334   int num_dimensions;
335 } TfLiteReshapeParams;
336 
337 typedef struct {
338   int ngram_size;
339   int max_skip_size;
340   bool include_all_ngrams;
341 } TfLiteSkipGramParams;
342 
343 typedef struct {
344   int block_size;
345 } TfLiteSpaceToDepthParams;
346 
347 typedef struct {
348   int block_size;
349 } TfLiteDepthToSpaceParams;
350 
351 typedef struct {
352   TfLiteType in_data_type;
353   TfLiteType out_data_type;
354 } TfLiteCastParams;
355 
356 typedef enum {
357   kTfLiteCombinerTypeSum = 0,
358   kTfLiteCombinerTypeMean = 1,
359   kTfLiteCombinerTypeSqrtn = 2,
360 } TfLiteCombinerType;
361 
362 typedef struct {
363   TfLiteCombinerType combiner;
364 } TfLiteEmbeddingLookupSparseParams;
365 
366 typedef struct {
367   int axis;
368   int batch_dims;
369 } TfLiteGatherParams;
370 
371 typedef struct {
372   EmptyStructPlaceholder placeholder;
373 } TfLiteTransposeParams;
374 
375 typedef struct {
376   bool keep_dims;
377 } TfLiteReducerParams;
378 
379 typedef struct {
380   int num_splits;
381 } TfLiteSplitParams;
382 
383 typedef struct {
384   int num_splits;
385 } TfLiteSplitVParams;
386 
387 typedef struct {
388   // TODO(ahentz): We can't have dynamic data in this struct, at least not yet.
389   // For now we will fix the maximum possible number of dimensions.
390   int squeeze_dims[8];
391   int num_squeeze_dims;
392 } TfLiteSqueezeParams;
393 
394 typedef struct {
395   int begin_mask;
396   int end_mask;
397   int ellipsis_mask;
398   int new_axis_mask;
399   int shrink_axis_mask;
400 } TfLiteStridedSliceParams;
401 
402 typedef struct {
403   TfLiteType output_type;
404 } TfLiteArgMaxParams;
405 
406 typedef struct {
407   TfLiteType output_type;
408 } TfLiteArgMinParams;
409 
410 typedef struct {
411   TfLitePadding padding;
412   int stride_width;
413   int stride_height;
414 } TfLiteTransposeConvParams;
415 
416 typedef struct {
417   bool validate_indices;
418 } TfLiteSparseToDenseParams;
419 
420 typedef struct {
421   TfLiteType out_type;
422 } TfLiteShapeParams;
423 
424 typedef struct {
425   EmptyStructPlaceholder placeholder;
426 } TfLiteRankParams;
427 
428 typedef struct {
429   // Parameters supported by version 1:
430   float min;
431   float max;
432   int num_bits;
433 
434   // Parameters supported by version 2:
435   bool narrow_range;
436 } TfLiteFakeQuantParams;
437 
438 typedef struct {
439   int values_count;
440   int axis;
441 } TfLitePackParams;
442 
443 typedef struct {
444   int axis;
445 } TfLiteOneHotParams;
446 
447 typedef struct {
448   int num;
449   int axis;
450 } TfLiteUnpackParams;
451 
452 typedef struct {
453   float alpha;
454 } TfLiteLeakyReluParams;
455 
456 typedef struct {
457   TfLiteType index_out_type;
458 } TfLiteUniqueParams;
459 
460 typedef struct {
461   int seq_dim;
462   int batch_dim;
463 } TfLiteReverseSequenceParams;
464 
465 typedef struct {
466   EmptyStructPlaceholder placeholder;
467 } TfLiteMatrixDiagParams;
468 
469 typedef struct {
470   EmptyStructPlaceholder placeholder;
471 } TfLiteMatrixSetDiagParams;
472 
473 typedef struct {
474   int then_subgraph_index;
475   int else_subgraph_index;
476 } TfLiteIfParams;
477 
478 typedef struct {
479   int cond_subgraph_index;
480   int body_subgraph_index;
481 } TfLiteWhileParams;
482 
483 typedef struct {
484   bool exclusive;
485   bool reverse;
486 } TfLiteCumsumParams;
487 
488 typedef struct {
489   int init_subgraph_index;
490 } TfLiteCallOnceParams;
491 
492 typedef struct {
493   int table_id;
494   TfLiteType key_dtype;
495   TfLiteType value_dtype;
496 } TfLiteHashtableParams;
497 
498 #ifdef __cplusplus
499 }  // extern "C"
500 #endif  // __cplusplus
501 
502 #endif  // TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_
503