builtin_op_data.h 11 KB

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