arm_convolve_1x1_s8.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * SPDX-FileCopyrightText: Copyright 2022-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
  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_convolve_1x1_s8.c
  21. * Description: Generic s8 version of 1x1 convolution
  22. *
  23. * $Date: 04 January 2024
  24. * $Revision: V.1.1.0
  25. *
  26. * Target : Arm(R) M-Profile Architecture
  27. *
  28. * -------------------------------------------------------------------- */
  29. #include "arm_nnfunctions.h"
  30. #include "arm_nnsupportfunctions.h"
  31. /**
  32. * @ingroup Public
  33. */
  34. /**
  35. * @addtogroup NNConv
  36. * @{
  37. */
  38. /*
  39. * A more generic version of s8 1x1 convolution intended for non-unity strides. This is slower
  40. * than the _fast() version if used for unity stride values.
  41. *
  42. * Refer header file for details.
  43. *
  44. */
  45. arm_cmsis_nn_status arm_convolve_1x1_s8(const cmsis_nn_context *ctx,
  46. const cmsis_nn_conv_params *conv_params,
  47. const cmsis_nn_per_channel_quant_params *quant_params,
  48. const cmsis_nn_dims *input_dims,
  49. const int8_t *input_data,
  50. const cmsis_nn_dims *filter_dims,
  51. const int8_t *filter_data,
  52. const cmsis_nn_dims *bias_dims,
  53. const int32_t *bias_data,
  54. const cmsis_nn_dims *output_dims,
  55. int8_t *output_data)
  56. {
  57. (void)ctx;
  58. (void)filter_dims;
  59. (void)bias_dims;
  60. if (conv_params->padding.w != 0 || conv_params->padding.h != 0)
  61. {
  62. return ARM_CMSIS_NN_ARG_ERROR;
  63. }
  64. const int32_t lhs_rows = output_dims->w;
  65. const int32_t rhs_rows = output_dims->c;
  66. const int32_t rhs_cols = input_dims->c;
  67. const int32_t stride_w = conv_params->stride.w;
  68. const int32_t input_inc = input_dims->w * conv_params->stride.h * rhs_cols;
  69. const int32_t output_inc = output_dims->w * rhs_rows;
  70. const int32_t output_h = output_dims->h;
  71. const int32_t batch = input_dims->n;
  72. const int8_t *input_data_ref = input_data;
  73. for (int i_batch = 0; i_batch < batch; i_batch++)
  74. {
  75. input_data = input_data_ref + (i_batch * rhs_cols * input_dims->w * input_dims->h);
  76. for (int i_output_h = 0; i_output_h < output_h; i_output_h++)
  77. {
  78. // Process one input row
  79. arm_cmsis_nn_status result = arm_nn_mat_mult_nt_t_s8(input_data,
  80. filter_data,
  81. bias_data,
  82. output_data,
  83. quant_params->multiplier,
  84. quant_params->shift,
  85. lhs_rows,
  86. rhs_rows,
  87. rhs_cols,
  88. conv_params->input_offset,
  89. conv_params->output_offset,
  90. conv_params->activation.min,
  91. conv_params->activation.max,
  92. rhs_rows,
  93. rhs_cols * stride_w);
  94. if (result != ARM_CMSIS_NN_SUCCESS)
  95. {
  96. return result;
  97. }
  98. input_data += input_inc;
  99. output_data += output_inc;
  100. }
  101. }
  102. /* Return to application */
  103. return ARM_CMSIS_NN_SUCCESS;
  104. }
  105. /**
  106. * @} end of NNConv group
  107. */