arm_softmax_s8.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2010-2020 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: March 31, 2020
  24. * $Revision: V.1.5.1
  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. static 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 + 15) / 16;
  87. uint32_t r_count = (uint32_t)row_size;
  88. for (int i = 0; i < vec_count; i++)
  89. {
  90. mve_pred16_t p = vctp8q(r_count);
  91. const int8x16_t ip = vldrbq_z_s8(&input[i * 16], p);
  92. max = vmaxvq_p_s8(max, ip, p);
  93. r_count -= 16;
  94. }
  95. vec_count = row_size / 4;
  96. int32_t idx = 0;
  97. int32_t sum = 0;
  98. while (vec_count)
  99. {
  100. int32x4_t ip = vldrbq_s32(&input[idx * 4]);
  101. ip = vsubq_n_s32(ip, max);
  102. mve_pred16_t p = vcmpgeq_n_s32(ip, diff_min);
  103. if (p != 0)
  104. {
  105. ip = vmulq_n_s32(ip, mask);
  106. int32x4_t res = MUL_SAT_MVE(ip, vdupq_n_s32(mult));
  107. res = arm_exp_on_negative_values_mve_32x4(res);
  108. res = DIV_POW2_MVE(res, ACCUM_BITS);
  109. res = vpselq_s32(res, vdupq_n_s32(0), p);
  110. sum += vaddvq_s32(res);
  111. }
  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((uint32_t)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 = vsubq_n_s32(ip, max);
  133. mve_pred16_t p = vcmpgeq_n_s32(ip, diff_min);
  134. int32x4_t tmp_res;
  135. if (p != 0)
  136. {
  137. ip = vmulq_n_s32(ip, mask);
  138. tmp_res = MUL_SAT_MVE(ip, vdupq_n_s32(mult));
  139. tmp_res = arm_exp_on_negative_values_mve_32x4(tmp_res);
  140. tmp_res = MUL_SAT_MVE(vdupq_n_s32(shifted_scale), tmp_res);
  141. tmp_res = DIV_POW2_MVE(tmp_res, bits_over_unit);
  142. tmp_res += vdupq_n_s32(ACT_MIN);
  143. tmp_res = vmaxq_s32(tmp_res, vdupq_n_s32(ACT_MIN));
  144. tmp_res = vminq_s32(tmp_res, vdupq_n_s32(ACT_MAX));
  145. tmp_res = vpselq_s32(tmp_res, vdupq_n_s32(ACT_MIN), p);
  146. }
  147. else
  148. {
  149. tmp_res = vdupq_n_s32(ACT_MIN);
  150. }
  151. vstrbq_s32(&output[idx], tmp_res);
  152. vec_count--;
  153. idx += 4;
  154. }
  155. for (int i = 0; i < (row_size & 3); i++)
  156. {
  157. int32_t diff = input[tail_idx + i] - max;
  158. if (diff >= diff_min)
  159. {
  160. const int32_t res = DIV_POW2(MUL_SAT(shifted_scale, EXP_ON_NEG(MUL_SAT(diff * mask, mult))), bits_over_unit) - 128;
  161. output[tail_idx + i] = (int8_t)CLAMP(res, (int32_t)ACT_MAX, (int32_t)ACT_MIN);
  162. }
  163. else
  164. {
  165. output[tail_idx + i] = ACT_MIN;
  166. }
  167. }
  168. input += row_size;
  169. output += row_size;
  170. }
  171. #else
  172. const int32_t mask = (1 << shift);
  173. int32_t col = 0;
  174. int32_t row_idx;
  175. for (row_idx = 0; row_idx < num_rows; ++row_idx)
  176. {
  177. // Find the maximum value in order to ensure numerical stability
  178. int8_t max = *input;
  179. for (col = 1; col < row_size; ++col)
  180. {
  181. max = MAX(max, input[col]);
  182. }
  183. int8_t diff = 0;
  184. int32_t sum = 0;
  185. for (col = 0; col < row_size; ++col)
  186. {
  187. diff = input[col] - max;
  188. if (diff >= diff_min)
  189. {
  190. sum += DIV_POW2(EXP_ON_NEG(MUL_SAT(diff * mask, mult)), ACCUM_BITS);
  191. }
  192. }
  193. const int32_t headroom = __CLZ(sum);
  194. const int32_t bits_over_unit = ACCUM_BITS - headroom + 23;
  195. const int32_t shifted_scale = ONE_OVER1((sum << headroom) - (1 << 31));
  196. for (col = 0; col < row_size; ++col)
  197. {
  198. diff = input[col] - max;
  199. if (diff >= diff_min)
  200. {
  201. const int32_t res = DIV_POW2(MUL_SAT(shifted_scale, EXP_ON_NEG(MUL_SAT(diff * mask, mult))), bits_over_unit) - 128;
  202. output[col] = (int8_t)CLAMP(res, (int32_t)127, (int32_t)-128);
  203. }
  204. else
  205. {
  206. output[col] = -128;
  207. }
  208. }
  209. input += row_size;
  210. output += row_size;
  211. }
  212. #endif
  213. }
  214. /**
  215. * @} end of Softmax group
  216. */