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