mul_common.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Copyright 2021 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/c/common.h"
  13. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  14. #include "tensorflow/lite/kernels/internal/reference/integer_ops/mul.h"
  15. #include "tensorflow/lite/kernels/internal/reference/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/kernels/mul.h"
  21. #include "tensorflow/lite/micro/memory_helpers.h"
  22. namespace tflite {
  23. const int kMulInput1Tensor = 0;
  24. const int kMulInput2Tensor = 1;
  25. const int kMulOutputTensor = 0;
  26. void* MulInit(TfLiteContext* context, const char* buffer, size_t length) {
  27. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  28. return context->AllocatePersistentBuffer(context, sizeof(OpDataMul));
  29. }
  30. TfLiteStatus CalculateOpDataMul(TfLiteContext* context, TfLiteNode* node,
  31. TfLiteMulParams* params, OpDataMul* data) {
  32. MicroContext* micro_context = GetMicroContext(context);
  33. TfLiteTensor* input1 =
  34. micro_context->AllocateTempInputTensor(node, kMulInput1Tensor);
  35. TF_LITE_ENSURE(context, input1 != nullptr);
  36. TfLiteTensor* input2 =
  37. micro_context->AllocateTempInputTensor(node, kMulInput2Tensor);
  38. TF_LITE_ENSURE(context, input2 != nullptr);
  39. TfLiteTensor* output =
  40. micro_context->AllocateTempOutputTensor(node, kMulOutputTensor);
  41. TF_LITE_ENSURE(context, output != nullptr);
  42. TF_LITE_ENSURE_EQ(context, NumInputs(node), 2);
  43. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  44. TF_LITE_ENSURE_TYPES_EQ(context, input1->type, input2->type);
  45. if (output->type == kTfLiteInt8) {
  46. TF_LITE_ENSURE_STATUS(CalculateActivationRangeQuantized(
  47. context, params->activation, output, &data->output_activation_min,
  48. &data->output_activation_max));
  49. double real_multiplier = static_cast<double>(input1->params.scale) *
  50. static_cast<double>(input2->params.scale) /
  51. static_cast<double>(output->params.scale);
  52. QuantizeMultiplier(real_multiplier, &data->output_multiplier,
  53. &data->output_shift);
  54. data->input1_zero_point = input1->params.zero_point;
  55. data->input2_zero_point = input2->params.zero_point;
  56. data->output_zero_point = output->params.zero_point;
  57. } else if (output->type == kTfLiteInt32) {
  58. CalculateActivationRange(params->activation, &data->output_activation_min,
  59. &data->output_activation_max);
  60. } else {
  61. CalculateActivationRange(params->activation,
  62. &data->output_activation_min_f32,
  63. &data->output_activation_max_f32);
  64. }
  65. micro_context->DeallocateTempTfLiteTensor(input1);
  66. micro_context->DeallocateTempTfLiteTensor(input2);
  67. micro_context->DeallocateTempTfLiteTensor(output);
  68. return kTfLiteOk;
  69. }
  70. TfLiteStatus MulPrepare(TfLiteContext* context, TfLiteNode* node) {
  71. TFLITE_DCHECK(node->builtin_data != nullptr);
  72. auto* params = reinterpret_cast<TfLiteMulParams*>(node->builtin_data);
  73. TFLITE_DCHECK(node->user_data != nullptr);
  74. OpDataMul* data = static_cast<OpDataMul*>(node->user_data);
  75. return CalculateOpDataMul(context, node, params, data);
  76. }
  77. void EvalMulQuantizedReference(TfLiteContext* context, TfLiteNode* node,
  78. const OpDataMul* data,
  79. const TfLiteEvalTensor* input1,
  80. const TfLiteEvalTensor* input2,
  81. TfLiteEvalTensor* output) {
  82. tflite::ArithmeticParams op_params = {};
  83. op_params.quantized_activation_min = data->output_activation_min;
  84. op_params.quantized_activation_max = data->output_activation_max;
  85. op_params.float_activation_max = data->output_activation_max_f32;
  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. if (input1->type == kTfLiteInt8) {
  95. if (need_broadcast) {
  96. reference_integer_ops::BroadcastMul4DSlow(
  97. op_params, tflite::micro::GetTensorShape(input1),
  98. tflite::micro::GetTensorData<int8_t>(input1),
  99. tflite::micro::GetTensorShape(input2),
  100. tflite::micro::GetTensorData<int8_t>(input2),
  101. tflite::micro::GetTensorShape(output),
  102. tflite::micro::GetTensorData<int8_t>(output));
  103. } else {
  104. reference_integer_ops::Mul(op_params,
  105. tflite::micro::GetTensorShape(input1),
  106. tflite::micro::GetTensorData<int8_t>(input1),
  107. tflite::micro::GetTensorShape(input2),
  108. tflite::micro::GetTensorData<int8_t>(input2),
  109. tflite::micro::GetTensorShape(output),
  110. tflite::micro::GetTensorData<int8_t>(output));
  111. }
  112. } else if (input1->type == kTfLiteInt32) {
  113. if (need_broadcast) {
  114. reference_ops::BroadcastMul4DSlow(
  115. op_params, tflite::micro::GetTensorShape(input1),
  116. tflite::micro::GetTensorData<int32_t>(input1),
  117. tflite::micro::GetTensorShape(input2),
  118. tflite::micro::GetTensorData<int32_t>(input2),
  119. tflite::micro::GetTensorShape(output),
  120. tflite::micro::GetTensorData<int32_t>(output));
  121. } else {
  122. reference_ops::Mul(op_params, tflite::micro::GetTensorShape(input1),
  123. tflite::micro::GetTensorData<int32_t>(input1),
  124. tflite::micro::GetTensorShape(input2),
  125. tflite::micro::GetTensorData<int32_t>(input2),
  126. tflite::micro::GetTensorShape(output),
  127. tflite::micro::GetTensorData<int32_t>(output));
  128. }
  129. }
  130. }
  131. void EvalMulFloatReference(TfLiteContext* context, TfLiteNode* node,
  132. TfLiteMulParams* params, const OpDataMul* data,
  133. const TfLiteEvalTensor* input1,
  134. const TfLiteEvalTensor* input2,
  135. TfLiteEvalTensor* output) {
  136. tflite::ArithmeticParams op_params = {};
  137. op_params.float_activation_min = data->output_activation_min_f32;
  138. op_params.float_activation_max = data->output_activation_max_f32;
  139. bool need_broadcast = reference_ops::ProcessBroadcastShapes(
  140. tflite::micro::GetTensorShape(input1),
  141. tflite::micro::GetTensorShape(input2), &op_params);
  142. if (need_broadcast) {
  143. reference_ops::BroadcastMul4DSlow(
  144. op_params, tflite::micro::GetTensorShape(input1),
  145. tflite::micro::GetTensorData<float>(input1),
  146. tflite::micro::GetTensorShape(input2),
  147. tflite::micro::GetTensorData<float>(input2),
  148. tflite::micro::GetTensorShape(output),
  149. tflite::micro::GetTensorData<float>(output));
  150. } else {
  151. reference_ops::Mul(op_params, tflite::micro::GetTensorShape(input1),
  152. tflite::micro::GetTensorData<float>(input1),
  153. tflite::micro::GetTensorShape(input2),
  154. tflite::micro::GetTensorData<float>(input2),
  155. tflite::micro::GetTensorShape(output),
  156. tflite::micro::GetTensorData<float>(output));
  157. }
  158. }
  159. } // namespace tflite