xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/kernels/internal/reference/prelu.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_PRELU_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PRELU_H_
17 
18 #include <algorithm>
19 
20 #include "tensorflow/lite/kernels/internal/common.h"
21 #include "tensorflow/lite/kernels/internal/compatibility.h"
22 #include "tensorflow/lite/kernels/internal/types.h"
23 
24 namespace tflite {
25 
26 namespace reference_ops {
27 
28 // Broadcast prelu to output_shape for quantized uint8_t/int8_t data.
29 template <typename T>
BroadcastPrelu4DSlow(const PreluParams & params,const RuntimeShape & input_shape,const T * input_data,const RuntimeShape & alpha_shape,const T * alpha_data,const RuntimeShape & output_shape,T * output_data)30 inline void BroadcastPrelu4DSlow(
31     const PreluParams& params, const RuntimeShape& input_shape,
32     const T* input_data, const RuntimeShape& alpha_shape, const T* alpha_data,
33     const RuntimeShape& output_shape, T* output_data) {
34   TFLITE_DCHECK_LE(input_shape.DimensionsCount(), 4);
35   TFLITE_DCHECK_LE(alpha_shape.DimensionsCount(), 4);
36   TFLITE_DCHECK_LE(output_shape.DimensionsCount(), 4);
37   const RuntimeShape extended_output_shape =
38       RuntimeShape::ExtendedShape(4, output_shape);
39   NdArrayDesc<4> desc1;
40   NdArrayDesc<4> desc2;
41   NdArrayDescsForElementwiseBroadcast(input_shape, alpha_shape, &desc1, &desc2);
42 
43   for (int b = 0; b < extended_output_shape.Dims(0); ++b) {
44     for (int y = 0; y < extended_output_shape.Dims(1); ++y) {
45       for (int x = 0; x < extended_output_shape.Dims(2); ++x) {
46         for (int c = 0; c < extended_output_shape.Dims(3); ++c) {
47           int output_index = Offset(extended_output_shape, b, y, x, c);
48           int input_index = SubscriptToIndex(desc1, b, y, x, c);
49           const int32_t input_value =
50               params.input_offset + input_data[input_index];
51           int32_t output_value;
52           if (input_value >= 0) {
53             output_value = MultiplyByQuantizedMultiplier(
54                 input_value, params.output_multiplier_1, params.output_shift_1);
55           } else {
56             auto alpha_index = SubscriptToIndex(desc2, b, y, x, c);
57             const int32_t alpha_value =
58                 params.alpha_offset + alpha_data[alpha_index];
59 
60             output_value = MultiplyByQuantizedMultiplier(
61                 input_value * alpha_value, params.output_multiplier_2,
62                 params.output_shift_2);
63           }
64           output_value += params.output_offset;
65 
66           const int32_t quantized_min = std::numeric_limits<T>::min();
67           const int32_t quantized_max = std::numeric_limits<T>::max();
68           const int32_t clamped_output =
69               std::min(quantized_max, std::max(quantized_min, output_value));
70           output_data[output_index] = static_cast<T>(clamped_output);
71         }
72       }
73     }
74   }
75 }
76 
77 template <typename T>
Prelu(const PreluParams & params,const RuntimeShape & input_shape,const T * input_data,const RuntimeShape & alpha_shape,const T * alpha_data,const RuntimeShape & output_shape,T * output_data)78 inline void Prelu(const PreluParams& params, const RuntimeShape& input_shape,
79                   const T* input_data, const RuntimeShape& alpha_shape,
80                   const T* alpha_data, const RuntimeShape& output_shape,
81                   T* output_data) {
82   const int32_t quantized_min = std::numeric_limits<T>::min();
83   const int32_t quantized_max = std::numeric_limits<T>::max();
84 
85   const int flat_size =
86       MatchingElementsSize(input_shape, alpha_shape, output_shape);
87   for (int i = 0; i < flat_size; ++i) {
88     const int32_t input_value = params.input_offset + input_data[i];
89     int32_t output_value;
90     if (input_value >= 0) {
91       output_value = MultiplyByQuantizedMultiplier(
92           input_value, params.output_multiplier_1, params.output_shift_1);
93     } else {
94       const int32_t alpha_value = params.alpha_offset + alpha_data[i];
95 
96       output_value = MultiplyByQuantizedMultiplier(input_value * alpha_value,
97                                                    params.output_multiplier_2,
98                                                    params.output_shift_2);
99     }
100     output_value += params.output_offset;
101 
102     const int32_t clamped_output =
103         std::min(quantized_max, std::max(quantized_min, output_value));
104     output_data[i] = static_cast<T>(clamped_output);
105   }
106 }
107 
108 }  // namespace reference_ops
109 }  // namespace tflite
110 
111 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_PRELU_H_
112