xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/internal/reference/integer_ops/tanh.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_TANH_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_TANH_H_
17 
18 #include <algorithm>
19 #include <limits>
20 
21 #include "fixedpoint/fixedpoint.h"
22 #include "tensorflow/lite/kernels/internal/common.h"
23 
24 namespace tflite {
25 namespace reference_integer_ops {
26 
Tanh(int32_t input_zero_point,int32_t input_range_radius,int32_t input_multiplier,int32_t input_shift,const RuntimeShape & input_shape,const int8_t * input_data,const RuntimeShape & output_shape,int8_t * output_data)27 inline void Tanh(int32_t input_zero_point, int32_t input_range_radius,
28                  int32_t input_multiplier, int32_t input_shift,
29                  const RuntimeShape& input_shape, const int8_t* input_data,
30                  const RuntimeShape& output_shape, int8_t* output_data) {
31   // Integer bits must be in sync with Prepare() function.
32   static constexpr int32_t kInputIntegerBits = 4;
33   static constexpr int32_t kOutputScale = 7;
34   static constexpr int32_t kMinInt8 = std::numeric_limits<int8_t>::min();
35   static constexpr int32_t kMaxInt8 = std::numeric_limits<int8_t>::max();
36   using F4 = gemmlowp::FixedPoint<int32_t, kInputIntegerBits>;
37 
38   const int flat_size = MatchingFlatSize(input_shape, output_shape);
39 
40   for (int i = 0; i < flat_size; ++i) {
41     const int32_t input =
42         static_cast<int32_t>(input_data[i]) - input_zero_point;
43     if (input <= -input_range_radius) {
44       output_data[i] = kMinInt8;
45     } else if (input >= input_range_radius) {
46       output_data[i] = kMaxInt8;
47     } else {
48       const int32_t input_in_q4 =
49           MultiplyByQuantizedMultiplier(input, input_multiplier, input_shift);
50       const int32_t output_in_q0 =
51           gemmlowp::tanh(F4::FromRaw(input_in_q4)).raw();
52 
53       // Rescale and downcast.
54       using gemmlowp::RoundingDivideByPOT;
55       int32_t output_in_q24 =
56           RoundingDivideByPOT(output_in_q0, 31 - kOutputScale);
57       output_in_q24 = std::min(std::max(output_in_q24, kMinInt8), kMaxInt8);
58       output_data[i] = static_cast<int8_t>(output_in_q24);
59     }
60   }
61 }
62 
Tanh(int32_t input_multiplier,int32_t input_left_shift,const RuntimeShape & input_shape,const int16_t * ptr_input_data,const RuntimeShape & output_shape,int16_t * ptr_output_data)63 inline void Tanh(int32_t input_multiplier, int32_t input_left_shift,
64                  const RuntimeShape& input_shape, const int16_t* ptr_input_data,
65                  const RuntimeShape& output_shape, int16_t* ptr_output_data) {
66   // We use the LUT for sigmoid and take into account, that
67   // tanh(x) = 2*sigmoid(2*x) - 1
68 
69   // We scale by 3/4 to expand range [-8,8]->[-10.7,10.7].
70   // In case of general parameter scale, multiplier 3 is taken into account
71   // in TanhPrepare function and it is included in
72   // input_multiplier already.
73 
74   if (input_multiplier == 0) {  // power of two case
75     input_multiplier = 3 << input_left_shift;
76     input_left_shift = 0;
77   }
78 
79   int32_t round = (input_left_shift > 0) ? 1 << (input_left_shift - 1) : 0;
80 
81   int flat_size = MatchingFlatSize(input_shape, output_shape);
82 
83   for (int i = 0; i < flat_size; ++i, ptr_input_data++, ptr_output_data++) {
84     int32_t input_data =
85         ((*ptr_input_data) * input_multiplier + round) >> input_left_shift;
86 
87     uint32_t abs_input_data = abs(input_data);
88     uint32_t uh = abs_input_data >> 8;
89     int32_t result;
90 
91     if (uh >= 255) {
92       // Saturate to maximum.
93       result = 0xFFFF << 8;
94     } else {
95       uint32_t ua = sigmoid_table_uint16[uh];
96       uint32_t ub = sigmoid_table_uint16[uh + 1];
97 
98       uint8_t ut = abs_input_data & 0xFF;
99 
100       result = (ua << 8) + ut * (ub - ua);
101     }
102 
103     result = (input_data >= 0)
104                  ? (result - (1 << (14 + 9)) + (1 << (9 - 2)))
105                  : (-result + (1 << (14 + 9)) + (1 << (9 - 2)) - 1);
106 
107     // Convert back to 16-bit.
108     result >>= (9 - 1);
109 
110     *ptr_output_data = result;
111   }
112 }
113 
114 }  // namespace reference_integer_ops
115 }  // namespace tflite
116 
117 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_TANH_H_
118