1 /* Copyright 2022 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_OPTIMIZED_INTEGER_OPS_LEAKY_RELU_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_OPTIMIZED_INTEGER_OPS_LEAKY_RELU_H_
17
18 #include <algorithm>
19
20 #include "tensorflow/lite/kernels/internal/common.h"
21 #include "tensorflow/lite/kernels/internal/optimized/avx2_quantization_utils.h"
22 #include "tensorflow/lite/kernels/internal/types.h"
23
24 namespace tflite {
25 namespace optimized_integer_ops {
26
QuantizeLeakyRelu(const LeakyReluParams & params,const RuntimeShape & input_shape,const int16 * input_data,const RuntimeShape & output_shape,int16 * output_data)27 inline void QuantizeLeakyRelu(const LeakyReluParams& params,
28 const RuntimeShape& input_shape,
29 const int16* input_data,
30 const RuntimeShape& output_shape,
31 int16* output_data) {
32 const int flat_size = MatchingFlatSize(input_shape, output_shape);
33 const int32_t quantized_min = std::numeric_limits<int16>::min();
34 const int32_t quantized_max = std::numeric_limits<int16>::max();
35 int i = 0;
36
37 #ifdef __AVX2__
38 const __m256i input_offset = _mm256_set1_epi32(params.input_offset);
39 const __m256i output_offset = _mm256_set1_epi32(params.output_offset);
40 const __m256i output_muliplier_identity =
41 _mm256_set1_epi32(params.output_multiplier_identity);
42 const __m256i output_shift_identity =
43 _mm256_set1_epi32(params.output_shift_identity);
44 const __m256i output_multiplier_alpha =
45 _mm256_set1_epi32(params.output_multiplier_alpha);
46 const __m256i output_shift_alpha =
47 _mm256_set1_epi32(params.output_shift_alpha);
48 const __m256i clamp_max_v = _mm256_set1_epi32(quantized_max);
49 const __m256i clamp_min_v = _mm256_set1_epi32(quantized_min);
50
51 for (; i <= flat_size - 16; i += 16) {
52 const __m256i input =
53 _mm256_loadu_si256(reinterpret_cast<__m256i const*>(input_data + i));
54 __m256i input_low = _mm256_cvtepi16_epi32(_mm256_castsi256_si128(input));
55 __m256i input_high =
56 _mm256_cvtepi16_epi32(_mm256_extracti128_si256(input, 1));
57 input_low = _mm256_sub_epi32(input_low, input_offset);
58 input_high = _mm256_sub_epi32(input_high, input_offset);
59
60 const __m256i zeros = _mm256_setzero_si256();
61 const __m256i input_low_mask = _mm256_cmpgt_epi32(input_low, zeros);
62 const __m256i input_high_mask = _mm256_cmpgt_epi32(input_high, zeros);
63 const __m256i input_low_output_multiplier = avx2_utils::mm256_blendv_epi32(
64 output_multiplier_alpha, output_muliplier_identity, input_low_mask);
65 const __m256i input_low_output_shift = avx2_utils::mm256_blendv_epi32(
66 output_shift_alpha, output_shift_identity, input_low_mask);
67 const __m256i input_high_output_multiplier = avx2_utils::mm256_blendv_epi32(
68 output_multiplier_alpha, output_muliplier_identity, input_high_mask);
69 const __m256i input_high_output_shift = avx2_utils::mm256_blendv_epi32(
70 output_shift_alpha, output_shift_identity, input_high_mask);
71
72 input_low = avx2_utils::MultiplyByQuantizedMultiplier(
73 input_low, input_low_output_multiplier, input_low_output_shift);
74 input_high = avx2_utils::MultiplyByQuantizedMultiplier(
75 input_high, input_high_output_multiplier, input_high_output_shift);
76
77 input_low = _mm256_add_epi32(input_low, output_offset);
78 input_high = _mm256_add_epi32(input_high, output_offset);
79
80 input_low = _mm256_min_epi32(input_low, clamp_max_v);
81 input_low = _mm256_max_epi32(input_low, clamp_min_v);
82 input_high = _mm256_min_epi32(input_high, clamp_max_v);
83 input_high = _mm256_max_epi32(input_high, clamp_min_v);
84
85 avx2_utils::CastInt32ToInt16AndStore(output_data + i, input_low);
86 avx2_utils::CastInt32ToInt16AndStore(output_data + i + 8, input_high);
87 }
88 #endif // __AVX2__
89
90 for (; i < flat_size; ++i) {
91 const int32_t input_value = input_data[i] - params.input_offset;
92 int32_t unclamped_output;
93 if (input_value >= 0) {
94 unclamped_output = params.output_offset +
95 MultiplyByQuantizedMultiplier(
96 input_value, params.output_multiplier_identity,
97 params.output_shift_identity);
98 } else {
99 unclamped_output = params.output_offset +
100 MultiplyByQuantizedMultiplier(
101 input_value, params.output_multiplier_alpha,
102 params.output_shift_alpha);
103 }
104 const int16 clamped_output =
105 std::min(quantized_max, std::max(quantized_min, unclamped_output));
106 output_data[i] = static_cast<int16>(clamped_output);
107 }
108 }
109
110 } // namespace optimized_integer_ops
111 } // namespace tflite
112
113 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_OPTIMIZED_INTEGER_OPS_LEAKY_RELU_H_
114