mul.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_MUL_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MUL_H_
  14. #include "tensorflow/lite/kernels/internal/common.h"
  15. namespace tflite {
  16. namespace reference_ops {
  17. // Element-wise mul that can often be used for inner loop of broadcast Mul as
  18. // well as the non-broadcast Mul.
  19. inline void MulElementwise(int size, const ArithmeticParams& params,
  20. const uint8_t* input1_data,
  21. const uint8_t* input2_data, uint8_t* output_data) {
  22. for (int i = 0; i < size; ++i) {
  23. const int32_t input1_val = params.input1_offset + input1_data[i];
  24. const int32_t input2_val = params.input2_offset + input2_data[i];
  25. const int32_t unclamped_result =
  26. params.output_offset +
  27. MultiplyByQuantizedMultiplier(input1_val * input2_val,
  28. params.output_multiplier,
  29. params.output_shift);
  30. const int32_t clamped_output =
  31. std::min(params.quantized_activation_max,
  32. std::max(params.quantized_activation_min, unclamped_result));
  33. output_data[i] = static_cast<uint8_t>(clamped_output);
  34. }
  35. }
  36. template <typename T>
  37. inline void Mul(const ArithmeticParams& params,
  38. const RuntimeShape& input1_shape, const T* input1_data,
  39. const RuntimeShape& input2_shape, const T* input2_data,
  40. const RuntimeShape& output_shape, T* output_data) {
  41. T output_activation_min;
  42. T output_activation_max;
  43. GetActivationParams(params, &output_activation_min, &output_activation_max);
  44. const int flat_size =
  45. MatchingFlatSize(input1_shape, input2_shape, output_shape);
  46. for (int i = 0; i < flat_size; ++i) {
  47. output_data[i] = ActivationFunctionWithMinMax(
  48. input1_data[i] * input2_data[i], output_activation_min,
  49. output_activation_max);
  50. }
  51. }
  52. inline void Mul(const ArithmeticParams& params,
  53. const RuntimeShape& input1_shape, const uint8_t* input1_data,
  54. const RuntimeShape& input2_shape, const uint8_t* input2_data,
  55. const RuntimeShape& output_shape, uint8_t* output_data) {
  56. TFLITE_DCHECK_LE(params.quantized_activation_min,
  57. params.quantized_activation_max);
  58. const int flat_size =
  59. MatchingFlatSize(input1_shape, input2_shape, output_shape);
  60. MulElementwise(flat_size, params, input1_data, input2_data, output_data);
  61. }
  62. inline void BroadcastMul4DSlow(const ArithmeticParams& params,
  63. const RuntimeShape& input1_shape,
  64. const uint8_t* input1_data,
  65. const RuntimeShape& input2_shape,
  66. const uint8_t* input2_data,
  67. const RuntimeShape& output_shape,
  68. uint8_t* output_data) {
  69. NdArrayDesc<4> desc1;
  70. NdArrayDesc<4> desc2;
  71. NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1,
  72. &desc2);
  73. const RuntimeShape extended_output_shape =
  74. RuntimeShape::ExtendedShape(4, output_shape);
  75. for (int b = 0; b < extended_output_shape.Dims(0); ++b) {
  76. for (int y = 0; y < extended_output_shape.Dims(1); ++y) {
  77. for (int x = 0; x < extended_output_shape.Dims(2); ++x) {
  78. for (int c = 0; c < extended_output_shape.Dims(3); ++c) {
  79. const int32_t input1_val =
  80. params.input1_offset +
  81. input1_data[SubscriptToIndex(desc1, b, y, x, c)];
  82. const int32_t input2_val =
  83. params.input2_offset +
  84. input2_data[SubscriptToIndex(desc2, b, y, x, c)];
  85. const int32_t unclamped_result =
  86. params.output_offset +
  87. MultiplyByQuantizedMultiplier(input1_val * input2_val,
  88. params.output_multiplier,
  89. params.output_shift);
  90. const int32_t clamped_output = std::min(
  91. params.quantized_activation_max,
  92. std::max(params.quantized_activation_min, unclamped_result));
  93. output_data[Offset(extended_output_shape, b, y, x, c)] =
  94. static_cast<uint8_t>(clamped_output);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. template <typename T>
  101. void BroadcastMul4DSlow(const ArithmeticParams& params,
  102. const RuntimeShape& unextended_input1_shape,
  103. const T* input1_data,
  104. const RuntimeShape& unextended_input2_shape,
  105. const T* input2_data,
  106. const RuntimeShape& unextended_output_shape,
  107. T* output_data) {
  108. T output_activation_min;
  109. T output_activation_max;
  110. GetActivationParams(params, &output_activation_min, &output_activation_max);
  111. TFLITE_DCHECK_LE(unextended_input1_shape.DimensionsCount(), 4);
  112. TFLITE_DCHECK_LE(unextended_input2_shape.DimensionsCount(), 4);
  113. TFLITE_DCHECK_LE(unextended_output_shape.DimensionsCount(), 4);
  114. const RuntimeShape output_shape =
  115. RuntimeShape::ExtendedShape(4, unextended_output_shape);
  116. NdArrayDesc<4> desc1;
  117. NdArrayDesc<4> desc2;
  118. NdArrayDescsForElementwiseBroadcast(unextended_input1_shape,
  119. unextended_input2_shape, &desc1, &desc2);
  120. // In Tensorflow, the dimensions are canonically named (batch_number, row,
  121. // col, channel), with extents (batches, height, width, depth), with the
  122. // trailing dimension changing most rapidly (channels has the smallest stride,
  123. // typically 1 element).
  124. //
  125. // In generated C code, we store arrays with the dimensions reversed. The
  126. // first dimension has smallest stride.
  127. //
  128. // We name our variables by their Tensorflow convention, but generate C code
  129. // nesting loops such that the innermost loop has the smallest stride for the
  130. // best cache behavior.
  131. for (int b = 0; b < output_shape.Dims(0); ++b) {
  132. for (int y = 0; y < output_shape.Dims(1); ++y) {
  133. for (int x = 0; x < output_shape.Dims(2); ++x) {
  134. for (int c = 0; c < output_shape.Dims(3); ++c) {
  135. output_data[Offset(output_shape, b, y, x, c)] =
  136. ActivationFunctionWithMinMax(
  137. input1_data[SubscriptToIndex(desc1, b, y, x, c)] *
  138. input2_data[SubscriptToIndex(desc2, b, y, x, c)],
  139. output_activation_min, output_activation_max);
  140. }
  141. }
  142. }
  143. }
  144. }
  145. } // namespace reference_ops
  146. } // namespace tflite
  147. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_MUL_H_