1 /*
2 * SPDX-FileCopyrightText: Copyright 2010-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 /* ----------------------------------------------------------------------
20 * Project: CMSIS NN Library
21 * Title: arm_convolve_1_x_n_s4.c
22 * Description: s4 version of 1xN convolution using symmetric quantization.
23 *
24 * $Date: 10 April 2024
25 * $Revision: V.1.0.0
26 *
27 * Target : Arm(R) M-Profile Architecture
28 *
29 * -------------------------------------------------------------------- */
30
31 #include "arm_nnfunctions.h"
32 #include "arm_nnsupportfunctions.h"
33 /**
34 * @ingroup Public
35 */
36
37 /**
38 * @addtogroup NNConv
39 * @{
40 */
41
42 /*
43 * 1xN s4 convolution function.
44 *
45 * Refer header file for details.
46 *
47 */
48
arm_convolve_1_x_n_s4(const cmsis_nn_context * ctx,const cmsis_nn_conv_params * conv_params,const cmsis_nn_per_channel_quant_params * quant_params,const cmsis_nn_dims * input_dims,const int8_t * input_data,const cmsis_nn_dims * filter_dims,const int8_t * filter_data,const cmsis_nn_dims * bias_dims,const int32_t * bias_data,const cmsis_nn_dims * output_dims,int8_t * output_data)49 arm_cmsis_nn_status arm_convolve_1_x_n_s4(const cmsis_nn_context *ctx,
50 const cmsis_nn_conv_params *conv_params,
51 const cmsis_nn_per_channel_quant_params *quant_params,
52 const cmsis_nn_dims *input_dims,
53 const int8_t *input_data,
54 const cmsis_nn_dims *filter_dims,
55 const int8_t *filter_data,
56 const cmsis_nn_dims *bias_dims,
57 const int32_t *bias_data,
58 const cmsis_nn_dims *output_dims,
59 int8_t *output_data)
60 {
61 arm_cmsis_nn_status status = ARM_CMSIS_NN_SUCCESS;
62 int32_t buffer_size = arm_convolve_1_x_n_s4_get_buffer_size(conv_params, input_dims, filter_dims, output_dims);
63 /* The wrapper API is the ultimate reference for argument check */
64 if ((input_dims->h != 1) || conv_params->dilation.w != 1 || (buffer_size != 0 && ctx->buf == NULL) ||
65 conv_params->stride.w == 0 || (conv_params->stride.w * input_dims->c % 4 != 0))
66 {
67 status = ARM_CMSIS_NN_ARG_ERROR;
68 goto out;
69 }
70
71 #if defined(ARM_MATH_MVEI)
72 (void)bias_dims;
73 const uint16_t input_x = input_dims->w;
74 const uint16_t kernel_x = filter_dims->w;
75 const uint16_t output_x = output_dims->w;
76 const uint16_t output_ch = output_dims->c;
77 const uint16_t input_ch = input_dims->c;
78 const uint16_t pad_x = conv_params->padding.w;
79 const uint16_t stride_x = conv_params->stride.w;
80
81 // Total pad for dilation of 1
82 const int32_t total_pad = ((output_x - 1) * stride_x + kernel_x - input_x);
83 const int32_t asym_pad = total_pad % 2;
84
85 if (pad_x * 2 + asym_pad != total_pad)
86 {
87 return ARM_CMSIS_NN_FAILURE;
88 }
89
90 const int32_t right_pad_num = pad_x + asym_pad != 0 ? MAX(1, (pad_x + asym_pad + stride_x - 1) / stride_x) : 0;
91 const int32_t left_pad_num = pad_x != 0 ? MAX(1, (pad_x + stride_x - 1) / stride_x) : 0;
92 const int32_t no_pad_num = MAX(output_x - (right_pad_num + left_pad_num), 0);
93
94 if (right_pad_num + no_pad_num + left_pad_num != output_x)
95 {
96 return ARM_CMSIS_NN_FAILURE;
97 }
98
99 for (int i_batch = 0; i_batch < input_dims->n; i_batch++)
100 {
101 // Handle left padded sections
102 int32_t lhs_rows = left_pad_num;
103 const int32_t rhs_cols = kernel_x * input_dims->c;
104 const int32_t rhs_rows = output_dims->c;
105 const int32_t lhs_offset = input_ch * stride_x;
106
107 int32_t out_idx = 0;
108
109 for (int i = 0; i < lhs_rows; i++)
110 {
111 const int32_t est_input_x_idx = stride_x * i - pad_x;
112 const int32_t ker_begin_idx = -est_input_x_idx;
113 const int32_t actual_kernel_len = kernel_x - ker_begin_idx;
114 status = arm_nn_mat_mul_core_1x_s4(actual_kernel_len * input_ch,
115 ker_begin_idx * input_ch,
116 input_data,
117 filter_data + ((ker_begin_idx * input_ch) >> 1),
118 output_ch,
119 conv_params,
120 quant_params,
121 bias_data,
122 output_data);
123 output_data += output_ch;
124 }
125
126 out_idx += lhs_rows;
127 int32_t input_start = stride_x * lhs_rows - pad_x;
128
129 if (input_start < 0)
130 {
131 return ARM_CMSIS_NN_FAILURE;
132 }
133 /* Non padded elements */
134 input_start *= input_ch;
135 lhs_rows = no_pad_num;
136 arm_nn_mat_mult_nt_t_s4(input_data + input_start,
137 filter_data,
138 bias_data,
139 output_data,
140 quant_params->multiplier,
141 quant_params->shift,
142 lhs_rows,
143 rhs_rows,
144 rhs_cols,
145 conv_params->input_offset,
146 conv_params->output_offset,
147 conv_params->activation.min,
148 conv_params->activation.max,
149 lhs_offset);
150
151 output_data += lhs_rows * rhs_rows;
152 /* Right padded elements */
153 out_idx += lhs_rows;
154 lhs_rows = output_x - out_idx;
155
156 if (lhs_rows < 0)
157 {
158 return ARM_CMSIS_NN_FAILURE;
159 }
160
161 for (int i = out_idx; i < output_x; i++)
162 {
163 const int32_t est_input_x_idx = stride_x * i - pad_x;
164 const int32_t ker_end_idx = MIN(kernel_x, input_x - est_input_x_idx);
165 status = arm_nn_mat_mul_core_1x_s4(ker_end_idx * input_ch,
166 (kernel_x - ker_end_idx) * input_ch,
167 input_data + est_input_x_idx * input_ch,
168 filter_data,
169 output_ch,
170 conv_params,
171 quant_params,
172 bias_data,
173 output_data);
174 output_data += output_ch;
175 }
176 /* Advance to the next batch */
177 input_data += (input_x * input_ch);
178 }
179 #else
180 status = arm_convolve_s4(ctx,
181 conv_params,
182 quant_params,
183 input_dims,
184 input_data,
185 filter_dims,
186 filter_data,
187 bias_dims,
188 bias_data,
189 output_dims,
190 output_data);
191
192 #endif
193
194 out:
195 /* Return to application */
196 return status;
197 }
198
199 /**
200 * @} end of NNConv group
201 */
202