kernel_util.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_KERNELS_KERNEL_UTIL_H_
  13. #define TENSORFLOW_LITE_KERNELS_KERNEL_UTIL_H_
  14. #include <stdint.h>
  15. #include <limits>
  16. #include "tensorflow/lite/c/builtin_op_data.h"
  17. #include "tensorflow/lite/c/common.h"
  18. namespace tflite {
  19. // A fair number of functions in this header have historically been inline.
  20. // It is ok to change functions to not be inline if the latency with
  21. // benchmark_model for MobileNet + MobileBERT is unaffected. If such a change is
  22. // made, move the newly non-inlined function declarations to the top of this
  23. // header file.
  24. // Note: You must check if result is not null:
  25. //
  26. // TfLiteTensor* my_tensor = GetInput(context, node, kMyTensorIdx);
  27. // TF_LITE_ENSURE(context, my_tensor != nullptr);
  28. //
  29. // This is because the index might point to the optional tensor constant
  30. // (kTfLiteOptionalTensor) in which case there is no tensor to return.
  31. const TfLiteTensor* GetInput(const TfLiteContext* context,
  32. const TfLiteNode* node, int index);
  33. // Same as `GetInput` but returns boolean and uses output argument for tensor.
  34. //
  35. // TfLiteTensor* my_tensor;
  36. // TF_LITE_ENSURE_OK(context,
  37. // GetInputSafe(context, node, kMyTensorIdx, &my_tensor));
  38. // // can use my_tensor directly from here onwards, it is not nullptr
  39. //
  40. // Should be used in cases where the binary size is too large.
  41. TfLiteStatus GetInputSafe(const TfLiteContext* context, const TfLiteNode* node,
  42. int index, const TfLiteTensor** tensor);
  43. // Note: You must check if result is not null:
  44. //
  45. // TfLiteTensor* my_tensor = GetVariableInput(context, node, kMyTensorIdx);
  46. // TF_LITE_ENSURE(context, my_tensor != nullptr);
  47. //
  48. // This is because the index might point to the optional tensor constant
  49. // (kTfLiteOptionalTensor) in which case there is no tensor to return.
  50. TfLiteTensor* GetVariableInput(TfLiteContext* context, const TfLiteNode* node,
  51. int index);
  52. // Note: You must check if result is not null:
  53. //
  54. // TfLiteTensor* my_tensor = GetOutput(context, node, kMyTensorIdx);
  55. // TF_LITE_ENSURE(context, my_tensor != nullptr);
  56. //
  57. // This is because the index might point to the optional tensor constant
  58. // (kTfLiteOptionalTensor) in which case there is no tensor to return.
  59. TfLiteTensor* GetOutput(TfLiteContext* context, const TfLiteNode* node,
  60. int index);
  61. // Same as `GetOutput` but returns boolean and uses output argument for tensor.
  62. //
  63. // TfLiteTensor* my_tensor;
  64. // TF_LITE_ENSURE_OK(context,
  65. // GetOutputSafe(context, node, kMyTensorIdx, &my_tensor));
  66. // // can use my_tensor directly from here onwards, it is not nullptr
  67. //
  68. // Should be used in cases where the binary size is too large.
  69. TfLiteStatus GetOutputSafe(const TfLiteContext* context, const TfLiteNode* node,
  70. int index, TfLiteTensor** tensor);
  71. // Note: You must check if result is not null:
  72. //
  73. // TfLiteTensor* my_tensor = GetOptionalInputTensor(context, node, kIdx);
  74. // TF_LITE_ENSURE(context, my_tensor != nullptr);
  75. //
  76. // This is because the index might point to the optional tensor constant
  77. // (kTfLiteOptionalTensor) in which case there is no tensor to return.
  78. //
  79. // Deprecated. GetInput has the same functionality.
  80. const TfLiteTensor* GetOptionalInputTensor(const TfLiteContext* context,
  81. const TfLiteNode* node, int index);
  82. #ifndef TF_LITE_STATIC_MEMORY
  83. // Note: You must check if result is not null:
  84. //
  85. // TfLiteTensor* my_tensor = GetTemporary(context, node, kMyTensorIdx);
  86. // TF_LITE_ENSURE(context, my_tensor != nullptr);
  87. //
  88. // This is because the index might point to the optional tensor constant
  89. // (kTfLiteOptionalTensor) in which case there is no tensor to return.
  90. TfLiteTensor* GetTemporary(TfLiteContext* context, const TfLiteNode* node,
  91. int index);
  92. // Same as `GetTemporary` but returns boolean and uses output argument for
  93. // tensor.
  94. //
  95. // TfLiteTensor* my_tensor;
  96. // TF_LITE_ENSURE_OK(context,
  97. // GetTemporarySafe(context, node, kMyTensorIdx,
  98. // &my_tensor));
  99. // // can use my_tensor directly from here onwards, it is not nullptr
  100. //
  101. // Should be used in cases where the binary size is too large.
  102. TfLiteStatus GetTemporarySafe(const TfLiteContext* context,
  103. const TfLiteNode* node, int index,
  104. TfLiteTensor** tensor);
  105. // Note: You must check if result is not null:
  106. //
  107. // TfLiteTensor* my_tensor = GetIntermediates(context, node, kMyTensorIdx);
  108. // TF_LITE_ENSURE(context, my_tensor != nullptr);
  109. //
  110. // This is because the index might point to the optional tensor constant
  111. // (kTfLiteOptionalTensor) in which case there is no tensor to return.
  112. const TfLiteTensor* GetIntermediates(TfLiteContext* context,
  113. const TfLiteNode* node, int index);
  114. // Same as `GetIntermediates` but returns boolean and uses output argument for
  115. // tensor.
  116. //
  117. // TfLiteTensor* my_tensor;
  118. // TF_LITE_ENSURE_OK(context,
  119. // GetIntermediatesSafe(context, node, kMyTensorIdx,
  120. // &my_tensor));
  121. // // can use my_tensor directly from here onwards, it is not nullptr
  122. //
  123. // Should be used in cases where the binary size is too large.
  124. TfLiteStatus GetIntermediatesSafe(const TfLiteContext* context,
  125. const TfLiteNode* node, int index,
  126. TfLiteTensor** tensor);
  127. #endif // TF_LITE_STATIC_MEMORY
  128. inline int NumDimensions(const TfLiteTensor* t) { return t->dims->size; }
  129. inline int SizeOfDimension(const TfLiteTensor* t, int dim) {
  130. return t->dims->data[dim];
  131. }
  132. inline int NumInputs(const TfLiteNode* node) { return node->inputs->size; }
  133. inline int NumOutputs(const TfLiteNode* node) { return node->outputs->size; }
  134. #ifndef TF_LITE_STATIC_MEMORY
  135. inline int NumIntermediates(const TfLiteNode* node) {
  136. return node->intermediates->size;
  137. }
  138. #endif // TF_LITE_STATIC_MEMORY
  139. inline int64_t NumElements(const TfLiteIntArray* dims) {
  140. int64_t count = 1;
  141. for (int i = 0; i < dims->size; ++i) {
  142. count *= dims->data[i];
  143. }
  144. return count;
  145. }
  146. inline int64_t NumElements(const TfLiteTensor* t) {
  147. return NumElements(t->dims);
  148. }
  149. // Determines whether tensor is constant.
  150. // TODO(b/138199592): Introduce new query which checks for constant OR
  151. // persistent-read-only, which would be useful for most tensor kernels that
  152. // are potentially dynamic based on the input tensor value availability at the
  153. // time of prepare.
  154. inline bool IsConstantTensor(const TfLiteTensor* tensor) {
  155. return tensor->allocation_type == kTfLiteMmapRo;
  156. }
  157. // Determines whether tensor is dynamic. Note that a tensor can be non-const and
  158. // not dynamic. This function specifically checks for a dynamic tensor.
  159. inline bool IsDynamicTensor(const TfLiteTensor* tensor) {
  160. return tensor->allocation_type == kTfLiteDynamic;
  161. }
  162. // Sets tensor to dynamic.
  163. inline void SetTensorToDynamic(TfLiteTensor* tensor) {
  164. if (tensor->allocation_type != kTfLiteDynamic) {
  165. tensor->allocation_type = kTfLiteDynamic;
  166. tensor->data.raw = nullptr;
  167. }
  168. }
  169. // Sets tensor to persistent and read-only.
  170. inline void SetTensorToPersistentRo(TfLiteTensor* tensor) {
  171. if (tensor->allocation_type != kTfLitePersistentRo) {
  172. tensor->allocation_type = kTfLitePersistentRo;
  173. tensor->data.raw = nullptr;
  174. }
  175. }
  176. // Determines whether it is a hybrid op - one that has float inputs and
  177. // quantized weights.
  178. inline bool IsHybridOp(const TfLiteTensor* input, const TfLiteTensor* weight) {
  179. return ((weight->type == kTfLiteUInt8 || weight->type == kTfLiteInt8) &&
  180. input->type == kTfLiteFloat32);
  181. }
  182. // Check dimensionality match and populate OpData for Conv and DepthwiseConv.
  183. TfLiteStatus PopulateConvolutionQuantizationParams(
  184. TfLiteContext* context, const TfLiteTensor* input,
  185. const TfLiteTensor* filter, const TfLiteTensor* bias, TfLiteTensor* output,
  186. const TfLiteFusedActivation& activation, int32_t* multiplier, int* shift,
  187. int32_t* output_activation_min, int32_t* output_activation_max,
  188. int32_t* per_channel_multiplier, int32_t* per_channel_shift);
  189. TfLiteStatus PopulateConvolutionQuantizationParams(
  190. TfLiteContext* context, const TfLiteTensor* input,
  191. const TfLiteTensor* filter, const TfLiteTensor* bias, TfLiteTensor* output,
  192. const TfLiteFusedActivation& activation, int32_t* multiplier, int* shift,
  193. int32_t* output_activation_min, int32_t* output_activation_max,
  194. int32_t* per_channel_multiplier, int32_t* per_channel_shift,
  195. int num_channels);
  196. // Calculates the multiplication factor for a quantized convolution (or
  197. // quantized depthwise convolution) involving the given tensors. Returns an
  198. // error if the scales of the tensors are not compatible.
  199. TfLiteStatus GetQuantizedConvolutionMultipler(TfLiteContext* context,
  200. const TfLiteTensor* input,
  201. const TfLiteTensor* filter,
  202. const TfLiteTensor* bias,
  203. TfLiteTensor* output,
  204. double* multiplier);
  205. TfLiteStatus GetQuantizedConvolutionMultipler(TfLiteContext* context,
  206. const TfLiteTensor* input,
  207. const TfLiteTensor* filter,
  208. TfLiteTensor* output,
  209. double* multiplier);
  210. // Calculates the useful quantized range of an activation layer given its
  211. // activation tensor.
  212. TfLiteStatus CalculateActivationRangeQuantized(TfLiteContext* context,
  213. TfLiteFusedActivation activation,
  214. TfLiteTensor* output,
  215. int32_t* act_min,
  216. int32_t* act_max);
  217. // Calculates the useful range of an activation layer given its activation
  218. // tensor.a
  219. template <typename T>
  220. void CalculateActivationRange(TfLiteFusedActivation activation,
  221. T* activation_min, T* activation_max) {
  222. if (activation == kTfLiteActRelu) {
  223. *activation_min = 0;
  224. *activation_max = std::numeric_limits<T>::max();
  225. } else if (activation == kTfLiteActRelu6) {
  226. *activation_min = 0;
  227. *activation_max = 6;
  228. } else if (activation == kTfLiteActReluN1To1) {
  229. *activation_min = -1;
  230. *activation_max = 1;
  231. } else {
  232. *activation_min = std::numeric_limits<T>::lowest();
  233. *activation_max = std::numeric_limits<T>::max();
  234. }
  235. }
  236. // Return true if the given tensors have the same shape.
  237. bool HaveSameShapes(const TfLiteTensor* input1, const TfLiteTensor* input2);
  238. // Calculates the output_shape that is necessary for element-wise operations
  239. // with broadcasting involving the two input tensors.
  240. TfLiteStatus CalculateShapeForBroadcast(TfLiteContext* context,
  241. const TfLiteTensor* input1,
  242. const TfLiteTensor* input2,
  243. TfLiteIntArray** output_shape);
  244. // Calculates the output_shape that is necessary for element-wise operations
  245. // with broadcasting involving the three input tensors.
  246. TfLiteStatus CalculateShapeForBroadcast(TfLiteContext* context,
  247. const TfLiteTensor* input1,
  248. const TfLiteTensor* input2,
  249. const TfLiteTensor* input3,
  250. TfLiteIntArray** output_shape);
  251. // Return the size of given type in bytes. Return 0 in in case of string.
  252. int TfLiteTypeGetSize(TfLiteType type);
  253. // Whether the current platform is mobile (Android or iOS).
  254. bool IsMobilePlatform();
  255. } // namespace tflite
  256. #endif // TENSORFLOW_LITE_KERNELS_KERNEL_UTIL_H_