softmax.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Copyright 2018 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/softmax.h"
  13. #include "tensorflow/lite/c/builtin_op_data.h"
  14. #include "tensorflow/lite/c/common.h"
  15. #include "tensorflow/lite/kernels/internal/common.h"
  16. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  17. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  18. #include "tensorflow/lite/kernels/kernel_util.h"
  19. #include "tensorflow/lite/kernels/op_macros.h"
  20. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  21. namespace tflite {
  22. namespace {
  23. // Softmax parameter data that persists in user_data
  24. static constexpr int kInt16LUTArraySize = 513;
  25. TfLiteStatus CalculateSoftmaxParams(TfLiteContext* context,
  26. const TfLiteTensor* input,
  27. TfLiteTensor* output,
  28. const TfLiteSoftmaxParams* params,
  29. SoftmaxParams* op_data) {
  30. if (input->type == kTfLiteUInt8 || input->type == kTfLiteInt8 ||
  31. input->type == kTfLiteInt16) {
  32. if (input->type == kTfLiteUInt8) {
  33. TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteUInt8);
  34. TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0);
  35. } else if (input->type == kTfLiteInt16) {
  36. TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0);
  37. TF_LITE_ENSURE_NEAR(context, output->params.scale, 1.f / 32768,
  38. (0.001f * 1.f / 32768));
  39. } else { // input->type == kTfLiteInt8
  40. TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteInt8);
  41. if (output->type == kTfLiteInt16) {
  42. TF_LITE_ENSURE_EQ(context, output->params.zero_point, -32768);
  43. TF_LITE_ENSURE_NEAR(context, output->params.scale, 1.f / 65536,
  44. (0.001f * 1.f / 65536));
  45. } else { // output->type == kTfLiteint8
  46. TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteInt8);
  47. TF_LITE_ENSURE_EQ(context, output->params.zero_point, -128);
  48. TF_LITE_ENSURE(context, output->params.scale == 1.f / 256);
  49. }
  50. }
  51. static const int kScaledDiffIntegerBits = 5;
  52. // Calculate input_multiplier and input_left_shift
  53. if (input->type == kTfLiteInt16) {
  54. int input_left_shift;
  55. double input_scale_beta_rescale =
  56. static_cast<double>(input->params.scale) *
  57. static_cast<double>(params->beta) /
  58. (10.0 / 65535.0); // scale the input_diff such that [-65535, 0]
  59. // correspond to [-10.0, 0.0]
  60. QuantizeMultiplier(input_scale_beta_rescale, &op_data->input_multiplier,
  61. &input_left_shift);
  62. op_data->input_left_shift = input_left_shift;
  63. } else {
  64. int input_left_shift;
  65. tflite::PreprocessSoftmaxScaling(
  66. static_cast<double>(params->beta),
  67. static_cast<double>(input->params.scale), kScaledDiffIntegerBits,
  68. &op_data->input_multiplier, &input_left_shift);
  69. op_data->input_left_shift = input_left_shift;
  70. op_data->diff_min =
  71. -1.0 * tflite::CalculateInputRadius(kScaledDiffIntegerBits,
  72. op_data->input_left_shift);
  73. }
  74. } else {
  75. TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32);
  76. TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteFloat32);
  77. op_data->beta = static_cast<double>(params->beta);
  78. }
  79. return kTfLiteOk;
  80. }
  81. // Takes a tensor and performs softmax along the last dimension.
  82. void SoftmaxFloat(const TfLiteEvalTensor* input, TfLiteEvalTensor* output,
  83. const SoftmaxParams& op_data) {
  84. tflite::reference_ops::Softmax(op_data, tflite::micro::GetTensorShape(input),
  85. tflite::micro::GetTensorData<float>(input),
  86. tflite::micro::GetTensorShape(output),
  87. tflite::micro::GetTensorData<float>(output));
  88. }
  89. void SoftmaxQuantized(const TfLiteEvalTensor* input, TfLiteEvalTensor* output,
  90. const SoftmaxParams& op_data) {
  91. if (input->type == kTfLiteUInt8) {
  92. tflite::reference_ops::Softmax(
  93. op_data, tflite::micro::GetTensorShape(input),
  94. tflite::micro::GetTensorData<uint8_t>(input),
  95. tflite::micro::GetTensorShape(output),
  96. tflite::micro::GetTensorData<uint8_t>(output));
  97. } else if (input->type == kTfLiteInt8) {
  98. if (output->type == kTfLiteInt16) {
  99. tflite::reference_ops::Softmax(
  100. op_data, tflite::micro::GetTensorShape(input),
  101. tflite::micro::GetTensorData<int8_t>(input),
  102. tflite::micro::GetTensorShape(output),
  103. tflite::micro::GetTensorData<int16_t>(output));
  104. } else {
  105. tflite::reference_ops::Softmax(
  106. op_data, tflite::micro::GetTensorShape(input),
  107. tflite::micro::GetTensorData<int8_t>(input),
  108. tflite::micro::GetTensorShape(output),
  109. tflite::micro::GetTensorData<int8_t>(output));
  110. }
  111. } else {
  112. tflite::reference_ops::SoftmaxInt16(
  113. op_data, tflite::micro::GetTensorShape(input),
  114. tflite::micro::GetTensorData<int16_t>(input),
  115. tflite::micro::GetTensorShape(output),
  116. tflite::micro::GetTensorData<int16_t>(output));
  117. }
  118. }
  119. void* SoftmaxInit(TfLiteContext* context, const char* buffer, size_t length) {
  120. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  121. return context->AllocatePersistentBuffer(context, sizeof(SoftmaxParams));
  122. }
  123. TfLiteStatus SoftmaxPrepare(TfLiteContext* context, TfLiteNode* node) {
  124. TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
  125. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  126. const TfLiteTensor* input = GetInput(context, node, 0);
  127. TF_LITE_ENSURE(context, input != nullptr);
  128. TF_LITE_ENSURE(context, NumDimensions(input) >= 1);
  129. TfLiteTensor* output = GetOutput(context, 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. // Only allocate LUTs for KTfLiteInt16 data type
  134. if (input->type == kTfLiteInt16) {
  135. void* raw_exp_lut = context->AllocatePersistentBuffer(
  136. context, sizeof(int16_t) * kInt16LUTArraySize);
  137. TF_LITE_ENSURE(context, raw_exp_lut != nullptr);
  138. op_data->exp_lut = reinterpret_cast<int16_t*>(raw_exp_lut);
  139. void* one_over_one_plus_x_lut = context->AllocatePersistentBuffer(
  140. context, sizeof(int16_t) * kInt16LUTArraySize);
  141. TF_LITE_ENSURE(context, one_over_one_plus_x_lut != nullptr);
  142. op_data->one_over_one_plus_x_lut =
  143. reinterpret_cast<int16_t*>(one_over_one_plus_x_lut);
  144. }
  145. if (output->type == kTfLiteInt16) {
  146. TF_LITE_ENSURE(context, input->type == kTfLiteInt8 ||
  147. input->type == kTfLiteUInt8 ||
  148. input->type == kTfLiteInt16);
  149. } else {
  150. TF_LITE_ENSURE_EQ(context, input->type, output->type);
  151. }
  152. // Populate LUT if required
  153. if (input->type == kTfLiteInt16) {
  154. TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0);
  155. // exp LUT only used on negative values
  156. // we consider exp(-10.0) is insignificant to accumulation
  157. gen_lut([](float value) { return std::exp(value); }, -10.0f, 0.0f,
  158. op_data->exp_lut, kInt16LUTArraySize);
  159. gen_lut([](float value) { return 1.0f / (1.0f + value); }, 0.0f, 1.0f,
  160. op_data->one_over_one_plus_x_lut, kInt16LUTArraySize);
  161. op_data->zero_point = output->params.zero_point;
  162. op_data->scale = output->params.scale;
  163. }
  164. auto* params = static_cast<TfLiteSoftmaxParams*>(node->builtin_data);
  165. return CalculateSoftmaxParams(context, input, output, params, op_data);
  166. }
  167. TfLiteStatus SoftmaxEval(TfLiteContext* context, TfLiteNode* node) {
  168. const TfLiteEvalTensor* input = tflite::micro::GetEvalInput(context, node, 0);
  169. TfLiteEvalTensor* output = tflite::micro::GetEvalOutput(context, node, 0);
  170. TFLITE_DCHECK(node->user_data != nullptr);
  171. SoftmaxParams op_data = *static_cast<SoftmaxParams*>(node->user_data);
  172. switch (input->type) {
  173. case kTfLiteFloat32: {
  174. SoftmaxFloat(input, output, op_data);
  175. return kTfLiteOk;
  176. }
  177. case kTfLiteInt8:
  178. case kTfLiteUInt8:
  179. case kTfLiteInt16: {
  180. SoftmaxQuantized(input, output, op_data);
  181. return kTfLiteOk;
  182. }
  183. default:
  184. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  185. TfLiteTypeGetName(input->type), input->type);
  186. return kTfLiteError;
  187. }
  188. }
  189. } // namespace
  190. TfLiteRegistration Register_SOFTMAX() {
  191. return {/*init=*/SoftmaxInit,
  192. /*free=*/nullptr,
  193. /*prepare=*/SoftmaxPrepare,
  194. /*invoke=*/SoftmaxEval,
  195. /*profiling_string=*/nullptr,
  196. /*builtin_code=*/0,
  197. /*custom_name=*/nullptr,
  198. /*version=*/0};
  199. }
  200. } // namespace tflite