• 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_MEAN_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MEAN_H_
17 
18 #include "tensorflow/lite/kernels/internal/common.h"
19 
20 namespace tflite {
21 namespace reference_integer_ops {
22 
23 template <typename integer_type>
Mean(const tflite::MeanParams & op_params,int32_t multiplier,int32_t shift,const RuntimeShape & unextended_input_shape,const integer_type * input_data,int32_t input_zero_point,const RuntimeShape & unextended_output_shape,integer_type * output_data,int32_t output_zero_point)24 inline void Mean(const tflite::MeanParams& op_params, int32_t multiplier,
25                  int32_t shift, const RuntimeShape& unextended_input_shape,
26                  const integer_type* input_data, int32_t input_zero_point,
27                  const RuntimeShape& unextended_output_shape,
28                  integer_type* output_data, int32_t output_zero_point) {
29   // Current implementation only supports dimension equals 4 and simultaneous
30   // reduction over width and height.
31   TFLITE_CHECK_EQ(unextended_input_shape.DimensionsCount(), 4);
32   TFLITE_CHECK_LE(unextended_output_shape.DimensionsCount(), 4);
33   const RuntimeShape input_shape =
34       RuntimeShape::ExtendedShape(4, unextended_input_shape);
35   const RuntimeShape output_shape =
36       RuntimeShape::ExtendedShape(4, unextended_output_shape);
37   const int output_batch = output_shape.Dims(0);
38   const int output_height = output_shape.Dims(1);
39   const int output_width = output_shape.Dims(2);
40   const int output_depth = output_shape.Dims(3);
41   const int input_height = input_shape.Dims(1);
42   const int input_width = input_shape.Dims(2);
43   const int num_elements_in_axis = input_width * input_height;
44 
45   TFLITE_CHECK_EQ(op_params.axis_count, 2);
46   TFLITE_CHECK((op_params.axis[0] == 1 && op_params.axis[1] == 2) ||
47                (op_params.axis[0] == 2 && op_params.axis[1] == 1));
48   TFLITE_CHECK_EQ(output_height, 1);
49   TFLITE_CHECK_EQ(output_width, 1);
50 
51   static constexpr int32_t kMinInt = std::numeric_limits<integer_type>::min();
52   static constexpr int32_t kMaxInt = std::numeric_limits<integer_type>::max();
53 
54   for (int out_b = 0; out_b < output_batch; ++out_b) {
55     for (int out_d = 0; out_d < output_depth; ++out_d) {
56       int32_t acc = 0;
57       for (int in_h = 0; in_h < input_height; ++in_h) {
58         for (int in_w = 0; in_w < input_width; ++in_w) {
59           acc += input_data[Offset(input_shape, out_b, in_h, in_w, out_d)] -
60                  input_zero_point;
61         }
62       }
63       acc = MultiplyByQuantizedMultiplier(acc, multiplier, shift);
64       acc = acc > 0 ? (acc + num_elements_in_axis / 2) / num_elements_in_axis
65                     : (acc - num_elements_in_axis / 2) / num_elements_in_axis;
66       acc += output_zero_point;
67       acc = std::min(std::max(acc, kMinInt), kMaxInt);
68       output_data[Offset(output_shape, out_b, 0, 0, out_d)] =
69           static_cast<integer_type>(acc);
70     }
71   }
72 }
73 
74 }  // namespace reference_integer_ops
75 }  // namespace tflite
76 
77 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MEAN_H_
78