xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/internal/reference/integer_ops/logistic.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_LOGISTIC_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_LOGISTIC_H_
17 
18 #include <algorithm>
19 #include <limits>
20 
21 #include "tensorflow/lite/kernels/internal/common.h"
22 
23 namespace tflite {
24 namespace reference_integer_ops {
25 
Logistic(int32_t input_zero_point,int32_t input_range_radius,int32_t input_multiplier,int32_t input_left_shift,int32_t input_size,const int8_t * input_data,int8_t * output_data)26 inline void Logistic(int32_t input_zero_point, int32_t input_range_radius,
27                      int32_t input_multiplier, int32_t input_left_shift,
28                      int32_t input_size, const int8_t* input_data,
29                      int8_t* output_data) {
30   // Integer bits must be in sync with Prepare() function.
31   static constexpr int32_t kInputIntegerBits = 4;
32   static constexpr int32_t kOutputIntegerBits = 8;
33   static constexpr int8_t kMinInt8 = std::numeric_limits<int8_t>::min();
34   static constexpr int8_t kMaxInt8 = std::numeric_limits<int8_t>::max();
35   static constexpr int32_t kOutputZeroPoint = -128;
36 
37   for (int i = 0; i < input_size; ++i) {
38     const int32_t input =
39         static_cast<int32_t>(input_data[i]) - input_zero_point;
40     if (input <= -input_range_radius) {
41       output_data[i] = kMinInt8;
42     } else if (input >= input_range_radius) {
43       output_data[i] = kMaxInt8;
44     } else {
45       const int32_t input_in_q4 = MultiplyByQuantizedMultiplier(
46           input, input_multiplier, input_left_shift);
47       using FixedPoint4 = gemmlowp::FixedPoint<int32_t, kInputIntegerBits>;
48       const int32_t output_in_q0 =
49           gemmlowp::logistic(FixedPoint4::FromRaw(input_in_q4)).raw();
50 
51       // Rescale and downcast.
52       using gemmlowp::RoundingDivideByPOT;
53       int32_t output_in_q23 =
54           RoundingDivideByPOT(output_in_q0, 31 - kOutputIntegerBits);
55       output_in_q23 = std::min(std::max(output_in_q23 + kOutputZeroPoint,
56                                         static_cast<int32_t>(kMinInt8)),
57                                static_cast<int32_t>(kMaxInt8));
58       output_data[i] = static_cast<int8_t>(output_in_q23);
59     }
60   }
61 }
62 
Logistic(int32_t input_multiplier,int32_t input_left_shift,int32_t input_size,const int16_t * ptr_input_data,int16_t * ptr_output_data)63 inline void Logistic(int32_t input_multiplier, int32_t input_left_shift,
64                      int32_t input_size, const int16_t* ptr_input_data,
65                      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   TFLITE_DCHECK_GE(input_left_shift, 0);
75   if (input_multiplier == 0) {  // power of two case
76     input_multiplier = 3 << input_left_shift;
77     input_left_shift = 0;
78   }
79 
80   int32_t round = (input_left_shift > 0) ? 1 << (input_left_shift - 1) : 0;
81 
82   for (int i = 0; i < input_size; ++i, ptr_input_data++, ptr_output_data++) {
83     int32_t input_data =
84         ((*ptr_input_data) * input_multiplier + round) >> input_left_shift;
85 
86     // We do interpolation on unsigned values.
87     uint32_t abs_input_data = abs(input_data);
88 
89     // We divide by 2 power of 9, because
90     // we need to divide by 2 in power of 7 for
91     // the input conversion + 1/4 from the scale above.
92 
93     // Define uh as uint32_t type not to make this function overflow.
94     uint32_t uh = abs_input_data >> 9;
95     uint32_t result;
96 
97     if (uh >= 255) {
98       // Saturate to maximum.
99       result = 0x7FFF << 10;
100     } else {
101       uint32_t ua = sigmoid_table_uint16[uh];
102       uint32_t ub = sigmoid_table_uint16[uh + 1];
103       uint32_t ut = abs_input_data & 0x1ff;
104       // Interpolation is done using the fractional bit.
105       result = (ua << 9) + ut * (ub - ua);
106     }
107 
108     result = (input_data >= 0) ? (result + (1 << 9))
109                                : ((1 << (16 + 9)) - result + (1 << 9) - 1);
110 
111     // Back to 16-bit.
112     result >>= 10;
113 
114     *ptr_output_data = result;
115   }
116 }
117 
118 }  // namespace reference_integer_ops
119 }  // namespace tflite
120 
121 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_LOGISTIC_H_
122