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_L2NORMALIZATION_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_L2NORMALIZATION_H_
17
18 #include <algorithm>
19 #include <cmath>
20
21 #include "tensorflow/lite/c/common.h"
22 #include "tensorflow/lite/kernels/internal/common.h"
23 #include "tensorflow/lite/kernels/internal/types.h"
24
25 namespace tflite {
26
27 namespace reference_ops {
28
29 inline void L2Normalization(const tflite::L2NormalizationParams& op_params,
30 const RuntimeShape& input_shape,
31 const float* input_data,
32 const RuntimeShape& output_shape,
33 float* output_data, float epsilon = 1e-6) {
34 const int trailing_dim = input_shape.DimensionsCount() - 1;
35 const int outer_size =
36 MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
37 const int depth =
38 MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
39 for (int i = 0; i < outer_size; ++i) {
40 float squared_l2_norm = 0;
41 for (int c = 0; c < depth; ++c) {
42 const float val = input_data[depth * i + c];
43 squared_l2_norm += val * val;
44 }
45 float l2_norm = std::sqrt(squared_l2_norm);
46 l2_norm = std::max(l2_norm, epsilon);
47 for (int c = 0; c < depth; ++c) {
48 output_data[depth * i + c] = input_data[depth * i + c] / l2_norm;
49 }
50 }
51 }
52
L2Normalization(const tflite::L2NormalizationParams & op_params,const RuntimeShape & input_shape,const uint8_t * input_data,const RuntimeShape & output_shape,uint8_t * output_data)53 inline void L2Normalization(const tflite::L2NormalizationParams& op_params,
54 const RuntimeShape& input_shape,
55 const uint8_t* input_data,
56 const RuntimeShape& output_shape,
57 uint8_t* output_data) {
58 const int trailing_dim = input_shape.DimensionsCount() - 1;
59 const int depth =
60 MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
61 const int outer_size =
62 MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
63 const int32_t input_zero_point = op_params.input_zero_point;
64
65 for (int i = 0; i < outer_size; ++i) {
66 int32_t square_l2_norm = 0;
67 for (int c = 0; c < depth; c++) {
68 int32_t diff = input_data[depth * i + c] - input_zero_point;
69 square_l2_norm += diff * diff;
70 }
71 int32_t inv_l2norm_multiplier;
72 int inv_l2norm_shift;
73 GetInvSqrtQuantizedMultiplierExp(square_l2_norm, kReverseShift,
74 &inv_l2norm_multiplier, &inv_l2norm_shift);
75 for (int c = 0; c < depth; c++) {
76 int32_t diff = input_data[depth * i + c] - input_zero_point;
77 int32_t rescaled_diff = MultiplyByQuantizedMultiplierSmallerThanOneExp(
78 128 * diff, inv_l2norm_multiplier, inv_l2norm_shift);
79 int32_t unclamped_output_val = 128 + rescaled_diff;
80 int32_t output_val =
81 std::min(static_cast<int32_t>(255),
82 std::max(static_cast<int32_t>(0), unclamped_output_val));
83 output_data[depth * i + c] = static_cast<uint8_t>(output_val);
84 }
85 }
86 }
87
88 } // namespace reference_ops
89 } // namespace tflite
90 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_L2NORMALIZATION_H_
91