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_svdf_s8.c
22  * Description:  S8 basic SVDF layer function
23  *
24  * $Date:        5 January 2023
25  * $Revision:    V.5.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 SVDF
40  * @{
41  */
42 
43 /*
44  * S8 SVDF layer function for TensorFlow Lite with 8 bit state tensor
45  *
46  * Refer to header file for details.
47  *
48  */
49 
arm_svdf_s8(const cmsis_nn_context * input_ctx,const cmsis_nn_context * output_ctx,const cmsis_nn_svdf_params * svdf_params,const cmsis_nn_per_tensor_quant_params * input_quant_params,const cmsis_nn_per_tensor_quant_params * output_quant_params,const cmsis_nn_dims * input_dims,const int8_t * input_data,const cmsis_nn_dims * state_dims,int8_t * state_data,const cmsis_nn_dims * weights_feature_dims,const int8_t * weights_feature_data,const cmsis_nn_dims * weights_time_dims,const int8_t * weights_time_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_svdf_s8(const cmsis_nn_context *input_ctx,
51                                 const cmsis_nn_context *output_ctx,
52                                 const cmsis_nn_svdf_params *svdf_params,
53                                 const cmsis_nn_per_tensor_quant_params *input_quant_params,
54                                 const cmsis_nn_per_tensor_quant_params *output_quant_params,
55                                 const cmsis_nn_dims *input_dims,
56                                 const int8_t *input_data,
57                                 const cmsis_nn_dims *state_dims,
58                                 int8_t *state_data,
59                                 const cmsis_nn_dims *weights_feature_dims,
60                                 const int8_t *weights_feature_data,
61                                 const cmsis_nn_dims *weights_time_dims,
62                                 const int8_t *weights_time_data,
63                                 const cmsis_nn_dims *bias_dims,
64                                 const int32_t *bias_data,
65                                 const cmsis_nn_dims *output_dims,
66                                 int8_t *output_data)
67 {
68     (void)bias_dims;
69     (void)state_dims;
70     (void)output_dims;
71 
72     const int32_t multiplier_in = input_quant_params->multiplier;
73     const int32_t shift_in = input_quant_params->shift;
74     const int32_t multiplier_out = output_quant_params->multiplier;
75     const int32_t shift_2 = output_quant_params->shift;
76     const int32_t zp_in = svdf_params->input_offset;
77     const int32_t zp_out = svdf_params->output_offset;
78     const int32_t in_activation_min = svdf_params->input_activation.min;
79     const int32_t in_activation_max = svdf_params->input_activation.max;
80     const int32_t out_activation_min = svdf_params->output_activation.min;
81     const int32_t out_activation_max = svdf_params->output_activation.max;
82     const int16_t rank = svdf_params->rank;
83 
84     const int32_t input_batches = input_dims->n;
85     const int32_t input_height = input_dims->h;
86     const int32_t feature_batches = weights_feature_dims->n;
87     const int32_t time_batches = weights_time_dims->h;
88     const int32_t unit_count = feature_batches / rank;
89 
90     if (input_ctx->buf == NULL)
91     {
92         return ARM_CMSIS_NN_ARG_ERROR;
93     }
94     int32_t *buffer_a = (int32_t *)input_ctx->buf;
95 
96     if (output_ctx->buf == NULL)
97     {
98         return ARM_CMSIS_NN_ARG_ERROR;
99     }
100     int32_t *buffer_b = (int32_t *)output_ctx->buf;
101 
102     // Left shift state
103     memmove((int8_t *)state_data,
104             (int8_t *)state_data + 1,
105             (size_t)((input_batches * feature_batches * time_batches - 1) * (int32_t)sizeof(int8_t)));
106 
107     // Matrix multiplication input * feature weight
108     for (int i_batch = 0; i_batch < input_batches; i_batch++)
109     {
110         int8_t *res_ptr = state_data + (time_batches * i_batch * feature_batches) + (time_batches - 1);
111         const int8_t *weight = weights_feature_data;
112         const int8_t *input = input_data + i_batch * input_height;
113 
114         arm_cmsis_nn_status res = arm_nn_vec_mat_mult_t_s8(input,
115                                                            weight,
116                                                            NULL,
117                                                            res_ptr,
118                                                            -zp_in,
119                                                            0,
120                                                            multiplier_in,
121                                                            shift_in,
122                                                            input_height,
123                                                            feature_batches,
124                                                            in_activation_min,
125                                                            in_activation_max,
126                                                            time_batches);
127 
128         if (res != ARM_CMSIS_NN_SUCCESS)
129         {
130             return res;
131         }
132     }
133 
134     // Matrix multiplicate time weight * state tensors
135     {
136         int32_t *ptr_a = buffer_a;
137         const int8_t *v2 = state_data;
138         for (int i_batch = 0; i_batch < input_batches; i_batch++)
139         {
140             const int8_t *v1 = weights_time_data;
141 
142             for (int i_feature_batch = 0; i_feature_batch < feature_batches; i_feature_batch++)
143             {
144                 *ptr_a = 0;
145                 int32_t sum = 0;
146 #if defined(ARM_MATH_DSP) && !defined(ARM_MATH_MVEI)
147                 // Perform matrix multiplication in blocks of four
148                 int j = 0;
149                 int32_t block_count = time_batches >> 2;
150                 for (int i = 0; i < block_count; i++)
151                 {
152                     j += 4;
153 
154                     int32_t r1_1, r1_2, r2_1, r2_2;
155                     v1 = read_and_pad_reordered(v1, &r1_1, &r1_2);
156                     v2 = read_and_pad_reordered(v2, &r2_1, &r2_2);
157                     sum = SMLAD(r1_1, r2_1, sum);
158                     sum = SMLAD(r1_2, r2_2, sum);
159                 }
160 
161                 // Process the remaining data
162                 for (; j < time_batches; j++)
163                 {
164                     sum += *v1 * *v2;
165                     v1++;
166                     v2++;
167                 }
168 #else
169                 for (int j = 0; j < time_batches; j++)
170                 {
171                     sum += *v1 * *v2;
172                     v1++;
173                     v2++;
174                 }
175 #endif
176 
177                 *ptr_a = sum;
178                 ptr_a++;
179             }
180         }
181     }
182 
183     if (bias_data)
184     {
185         if (unit_count == feature_batches)
186         {
187             for (int i = 0; i < input_batches; i++)
188             {
189                 int32_t *output_temp = buffer_b + i * feature_batches;
190                 const int32_t *ptr_a = buffer_a + i * feature_batches;
191 
192                 const int32_t *bi = bias_data;
193                 for (int j = 0; j < feature_batches; j++)
194                 {
195                     output_temp[j] = ptr_a[j] + bi[j];
196                 }
197             }
198         }
199         else
200         {
201             for (int i_batch = 0; i_batch < input_batches; i_batch++)
202             {
203                 int32_t *output_data_temp = buffer_b + i_batch * unit_count;
204                 int32_t *ptr_a = buffer_a + i_batch * feature_batches;
205 
206                 for (int i = 0; i < unit_count; i++)
207                 {
208                     int32_t sum = bias_data[i];
209                     for (int j = 0; j < rank; j++)
210                     {
211                         sum += *ptr_a;
212                         ptr_a++;
213                     }
214                     output_data_temp[i] = sum;
215                 }
216             }
217         }
218     }
219     else
220     {
221         for (int i_batch = 0; i_batch < input_batches; i_batch++)
222         {
223             int32_t *output_data_temp = buffer_b + i_batch * unit_count;
224             int32_t *ptr_a = buffer_a + i_batch * feature_batches;
225 
226             for (int i = 0; i < unit_count; i++)
227             {
228                 int32_t sum = 0;
229                 for (int j = 0; j < rank; j++)
230                 {
231                     sum += *ptr_a;
232                     ptr_a++;
233                 }
234                 output_data_temp[i] = sum;
235             }
236         }
237     }
238 
239 #if defined(ARM_MATH_MVEI)
240     int32_t num_elements = input_batches * unit_count;
241     const int32_t loop_count = (num_elements + 3) / 4;
242     for (int i_op = 0; i_op < loop_count; i_op++)
243     {
244         mve_pred16_t p = vctp32q((uint32_t)num_elements);
245         int32x4_t op = vldrwq_z_s32(buffer_b, p);
246         op = arm_requantize_mve(op, multiplier_out, shift_2);
247         op = vaddq_n_s32(op, zp_out);
248         const int32x4_t min_vec = vdupq_n_s32((int8_t)out_activation_min);
249         const int32x4_t max_vec = vdupq_n_s32((int8_t)out_activation_max);
250         op = vmaxq_s32(op, min_vec);
251         op = vminq_s32(op, max_vec);
252         vstrbq_p_s32(output_data, op, p);
253         output_data += 4;
254         buffer_b += 4;
255         num_elements -= 4;
256     }
257 #else
258     for (int i = 0; i < input_batches * unit_count; i++)
259     {
260         output_data[i] = (int8_t)CLAMP(
261             arm_nn_requantize(buffer_b[i], multiplier_out, shift_2) + zp_out, out_activation_max, out_activation_min);
262     }
263 #endif
264 
265     return (ARM_CMSIS_NN_SUCCESS);
266 }
267 
268 /**
269  * @} end of SVDF group
270  */
271