• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_L2NORMALIZATION_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_L2NORMALIZATION_H_
17 
18 #include "tensorflow/lite/kernels/internal/common.h"
19 
20 namespace tflite {
21 namespace reference_integer_ops {
22 
L2Normalization(int32_t input_zero_point,int32_t outer_size,int32_t depth,const int8_t * input_data,int8_t * output_data)23 inline void L2Normalization(int32_t input_zero_point, int32_t outer_size,
24                             int32_t depth, const int8_t* input_data,
25                             int8_t* output_data) {
26   static constexpr int8_t kMinInt8 = std::numeric_limits<int8_t>::min();
27   static constexpr int8_t kMaxInt8 = std::numeric_limits<int8_t>::max();
28   // The output scale must be in sync with Prepare().
29   // Output is in 1/128 scale so the actual output range is nudged from [-1, 1]
30   // to [-1, 127/128].
31   static constexpr int32_t kOutputScale = 7;
32   for (int outer_index = 0; outer_index < outer_size; ++outer_index) {
33     // int32_t = (int8_t - int8_t) ^ 2.
34     // ([-128, 127] - [-128, 127]) ^ 2 = [0, (2^8 - 1)^2] so the accumulator is
35     // safe from overflowing in at least 2^16 steps.
36     int32_t acc = 0;
37     for (int inner_index = 0; inner_index < depth; ++inner_index) {
38       int32_t input =
39           input_data[depth * outer_index + inner_index] - input_zero_point;
40       acc += input * input;
41     }
42     int32_t inv_l2norm_multiplier;
43     int inv_l2norm_shift;
44     GetInvSqrtQuantizedMultiplierExp(acc, kReverseShift, &inv_l2norm_multiplier,
45                                      &inv_l2norm_shift);
46 
47     for (int inner_index = 0; inner_index < depth; ++inner_index) {
48       int32_t input =
49           input_data[depth * outer_index + inner_index] - input_zero_point;
50 
51       // Rescale and downcast. Rescale is folded into the division.
52       int32_t output_in_q24 = MultiplyByQuantizedMultiplier(
53           input, inv_l2norm_multiplier, inv_l2norm_shift + kOutputScale);
54       output_in_q24 =
55           std::min(static_cast<int32_t>(kMaxInt8),
56                    std::max(static_cast<int32_t>(kMinInt8), output_in_q24));
57       output_data[depth * outer_index + inner_index] =
58           static_cast<int8_t>(output_in_q24);
59     }
60   }
61 }
62 }  // namespace reference_integer_ops
63 }  // namespace tflite
64 
65 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_L2NORMALIZATION_H_
66