log_softmax.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/kernels/internal/reference/log_softmax.h"
  13. #include <cstddef>
  14. #include <cstdint>
  15. #include "tensorflow/lite/c/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/internal/types.h"
  19. #include "tensorflow/lite/kernels/kernel_util.h"
  20. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  21. namespace tflite {
  22. namespace {
  23. // used only with quantized data
  24. struct LogSoftmaxOpData {
  25. int32_t input_multiplier;
  26. int32_t input_left_shift;
  27. int32_t reverse_scaling_divisor;
  28. int32_t reverse_scaling_right_shift;
  29. int diff_min;
  30. size_t outer_size; // number of tensor elements skipping computation axis
  31. size_t depth; // number of tensor elements on computation axis
  32. };
  33. // input/output tensor index
  34. constexpr int kInputTensor = 0;
  35. constexpr int kOutputTensor = 0;
  36. TfLiteStatus CalculateOpData(TfLiteContext* context, TfLiteNode* node) {
  37. TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
  38. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  39. const TfLiteTensor* input;
  40. TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputTensor, &input));
  41. TfLiteTensor* output;
  42. TF_LITE_ENSURE_OK(context,
  43. GetOutputSafe(context, node, kOutputTensor, &output));
  44. TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type);
  45. TF_LITE_ENSURE(context, HaveSameShapes(input, output));
  46. if (input->type == kTfLiteInt8) {
  47. node->user_data =
  48. context->AllocatePersistentBuffer(context, sizeof(LogSoftmaxOpData));
  49. auto data = static_cast<LogSoftmaxOpData*>(node->user_data);
  50. // quantization datum
  51. constexpr int32_t kOutputZeroPoint = 127;
  52. constexpr float kOutputScale = 16.0 / 256;
  53. constexpr double kBeta = 1.0;
  54. constexpr int kScaledDiffIntegerBits = 5;
  55. TF_LITE_ENSURE(context, output->params.scale == kOutputScale);
  56. TF_LITE_ENSURE(context, output->params.zero_point == kOutputZeroPoint);
  57. int input_left_shift;
  58. int reverse_scaling_right_shift;
  59. tflite::PreprocessLogSoftmaxScalingExp(
  60. kBeta, static_cast<double>(input->params.scale), kScaledDiffIntegerBits,
  61. &data->input_multiplier, &input_left_shift,
  62. &data->reverse_scaling_divisor, &reverse_scaling_right_shift);
  63. data->input_left_shift = static_cast<int32_t>(input_left_shift);
  64. data->reverse_scaling_right_shift =
  65. static_cast<int32_t>(-reverse_scaling_right_shift);
  66. // diff_min has a negative value, and is used to limit the maximum magnitude
  67. // of the diffs, which are <= 0.
  68. data->diff_min =
  69. -tflite::CalculateInputRadius(kScaledDiffIntegerBits, input_left_shift);
  70. RuntimeShape input_shape = GetTensorShape(input);
  71. const int trailing_dim = input_shape.DimensionsCount() - 1;
  72. data->outer_size =
  73. static_cast<size_t>(FlatSizeSkipDim(input_shape, trailing_dim));
  74. data->depth = static_cast<size_t>(input_shape.Dims(trailing_dim));
  75. }
  76. return kTfLiteOk;
  77. }
  78. TfLiteStatus LogSoftmaxPrepare(TfLiteContext* context, TfLiteNode* node) {
  79. return CalculateOpData(context, node);
  80. }
  81. TfLiteStatus LogSoftmaxEval(TfLiteContext* context, TfLiteNode* node) {
  82. const LogSoftmaxOpData* data =
  83. static_cast<LogSoftmaxOpData*>(node->user_data);
  84. const TfLiteEvalTensor* input =
  85. tflite::micro::GetEvalInput(context, node, kInputTensor);
  86. TfLiteEvalTensor* output =
  87. tflite::micro::GetEvalOutput(context, node, kOutputTensor);
  88. switch (input->type) {
  89. case kTfLiteFloat32: {
  90. SoftmaxParams op_params = {};
  91. reference_ops::LogSoftmax(op_params, tflite::micro::GetTensorShape(input),
  92. tflite::micro::GetTensorData<float>(input),
  93. tflite::micro::GetTensorShape(output),
  94. tflite::micro::GetTensorData<float>(output));
  95. return kTfLiteOk;
  96. }
  97. case kTfLiteInt8: {
  98. SoftmaxParams op_params = {};
  99. op_params.input_multiplier = data->input_multiplier;
  100. op_params.input_left_shift = data->input_left_shift;
  101. op_params.reverse_scaling_divisor = data->reverse_scaling_divisor;
  102. op_params.reverse_scaling_right_shift = data->reverse_scaling_right_shift;
  103. op_params.diff_min = data->diff_min;
  104. reference_ops::LogSoftmax(op_params, data->outer_size, data->depth,
  105. tflite::micro::GetTensorShape(input),
  106. tflite::micro::GetTensorData<int8_t>(input),
  107. tflite::micro::GetTensorShape(output),
  108. tflite::micro::GetTensorData<int8_t>(output));
  109. return kTfLiteOk;
  110. }
  111. default:
  112. TF_LITE_KERNEL_LOG(context,
  113. "LOG_SOFTMAX only supports float32, int8, got %s.",
  114. TfLiteTypeGetName(input->type));
  115. return kTfLiteError;
  116. }
  117. }
  118. } // namespace
  119. TfLiteRegistration Register_LOG_SOFTMAX() {
  120. return {/*init=*/nullptr,
  121. /*free=*/nullptr,
  122. /*prepare=*/LogSoftmaxPrepare,
  123. /*invoke=*/LogSoftmaxEval,
  124. /*profiling_string=*/nullptr,
  125. /*builtin_code=*/0,
  126. /*custom_name=*/nullptr,
  127. /*version=*/0};
  128. }
  129. } // namespace tflite