arm_softmax_u8.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_u8.c
  21. * Description: U8 softmax function
  22. *
  23. * $Date: March 31, 2020
  24. * $Revision: V.1.0.0
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #define ACCUM_BITS 12
  31. /**
  32. * @ingroup groupNN
  33. */
  34. /**
  35. * @addtogroup Softmax
  36. * @{
  37. */
  38. void arm_softmax_u8(const uint8_t *input,
  39. const int32_t num_rows,
  40. const int32_t row_size,
  41. const int32_t mult,
  42. const int32_t shift,
  43. const int32_t diff_min,
  44. uint8_t *output)
  45. {
  46. const int32_t mask = (1 << shift);
  47. int32_t col = 0;
  48. int32_t row_idx;
  49. for(row_idx = 0; row_idx < num_rows; ++row_idx)
  50. {
  51. // Find the maximum value in order to ensure numerical stability
  52. uint8_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. for (col = 0; col < row_size; ++col)
  60. {
  61. diff = input[col] - max;
  62. if(diff >= diff_min)
  63. {
  64. sum += DIV_POW2(EXP_ON_NEG(MUL_SAT(diff * mask, mult)), ACCUM_BITS);
  65. }
  66. }
  67. const int32_t headroom = __CLZ((uint32_t)sum);
  68. const int32_t bits_over_unit = ACCUM_BITS - headroom + 23;
  69. const int32_t shifted_scale = ONE_OVER1((sum << headroom) - (1 << 31));
  70. for (col = 0; col < row_size; ++col)
  71. {
  72. diff = input[col] - max;
  73. if (diff >= diff_min)
  74. {
  75. const int32_t res = DIV_POW2(MUL_SAT(shifted_scale, EXP_ON_NEG(MUL_SAT(diff * mask, mult))), bits_over_unit);
  76. output[col] = (int8_t) CLAMP(res, (int32_t)255, (int32_t)0);
  77. }
  78. else
  79. {
  80. output[col] = 0;
  81. }
  82. }
  83. input += row_size;
  84. output += row_size;
  85. }
  86. }
  87. /**
  88. * @} end of Softmax group
  89. */