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