mul.cc 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Copyright 2019 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. #include "tensorflow/lite/kernels/internal/reference/mul.h"
  13. #include "cmsis/CMSIS/NN/Include/arm_nnfunctions.h"
  14. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  15. #include "tensorflow/lite/kernels/internal/reference/integer_ops/mul.h"
  16. #include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h"
  17. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  18. #include "tensorflow/lite/kernels/kernel_util.h"
  19. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  20. #include "tensorflow/lite/micro/memory_helpers.h"
  21. namespace tflite {
  22. namespace ops {
  23. namespace micro {
  24. namespace mul {
  25. constexpr int kInput1Tensor = 0;
  26. constexpr int kInput2Tensor = 1;
  27. constexpr int kOutputTensor = 0;
  28. struct OpData {
  29. int32_t output_activation_min;
  30. int32_t output_activation_max;
  31. int32_t output_multiplier;
  32. int output_shift;
  33. // Cached tensor zero point values for quantized operations.
  34. int32_t input1_zero_point;
  35. int32_t input2_zero_point;
  36. int32_t output_zero_point;
  37. };
  38. TfLiteStatus CalculateOpData(TfLiteContext* context, TfLiteNode* node,
  39. TfLiteMulParams* params, OpData* data) {
  40. const TfLiteTensor* input1 = GetInput(context, node, kInput1Tensor);
  41. const TfLiteTensor* input2 = GetInput(context, node, kInput2Tensor);
  42. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  43. TF_LITE_ENSURE_EQ(context, NumInputs(node), 2);
  44. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  45. TF_LITE_ENSURE_TYPES_EQ(context, input1->type, input2->type);
  46. if (output->type == kTfLiteUInt8 || output->type == kTfLiteInt8) {
  47. TF_LITE_ENSURE_STATUS(CalculateActivationRangeQuantized(
  48. context, params->activation, output, &data->output_activation_min,
  49. &data->output_activation_max));
  50. double real_multiplier =
  51. input1->params.scale * input2->params.scale / output->params.scale;
  52. QuantizeMultiplier(real_multiplier, &data->output_multiplier,
  53. &data->output_shift);
  54. }
  55. return kTfLiteOk;
  56. }
  57. void* Init(TfLiteContext* context, const char* buffer, size_t length) {
  58. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  59. return context->AllocatePersistentBuffer(context, sizeof(OpData));
  60. }
  61. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  62. const TfLiteTensor* input1 = GetInput(context, node, kInput1Tensor);
  63. const TfLiteTensor* input2 = GetInput(context, node, kInput2Tensor);
  64. TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
  65. if (output->dims->size == 0) {
  66. return AllocateOutputDimensionsFromInput(context, input1, input2, output);
  67. }
  68. TFLITE_DCHECK(node->builtin_data != nullptr);
  69. auto* params = reinterpret_cast<TfLiteMulParams*>(node->builtin_data);
  70. TFLITE_DCHECK(node->user_data != nullptr);
  71. OpData* data = static_cast<OpData*>(node->user_data);
  72. data->input1_zero_point = input1->params.zero_point;
  73. data->input2_zero_point = input2->params.zero_point;
  74. data->output_zero_point = output->params.zero_point;
  75. CalculateOpData(context, node, params, data);
  76. return kTfLiteOk;
  77. }
  78. void EvalQuantized(TfLiteContext* context, TfLiteNode* node,
  79. TfLiteMulParams* params, const OpData& data,
  80. const TfLiteEvalTensor* input1,
  81. const TfLiteEvalTensor* input2, TfLiteEvalTensor* output) {
  82. if (output->type == kTfLiteInt8 || output->type == kTfLiteUInt8) {
  83. tflite::ArithmeticParams op_params;
  84. SetActivationParams(data.output_activation_min, data.output_activation_max,
  85. &op_params);
  86. op_params.input1_offset = -data.input1_zero_point;
  87. op_params.input2_offset = -data.input2_zero_point;
  88. op_params.output_offset = data.output_zero_point;
  89. op_params.output_multiplier = data.output_multiplier;
  90. op_params.output_shift = data.output_shift;
  91. bool need_broadcast = reference_ops::ProcessBroadcastShapes(
  92. tflite::micro::GetTensorShape(input1),
  93. tflite::micro::GetTensorShape(input2), &op_params);
  94. #define TF_LITE_MUL(type, opname, dtype) \
  95. type::opname(op_params, tflite::micro::GetTensorShape(input1), \
  96. tflite::micro::GetTensorData<dtype>(input1), \
  97. tflite::micro::GetTensorShape(input2), \
  98. tflite::micro::GetTensorData<dtype>(input2), \
  99. tflite::micro::GetTensorShape(output), \
  100. tflite::micro::GetTensorData<dtype>(output));
  101. if (output->type == kTfLiteInt8) {
  102. if (need_broadcast) {
  103. TF_LITE_MUL(reference_integer_ops, BroadcastMul4DSlow, int8_t);
  104. } else {
  105. arm_elementwise_mul_s8(
  106. tflite::micro::GetTensorData<int8_t>(input1),
  107. tflite::micro::GetTensorData<int8_t>(input2),
  108. op_params.input1_offset, op_params.input2_offset,
  109. tflite::micro::GetTensorData<int8_t>(output),
  110. op_params.output_offset, op_params.output_multiplier,
  111. op_params.output_shift, op_params.quantized_activation_min,
  112. op_params.quantized_activation_max,
  113. MatchingElementsSize(tflite::micro::GetTensorShape(input1),
  114. tflite::micro::GetTensorShape(input2),
  115. tflite::micro::GetTensorShape(output)));
  116. }
  117. } else if (output->type == kTfLiteUInt8) {
  118. if (need_broadcast) {
  119. TF_LITE_MUL(reference_ops, BroadcastMul4DSlow, uint8_t);
  120. } else {
  121. TF_LITE_MUL(reference_ops, Mul, uint8_t);
  122. }
  123. }
  124. #undef TF_LITE_MUL
  125. }
  126. }
  127. void EvalFloat(TfLiteContext* context, TfLiteNode* node,
  128. TfLiteMulParams* params, const TfLiteEvalTensor* input1,
  129. const TfLiteEvalTensor* input2, TfLiteEvalTensor* output) {
  130. float output_activation_min, output_activation_max;
  131. CalculateActivationRange(params->activation, &output_activation_min,
  132. &output_activation_max);
  133. tflite::ArithmeticParams op_params;
  134. SetActivationParams(output_activation_min, output_activation_max, &op_params);
  135. bool need_broadcast = reference_ops::ProcessBroadcastShapes(
  136. tflite::micro::GetTensorShape(input1),
  137. tflite::micro::GetTensorShape(input2), &op_params);
  138. #define TF_LITE_MUL(opname) \
  139. reference_ops::opname(op_params, tflite::micro::GetTensorShape(input1), \
  140. tflite::micro::GetTensorData<float>(input1), \
  141. tflite::micro::GetTensorShape(input2), \
  142. tflite::micro::GetTensorData<float>(input2), \
  143. tflite::micro::GetTensorShape(output), \
  144. tflite::micro::GetTensorData<float>(output));
  145. if (need_broadcast) {
  146. TF_LITE_MUL(BroadcastMul4DSlow);
  147. } else {
  148. TF_LITE_MUL(Mul);
  149. }
  150. #undef TF_LITE_MUL
  151. }
  152. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  153. auto* params = reinterpret_cast<TfLiteMulParams*>(node->builtin_data);
  154. const TfLiteEvalTensor* input1 =
  155. tflite::micro::GetEvalInput(context, node, kInput1Tensor);
  156. const TfLiteEvalTensor* input2 =
  157. tflite::micro::GetEvalInput(context, node, kInput2Tensor);
  158. TfLiteEvalTensor* output =
  159. tflite::micro::GetEvalOutput(context, node, kOutputTensor);
  160. TFLITE_DCHECK(node->user_data != nullptr);
  161. const OpData& data = *(static_cast<const OpData*>(node->user_data));
  162. switch (input1->type) {
  163. case kTfLiteUInt8:
  164. case kTfLiteInt8:
  165. EvalQuantized(context, node, params, data, input1, input2, output);
  166. break;
  167. case kTfLiteFloat32:
  168. EvalFloat(context, node, params, input1, input2, output);
  169. break;
  170. default:
  171. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  172. TfLiteTypeGetName(input1->type), input1->type);
  173. return kTfLiteError;
  174. }
  175. return kTfLiteOk;
  176. }
  177. } // namespace mul
  178. TfLiteRegistration Register_MUL() {
  179. return {mul::Init, nullptr /* Free */, mul::Prepare, mul::Eval};
  180. }
  181. } // namespace micro
  182. } // namespace ops
  183. } // namespace tflite