arm_softmax_q7.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2010-2018 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_q7.c
  21. * Description: Q7 softmax function
  22. *
  23. * $Date: 20. February 2018
  24. * $Revision: V.1.0.0
  25. *
  26. * Target Processor: Cortex-M cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_math.h"
  30. #include "arm_nnfunctions.h"
  31. /**
  32. * @ingroup groupNN
  33. */
  34. /**
  35. * @addtogroup Softmax
  36. * @{
  37. */
  38. /**
  39. * @brief Q7 softmax function
  40. * @param[in] vec_in pointer to input vector
  41. * @param[in] dim_vec input vector dimention
  42. * @param[out] p_out pointer to output vector
  43. * @return none.
  44. *
  45. * @details
  46. *
  47. * Here, instead of typical natural logarithm e based softmax, we use
  48. * 2-based softmax here, i.e.,:
  49. *
  50. * y_i = 2^(x_i) / sum(2^x_j)
  51. *
  52. * The relative output will be different here.
  53. * But mathematically, the gradient will be the same
  54. * with a log(2) scaling factor.
  55. *
  56. */
  57. void arm_softmax_q7(const q7_t * vec_in, const uint16_t dim_vec, q7_t * p_out)
  58. {
  59. q31_t sum;
  60. int16_t i;
  61. uint8_t shift;
  62. q15_t base;
  63. base = -257;
  64. /* We first search for the maximum */
  65. for (i = 0; i < dim_vec; i++)
  66. {
  67. if (vec_in[i] > base)
  68. {
  69. base = vec_in[i];
  70. }
  71. }
  72. /*
  73. * So the base is set to max-8, meaning
  74. * that we ignore really small values.
  75. * anyway, they will be 0 after shrinking to q7_t.
  76. */
  77. base = base - 8;
  78. sum = 0;
  79. for (i = 0; i < dim_vec; i++)
  80. {
  81. if (vec_in[i] > base)
  82. {
  83. shift = (uint8_t)__USAT(vec_in[i] - base, 5);
  84. sum += 0x1 << shift;
  85. }
  86. }
  87. /* This is effectively (0x1 << 20) / sum */
  88. int output_base = 0x100000 / sum;
  89. /*
  90. * Final confidence will be output_base >> ( 13 - (vec_in[i] - base) )
  91. * so 128 (0x1<<7) -> 100% confidence when sum = 0x1 << 8, output_base = 0x1 << 12
  92. * and vec_in[i]-base = 8
  93. */
  94. for (i = 0; i < dim_vec; i++)
  95. {
  96. if (vec_in[i] > base)
  97. {
  98. /* Here minimum value of 13+base-vec_in[i] will be 5 */
  99. shift = (uint8_t)__USAT(13+base-vec_in[i], 5);
  100. p_out[i] = (q7_t) __SSAT((output_base >> shift), 8);
  101. } else {
  102. p_out[i] = 0;
  103. }
  104. }
  105. }
  106. /**
  107. * @} end of Softmax group
  108. */