mean.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MEAN_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MEAN_H_
  14. #include "tflite/kernels/internal/common.h"
  15. namespace tflite {
  16. namespace reference_integer_ops {
  17. inline void Mean(const tflite::MeanParams& op_params, int32_t multiplier,
  18. int32_t shift, const RuntimeShape& unextended_input_shape,
  19. const int8_t* input_data, int32 input_zero_point,
  20. const RuntimeShape& unextended_output_shape,
  21. int8_t* output_data, int32 output_zero_point) {
  22. // Current implementation only supports dimension equals 4 and simultaneous
  23. // reduction over width and height.
  24. TFLITE_CHECK_EQ(unextended_input_shape.DimensionsCount(), 4);
  25. TFLITE_CHECK_LE(unextended_output_shape.DimensionsCount(), 4);
  26. const RuntimeShape input_shape =
  27. RuntimeShape::ExtendedShape(4, unextended_input_shape);
  28. const RuntimeShape output_shape =
  29. RuntimeShape::ExtendedShape(4, unextended_output_shape);
  30. const int output_batch = output_shape.Dims(0);
  31. const int output_height = output_shape.Dims(1);
  32. const int output_width = output_shape.Dims(2);
  33. const int output_depth = output_shape.Dims(3);
  34. const int input_height = input_shape.Dims(1);
  35. const int input_width = input_shape.Dims(2);
  36. const int num_elements_in_axis = input_width * input_height;
  37. TFLITE_CHECK_EQ(op_params.axis_count, 2);
  38. TFLITE_CHECK((op_params.axis[0] == 1 && op_params.axis[1] == 2) ||
  39. (op_params.axis[0] == 2 && op_params.axis[1] == 1));
  40. TFLITE_CHECK_EQ(output_height, 1);
  41. TFLITE_CHECK_EQ(output_width, 1);
  42. static constexpr int32_t kMinInt8 = std::numeric_limits<int8_t>::min();
  43. static constexpr int32_t kMaxInt8 = std::numeric_limits<int8_t>::max();
  44. for (int out_b = 0; out_b < output_batch; ++out_b) {
  45. for (int out_d = 0; out_d < output_depth; ++out_d) {
  46. int32 acc = 0;
  47. for (int in_h = 0; in_h < input_height; ++in_h) {
  48. for (int in_w = 0; in_w < input_width; ++in_w) {
  49. acc += input_data[Offset(input_shape, out_b, in_h, in_w, out_d)] -
  50. input_zero_point;
  51. }
  52. }
  53. acc = MultiplyByQuantizedMultiplier(acc, multiplier, shift);
  54. acc = acc > 0 ? (acc + num_elements_in_axis / 2) / num_elements_in_axis
  55. : (acc - num_elements_in_axis / 2) / num_elements_in_axis;
  56. acc += output_zero_point;
  57. acc = std::min(std::max(acc, kMinInt8), kMaxInt8);
  58. output_data[Offset(output_shape, out_b, 0, 0, out_d)] =
  59. static_cast<int8_t>(acc);
  60. }
  61. }
  62. }
  63. } // namespace reference_integer_ops
  64. } // namespace tflite
  65. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MEAN_H_