softmax_common.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Copyright 2022 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/builtin_op_data.h"
  13. #include "tensorflow/lite/c/common.h"
  14. #include "tensorflow/lite/kernels/internal/common.h"
  15. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  16. #include "tensorflow/lite/kernels/kernel_util.h"
  17. #include "tensorflow/lite/kernels/op_macros.h"
  18. #include "tensorflow/lite/micro/kernels/softmax.h"
  19. #include "tensorflow/lite/micro/micro_context.h"
  20. namespace tflite {
  21. namespace {
  22. // Softmax parameter data that persists in user_data
  23. const int kInt16LUTArraySize = 513;
  24. TfLiteStatus InitializeLutForInt16(TfLiteContext* context,
  25. const TfLiteTensor* input,
  26. TfLiteTensor* output,
  27. SoftmaxParams* op_data) {
  28. // Only allocate LUTs for KTfLiteInt16 data type
  29. if (input->type == kTfLiteInt16) {
  30. void* raw_exp_lut = context->AllocatePersistentBuffer(
  31. context, sizeof(int16_t) * kInt16LUTArraySize);
  32. TF_LITE_ENSURE(context, raw_exp_lut != nullptr);
  33. op_data->exp_lut = reinterpret_cast<int16_t*>(raw_exp_lut);
  34. void* one_over_one_plus_x_lut = context->AllocatePersistentBuffer(
  35. context, sizeof(int16_t) * kInt16LUTArraySize);
  36. TF_LITE_ENSURE(context, one_over_one_plus_x_lut != nullptr);
  37. op_data->one_over_one_plus_x_lut =
  38. reinterpret_cast<int16_t*>(one_over_one_plus_x_lut);
  39. }
  40. if (output->type == kTfLiteInt16) {
  41. TF_LITE_ENSURE(context,
  42. input->type == kTfLiteInt8 || input->type == kTfLiteInt16);
  43. } else {
  44. TF_LITE_ENSURE_EQ(context, input->type, output->type);
  45. }
  46. // Populate LUT if required
  47. if (input->type == kTfLiteInt16) {
  48. TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0);
  49. // exp LUT only used on negative values
  50. // we consider exp(-10.0) is insignificant to accumulation
  51. gen_lut<float, int16_t, int16_t>(
  52. [](float value) { return std::exp(value); }, -10.0f, 0.0f, -1.0f, 1.0f,
  53. op_data->exp_lut);
  54. gen_lut<float, int16_t, int16_t>(
  55. [](float value) { return 1.0f / (1.0f + value); }, 0.0f, 1.0f, -1.0f,
  56. 1.0f, op_data->one_over_one_plus_x_lut);
  57. op_data->zero_point = output->params.zero_point;
  58. op_data->scale = output->params.scale;
  59. }
  60. return kTfLiteOk;
  61. }
  62. } // namespace
  63. TfLiteStatus CalculateSoftmaxParams(TfLiteContext* context,
  64. const TfLiteTensor* input,
  65. TfLiteTensor* output,
  66. const TfLiteSoftmaxParams* params,
  67. SoftmaxParams* op_data) {
  68. if (InitializeLutForInt16(context, input, output, op_data) != kTfLiteOk) {
  69. return kTfLiteError;
  70. }
  71. if (input->type == kTfLiteInt8 || input->type == kTfLiteInt16) {
  72. if (input->type == kTfLiteInt16) {
  73. TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0);
  74. TF_LITE_ENSURE_NEAR(context, output->params.scale, 1.f / 32768,
  75. (0.001f * 1.f / 32768));
  76. } else { // input->type == kTfLiteInt8
  77. TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteInt8);
  78. if (output->type == kTfLiteInt16) {
  79. TF_LITE_ENSURE_EQ(context, output->params.zero_point, -32768);
  80. TF_LITE_ENSURE_NEAR(context, output->params.scale, 1.f / 65536,
  81. (0.001f * 1.f / 65536));
  82. } else { // output->type == kTfLiteint8
  83. TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteInt8);
  84. TF_LITE_ENSURE_EQ(context, output->params.zero_point, -128);
  85. TF_LITE_ENSURE(context, output->params.scale == 1.f / 256);
  86. }
  87. }
  88. static const int kScaledDiffIntegerBits = 5;
  89. // Calculate input_multiplier and input_left_shift
  90. if (input->type == kTfLiteInt16) {
  91. int input_left_shift;
  92. double input_scale_beta_rescale =
  93. static_cast<double>(input->params.scale) *
  94. static_cast<double>(params->beta) /
  95. (10.0 / 65535.0); // scale the input_diff such that [-65535, 0]
  96. // correspond to [-10.0, 0.0]
  97. QuantizeMultiplier(input_scale_beta_rescale, &op_data->input_multiplier,
  98. &input_left_shift);
  99. op_data->input_left_shift = input_left_shift;
  100. } else {
  101. int input_left_shift;
  102. tflite::PreprocessSoftmaxScaling(
  103. static_cast<double>(params->beta),
  104. static_cast<double>(input->params.scale), kScaledDiffIntegerBits,
  105. &op_data->input_multiplier, &input_left_shift);
  106. op_data->input_left_shift = input_left_shift;
  107. op_data->diff_min =
  108. -1.0 * tflite::CalculateInputRadius(kScaledDiffIntegerBits,
  109. op_data->input_left_shift);
  110. }
  111. } else {
  112. TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32);
  113. TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteFloat32);
  114. op_data->beta = static_cast<double>(params->beta);
  115. }
  116. return kTfLiteOk;
  117. }
  118. void* SoftmaxInit(TfLiteContext* context, const char* buffer, size_t length) {
  119. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  120. return context->AllocatePersistentBuffer(context, sizeof(SoftmaxParams));
  121. }
  122. TfLiteStatus SoftmaxPrepare(TfLiteContext* context, TfLiteNode* node) {
  123. MicroContext* micro_context = GetMicroContext(context);
  124. TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
  125. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  126. TfLiteTensor* input = micro_context->AllocateTempInputTensor(node, 0);
  127. TF_LITE_ENSURE(context, input != nullptr);
  128. TF_LITE_ENSURE(context, NumDimensions(input) >= 1);
  129. TfLiteTensor* output = micro_context->AllocateTempOutputTensor(node, 0);
  130. TF_LITE_ENSURE(context, output != nullptr);
  131. TF_LITE_ENSURE(context, node->user_data != nullptr);
  132. SoftmaxParams* op_data = static_cast<SoftmaxParams*>(node->user_data);
  133. auto* params = static_cast<TfLiteSoftmaxParams*>(node->builtin_data);
  134. auto ret_val =
  135. CalculateSoftmaxParams(context, input, output, params, op_data);
  136. micro_context->DeallocateTempTfLiteTensor(input);
  137. micro_context->DeallocateTempTfLiteTensor(output);
  138. return ret_val;
  139. }
  140. } // namespace tflite