mul.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_MUL_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MUL_H_
  14. #include "fixedpoint/fixedpoint.h"
  15. #include "ruy/profiler/instrumentation.h" // from @ruy
  16. #include "tensorflow/lite/kernels/internal/common.h"
  17. namespace tflite {
  18. namespace reference_integer_ops {
  19. template <typename T>
  20. inline void MulElementwise(int size, const ArithmeticParams& params,
  21. const T* input1_data, const T* input2_data,
  22. T* output_data) {
  23. for (int i = 0; i < size; ++i) {
  24. const int32_t input1_val = params.input1_offset + input1_data[i];
  25. const int32_t input2_val = params.input2_offset + input2_data[i];
  26. const int32_t unclamped_result =
  27. params.output_offset +
  28. MultiplyByQuantizedMultiplier(input1_val * input2_val,
  29. params.output_multiplier,
  30. params.output_shift);
  31. const int32_t clamped_output =
  32. std::min(params.quantized_activation_max,
  33. std::max(params.quantized_activation_min, unclamped_result));
  34. output_data[i] = static_cast<T>(clamped_output);
  35. }
  36. }
  37. template <typename T>
  38. inline void Mul(const ArithmeticParams& params,
  39. const RuntimeShape& input1_shape, const T* input1_data,
  40. const RuntimeShape& input2_shape, const T* input2_data,
  41. const RuntimeShape& output_shape, T* output_data) {
  42. TFLITE_DCHECK_LE(params.quantized_activation_min,
  43. params.quantized_activation_max);
  44. ruy::profiler::ScopeLabel label("Mul/8bit");
  45. const int flat_size =
  46. MatchingElementsSize(input1_shape, input2_shape, output_shape);
  47. MulElementwise(flat_size, params, input1_data, input2_data, output_data);
  48. }
  49. // Mul with 16 bit inputs and int8_t outputs.
  50. inline void Mul(const ArithmeticParams& params,
  51. const RuntimeShape& input1_shape, const int16_t* input1_data,
  52. const RuntimeShape& input2_shape, const int16_t* input2_data,
  53. const RuntimeShape& output_shape, int8_t* output_data) {
  54. ruy::profiler::ScopeLabel label("Mul/Int16Int8");
  55. int32_t output_offset = params.output_offset;
  56. int32_t output_activation_min = params.quantized_activation_min;
  57. int32_t output_activation_max = params.quantized_activation_max;
  58. TFLITE_DCHECK_LE(output_activation_min, output_activation_max);
  59. const int flat_size =
  60. MatchingElementsSize(input1_shape, input2_shape, output_shape);
  61. for (int i = 0; i < flat_size; i++) {
  62. // F0 uses 0 integer bits, range [-1, 1].
  63. using F0 = gemmlowp::FixedPoint<std::int16_t, 0>;
  64. F0 unclamped_result =
  65. F0::FromRaw(input1_data[i]) * F0::FromRaw(input2_data[i]);
  66. int16_t rescaled_result =
  67. gemmlowp::RoundingDivideByPOT(unclamped_result.raw(), 8);
  68. int16_t clamped_result = std::min<int16_t>(
  69. output_activation_max - output_offset, rescaled_result);
  70. clamped_result = std::max<int16_t>(output_activation_min - output_offset,
  71. clamped_result);
  72. output_data[i] = output_offset + clamped_result;
  73. }
  74. }
  75. template <typename T>
  76. inline void BroadcastMul4DSlow(
  77. const ArithmeticParams& params, const RuntimeShape& input1_shape,
  78. const T* input1_data, const RuntimeShape& input2_shape,
  79. const T* input2_data, const RuntimeShape& output_shape, T* output_data) {
  80. ruy::profiler::ScopeLabel label("BroadcastMul4DSlow");
  81. NdArrayDesc<4> desc1;
  82. NdArrayDesc<4> desc2;
  83. // The input shapes are extended as part of NdArrayDesc initialization.
  84. NdArrayDescsForElementwiseBroadcast(input1_shape, input2_shape, &desc1,
  85. &desc2);
  86. const RuntimeShape extended_output_shape =
  87. RuntimeShape::ExtendedShape(4, output_shape);
  88. for (int b = 0; b < extended_output_shape.Dims(0); ++b) {
  89. for (int y = 0; y < extended_output_shape.Dims(1); ++y) {
  90. for (int x = 0; x < extended_output_shape.Dims(2); ++x) {
  91. for (int c = 0; c < extended_output_shape.Dims(3); ++c) {
  92. const int32_t input1_val =
  93. params.input1_offset +
  94. input1_data[SubscriptToIndex(desc1, b, y, x, c)];
  95. const int32_t input2_val =
  96. params.input2_offset +
  97. input2_data[SubscriptToIndex(desc2, b, y, x, c)];
  98. const int32_t unclamped_result =
  99. params.output_offset +
  100. MultiplyByQuantizedMultiplier(input1_val * input2_val,
  101. params.output_multiplier,
  102. params.output_shift);
  103. const int32_t clamped_output = std::min(
  104. params.quantized_activation_max,
  105. std::max(params.quantized_activation_min, unclamped_result));
  106. output_data[Offset(extended_output_shape, b, y, x, c)] =
  107. static_cast<T>(clamped_output);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. } // namespace reference_integer_ops
  114. } // namespace tflite
  115. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_INTEGER_OPS_MUL_H_