kernel_util.h 13 KB

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