tanh.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Copyright 2019 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. #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_TANH_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_TANH_H_
  14. #include <limits>
  15. #include "fixedpoint/fixedpoint.h"
  16. #include "tensorflow/lite/kernels/internal/common.h"
  17. namespace tflite {
  18. namespace reference_integer_ops {
  19. inline void Tanh(int32_t input_zero_point, int32_t input_range_radius,
  20. int32_t input_multiplier, int32_t input_shift,
  21. int32_t input_size, const int8_t* input_data,
  22. int8_t* output_data) {
  23. // Integer bits must be in sync with Prepare() function.
  24. static constexpr int32_t kInputIntegerBits = 4;
  25. static constexpr int32_t kOutputScale = 7;
  26. static constexpr int32_t kMinInt8 = std::numeric_limits<int8_t>::min();
  27. static constexpr int32_t kMaxInt8 = std::numeric_limits<int8_t>::max();
  28. using F4 = gemmlowp::FixedPoint<int32_t, kInputIntegerBits>;
  29. for (int i = 0; i < input_size; ++i) {
  30. const int32_t input =
  31. static_cast<int32_t>(input_data[i]) - input_zero_point;
  32. if (input <= -input_range_radius) {
  33. output_data[i] = kMinInt8;
  34. } else if (input >= input_range_radius) {
  35. output_data[i] = kMaxInt8;
  36. } else {
  37. const int32_t input_in_q4 =
  38. MultiplyByQuantizedMultiplier(input, input_multiplier, input_shift);
  39. const int32_t output_in_q0 =
  40. gemmlowp::tanh(F4::FromRaw(input_in_q4)).raw();
  41. // Rescale and downcast.
  42. using gemmlowp::RoundingDivideByPOT;
  43. int32_t output_in_q24 =
  44. RoundingDivideByPOT(output_in_q0, 31 - kOutputScale);
  45. output_in_q24 = std::min(std::max(output_in_q24, kMinInt8), kMaxInt8);
  46. output_data[i] = static_cast<int8_t>(output_in_q24);
  47. }
  48. }
  49. }
  50. inline void Tanh(int32_t input_multiplier, int32_t input_left_shift,
  51. int32_t input_size, const int16_t* ptr_input_data,
  52. int16_t* ptr_output_data) {
  53. // We use the LUT for sigmoid and take into account, that
  54. // tanh(x) = 2*sigmoid(2*x) - 1
  55. int32_t input_data_mul = (input_multiplier > 0) ? input_multiplier : 1;
  56. for (int i = 0; i < input_size; ++i, ptr_input_data++, ptr_output_data++) {
  57. int32_t input_data = (*ptr_input_data) * input_data_mul;
  58. if (input_left_shift == 1) {
  59. input_data <<= 1;
  60. }
  61. // Scale by 3/4 to expand range [-8,8]->[-10.7,10.7].
  62. uint32_t abs_input_data = 3 * abs(input_data);
  63. uint32_t uh = abs_input_data >> 8;
  64. int32_t result;
  65. if (uh >= 255) {
  66. // Saturate to maximum.
  67. result = 0xFFFF << 8;
  68. } else {
  69. uint32_t ua = sigmoid_table_uint16[uh];
  70. uint32_t ub = sigmoid_table_uint16[uh + 1];
  71. uint8_t ut = abs_input_data & 0xFF;
  72. result = (ua << 8) + ut * (ub - ua);
  73. }
  74. result = (input_data >= 0)
  75. ? (result - (1 << (14 + 9)) + (1 << (9 - 2)))
  76. : (-result + (1 << (14 + 9)) + (1 << (9 - 2)) - 1);
  77. // Convert back to 16-bit.
  78. result >>= (9 - 1);
  79. *ptr_output_data = result;
  80. }
  81. }
  82. } // namespace reference_integer_ops
  83. } // namespace tflite
  84. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_TANH_H_