1 /* Copyright 2020 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_LEAKY_RELU_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LEAKY_RELU_H_
17 
18 #include <algorithm>
19 #include <limits>
20 
21 #include "tensorflow/lite/kernels/internal/common.h"
22 
23 namespace tflite {
24 namespace reference_ops {
25 
LeakyRelu(const tflite::LeakyReluParams & params,const RuntimeShape & input_shape,const float * input_data,const RuntimeShape & output_shape,float * output_data)26 inline void LeakyRelu(const tflite::LeakyReluParams& params,
27                       const RuntimeShape& input_shape, const float* input_data,
28                       const RuntimeShape& output_shape, float* output_data) {
29   const int flat_size = MatchingFlatSize(input_shape, output_shape);
30   for (int i = 0; i < flat_size; ++i) {
31     const float val = input_data[i];
32     // Note that alpha might be > 1 or < 0, so we don't use std::max here.
33     output_data[i] = val > 0 ? val : val * params.alpha;
34   }
35 }
36 
37 template <typename T>
QuantizeLeakyRelu(const LeakyReluParams & params,const RuntimeShape & input_shape,const T * input_data,const RuntimeShape & output_shape,T * output_data)38 inline void QuantizeLeakyRelu(const LeakyReluParams& params,
39                               const RuntimeShape& input_shape,
40                               const T* input_data,
41                               const RuntimeShape& output_shape,
42                               T* output_data) {
43   const int flat_size = MatchingFlatSize(input_shape, output_shape);
44   static const int32_t quantized_min = std::numeric_limits<T>::min();
45   static const int32_t quantized_max = std::numeric_limits<T>::max();
46   for (int i = 0; i < flat_size; ++i) {
47     const int32_t input_value = input_data[i] - params.input_offset;
48     int32_t unclamped_output;
49     if (input_value >= 0) {
50       unclamped_output = params.output_offset +
51                          MultiplyByQuantizedMultiplier(
52                              input_value, params.output_multiplier_identity,
53                              params.output_shift_identity);
54     } else {
55       unclamped_output = params.output_offset +
56                          MultiplyByQuantizedMultiplier(
57                              input_value, params.output_multiplier_alpha,
58                              params.output_shift_alpha);
59     }
60     const T clamped_output =
61         std::min(quantized_max, std::max(quantized_min, unclamped_output));
62     output_data[i] = static_cast<T>(clamped_output);
63   }
64 }
65 
66 }  // namespace reference_ops
67 }  // namespace tflite
68 
69 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_LEAKY_RELU_H_
70