cumsum.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Copyright 2021 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_CUMSUM_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CUMSUM_H_
  14. #include <algorithm>
  15. #include <cstdint>
  16. #include <limits>
  17. #include "tensorflow/lite/kernels/internal/common.h"
  18. #include "tensorflow/lite/kernels/internal/compatibility.h"
  19. namespace tflite {
  20. namespace reference_ops {
  21. template <typename T>
  22. inline void CumSum(const T* input_data, const RuntimeShape& shape, int32_t axis,
  23. bool exclusive, bool reverse, T* output_data) {
  24. const int32_t rank = shape.DimensionsCount();
  25. TFLITE_DCHECK_GE(rank, 1);
  26. TFLITE_DCHECK_GE(axis, 0);
  27. TFLITE_DCHECK_LT(axis, rank);
  28. size_t inner = 1;
  29. size_t outer = 1;
  30. size_t depth = 1;
  31. for (int32_t i = 0; i < rank; i++) {
  32. if (i < axis)
  33. inner *= shape.Dims(i);
  34. else if (i > axis)
  35. outer *= shape.Dims(i);
  36. else
  37. depth = shape.Dims(i);
  38. }
  39. for (size_t outer_index = 0; outer_index < outer; outer_index++) {
  40. size_t outer_index_adj;
  41. if (reverse)
  42. outer_index_adj = (outer - 1) - outer_index;
  43. else
  44. outer_index_adj = outer_index;
  45. for (size_t inner_index = 0; inner_index < inner; inner_index++) {
  46. T accumulator = 0;
  47. size_t inner_index_adj;
  48. if (reverse)
  49. inner_index_adj = (inner - 1) - inner_index;
  50. else
  51. inner_index_adj = inner_index;
  52. for (size_t depth_index = 0; depth_index < depth; depth_index++) {
  53. size_t depth_index_adj;
  54. if (reverse)
  55. depth_index_adj = (depth - 1) - depth_index;
  56. else
  57. depth_index_adj = depth_index;
  58. size_t index = outer_index_adj;
  59. index += inner_index_adj * depth * outer;
  60. index += depth_index_adj * outer;
  61. if (exclusive) {
  62. output_data[index] = accumulator;
  63. accumulator += input_data[index];
  64. } else {
  65. accumulator += input_data[index];
  66. output_data[index] = accumulator;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. //
  73. // Quantized INT8 CUMSUM
  74. //
  75. inline void CumSum(const ArithmeticParams& params, const int8_t* input_data,
  76. const RuntimeShape& shape, int32_t axis, bool exclusive,
  77. bool reverse, int8_t* output_data) {
  78. TFLITE_DCHECK_LE(params.quantized_activation_min,
  79. params.quantized_activation_max);
  80. // Input offset is negative input zero point. Activation tensors are
  81. // asymmetric quantized so they span the full int8 range.
  82. // All inputs should have same zero-point and scale, this is checked during
  83. // Prepare stage.
  84. TFLITE_DCHECK_GE(-params.input1_offset, std::numeric_limits<int8_t>::min());
  85. TFLITE_DCHECK_LE(-params.input1_offset, std::numeric_limits<int8_t>::max());
  86. const int32_t rank = shape.DimensionsCount();
  87. TFLITE_DCHECK_GE(rank, 1);
  88. TFLITE_DCHECK_GE(axis, 0);
  89. TFLITE_DCHECK_LT(axis, rank);
  90. size_t inner = 1;
  91. size_t outer = 1;
  92. size_t depth = 1;
  93. for (int32_t i = 0; i < rank; i++) {
  94. if (i < axis)
  95. inner *= shape.Dims(i);
  96. else if (i > axis)
  97. outer *= shape.Dims(i);
  98. else
  99. depth = shape.Dims(i);
  100. }
  101. for (size_t outer_index = 0; outer_index < outer; outer_index++) {
  102. size_t outer_index_adj;
  103. if (reverse)
  104. outer_index_adj = (outer - 1) - outer_index;
  105. else
  106. outer_index_adj = outer_index;
  107. for (size_t inner_index = 0; inner_index < inner; inner_index++) {
  108. int32_t accumulator = params.input1_offset; // accumulator = 0
  109. accumulator *= (1 << params.left_shift);
  110. accumulator = MultiplyByQuantizedMultiplierSmallerThanOneExp(
  111. accumulator, params.input1_multiplier, params.input1_shift);
  112. size_t inner_index_adj;
  113. if (reverse)
  114. inner_index_adj = (inner - 1) - inner_index;
  115. else
  116. inner_index_adj = inner_index;
  117. for (size_t depth_index = 0; depth_index < depth; depth_index++) {
  118. size_t depth_index_adj;
  119. if (reverse)
  120. depth_index_adj = (depth - 1) - depth_index;
  121. else
  122. depth_index_adj = depth_index;
  123. size_t index = outer_index_adj;
  124. index += inner_index_adj * depth * outer;
  125. index += depth_index_adj * outer;
  126. const int32_t y = params.input1_offset + input_data[index];
  127. const int32_t shifted_y = y * (1 << params.left_shift);
  128. const int32_t scaled_y = MultiplyByQuantizedMultiplierSmallerThanOneExp(
  129. shifted_y, params.input1_multiplier, params.input1_shift);
  130. int32_t scaled_output;
  131. if (exclusive) {
  132. scaled_output = accumulator;
  133. accumulator += scaled_y;
  134. } else {
  135. accumulator += scaled_y;
  136. scaled_output = accumulator;
  137. }
  138. const int32_t raw_output =
  139. MultiplyByQuantizedMultiplierSmallerThanOneExp(
  140. scaled_output, params.output_multiplier, params.output_shift) +
  141. params.output_offset;
  142. const int32_t clamped_output =
  143. std::min(params.quantized_activation_max,
  144. std::max(params.quantized_activation_min, raw_output));
  145. output_data[index] = static_cast<int8_t>(clamped_output);
  146. }
  147. }
  148. }
  149. }
  150. } // namespace reference_ops
  151. } // namespace tflite
  152. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_CUMSUM_H_