leaky_relu.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Copyright 2020 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/leaky_relu.h"
  13. #include "tensorflow/lite/c/common.h"
  14. #include "tensorflow/lite/kernels/internal/quantization_util.h"
  15. #include "tensorflow/lite/kernels/internal/reference/process_broadcast_shapes.h"
  16. #include "tensorflow/lite/kernels/internal/types.h"
  17. #include "tensorflow/lite/kernels/kernel_util.h"
  18. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  19. namespace tflite {
  20. namespace {
  21. // Input/output tensor index.
  22. constexpr int kInputTensor = 0;
  23. constexpr int kOutputTensor = 0;
  24. struct LeakyReluOpData {
  25. // quantization parameters
  26. int32_t output_multiplier_alpha;
  27. int32_t output_shift_alpha;
  28. int32_t output_multiplier_identity;
  29. int32_t output_shift_identity;
  30. int32_t input_zero_point;
  31. int32_t output_zero_point;
  32. };
  33. template <typename T>
  34. void QuantizeLeakyRelu(const LeakyReluOpData& data,
  35. const TfLiteEvalTensor* input,
  36. TfLiteEvalTensor* output) {
  37. LeakyReluParams op_params = {};
  38. op_params.input_offset = data.input_zero_point;
  39. op_params.output_offset = data.output_zero_point;
  40. op_params.output_multiplier_alpha = data.output_multiplier_alpha;
  41. op_params.output_shift_alpha = data.output_shift_alpha;
  42. op_params.output_multiplier_identity = data.output_multiplier_identity;
  43. op_params.output_shift_identity = data.output_shift_identity;
  44. reference_ops::QuantizeLeakyRelu(op_params,
  45. tflite::micro::GetTensorShape(input),
  46. tflite::micro::GetTensorData<T>(input),
  47. tflite::micro::GetTensorShape(output),
  48. tflite::micro::GetTensorData<T>(output));
  49. }
  50. TfLiteStatus CalculateOpData(TfLiteContext* context, TfLiteNode* node) {
  51. TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
  52. TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
  53. const TfLiteTensor* input;
  54. TF_LITE_ENSURE_OK(context, GetInputSafe(context, node, kInputTensor, &input));
  55. TfLiteTensor* output;
  56. TF_LITE_ENSURE_OK(context,
  57. GetOutputSafe(context, node, kOutputTensor, &output));
  58. TF_LITE_ENSURE_TYPES_EQ(context, input->type, output->type);
  59. if (output->type == kTfLiteInt8 || output->type == kTfLiteInt16) {
  60. LeakyReluOpData* data = static_cast<LeakyReluOpData*>(node->user_data);
  61. const auto* params =
  62. static_cast<TfLiteLeakyReluParams*>(node->builtin_data);
  63. data->input_zero_point = input->params.zero_point;
  64. data->output_zero_point = output->params.zero_point;
  65. int output_shift_alpha;
  66. double alpha_multiplier = static_cast<double>(
  67. input->params.scale * params->alpha / output->params.scale);
  68. QuantizeMultiplier(alpha_multiplier, &data->output_multiplier_alpha,
  69. &output_shift_alpha);
  70. data->output_shift_alpha = static_cast<int32_t>(output_shift_alpha);
  71. int output_shift_identity;
  72. double identity_multiplier =
  73. static_cast<double>(input->params.scale / output->params.scale);
  74. QuantizeMultiplier(identity_multiplier, &data->output_multiplier_identity,
  75. &output_shift_identity);
  76. data->output_shift_identity = static_cast<int32_t>(output_shift_identity);
  77. }
  78. return kTfLiteOk;
  79. }
  80. void* LeakyReluInit(TfLiteContext* context, const char* buffer, size_t length) {
  81. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  82. return context->AllocatePersistentBuffer(context, sizeof(LeakyReluOpData));
  83. }
  84. TfLiteStatus LeakyReluPrepare(TfLiteContext* context, TfLiteNode* node) {
  85. return CalculateOpData(context, node);
  86. }
  87. TfLiteStatus LeakyReluEval(TfLiteContext* context, TfLiteNode* node) {
  88. const TfLiteEvalTensor* input =
  89. tflite::micro::GetEvalInput(context, node, kInputTensor);
  90. TfLiteEvalTensor* output =
  91. tflite::micro::GetEvalOutput(context, node, kOutputTensor);
  92. const LeakyReluOpData& data = *static_cast<LeakyReluOpData*>(node->user_data);
  93. switch (input->type) {
  94. case kTfLiteFloat32: {
  95. LeakyReluParams op_params = {};
  96. const auto* params =
  97. static_cast<TfLiteLeakyReluParams*>(node->builtin_data);
  98. op_params.alpha = params->alpha;
  99. reference_ops::LeakyRelu(op_params, tflite::micro::GetTensorShape(input),
  100. tflite::micro::GetTensorData<float>(input),
  101. tflite::micro::GetTensorShape(output),
  102. tflite::micro::GetTensorData<float>(output));
  103. return kTfLiteOk;
  104. } break;
  105. case kTfLiteInt8: {
  106. QuantizeLeakyRelu<int8_t>(data, input, output);
  107. return kTfLiteOk;
  108. } break;
  109. case kTfLiteInt16: {
  110. QuantizeLeakyRelu<int16_t>(data, input, output);
  111. return kTfLiteOk;
  112. } break;
  113. default:
  114. TF_LITE_KERNEL_LOG(
  115. context, "Only float32, int8 are supported by LEAKY_RELU, got %s.",
  116. TfLiteTypeGetName(input->type));
  117. return kTfLiteError;
  118. }
  119. return kTfLiteError;
  120. }
  121. } // namespace
  122. TfLiteRegistration Register_LEAKY_RELU() {
  123. return {/*init=*/LeakyReluInit,
  124. /*free=*/nullptr,
  125. /*prepare=*/LeakyReluPrepare,
  126. /*invoke=*/LeakyReluEval,
  127. /*profiling_string=*/nullptr,
  128. /*builtin_code=*/0,
  129. /*custom_name=*/nullptr,
  130. /*version=*/0};
  131. }
  132. } // namespace tflite