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 /* ----------------------------------------------------------------------
20 * Project: CMSIS NN Library
21 * Title: arm_convolve_1x1_s8.c
22 * Description: Generic s8 version of 1x1 convolution
23 *
24 * $Date: 04 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 * A more generic version of s8 1x1 convolution intended for non-unity strides. This is slower
45 * than the _fast() version if used for unity stride values.
46 *
47 * Refer header file for details.
48 *
49 */
arm_convolve_1x1_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_1x1_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 (void)ctx;
63 (void)filter_dims;
64 (void)bias_dims;
65 if (conv_params->padding.w != 0 || conv_params->padding.h != 0)
66 {
67 return ARM_CMSIS_NN_ARG_ERROR;
68 }
69
70 const int32_t lhs_rows = output_dims->w;
71 const int32_t rhs_rows = output_dims->c;
72 const int32_t rhs_cols = input_dims->c;
73 const int32_t stride_w = conv_params->stride.w;
74 const int32_t input_inc = input_dims->w * conv_params->stride.h * rhs_cols;
75 const int32_t output_inc = output_dims->w * rhs_rows;
76 const int32_t output_h = output_dims->h;
77 const int32_t batch = input_dims->n;
78 const int8_t *input_data_ref = input_data;
79
80 for (int i_batch = 0; i_batch < batch; i_batch++)
81 {
82 input_data = input_data_ref + (i_batch * rhs_cols * input_dims->w * input_dims->h);
83 for (int i_output_h = 0; i_output_h < output_h; i_output_h++)
84 {
85 // Process one input row
86 arm_cmsis_nn_status result = arm_nn_mat_mult_nt_t_s8(input_data,
87 filter_data,
88 bias_data,
89 output_data,
90 quant_params->multiplier,
91 quant_params->shift,
92 lhs_rows,
93 rhs_rows,
94 rhs_cols,
95 conv_params->input_offset,
96 conv_params->output_offset,
97 conv_params->activation.min,
98 conv_params->activation.max,
99 rhs_rows,
100 rhs_cols * stride_w);
101 if (result != ARM_CMSIS_NN_SUCCESS)
102 {
103 return result;
104 }
105 input_data += input_inc;
106 output_data += output_inc;
107 }
108 }
109
110 /* Return to application */
111 return ARM_CMSIS_NN_SUCCESS;
112 }
113
114 /**
115 * @} end of NNConv group
116 */
117