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 TfLiteConv3DParams TfLiteConv3DTransposeParams;
101 
102 typedef struct {
103   TfLitePadding padding;
104   int stride_width;
105   int stride_height;
106   int filter_width;
107   int filter_height;
108   TfLiteFusedActivation activation;
109   struct {
110     TfLitePaddingValues padding;
111   } computed;
112 } TfLitePoolParams;
113 
114 typedef struct {
115   // Parameters for DepthwiseConv version 1 or above.
116   TfLitePadding padding;
117   int stride_width;
118   int stride_height;
119   // `depth_multiplier` is redundant. It's used by CPU kernels in
120   // TensorFlow 2.0 or below, but ignored in versions above.
121   //
122   // The information can be deduced from the shape of input and the shape of
123   // weights. Since the TFLiteConverter toolchain doesn't support partially
124   // specified shapes, relying on `depth_multiplier` stops us from supporting
125   // graphs with dynamic shape tensors.
126   //
127   // Note: Some of the delegates (e.g. NNAPI, GPU) are still relying on this
128   // field.
129   int depth_multiplier;
130   TfLiteFusedActivation activation;
131   // Parameters for DepthwiseConv version 2 or above.
132   int dilation_width_factor;
133   int dilation_height_factor;
134 } TfLiteDepthwiseConvParams;
135 
136 typedef struct {
137   int rank;
138   TfLiteFusedActivation activation;
139 
140   // Parameter for SVDF version 4.
141   bool asymmetric_quantize_inputs;
142 } TfLiteSVDFParams;
143 
144 typedef struct {
145   TfLiteFusedActivation activation;
146 
147   // Parameter for RNN version 3.
148   bool asymmetric_quantize_inputs;
149 } TfLiteRNNParams;
150 
151 typedef struct {
152   bool time_major;
153   TfLiteFusedActivation activation;
154 
155   // Parameter for Sequence RNN version 3.
156   bool asymmetric_quantize_inputs;
157 } TfLiteSequenceRNNParams;
158 
159 typedef struct {
160   bool time_major;
161   TfLiteFusedActivation activation;
162   bool merge_outputs;
163 
164   // Parameter for Bidirectional RNN verison 3.
165   bool asymmetric_quantize_inputs;
166 } TfLiteBidirectionalSequenceRNNParams;
167 
168 typedef enum {
169   kTfLiteFullyConnectedWeightsFormatDefault = 0,
170   kTfLiteFullyConnectedWeightsFormatShuffled4x16Int8 = 1,
171 } TfLiteFullyConnectedWeightsFormat;
172 
173 typedef struct {
174   // Parameters for FullyConnected version 1 or above.
175   TfLiteFusedActivation activation;
176 
177   // Parameters for FullyConnected version 2 or above.
178   TfLiteFullyConnectedWeightsFormat weights_format;
179 
180   // Parameters for FullyConnected version 5 or above.
181   // If set to true, then the number of dimensions in the input and the output
182   // tensors are the same. Furthermore, all but the last dimension of the input
183   // and output shapes will be equal.
184   bool keep_num_dims;
185 
186   // Parameters for FullyConnected version 7 or above.
187   // If set to true and the weights are quantized, then non constant inputs
188   // are quantized at evaluation time with asymmetric quantization.
189   bool asymmetric_quantize_inputs;
190 } TfLiteFullyConnectedParams;
191 
192 typedef enum {
193   kTfLiteLshProjectionUnknown = 0,
194   kTfLiteLshProjectionSparse = 1,
195   kTfLiteLshProjectionDense = 2,
196 } TfLiteLSHProjectionType;
197 
198 typedef struct {
199   TfLiteLSHProjectionType type;
200 } TfLiteLSHProjectionParams;
201 
202 typedef struct {
203   float beta;
204 } TfLiteSoftmaxParams;
205 
206 typedef struct {
207   int axis;
208   TfLiteFusedActivation activation;
209 } TfLiteConcatenationParams;
210 
211 typedef struct {
212   TfLiteFusedActivation activation;
213   // Parameter added for the version 4.
214   bool pot_scale_int16;
215 } TfLiteAddParams;
216 
217 typedef struct {
218   EmptyStructPlaceholder placeholder;
219 } TfLiteSpaceToBatchNDParams;
220 
221 typedef struct {
222   EmptyStructPlaceholder placeholder;
223 } TfLiteBatchToSpaceNDParams;
224 
225 typedef struct {
226   bool adj_x;
227   bool adj_y;
228   // Parameters for BatchMatMul version 4 or above.
229   // If set to true and the weights are quantized, then non constant inputs
230   // are quantized at evaluation time with asymmetric quantization.
231   bool asymmetric_quantize_inputs;
232 } TfLiteBatchMatMulParams;
233 
234 typedef struct {
235   TfLiteFusedActivation activation;
236 } TfLiteMulParams;
237 
238 typedef struct {
239   TfLiteFusedActivation activation;
240   // Parameter added for the version 5.
241   bool pot_scale_int16;
242 } TfLiteSubParams;
243 
244 typedef struct {
245   TfLiteFusedActivation activation;
246 } TfLiteDivParams;
247 
248 typedef struct {
249   TfLiteFusedActivation activation;
250 } TfLiteL2NormParams;
251 
252 typedef struct {
253   int radius;
254   float bias;
255   float alpha;
256   float beta;
257 } TfLiteLocalResponseNormParams;
258 
259 typedef enum {
260   kTfLiteLSTMFullKernel = 0,
261   kTfLiteLSTMBasicKernel
262 } TfLiteLSTMKernelType;
263 
264 typedef struct {
265   // Parameters for LSTM version 1.
266   TfLiteFusedActivation activation;
267   float cell_clip;
268   float proj_clip;
269 
270   // Parameters for LSTM version 2.
271   // kTfLiteLSTMBasicKernel is only supported in version 2 or above.
272   TfLiteLSTMKernelType kernel_type;
273 
274   // Parameters for LSTM version 4.
275   bool asymmetric_quantize_inputs;
276 } TfLiteLSTMParams;
277 
278 typedef struct {
279   // Parameters needed for the underlying LSTM.
280   TfLiteFusedActivation activation;
281   float cell_clip;
282   float proj_clip;
283 
284   // If set to true then the first dimension is time, otherwise batch.
285   bool time_major;
286 
287   // Parameter for unidirectional sequence RNN version 3.
288   bool asymmetric_quantize_inputs;
289 } TfLiteUnidirectionalSequenceLSTMParams;
290 
291 typedef struct {
292   // Parameters supported by version 1:
293   // Parameters inherited for the LSTM kernel.
294   TfLiteFusedActivation activation;
295   float cell_clip;
296   float proj_clip;
297 
298   // If true, store the outputs of both directions in the first output.
299   bool merge_outputs;
300 
301   // Parameters supported by version 2:
302   // If set to true then the first dimension is time, otherwise batch.
303   bool time_major;
304 
305   // Parameters supported by version 4:
306   // If set to true, then hybrid ops use asymmetric quantization for inputs.
307   bool asymmetric_quantize_inputs;
308 } TfLiteBidirectionalSequenceLSTMParams;
309 
310 typedef struct {
311   bool align_corners;
312   // half_pixel_centers assumes pixels are of half the actual dimensions, and
313   // yields more accurate resizes. Corresponds to the same argument for the
314   // original TensorFlow op in TF2.0.
315   bool half_pixel_centers;
316 } TfLiteResizeBilinearParams;
317 
318 typedef struct {
319   bool align_corners;
320   bool half_pixel_centers;
321 } TfLiteResizeNearestNeighborParams;
322 
323 typedef struct {
324   EmptyStructPlaceholder placeholder;
325 } TfLitePadParams;
326 
327 typedef struct {
328   EmptyStructPlaceholder placeholder;
329 } TfLitePadV2Params;
330 
331 typedef struct {
332   // These fields are only used in old models for backward compatibility.
333   // In the current implementation, we use the 2nd input of the op as the shape,
334   // and these fields are unused.
335   int shape[TFLITE_RESHAPE_PARAMS_MAX_DIMENSION_COUNT];
336   int num_dimensions;
337 } TfLiteReshapeParams;
338 
339 typedef struct {
340   int ngram_size;
341   int max_skip_size;
342   bool include_all_ngrams;
343 } TfLiteSkipGramParams;
344 
345 typedef struct {
346   int block_size;
347 } TfLiteSpaceToDepthParams;
348 
349 typedef struct {
350   int block_size;
351 } TfLiteDepthToSpaceParams;
352 
353 typedef struct {
354   TfLiteType in_data_type;
355   TfLiteType out_data_type;
356 } TfLiteCastParams;
357 
358 typedef enum {
359   kTfLiteCombinerTypeSum = 0,
360   kTfLiteCombinerTypeMean = 1,
361   kTfLiteCombinerTypeSqrtn = 2,
362 } TfLiteCombinerType;
363 
364 typedef struct {
365   TfLiteCombinerType combiner;
366 } TfLiteEmbeddingLookupSparseParams;
367 
368 typedef struct {
369   int axis;
370   int batch_dims;
371 } TfLiteGatherParams;
372 
373 typedef struct {
374   EmptyStructPlaceholder placeholder;
375 } TfLiteTransposeParams;
376 
377 typedef struct {
378   bool keep_dims;
379 } TfLiteReducerParams;
380 
381 typedef struct {
382   int num_splits;
383 } TfLiteSplitParams;
384 
385 typedef struct {
386   int num_splits;
387 } TfLiteSplitVParams;
388 
389 typedef struct {
390   // TODO(ahentz): We can't have dynamic data in this struct, at least not yet.
391   // For now we will fix the maximum possible number of dimensions.
392   int squeeze_dims[8];
393   int num_squeeze_dims;
394 } TfLiteSqueezeParams;
395 
396 typedef struct {
397   int begin_mask;
398   int end_mask;
399   int ellipsis_mask;
400   int new_axis_mask;
401   int shrink_axis_mask;
402 } TfLiteStridedSliceParams;
403 
404 typedef struct {
405   TfLiteType output_type;
406 } TfLiteArgMaxParams;
407 
408 typedef struct {
409   TfLiteType output_type;
410 } TfLiteArgMinParams;
411 
412 typedef struct {
413   TfLitePadding padding;
414   int stride_width;
415   int stride_height;
416 } TfLiteTransposeConvParams;
417 
418 typedef struct {
419   bool validate_indices;
420 } TfLiteSparseToDenseParams;
421 
422 typedef struct {
423   TfLiteType out_type;
424 } TfLiteShapeParams;
425 
426 typedef struct {
427   EmptyStructPlaceholder placeholder;
428 } TfLiteRankParams;
429 
430 typedef struct {
431   // Parameters supported by version 1:
432   float min;
433   float max;
434   int num_bits;
435 
436   // Parameters supported by version 2:
437   bool narrow_range;
438 } TfLiteFakeQuantParams;
439 
440 typedef struct {
441   int values_count;
442   int axis;
443 } TfLitePackParams;
444 
445 typedef struct {
446   int axis;
447 } TfLiteOneHotParams;
448 
449 typedef struct {
450   int num;
451   int axis;
452 } TfLiteUnpackParams;
453 
454 typedef struct {
455   float alpha;
456 } TfLiteLeakyReluParams;
457 
458 typedef struct {
459   TfLiteType index_out_type;
460 } TfLiteUniqueParams;
461 
462 typedef struct {
463   int seq_dim;
464   int batch_dim;
465 } TfLiteReverseSequenceParams;
466 
467 typedef struct {
468   EmptyStructPlaceholder placeholder;
469 } TfLiteMatrixDiagParams;
470 
471 typedef struct {
472   EmptyStructPlaceholder placeholder;
473 } TfLiteMatrixSetDiagParams;
474 
475 typedef struct {
476   int then_subgraph_index;
477   int else_subgraph_index;
478 } TfLiteIfParams;
479 
480 typedef struct {
481   int cond_subgraph_index;
482   int body_subgraph_index;
483 } TfLiteWhileParams;
484 
485 typedef struct {
486   bool exclusive;
487   bool reverse;
488 } TfLiteCumsumParams;
489 
490 typedef struct {
491   int init_subgraph_index;
492 } TfLiteCallOnceParams;
493 
494 typedef struct {
495   int table_id;
496   TfLiteType key_dtype;
497   TfLiteType value_dtype;
498 } TfLiteHashtableParams;
499 
500 typedef struct {
501   const char* container;
502   const char* shared_name;
503 } TfLiteVarHandleParams;
504 
505 #ifdef __cplusplus
506 }  // extern "C"
507 #endif  // __cplusplus
508 
509 #endif  // TENSORFLOW_LITE_C_BUILTIN_OP_DATA_H_
510