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