softmax.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. namespace tflite {
  21. namespace ops {
  22. namespace micro {
  23. namespace activations {
  24. namespace {
  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. if (input->type == kTfLiteUInt8) {
  32. TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteUInt8);
  33. TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0);
  34. } else {
  35. TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteInt8);
  36. if (output->type == kTfLiteInt16) {
  37. TF_LITE_ENSURE_EQ(context, output->params.zero_point, -32768);
  38. // NOTE: Current int16 softmax output does not require symmetric scaling
  39. // - so no need to verify scale here.
  40. } else {
  41. TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteInt8);
  42. TF_LITE_ENSURE_EQ(context, output->params.zero_point, -128);
  43. TF_LITE_ENSURE(context, output->params.scale == 1.f / 256);
  44. }
  45. }
  46. static const int kScaledDiffIntegerBits = 5;
  47. int input_left_shift;
  48. tflite::PreprocessSoftmaxScaling(
  49. static_cast<double>(params->beta),
  50. static_cast<double>(input->params.scale), kScaledDiffIntegerBits,
  51. &op_data->input_multiplier, &input_left_shift);
  52. op_data->input_left_shift = input_left_shift;
  53. op_data->diff_min =
  54. -1.0 * tflite::CalculateInputRadius(kScaledDiffIntegerBits,
  55. op_data->input_left_shift);
  56. } else {
  57. TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32);
  58. TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteFloat32);
  59. op_data->beta = static_cast<double>(params->beta);
  60. }
  61. return kTfLiteOk;
  62. }
  63. } // namespace
  64. TfLiteStatus SoftmaxPrepare(TfLiteContext* context, TfLiteNode* node) {
  65. TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
  66. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  67. const TfLiteTensor* input = GetInput(context, node, 0);
  68. TF_LITE_ENSURE(context, NumDimensions(input) >= 1);
  69. return kTfLiteOk;
  70. }
  71. // Takes a tensor and performs softmax along the last dimension.
  72. void SoftmaxFloat(const TfLiteTensor* input, TfLiteTensor* output,
  73. const SoftmaxParams& op_data) {
  74. tflite::reference_ops::Softmax(
  75. op_data, GetTensorShape(input), GetTensorData<float>(input),
  76. GetTensorShape(output), GetTensorData<float>(output));
  77. }
  78. void SoftmaxQuantized(const TfLiteTensor* input, TfLiteTensor* output,
  79. const SoftmaxParams& op_data) {
  80. if (input->type == kTfLiteUInt8) {
  81. tflite::reference_ops::Softmax(
  82. op_data, GetTensorShape(input), GetTensorData<uint8_t>(input),
  83. GetTensorShape(output), GetTensorData<uint8_t>(output));
  84. } else {
  85. if (output->type == kTfLiteInt16) {
  86. tflite::reference_ops::Softmax(
  87. op_data, GetTensorShape(input), GetTensorData<int8_t>(input),
  88. GetTensorShape(output), GetTensorData<int16_t>(output));
  89. } else {
  90. tflite::reference_ops::Softmax(
  91. op_data, GetTensorShape(input), GetTensorData<int8_t>(input),
  92. GetTensorShape(output), GetTensorData<int8_t>(output));
  93. }
  94. }
  95. }
  96. TfLiteStatus SoftmaxEval(TfLiteContext* context, TfLiteNode* node) {
  97. auto* params = static_cast<TfLiteSoftmaxParams*>(node->builtin_data);
  98. const TfLiteTensor* input = GetInput(context, node, 0);
  99. TfLiteTensor* output = GetOutput(context, node, 0);
  100. SoftmaxParams op_data;
  101. TF_LITE_ENSURE_STATUS(
  102. CalculateSoftmaxParams(context, input, output, params, &op_data));
  103. switch (input->type) {
  104. case kTfLiteFloat32: {
  105. SoftmaxFloat(input, output, op_data);
  106. return kTfLiteOk;
  107. }
  108. case kTfLiteInt8:
  109. case kTfLiteUInt8: {
  110. SoftmaxQuantized(input, output, op_data);
  111. return kTfLiteOk;
  112. }
  113. default:
  114. TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
  115. TfLiteTypeGetName(input->type), input->type);
  116. return kTfLiteError;
  117. }
  118. }
  119. } // namespace activations
  120. TfLiteRegistration* Register_SOFTMAX() {
  121. static TfLiteRegistration r = {/*init=*/nullptr,
  122. /*free=*/nullptr,
  123. /*prepare=*/activations::SoftmaxPrepare,
  124. /*invoke=*/activations::SoftmaxEval,
  125. /*profiling_string=*/nullptr,
  126. /*builtin_code=*/0,
  127. /*custom_name=*/nullptr,
  128. /*version=*/0};
  129. return &r;
  130. }
  131. } // namespace micro
  132. } // namespace ops
  133. } // namespace tflite