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