softmax.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Copyright 2017 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_SOFTMAX_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SOFTMAX_H_
  14. #include <limits>
  15. #include <vector>
  16. #include "fixedpoint/fixedpoint.h"
  17. #include "tflite/kernels/internal/common.h"
  18. #include "tflite/kernels/internal/cppmath.h"
  19. #include "tflite/kernels/internal/quantization_util.h"
  20. #include "tflite/kernels/internal/types.h"
  21. #include "op_macros.h"
  22. namespace tflite {
  23. namespace reference_ops {
  24. inline void Softmax(const SoftmaxParams& params,
  25. const RuntimeShape& input_shape, const float* input_data,
  26. const RuntimeShape& output_shape, float* output_data) {
  27. const int trailing_dim = input_shape.DimensionsCount() - 1;
  28. const int outer_size =
  29. MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
  30. const int depth =
  31. MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
  32. for (int i = 0; i < outer_size; ++i) {
  33. // Find max element value which we'll use to ensure numerical stability
  34. // taking advantage of the following equality:
  35. // exp(x[i])/sum(exp(x[i])) == exp(x[i]+C)/sum(exp(x[i]+C))
  36. float max = std::numeric_limits<float>::lowest();
  37. for (int c = 0; c < depth; ++c) {
  38. max = std::max(max, input_data[i * depth + c]);
  39. }
  40. // Compute sum.
  41. float sum = 0.f;
  42. for (int c = 0; c < depth; ++c) {
  43. sum += std::exp((input_data[i * depth + c] - max) *
  44. static_cast<float>(params.beta));
  45. }
  46. // Compute result.
  47. for (int c = 0; c < depth; ++c) {
  48. output_data[i * depth + c] = std::exp((input_data[i * depth + c] - max) *
  49. static_cast<float>(params.beta)) /
  50. sum;
  51. }
  52. }
  53. }
  54. // Quantized softmax with int8/uint8 input and int8/uint8/int16 output.
  55. template <typename InputT, typename OutputT>
  56. inline void Softmax(const SoftmaxParams& params,
  57. const RuntimeShape& input_shape, const InputT* input_data,
  58. const RuntimeShape& output_shape, OutputT* output_data) {
  59. const int32 input_beta_multiplier = params.input_multiplier;
  60. const int32 input_beta_left_shift = params.input_left_shift;
  61. const int diff_min = params.diff_min;
  62. // The representation chosen for the input to the exp() function is Q5.26.
  63. // We need to leave extra space since values that we skip might be as large as
  64. // -32 before multiplying by input_beta_multiplier, and therefore as large as
  65. // -16 afterwards. Note that exp(-8) is definitely not insignificant to
  66. // accumulation, but exp(-16) definitely is.
  67. static const int kScaledDiffIntegerBits = 5;
  68. static const int kAccumulationIntegerBits = 12;
  69. using FixedPointScaledDiff =
  70. gemmlowp::FixedPoint<int32, kScaledDiffIntegerBits>;
  71. using FixedPointAccum = gemmlowp::FixedPoint<int32, kAccumulationIntegerBits>;
  72. using FixedPoint0 = gemmlowp::FixedPoint<int32, 0>;
  73. const int trailing_dim = input_shape.DimensionsCount() - 1;
  74. const int outer_size =
  75. MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
  76. const int depth =
  77. MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
  78. for (int i = 0; i < outer_size; ++i) {
  79. InputT max_in_row = std::numeric_limits<InputT>::min();
  80. for (int c = 0; c < depth; ++c) {
  81. max_in_row = std::max(max_in_row, input_data[i * depth + c]);
  82. }
  83. FixedPointAccum sum_of_exps = FixedPointAccum::Zero();
  84. for (int c = 0; c < depth; ++c) {
  85. int32 input_diff =
  86. static_cast<int32>(input_data[i * depth + c]) - max_in_row;
  87. if (input_diff >= diff_min) {
  88. const int32 input_diff_rescaled =
  89. MultiplyByQuantizedMultiplierGreaterThanOne(
  90. input_diff, input_beta_multiplier, input_beta_left_shift);
  91. const FixedPointScaledDiff scaled_diff_f8 =
  92. FixedPointScaledDiff::FromRaw(input_diff_rescaled);
  93. sum_of_exps = sum_of_exps + gemmlowp::Rescale<kAccumulationIntegerBits>(
  94. exp_on_negative_values(scaled_diff_f8));
  95. }
  96. }
  97. int num_bits_over_unit;
  98. FixedPoint0 shifted_scale = FixedPoint0::FromRaw(GetReciprocal(
  99. sum_of_exps.raw(), kAccumulationIntegerBits, &num_bits_over_unit));
  100. for (int c = 0; c < depth; ++c) {
  101. int32 input_diff =
  102. static_cast<int32>(input_data[i * depth + c]) - max_in_row;
  103. if (input_diff >= diff_min) {
  104. const int32 input_diff_rescaled =
  105. MultiplyByQuantizedMultiplierGreaterThanOne(
  106. input_diff, input_beta_multiplier, input_beta_left_shift);
  107. const FixedPointScaledDiff scaled_diff_f8 =
  108. FixedPointScaledDiff::FromRaw(input_diff_rescaled);
  109. FixedPoint0 exp_in_0 = exp_on_negative_values(scaled_diff_f8);
  110. int32 unsat_output = gemmlowp::RoundingDivideByPOT(
  111. (shifted_scale * exp_in_0).raw(),
  112. num_bits_over_unit + 31 - (sizeof(OutputT) * 8));
  113. const int32 shifted_output =
  114. unsat_output +
  115. static_cast<int32>(std::numeric_limits<OutputT>::min());
  116. output_data[i * depth + c] = static_cast<OutputT>(std::max(
  117. std::min(shifted_output,
  118. static_cast<int32>(std::numeric_limits<OutputT>::max())),
  119. static_cast<int32>(std::numeric_limits<OutputT>::min())));
  120. } else {
  121. output_data[i * depth + c] = std::numeric_limits<OutputT>::min();
  122. }
  123. }
  124. }
  125. }
  126. // Quantized softmax with int16 input and int16 output.
  127. inline void SoftmaxInt16(const SoftmaxParams& params,
  128. const RuntimeShape& input_shape,
  129. const int16_t* input_data,
  130. const RuntimeShape& output_shape,
  131. int16_t* output_data) {
  132. const int trailing_dim = input_shape.DimensionsCount() - 1;
  133. const int outer_size =
  134. MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
  135. const int depth =
  136. MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
  137. for (int i = 0; i < outer_size; ++i) {
  138. // Find the largest element
  139. int16_t max_in_row = std::numeric_limits<int16_t>::min();
  140. for (int c = 0; c < depth; ++c) {
  141. max_in_row = std::max(max_in_row, input_data[i * depth + c]);
  142. }
  143. // Compute exp(input - max_input)
  144. std::vector<int16_t> exp_result_Q015(depth);
  145. for (int c = 0; c < depth; ++c) {
  146. int32_t input_diff = input_data[i * depth + c] - max_in_row;
  147. // scale the input_diff such that [-65535, 0] correspond to [-10.0, 0.0]
  148. int32_t scaled_diff = MultiplyByQuantizedMultiplier(
  149. input_diff, params.input_multiplier, params.input_left_shift);
  150. // recenter to [-32768, 32767]
  151. int32_t sym_scaled_diff = scaled_diff + 32767;
  152. int16_t sat_sym_scaled_diff =
  153. std::min(std::max(sym_scaled_diff, static_cast<int32_t>(-32768)),
  154. static_cast<int32_t>(32767));
  155. // apply the exp() LUT activation function
  156. exp_result_Q015[c] =
  157. generic_int16_table_lookup(sat_sym_scaled_diff, params.exp_lut);
  158. }
  159. // sum_of_exps is a Q16.15 fixed point format.
  160. int32_t sum_of_exps = 0;
  161. for (int c = 0; c < depth; ++c) {
  162. // Q16.15 + Q0.15
  163. sum_of_exps += exp_result_Q015[c];
  164. }
  165. // Compute the reciprocal 1/sum_of_exps
  166. uint8_t headroom_plus_one =
  167. CountLeadingZeros(static_cast<uint32_t>(sum_of_exps));
  168. int32_t shifted_sum =
  169. ((static_cast<int64_t>(sum_of_exps) << (headroom_plus_one - 1)) +
  170. (1 << 13)) >>
  171. 14;
  172. // since the LUT computes 1/(1 + x) we need to first compute x = (sum - 1).
  173. // also, the LUT expects a symmetrical input, so we must also recenter x
  174. // from [0, 65535] to [-32768, 32767].
  175. int32_t sym_shifted_sum = shifted_sum + (-((1 << 15) + (1 << 16)));
  176. int16_t sat_sym_shifted_sum = static_cast<int16_t>(
  177. std::min(std::max(sym_shifted_sum, static_cast<int32_t>(-32768)),
  178. static_cast<int32_t>(32767)));
  179. // apply 1/(1 + x) LUT activation function
  180. int16_t reciprocal_scale_Q015 = generic_int16_table_lookup(
  181. sat_sym_shifted_sum, params.one_over_one_plus_x_lut);
  182. // Rescale the exp_result with reciprocal
  183. // range of output is [0, 32767] correspond to [0.0, 1.0]
  184. for (int c = 0; c < depth; ++c) {
  185. uint8_t right_shift = 31 - headroom_plus_one;
  186. int64_t round = 1 << (right_shift - 1);
  187. int32_t result = (static_cast<int64_t>(exp_result_Q015[c]) *
  188. static_cast<int64_t>(reciprocal_scale_Q015) +
  189. round) >>
  190. right_shift;
  191. output_data[i * depth + c] = static_cast<int16_t>(
  192. std::min(std::max(result, static_cast<int32_t>(0)),
  193. static_cast<int32_t>(32767)));
  194. }
  195. }
  196. }
  197. } // namespace reference_ops
  198. } // namespace tflite
  199. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_REFERENCE_SOFTMAX_H_