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_fully_connected_per_channel_s8
22  * Description:  Fully connected function compatible with TF Lite.
23  *
24  * $Date:        15 Aug 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 /**
35  *  @ingroup Public
36  */
37 
38 /**
39  * @addtogroup FC
40  * @{
41  */
42 
43 /*
44  * S8 basic fully-connected and matrix multiplication layer function using per-channel quantization for TensorFlow Lite
45  *
46  * Refer header file for details.
47  *
48  */
arm_fully_connected_per_channel_s8(const cmsis_nn_context * ctx,const cmsis_nn_fc_params * fc_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 * kernel,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_fully_connected_per_channel_s8(const cmsis_nn_context *ctx,
50                                                        const cmsis_nn_fc_params *fc_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 *kernel,
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     (void)bias_dims;
62 
63     int32_t batch_cnt = input_dims->n;
64 
65 #if defined(ARM_MATH_MVEI)
66     if (ctx->buf == NULL)
67     {
68         return (ARM_CMSIS_NN_ARG_ERROR);
69     }
70 #endif
71 
72     const int32_t *kernel_sum = (const int32_t *)ctx->buf;
73 
74     while (batch_cnt)
75     {
76 
77         arm_nn_vec_mat_mult_t_per_ch_s8(input_data,
78                                         kernel,
79                                         kernel_sum,
80                                         bias_data,
81                                         output_data,
82                                         fc_params->input_offset,
83                                         fc_params->output_offset,
84                                         quant_params->multiplier,
85                                         quant_params->shift,
86                                         filter_dims->n, /* col_dim or accum_depth */
87                                         output_dims->c, /* row_dim or output_depth */
88                                         fc_params->activation.min,
89                                         fc_params->activation.max,
90                                         1L,
91                                         fc_params->filter_offset);
92 
93         input_data += filter_dims->n;
94         output_data += output_dims->c;
95         batch_cnt--;
96     }
97     return (ARM_CMSIS_NN_SUCCESS);
98 }
99 
100 /**
101  * @} end of FC group
102  */
103