1 /*
2 * SPDX-FileCopyrightText: Copyright 2023-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_transpose_conv_s8.c
22 * Description: s8 version of transpose convolution using symmetric quantization.
23 *
24 * $Date: 31 January 2024
25 * $Revision: V.1.1.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 * Basic s8 transpose convolution function.
45 *
46 * Refer header file for details.
47 *
48 */
arm_transpose_conv_s8(const cmsis_nn_context * ctx,const cmsis_nn_context * output_ctx,const cmsis_nn_transpose_conv_params * transpose_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_transpose_conv_s8(const cmsis_nn_context *ctx,
50 const cmsis_nn_context *output_ctx,
51 const cmsis_nn_transpose_conv_params *transpose_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 (void)bias_dims;
63
64 if (ctx->buf == NULL || output_ctx->buf == NULL)
65 {
66 return ARM_CMSIS_NN_ARG_ERROR;
67 }
68
69 const int32_t activation_min = transpose_conv_params->activation.min;
70 const int32_t activation_max = transpose_conv_params->activation.max;
71
72 const int32_t input_ch = input_dims->c;
73 const int32_t input_size = input_dims->w * input_dims->h;
74
75 const uint16_t kernel_x = filter_dims->w;
76 const uint16_t kernel_y = filter_dims->h;
77
78 const int32_t output_x = output_dims->w;
79 const int32_t output_y = output_dims->h;
80 const int32_t output_ch = output_dims->c;
81
82 const int32_t pad_x = transpose_conv_params->padding.w;
83 const int32_t pad_y = transpose_conv_params->padding.h;
84 const int32_t pad_x_offset = transpose_conv_params->padding_offsets.w;
85 const int32_t pad_y_offset = transpose_conv_params->padding_offsets.h;
86
87 const int32_t stride_x = transpose_conv_params->stride.w;
88 const int32_t stride_y = transpose_conv_params->stride.h;
89 const int32_t filter_size = filter_dims->w * filter_dims->h;
90
91 const int32_t *output_multiplier = quant_params->multiplier;
92 const int32_t *output_shift = quant_params->shift;
93
94 const int32_t out_offset = transpose_conv_params->output_offset;
95 const int32_t input_offset = transpose_conv_params->input_offset;
96
97 const int8_t *input_data_ptr = input_data;
98 int8_t *output_data_ptr = output_data;
99
100 int32_t *const col_data = (int32_t *)ctx->buf;
101 const int32_t col_buf_size = arm_transpose_conv_s8_get_buffer_size(input_dims, filter_dims, output_dims);
102
103 int32_t batch_cnt = input_dims->n;
104
105 int32_t *const img_buf = output_ctx->buf;
106 int32_t *img_buf_ptr = img_buf;
107
108 while (batch_cnt)
109 {
110 if (bias_data == NULL)
111 {
112 arm_memset_s8((int8_t *)img_buf_ptr, 0, output_x * output_y * output_ch * sizeof(int32_t));
113 }
114 else
115 {
116 int32_t *img_data = img_buf;
117
118 for (int i = 0; i < output_x * output_y; i++)
119 {
120 memcpy(img_data, bias_data, output_ch * sizeof(int32_t));
121 img_data += output_ch;
122 }
123 }
124
125 int32_t *col_data_ptr = col_data;
126 const int8_t *filter_data_ptr = filter_data;
127
128 arm_memset_s8((int8_t *)col_data_ptr, 0, col_buf_size);
129
130 for (int i_output_ch = 0; i_output_ch < output_ch; i_output_ch++)
131 {
132 arm_nn_mat_mult_nt_t_s8_s32(input_data_ptr,
133 filter_data_ptr,
134 col_data_ptr,
135 input_size,
136 input_ch,
137 filter_size,
138 input_offset,
139 output_ch);
140
141 filter_data_ptr += (input_ch * filter_size);
142 col_data_ptr++;
143 }
144
145 int32_t *col_buf = col_data;
146 int32_t *img_data = img_buf_ptr;
147 const int32_t col_y = (output_y + pad_y_offset + pad_y - kernel_y) / stride_y + 1;
148 const int32_t col_x = (output_x + pad_x_offset + pad_x - kernel_x) / stride_x + 1;
149
150 // Column to image
151 for (int i_col_y = 0, i_pad_y = -pad_y; i_col_y < col_y; i_col_y++, i_pad_y += stride_y)
152 {
153 for (int i_col_x = 0, i_pad_x = -pad_x; i_col_x < col_x; i_col_x++, i_pad_x += stride_x)
154 {
155 int32_t *dst_data = img_data + (i_pad_y * output_x + i_pad_x) * output_ch;
156
157 for (int32_t i_ker_y = i_pad_y; i_ker_y < i_pad_y + kernel_y; i_ker_y++)
158 {
159 for (int32_t i_ker_x = i_pad_x; i_ker_x < i_pad_x + kernel_x; i_ker_x++)
160 {
161 if (i_ker_y >= 0 && i_ker_y < output_y && i_ker_x >= 0 && i_ker_x < output_x)
162 {
163 for (int i_output_ch = 0; i_output_ch < output_ch; i_output_ch++)
164 {
165 dst_data[i_output_ch] += col_buf[i_output_ch];
166 }
167 }
168 dst_data += output_ch;
169 col_buf += output_ch;
170 }
171 dst_data += (output_x - kernel_x) * output_ch;
172 }
173 }
174 }
175 img_data = img_buf_ptr;
176 for (int i = 0; i < output_x * output_y; i++)
177 {
178 #if defined(ARM_MATH_MVEI)
179 int output_ch_idx = 0;
180 int8_t *ip_out_data = output_data_ptr;
181 for (int32_t i_channel_rmdr = output_ch; i_channel_rmdr > 0; i_channel_rmdr -= 4)
182 {
183 mve_pred16_t p = vctp32q((uint32_t)i_channel_rmdr);
184 int32x4_t result = vldrwq_z_s32(&img_data[output_ch_idx], p);
185 result = arm_requantize_mve_32x4(result,
186 vldrwq_z_s32(&output_multiplier[output_ch_idx], p),
187 vldrwq_z_s32(&output_shift[output_ch_idx], p));
188 result = vaddq_n_s32(result, out_offset);
189 result = vmaxq_s32(result, vdupq_n_s32(activation_min));
190 result = vminq_s32(result, vdupq_n_s32(activation_max));
191 vstrbq_p_s32(ip_out_data, result, p);
192 ip_out_data += 4;
193 output_ch_idx += 4;
194 }
195 output_data_ptr += output_ch;
196 #else
197 int i_output_ch = 0;
198 for (; i_output_ch < output_ch; i_output_ch++)
199 {
200 int32_t result =
201 arm_nn_requantize(img_data[i_output_ch], output_multiplier[i_output_ch], output_shift[i_output_ch]);
202 result += out_offset;
203 result = MAX(result, activation_min);
204 result = MIN(result, activation_max);
205 *output_data_ptr++ = (int8_t)result;
206 }
207 #endif
208 img_data += output_ch;
209 }
210 input_data_ptr += (input_size * input_ch);
211 batch_cnt--;
212 }
213 /* Return to application */
214 return ARM_CMSIS_NN_SUCCESS;
215 }
216
217 /**
218 * @} end of NNConv group
219 */
220