arm_softmax_s8.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (C) 2010-2019 Arm Limited or its affiliates. All rights reserved.
  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_s8.c
  21. * Description: S8 softmax function
  22. *
  23. * $Date: November 2019
  24. * $Revision: V.1.5.0
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnsupportfunctions.h"
  30. #define ACCUM_BITS 12
  31. #ifdef ARM_MATH_MVEI
  32. int32x4_t arm_exp_on_negative_values_mve_32x4(int32x4_t val)
  33. {
  34. #define SHIFT_START (24)
  35. int32_t shift = SHIFT_START;
  36. int32x4_t mask;
  37. const int32x4_t val_mod_minus_quarter = vandq_s32(val, vdupq_n_s32((1 << SHIFT_START) - 1)) - vdupq_n_s32(1 << SHIFT_START);
  38. const int32x4_t remainder = vsubq_s32(val_mod_minus_quarter, val);
  39. const int32x4_t x = vaddq_n_s32(val_mod_minus_quarter << 5, 1 << 28);
  40. const int32x4_t x2 = MUL_SAT_MVE(x, x);
  41. const int32x4_t op_1 = DIV_POW2_MVE(MUL_SAT_MVE(x2, x2), 2) + MUL_SAT_MVE(x2, x);
  42. const int32x4_t op_2 = x + DIV_POW2_MVE(MUL_SAT_MVE(op_1, vdupq_n_s32(715827883)) + x2, 1);
  43. int32x4_t result = vdupq_n_s32(1895147668) + MUL_SAT_MVE(vdupq_n_s32(1895147668), op_2);
  44. #define SELECT_IF_NON_ZERO(x) \
  45. { \
  46. mve_pred16_t p = vcmpneq_n_s32(remainder & vdupq_n_s32(1 << shift++), 0); \
  47. mask = vmvnq_m_s32(vdupq_n_s32(0), vdupq_n_s32(0), p); \
  48. result = SELECT_USING_MASK(mask, MUL_SAT_MVE(result, vdupq_n_s32(x)), result); \
  49. }
  50. SELECT_IF_NON_ZERO(1672461947)
  51. SELECT_IF_NON_ZERO(1302514674)
  52. SELECT_IF_NON_ZERO(790015084)
  53. SELECT_IF_NON_ZERO(290630308)
  54. SELECT_IF_NON_ZERO(39332535)
  55. SELECT_IF_NON_ZERO(720401)
  56. SELECT_IF_NON_ZERO(242)
  57. #undef SELECT_IF_NON_ZERO
  58. mve_pred16_t p = vcmpeqq_n_s32(val, 0);
  59. mask = vmvnq_m_s32(vdupq_n_s32(0), vdupq_n_s32(0), p);
  60. result = SELECT_USING_MASK(mask, vdupq_n_s32(Q31_MAX), result);
  61. return result;
  62. }
  63. #endif
  64. /**
  65. * @ingroup groupNN
  66. */
  67. /**
  68. * @addtogroup Softmax
  69. * @{
  70. */
  71. void arm_softmax_s8(const int8_t *input,
  72. const int32_t num_rows,
  73. const int32_t row_size,
  74. const int32_t mult,
  75. const int32_t shift,
  76. const int8_t diff_min,
  77. int8_t *output)
  78. {
  79. #ifdef ARM_MATH_MVEI
  80. #define ACT_MIN ((int8_t)Q7_MIN)
  81. #define ACT_MAX ((int8_t)Q7_MAX)
  82. const int32_t mask = (1 << shift);
  83. for (int i_num_rows = 0; i_num_rows < num_rows; ++i_num_rows)
  84. {
  85. int8_t max = ACT_MIN;
  86. int32_t vec_count = row_size / 16;
  87. for (int i = 0; i < vec_count; i++)
  88. {
  89. const int8x16_t ip = vldrbq_s8(&input[i * 16]);
  90. max = vmaxvq_s8(max, ip);
  91. }
  92. mve_pred16_t p = vctp8q(row_size & 0xF);
  93. if (p != 0)
  94. {
  95. const int8x16_t ip = vldrbq_z_s8(&input[vec_count * 16], p);
  96. max = vmaxvq_p_s8(max, ip, p);
  97. }
  98. vec_count = row_size / 4;
  99. int32_t idx = 0;
  100. int32_t sum = 0;
  101. while (vec_count)
  102. {
  103. int32x4_t ip = vldrbq_s32(&input[idx * 4]);
  104. ip = ip - vdupq_n_s32(max);
  105. p = vcmpgeq_s32(ip, vdupq_n_s32(diff_min));
  106. ip = vmulq_n_s32(ip, mask);
  107. int32x4_t res = MUL_SAT_MVE(ip, vdupq_n_s32(mult));
  108. res = arm_exp_on_negative_values_mve_32x4(res);
  109. res = DIV_POW2_MVE(res, ACCUM_BITS);
  110. res = vpselq_s32(res, vdupq_n_s32(0), p);
  111. sum += vaddvq_s32(res);
  112. vec_count--;
  113. idx++;
  114. }
  115. const int32_t tail_idx = row_size & ~3;
  116. for (int i = 0; i < (row_size & 3); i++)
  117. {
  118. const int32_t diff = input[tail_idx + i] - max;
  119. if (diff >= diff_min)
  120. {
  121. sum += DIV_POW2(EXP_ON_NEG(MUL_SAT(diff * mask, mult)), ACCUM_BITS);
  122. }
  123. }
  124. const int32_t headroom = __CLZ(sum);
  125. const int32_t bits_over_unit = ACCUM_BITS - headroom + 23;
  126. const int32_t shifted_scale = ONE_OVER1((sum << headroom) - (1 << 31));
  127. vec_count = row_size / 4;
  128. idx = 0;
  129. while (vec_count)
  130. {
  131. int32x4_t ip = vldrbq_s32(&input[idx]);
  132. ip = ip - vdupq_n_s32(max);
  133. p = vcmpgeq_s32(ip, vdupq_n_s32(diff_min));
  134. ip = vmulq_n_s32(ip, mask);
  135. int32x4_t tmp_res = MUL_SAT_MVE(ip, vdupq_n_s32(mult));
  136. tmp_res = arm_exp_on_negative_values_mve_32x4(tmp_res);
  137. tmp_res = MUL_SAT_MVE(vdupq_n_s32(shifted_scale), tmp_res);
  138. tmp_res = DIV_POW2_MVE(tmp_res, bits_over_unit);
  139. tmp_res += vdupq_n_s32(ACT_MIN);
  140. tmp_res = vmaxq_s32(tmp_res, vdupq_n_s32(ACT_MIN));
  141. tmp_res = vminq_s32(tmp_res, vdupq_n_s32(ACT_MAX));
  142. tmp_res = vpselq_s32(tmp_res, vdupq_n_s32(ACT_MIN), p);
  143. vstrbq_s32(&output[idx], tmp_res);
  144. vec_count--;
  145. idx += 4;
  146. }
  147. for (int i = 0; i < (row_size & 3); i++)
  148. {
  149. int32_t diff = input[tail_idx + i] - max;
  150. if (diff >= diff_min)
  151. {
  152. const int32_t res = DIV_POW2(MUL_SAT(shifted_scale, EXP_ON_NEG(MUL_SAT(diff * mask, mult))), bits_over_unit) - 128;
  153. output[tail_idx + i] = (int8_t)CLAMP(res, (int32_t)ACT_MAX, (int32_t)ACT_MIN);
  154. }
  155. else
  156. {
  157. output[tail_idx + i] = ACT_MIN;
  158. }
  159. }
  160. input += row_size;
  161. output += row_size;
  162. }
  163. #else
  164. const int32_t mask = (1 << shift);
  165. int32_t col = 0;
  166. int32_t row_idx;
  167. for (row_idx = 0; row_idx < num_rows; ++row_idx)
  168. {
  169. // Find the maximum value in order to ensure numerical stability
  170. int8_t max = *input;
  171. for (col = 1; col < row_size; ++col)
  172. {
  173. max = MAX(max, input[col]);
  174. }
  175. int8_t diff = 0;
  176. int32_t sum = 0;
  177. for (col = 0; col < row_size; ++col)
  178. {
  179. diff = input[col] - max;
  180. if (diff >= diff_min)
  181. {
  182. sum += DIV_POW2(EXP_ON_NEG(MUL_SAT(diff * mask, mult)), ACCUM_BITS);
  183. }
  184. }
  185. const int32_t headroom = __CLZ(sum);
  186. const int32_t bits_over_unit = ACCUM_BITS - headroom + 23;
  187. const int32_t shifted_scale = ONE_OVER1((sum << headroom) - (1 << 31));
  188. for (col = 0; col < row_size; ++col)
  189. {
  190. diff = input[col] - max;
  191. if (diff >= diff_min)
  192. {
  193. const int32_t res = DIV_POW2(MUL_SAT(shifted_scale, EXP_ON_NEG(MUL_SAT(diff * mask, mult))), bits_over_unit) - 128;
  194. output[col] = (int8_t)CLAMP(res, (int32_t)127, (int32_t)-128);
  195. }
  196. else
  197. {
  198. output[col] = -128;
  199. }
  200. }
  201. input += row_size;
  202. output += row_size;
  203. }
  204. #endif
  205. }
  206. /**
  207. * @} end of Softmax group
  208. */