softmax_common.cc 6.0 KB

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