kernel_util.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. const TfLiteTensor* GetInput(const TfLiteContext* context,
  25. const TfLiteNode* node, int index);
  26. // Note: You must check if result is not null:
  27. // TfLiteTensor* my_tensor = GetVariableInput(context, node, kMyTensorIdx);
  28. // TF_LITE_ENSURE(context, my_tensor != nullptr);
  29. TfLiteTensor* GetVariableInput(TfLiteContext* context, const TfLiteNode* node,
  30. int index);
  31. TfLiteTensor* GetOutput(TfLiteContext* context, const TfLiteNode* node,
  32. int index);
  33. const TfLiteTensor* GetOptionalInputTensor(const TfLiteContext* context,
  34. const TfLiteNode* node, int index);
  35. inline int NumDimensions(const TfLiteTensor* t) { return t->dims->size; }
  36. inline int SizeOfDimension(const TfLiteTensor* t, int dim) {
  37. return t->dims->data[dim];
  38. }
  39. #ifndef TF_LITE_STATIC_MEMORY
  40. inline TfLiteTensor* GetTemporary(TfLiteContext* context,
  41. const TfLiteNode* node, int index) {
  42. return &context->tensors[node->temporaries->data[index]];
  43. }
  44. inline const TfLiteTensor* GetIntermediates(TfLiteContext* context,
  45. const TfLiteNode* node, int index) {
  46. return &context->tensors[node->intermediates->data[index]];
  47. }
  48. inline int NumIntermediates(const TfLiteNode* node) {
  49. return node->intermediates->size;
  50. }
  51. #endif // TF_LITE_STATIC_MEMORY
  52. inline int NumInputs(const TfLiteNode* node) { return node->inputs->size; }
  53. inline int NumOutputs(const TfLiteNode* node) { return node->outputs->size; }
  54. inline int64_t NumElements(const TfLiteIntArray* dims) {
  55. int64_t count = 1;
  56. for (int i = 0; i < dims->size; ++i) {
  57. count *= dims->data[i];
  58. }
  59. return count;
  60. }
  61. inline int64_t NumElements(const TfLiteTensor* t) {
  62. return NumElements(t->dims);
  63. }
  64. // Determines whether tensor is constant.
  65. // TODO(b/138199592): Introduce new query which checks for constant OR
  66. // persistent-read-only, which would be useful for most tensor kernels that
  67. // are potentially dynamic based on the input tensor value availability at the
  68. // time of prepare.
  69. inline bool IsConstantTensor(const TfLiteTensor* tensor) {
  70. return tensor->allocation_type == kTfLiteMmapRo;
  71. }
  72. // Determines whether tensor is dynamic. Note that a tensor can be non-const and
  73. // not dynamic. This function specifically checks for a dynamic tensor.
  74. inline bool IsDynamicTensor(const TfLiteTensor* tensor) {
  75. return tensor->allocation_type == kTfLiteDynamic;
  76. }
  77. // Sets tensor to dynamic.
  78. inline void SetTensorToDynamic(TfLiteTensor* tensor) {
  79. if (tensor->allocation_type != kTfLiteDynamic) {
  80. tensor->allocation_type = kTfLiteDynamic;
  81. tensor->data.raw = nullptr;
  82. }
  83. }
  84. // Sets tensor to persistent and read-only.
  85. inline void SetTensorToPersistentRo(TfLiteTensor* tensor) {
  86. if (tensor->allocation_type != kTfLitePersistentRo) {
  87. tensor->allocation_type = kTfLitePersistentRo;
  88. tensor->data.raw = nullptr;
  89. }
  90. }
  91. // Determines whether it is a hybrid op - one that has float inputs and
  92. // quantized weights.
  93. inline bool IsHybridOp(const TfLiteTensor* input, const TfLiteTensor* weight) {
  94. return ((weight->type == kTfLiteUInt8 || weight->type == kTfLiteInt8) &&
  95. input->type == kTfLiteFloat32);
  96. }
  97. // Check dimensionality match and populate OpData for Conv and DepthwiseConv.
  98. TfLiteStatus PopulateConvolutionQuantizationParams(
  99. TfLiteContext* context, const TfLiteTensor* input,
  100. const TfLiteTensor* filter, const TfLiteTensor* bias, TfLiteTensor* output,
  101. const TfLiteFusedActivation& activation, int32_t* multiplier, int* shift,
  102. int32_t* output_activation_min, int32_t* output_activation_max,
  103. int32_t* per_channel_multiplier, int* per_channel_shift);
  104. TfLiteStatus PopulateConvolutionQuantizationParams(
  105. TfLiteContext* context, const TfLiteTensor* input,
  106. const TfLiteTensor* filter, const TfLiteTensor* bias, TfLiteTensor* output,
  107. const TfLiteFusedActivation& activation, int32_t* multiplier, int* shift,
  108. int32_t* output_activation_min, int32_t* output_activation_max,
  109. int32_t* per_channel_multiplier, int* per_channel_shift, int num_channels);
  110. // Calculates the multiplication factor for a quantized convolution (or
  111. // quantized depthwise convolution) involving the given tensors. Returns an
  112. // error if the scales of the tensors are not compatible.
  113. TfLiteStatus GetQuantizedConvolutionMultipler(TfLiteContext* context,
  114. const TfLiteTensor* input,
  115. const TfLiteTensor* filter,
  116. const TfLiteTensor* bias,
  117. TfLiteTensor* output,
  118. double* multiplier);
  119. TfLiteStatus GetQuantizedConvolutionMultipler(TfLiteContext* context,
  120. const TfLiteTensor* input,
  121. const TfLiteTensor* filter,
  122. TfLiteTensor* output,
  123. double* multiplier);
  124. // Calculates the useful quantized range of an activation layer given its
  125. // activation tensor.
  126. TfLiteStatus CalculateActivationRangeQuantized(TfLiteContext* context,
  127. TfLiteFusedActivation activation,
  128. TfLiteTensor* output,
  129. int32_t* act_min,
  130. int32_t* act_max);
  131. // Calculates the useful range of an activation layer given its activation
  132. // tensor.a
  133. template <typename T>
  134. void CalculateActivationRange(TfLiteFusedActivation activation,
  135. T* activation_min, T* activation_max) {
  136. if (activation == kTfLiteActRelu) {
  137. *activation_min = 0;
  138. *activation_max = std::numeric_limits<T>::max();
  139. } else if (activation == kTfLiteActRelu6) {
  140. *activation_min = 0;
  141. *activation_max = 6;
  142. } else if (activation == kTfLiteActReluN1To1) {
  143. *activation_min = -1;
  144. *activation_max = 1;
  145. } else {
  146. *activation_min = std::numeric_limits<T>::lowest();
  147. *activation_max = std::numeric_limits<T>::max();
  148. }
  149. }
  150. // Return true if the given tensors have the same shape.
  151. bool HaveSameShapes(const TfLiteTensor* input1, const TfLiteTensor* input2);
  152. // Calculates the output_shape that is necessary for element-wise operations
  153. // with broadcasting involving the two input tensors.
  154. TfLiteStatus CalculateShapeForBroadcast(TfLiteContext* context,
  155. const TfLiteTensor* input1,
  156. const TfLiteTensor* input2,
  157. TfLiteIntArray** output_shape);
  158. // Calculates the output_shape that is necessary for element-wise operations
  159. // with broadcasting involving the three input tensors.
  160. TfLiteStatus CalculateShapeForBroadcast(TfLiteContext* context,
  161. const TfLiteTensor* input1,
  162. const TfLiteTensor* input2,
  163. const TfLiteTensor* input3,
  164. TfLiteIntArray** output_shape);
  165. } // namespace tflite
  166. #endif // TENSORFLOW_LITE_KERNELS_KERNEL_UTIL_H_