arm_nn_softmax_common_s8.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_nn_softmax_common_s8.c
  21. * Description: Softmax with s8 input and output of s8 or s16.
  22. *
  23. * $Date: 17 March 2022
  24. * $Revision: V.1.0.1
  25. *
  26. * Target Processor: Cortex-M processors
  27. * -------------------------------------------------------------------- */
  28. #include "arm_nnsupportfunctions.h"
  29. #define ACCUM_BITS 12
  30. /**
  31. * @ingroup groupSupport
  32. */
  33. /**
  34. * @addtogroup Softmax
  35. * @{
  36. */
  37. /*
  38. * Softmax function with s8 input and output of s8 or s16.
  39. *
  40. * Refer header file for details.
  41. *
  42. */
  43. void arm_nn_softmax_common_s8(const int8_t *input,
  44. const int32_t num_rows,
  45. const int32_t row_size,
  46. const int32_t mult,
  47. const int32_t shift,
  48. const int32_t diff_min,
  49. const bool int16_output,
  50. void *output)
  51. {
  52. const int32_t mask = (1 << shift);
  53. int32_t col = 0;
  54. int32_t row_idx;
  55. for (row_idx = 0; row_idx < num_rows; ++row_idx)
  56. {
  57. // Find the maximum value in order to ensure numerical stability
  58. int8_t max = *input;
  59. for (col = 1; col < row_size; ++col)
  60. {
  61. max = MAX(max, input[col]);
  62. }
  63. int32_t diff = 0;
  64. int32_t sum = 0;
  65. for (col = 0; col < row_size; ++col)
  66. {
  67. diff = input[col] - max;
  68. if (diff >= diff_min)
  69. {
  70. sum += DIV_POW2(EXP_ON_NEG(MUL_SAT(diff * mask, mult)), ACCUM_BITS);
  71. }
  72. }
  73. const int32_t headroom = __CLZ(sum);
  74. const int32_t shifted_scale = ONE_OVER1((sum > 0 ? sum << headroom : 0) - (1 << 31));
  75. int32_t bits_over_unit;
  76. if (int16_output)
  77. {
  78. int16_t *output_s16 = (int16_t *)output + row_idx * row_size;
  79. bits_over_unit = ACCUM_BITS - headroom + 15;
  80. for (col = 0; col < row_size; ++col)
  81. {
  82. diff = input[col] - max;
  83. if (diff >= diff_min)
  84. {
  85. const int32_t res =
  86. DIV_POW2(MUL_SAT(shifted_scale, EXP_ON_NEG(MUL_SAT(diff * mask, mult))), bits_over_unit) +
  87. NN_Q15_MIN;
  88. output_s16[col] = (int16_t)CLAMP(res, (int32_t)NN_Q15_MAX, (int32_t)NN_Q15_MIN);
  89. }
  90. else
  91. {
  92. output_s16[col] = NN_Q15_MIN;
  93. }
  94. }
  95. }
  96. else
  97. {
  98. int8_t *output_s8 = (int8_t *)output + row_idx * row_size;
  99. bits_over_unit = ACCUM_BITS - headroom + 23;
  100. for (col = 0; col < row_size; ++col)
  101. {
  102. diff = input[col] - max;
  103. if (diff >= diff_min)
  104. {
  105. const int32_t res =
  106. DIV_POW2(MUL_SAT(shifted_scale, EXP_ON_NEG(MUL_SAT(diff * mask, mult))), bits_over_unit) +
  107. NN_Q7_MIN;
  108. output_s8[col] = (int8_t)CLAMP(res, (int32_t)NN_Q7_MAX, (int32_t)NN_Q7_MIN);
  109. }
  110. else
  111. {
  112. output_s8[col] = NN_Q7_MIN;
  113. }
  114. }
  115. }
  116. input += row_size;
  117. }
  118. }
  119. /**
  120. * @} end of NNBasicMath group
  121. */