arm_softmax_s16.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2022 Arm Limited or its affiliates.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /* ----------------------------------------------------------------------
  19. * Project: CMSIS NN Library
  20. * Title: arm_softmax_s16.c
  21. * Description: S16 softmax function
  22. *
  23. * $Date: 9 March 2022
  24. * $Revision: V.1.0.0
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. /**
  32. * @addtogroup Softmax
  33. * @{
  34. */
  35. arm_status arm_softmax_s16(const int16_t *input,
  36. const int32_t num_rows,
  37. const int32_t row_size,
  38. const int32_t mult,
  39. const int32_t shift,
  40. const cmsis_nn_softmax_lut_s16 *softmax_params,
  41. int16_t *output)
  42. {
  43. int32_t col = 0;
  44. int32_t row_idx;
  45. if (softmax_params->exp_lut == NULL || softmax_params->one_by_one_lut == NULL)
  46. {
  47. return ARM_MATH_ARGUMENT_ERROR;
  48. }
  49. for (row_idx = 0; row_idx < num_rows; ++row_idx)
  50. {
  51. // Find the maximum value in order to ensure numerical stability
  52. int16_t max = *input;
  53. for (col = 1; col < row_size; ++col)
  54. {
  55. max = MAX(max, input[col]);
  56. }
  57. int32_t diff = 0;
  58. int32_t sum = 0;
  59. int16_t *cached_exp_results = output;
  60. for (col = 0; col < row_size; ++col)
  61. {
  62. diff = input[col] - max;
  63. const int32_t scaled_diff = arm_nn_requantize(diff, mult, shift);
  64. const int32_t symmetric_scaled_diff = scaled_diff + NN_Q15_MAX;
  65. const int16_t saturated_symmetric_scaled_diff = MIN(MAX(symmetric_scaled_diff, NN_Q15_MIN), NN_Q15_MAX);
  66. // Lookup from exp table and cache result for next step
  67. const int16_t index = (256 + (saturated_symmetric_scaled_diff >> 7));
  68. const int16_t offset = saturated_symmetric_scaled_diff & 0x7f;
  69. const int16_t base = softmax_params->exp_lut[index];
  70. const int16_t slope = softmax_params->exp_lut[index + 1] - softmax_params->exp_lut[index];
  71. const int16_t delta = (slope * offset + 64) >> 7;
  72. const int16_t result = (base + delta);
  73. cached_exp_results[col] = result;
  74. sum += cached_exp_results[col];
  75. }
  76. const int32_t headroom = __CLZ(sum);
  77. // Compute the reciprocal 1/sum
  78. const int32_t shifted_sum = (((sum) << (headroom - 1)) + (1 << 13)) >> 14;
  79. // Since LUT computes 1/(1 + x), compute x = (sum - 1) => -65536
  80. // Since LUT expects a symmetrical input, recenter from [UINT16_MIN, UINT16_MAX] to [INT16_MIN, INT16_MAX] =>
  81. // -32768 ==> So in total -65536 -32768 => -98304
  82. const int16_t symmetric_shifted_sum = shifted_sum - 98304;
  83. // Lookup from one by one table
  84. const int16_t index = (256 + (symmetric_shifted_sum >> 7));
  85. const int16_t offset = symmetric_shifted_sum & 0x7f;
  86. const int16_t base = softmax_params->one_by_one_lut[index];
  87. const int16_t slope = softmax_params->one_by_one_lut[index + 1] - softmax_params->one_by_one_lut[index];
  88. const int16_t delta = (slope * offset + 64) >> 7;
  89. const int16_t one_by_one_result = (base + delta);
  90. for (col = 0; col < row_size; ++col)
  91. {
  92. const int16_t right_shift = 30 - headroom;
  93. int32_t result = (cached_exp_results[col] * one_by_one_result) >> right_shift;
  94. result = (result + 1) >> 1; // Last shift position and insert round
  95. output[col] = (int16_t)result;
  96. }
  97. output += row_size;
  98. input += row_size;
  99. }
  100. return ARM_MATH_SUCCESS;
  101. }
  102. /**
  103. * @} end of Softmax group
  104. */