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_svdf_state_s16_s8.c
22  * Description:  S8 basic SVDF layer function with s16 state tensor
23  *
24  * $Date:        24 Sep 2024
25  * $Revision:    V.3.1.1
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 16 bit state tensor
45  *
46  * Refer to header file for details.
47  *
48  */
49 
arm_svdf_state_s16_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,int16_t * state_data,const cmsis_nn_dims * weights_feature_dims,const int8_t * weights_feature_data,const cmsis_nn_dims * weights_time_dims,const int16_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_state_s16_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                                           int16_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 int16_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     // Using memcpy on overlapping data is in general undefined behaviour, but since the behaviour of arm_memcpy_s8 is
104     // known it is certain that the data has been copied before it is overwritten in this case.
105 #ifdef ARM_MATH_MVEI
106     arm_memcpy_s8((int8_t *)state_data,
107                   (int8_t *)(state_data + 1),
108                   (size_t)((input_batches * feature_batches * time_batches - 1) * (int32_t)sizeof(int16_t)));
109 #else
110     memmove(state_data,
111             state_data + 1,
112             (size_t)((input_batches * feature_batches * time_batches - 1) * (int32_t)sizeof(int16_t)));
113 #endif
114 
115     // Matrix multiplication input * feature weight
116     for (int i_batch = 0; i_batch < input_batches; i_batch++)
117     {
118         int16_t *res_ptr = state_data + (time_batches * i_batch * feature_batches) + (time_batches - 1);
119         const int8_t *weight = weights_feature_data;
120         const int8_t *input = input_data + i_batch * input_height;
121 
122         arm_cmsis_nn_status res = arm_nn_vec_mat_mult_t_svdf_s8(input,
123                                                                 weight,
124                                                                 res_ptr,
125                                                                 -zp_in,
126                                                                 time_batches,
127                                                                 multiplier_in,
128                                                                 shift_in,
129                                                                 input_height,
130                                                                 feature_batches,
131                                                                 in_activation_min,
132                                                                 in_activation_max);
133 
134         if (res != ARM_CMSIS_NN_SUCCESS)
135         {
136             return res;
137         }
138     }
139 
140     {
141         // Matrix multiplication time weight * state tensors
142         int32_t *ptr_a = buffer_a;
143         const int16_t *v2 = state_data;
144         for (int i_batch = 0; i_batch < input_batches; i_batch++)
145         {
146             const int16_t *v1 = weights_time_data;
147 
148             for (int i_feature_batch = 0; i_feature_batch < feature_batches; i_feature_batch++)
149             {
150                 *ptr_a = 0;
151                 int32_t sum = 0;
152 #if defined(ARM_MATH_DSP) && !defined(ARM_MATH_MVEI)
153                 // Perform matrix multiplication in blocks of two
154                 int j = 0;
155                 int32_t block_count = time_batches >> 1;
156                 for (int i = 0; i < block_count; i++)
157                 {
158                     j += 2;
159                     int32_t r1 = arm_nn_read_q15x2_ia(&v1);
160                     int32_t r2 = arm_nn_read_q15x2_ia(&v2);
161 
162                     sum = SMLAD(r1, r2, sum);
163                 }
164 
165                 // Process the remaining data
166                 for (; j < time_batches; j++)
167                 {
168                     sum += *v1 * *v2;
169                     v1++;
170                     v2++;
171                 }
172 #else
173                 for (int j = 0; j < time_batches; j++)
174                 {
175                     sum += *v1 * *v2;
176                     v1++;
177                     v2++;
178                 }
179 #endif
180 
181                 *ptr_a = sum;
182                 ptr_a++;
183             }
184         }
185     }
186 
187     if (bias_data)
188     {
189         if (unit_count == feature_batches)
190         {
191             for (int i = 0; i < input_batches; i++)
192             {
193                 int32_t *output_temp = buffer_b + i * feature_batches;
194                 const int32_t *ptr_a = buffer_a + i * feature_batches;
195 
196                 const int32_t *bi = bias_data;
197                 for (int j = 0; j < feature_batches; j++)
198                 {
199                     output_temp[j] = ptr_a[j] + bi[j];
200                 }
201             }
202         }
203         else
204         {
205             for (int i_batch = 0; i_batch < input_batches; i_batch++)
206             {
207                 int32_t *output_data_temp = buffer_b + i_batch * unit_count;
208                 int32_t *ptr_a = buffer_a + i_batch * feature_batches;
209 
210                 for (int i = 0; i < unit_count; i++)
211                 {
212                     int32_t sum = bias_data[i];
213                     for (int j = 0; j < rank; j++)
214                     {
215                         sum += *ptr_a;
216                         ptr_a++;
217                     }
218                     output_data_temp[i] = sum;
219                 }
220             }
221         }
222     }
223     else
224     {
225         for (int i_batch = 0; i_batch < input_batches; i_batch++)
226         {
227             int32_t *output_data_temp = buffer_b + i_batch * unit_count;
228             int32_t *ptr_a = buffer_a + i_batch * feature_batches;
229 
230             for (int i = 0; i < unit_count; i++)
231             {
232                 int32_t sum = 0;
233                 for (int j = 0; j < rank; j++)
234                 {
235                     sum += *ptr_a;
236                     ptr_a++;
237                 }
238                 output_data_temp[i] = sum;
239             }
240         }
241     }
242 
243 #if defined(ARM_MATH_MVEI)
244     int32_t num_elements = input_batches * unit_count;
245     const int32_t loop_count = (num_elements + 3) / 4;
246     for (int i_op = 0; i_op < loop_count; i_op++)
247     {
248         mve_pred16_t p = vctp32q((uint32_t)num_elements);
249         int32x4_t op = vldrwq_z_s32(buffer_b, p);
250         op = arm_requantize_mve(op, multiplier_out, shift_2);
251         op = vaddq_n_s32(op, zp_out);
252         const int32x4_t min_vec = vdupq_n_s32((int8_t)out_activation_min);
253         const int32x4_t max_vec = vdupq_n_s32((int8_t)out_activation_max);
254         op = vmaxq_s32(op, min_vec);
255         op = vminq_s32(op, max_vec);
256         vstrbq_p_s32(output_data, op, p);
257         output_data += 4;
258         buffer_b += 4;
259         num_elements -= 4;
260     }
261 #else
262     for (int i = 0; i < input_batches * unit_count; i++)
263     {
264         output_data[i] = (int8_t)CLAMP(
265             arm_nn_requantize(buffer_b[i], multiplier_out, shift_2) + zp_out, out_activation_max, out_activation_min);
266     }
267 #endif
268 
269     return (ARM_CMSIS_NN_SUCCESS);
270 }
271 
272 /**
273  * @} end of SVDF group
274  */
275