1 /*
2  * Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
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_wrapper_s8.c
22  * Description:  s8 convolution layer wrapper function with the main purpose to call the optimal kernel available in
23  * cmsis-nn to perform the convolution.
24  *
25  * $Date:        09. October 2020
26  * $Revision:    V.1.0.1
27  *
28  * Target Processor:  Cortex-M cores
29  *
30  * -------------------------------------------------------------------- */
31 
32 #include "arm_nnfunctions.h"
33 
34 /**
35  *  @ingroup groupNN
36  */
37 
38 /**
39  * @addtogroup NNConv
40  * @{
41  */
42 
43 /*
44  * Convolution layer
45  *
46  * Refer header file for details.
47  *
48  */
49 
arm_convolve_wrapper_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 q7_t * input_data,const cmsis_nn_dims * filter_dims,const q7_t * filter_data,const cmsis_nn_dims * bias_dims,const int32_t * bias_data,const cmsis_nn_dims * output_dims,q7_t * output_data)50 arm_status arm_convolve_wrapper_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 q7_t *input_data,
55                                    const cmsis_nn_dims *filter_dims,
56                                    const q7_t *filter_data,
57                                    const cmsis_nn_dims *bias_dims,
58                                    const int32_t *bias_data,
59                                    const cmsis_nn_dims *output_dims,
60                                    q7_t *output_data)
61 {
62     if ((conv_params->padding.w == 0) && (conv_params->padding.h == 0) && (input_dims->c % 4 == 0) &&
63         (conv_params->stride.w == 1) && (conv_params->stride.h == 1) && (filter_dims->w == 1) && (filter_dims->h == 1))
64     {
65         return arm_convolve_1x1_s8_fast(ctx,
66                                         conv_params,
67                                         quant_params,
68                                         input_dims,
69                                         input_data,
70                                         filter_dims,
71                                         filter_data,
72                                         bias_dims,
73                                         bias_data,
74                                         output_dims,
75                                         output_data);
76     }
77     else if ((output_dims->h == 1) && (input_dims->h == 1) && (filter_dims->h == 1) && (output_dims->w % 4 == 0) &&
78              (input_dims->n == 1))
79     {
80         return arm_convolve_1_x_n_s8(ctx,
81                                      conv_params,
82                                      quant_params,
83                                      input_dims,
84                                      input_data,
85                                      filter_dims,
86                                      filter_data,
87                                      bias_dims,
88                                      bias_data,
89                                      output_dims,
90                                      output_data);
91     }
92     else
93     {
94         return arm_convolve_s8(ctx,
95                                conv_params,
96                                quant_params,
97                                input_dims,
98                                input_data,
99                                filter_dims,
100                                filter_data,
101                                bias_dims,
102                                bias_data,
103                                output_dims,
104                                output_data);
105     }
106 }
107 
arm_convolve_wrapper_s8_get_buffer_size(const cmsis_nn_conv_params * conv_params,const cmsis_nn_dims * input_dims,const cmsis_nn_dims * filter_dims,const cmsis_nn_dims * output_dims)108 int32_t arm_convolve_wrapper_s8_get_buffer_size(const cmsis_nn_conv_params *conv_params,
109                                                 const cmsis_nn_dims *input_dims,
110                                                 const cmsis_nn_dims *filter_dims,
111                                                 const cmsis_nn_dims *output_dims)
112 {
113     if ((conv_params->padding.w == 0) && (conv_params->padding.h == 0) && (input_dims->c % 4 == 0) &&
114         (conv_params->stride.w == 1) && (conv_params->stride.h == 1) && (filter_dims->w == 1) && (filter_dims->h == 1))
115     {
116         return arm_convolve_1x1_s8_fast_get_buffer_size(input_dims);
117     }
118     else if ((output_dims->h == 1) && (input_dims->h == 1) && (filter_dims->h == 1) && (output_dims->w % 4 == 0) &&
119              (input_dims->n == 1))
120     {
121         return arm_convolve_1_x_n_s8_get_buffer_size(input_dims, filter_dims);
122     }
123     else
124     {
125         return arm_convolve_s8_get_buffer_size(input_dims, filter_dims);
126     }
127 }
128 
129 /**
130  * @} end of NNConv group
131  */
132