arm_softmax_u8.c 2.8 KB

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