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