arm_softmax_with_batch_q7.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_with_batch_q7.c
  21. * Description: Q7 softmax function
  22. *
  23. * $Date: 09. October 2020
  24. * $Revision: V.1.0.1
  25. *
  26. * Target Processor: Cortex-M and Cortex-A cores
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. /**
  31. * @ingroup groupNN
  32. */
  33. /**
  34. * @addtogroup Softmax
  35. * @{
  36. */
  37. /**
  38. * @brief Q7 softmax function with batch parameter
  39. * @param[in] vec_in pointer to input vector
  40. * @param[in] nb_batches number of batches
  41. * @param[in] dim_vec input vector dimention
  42. * @param[out] p_out pointer to output vector
  43. *
  44. * @details
  45. *
  46. * Here, instead of typical natural logarithm e based softmax, we use
  47. * 2-based softmax here, i.e.,:
  48. *
  49. * y_i = 2^(x_i) / sum(2^x_j)
  50. *
  51. * The relative output will be different here.
  52. * But mathematically, the gradient will be the same
  53. * with a log(2) scaling factor.
  54. *
  55. */
  56. void arm_softmax_with_batch_q7(const q7_t *vec_in, const uint16_t nb_batches, const uint16_t dim_vec, q7_t *p_out)
  57. {
  58. for (int i = 0; i < nb_batches; i++)
  59. {
  60. arm_softmax_q7(vec_in, dim_vec, p_out);
  61. vec_in += dim_vec;
  62. p_out += dim_vec;
  63. }
  64. }
  65. /**
  66. * @} end of Softmax group
  67. */