1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/*
* Copyright (C) 2010-2022 Arm Limited or its affiliates.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* ----------------------------------------------------------------------
* Project: CMSIS NN Library
* Title: arm_svdf_s8.c
* Description: S8 basic SVDF layer function
*
* $Date: 28 April 2022
* $Revision: V.3.0.1
*
* Target Processor: Cortex-M processors
*
* -------------------------------------------------------------------- */
#include "arm_nnfunctions.h"
#include "arm_nnsupportfunctions.h"
/**
* @ingroup groupNN
*/
/**
* @addtogroup SVDF
* @{
*/
/*
* S8 SVDF layer function for TensorFlow Lite with 8 bit state tensor
*
* Refer to header file for details.
*
*/
arm_status 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 q7_t *input_data,
const cmsis_nn_dims *state_dims,
q7_t *state_data,
const cmsis_nn_dims *weights_feature_dims,
const q7_t *weights_feature_data,
const cmsis_nn_dims *weights_time_dims,
const q7_t *weights_time_data,
const cmsis_nn_dims *bias_dims,
const q31_t *bias_data,
const cmsis_nn_dims *output_dims,
q7_t *output_data)
{
(void)bias_dims;
(void)state_dims;
(void)output_dims;
const q31_t multiplier_in = input_quant_params->multiplier;
const q31_t shift_in = input_quant_params->shift;
const q31_t multiplier_out = output_quant_params->multiplier;
const q31_t shift_2 = output_quant_params->shift;
const int32_t zp_in = svdf_params->input_offset;
const int32_t zp_out = svdf_params->output_offset;
const int32_t in_activation_min = svdf_params->input_activation.min;
const int32_t in_activation_max = svdf_params->input_activation.max;
const int32_t out_activation_min = svdf_params->output_activation.min;
const int32_t out_activation_max = svdf_params->output_activation.max;
const int16_t rank = svdf_params->rank;
const int32_t input_batches = input_dims->n;
const int32_t input_height = input_dims->h;
const int32_t feature_batches = weights_feature_dims->n;
const int32_t time_batches = weights_time_dims->h;
const int32_t unit_count = feature_batches / rank;
if (input_ctx->buf == NULL)
{
return ARM_MATH_ARGUMENT_ERROR;
}
q31_t *buffer_a = (q31_t *)input_ctx->buf;
if (output_ctx->buf == NULL)
{
return ARM_MATH_ARGUMENT_ERROR;
}
q31_t *buffer_b = (q31_t *)output_ctx->buf;
// Left shift state
memmove((int8_t *)state_data,
(int8_t *)state_data + 1,
(size_t)((input_batches * feature_batches * time_batches - 1) * (int32_t)sizeof(int8_t)));
// Matrix multiplication input * feature weight
for (int i_batch = 0; i_batch < input_batches; i_batch++)
{
q7_t *res_ptr = state_data + (time_batches * i_batch * feature_batches) + (time_batches - 1);
const q7_t *weight = weights_feature_data;
const q7_t *input = input_data + i_batch * input_height;
arm_status res = arm_nn_vec_mat_mult_t_s8(input,
weight,
NULL,
res_ptr,
-zp_in,
0,
0,
multiplier_in,
shift_in,
input_height,
feature_batches,
in_activation_min,
in_activation_max,
time_batches);
if (res != ARM_MATH_SUCCESS)
{
return res;
}
}
// Matrix multiplicate time weight * state tensors
{
q31_t *ptr_a = buffer_a;
const int8_t *v2 = state_data;
for (int i_batch = 0; i_batch < input_batches; i_batch++)
{
const int8_t *v1 = weights_time_data;
for (int i_feature_batch = 0; i_feature_batch < feature_batches; i_feature_batch++)
{
*ptr_a = 0;
int32_t sum = 0;
#if defined(ARM_MATH_DSP) && !defined(ARM_MATH_MVEI)
// Perform matrix multiplication in blocks of four
int j = 0;
int32_t block_count = time_batches >> 2;
for (int i = 0; i < block_count; i++)
{
j += 4;
q31_t r1_1, r1_2, r2_1, r2_2;
v1 = read_and_pad_reordered(v1, &r1_1, &r1_2);
v2 = read_and_pad_reordered(v2, &r2_1, &r2_2);
sum = __SMLAD(r1_1, r2_1, sum);
sum = __SMLAD(r1_2, r2_2, sum);
}
// Process the remaining data
for (; j < time_batches; j++)
{
sum += *v1 * *v2;
v1++;
v2++;
}
#else
for (int j = 0; j < time_batches; j++)
{
sum += *v1 * *v2;
v1++;
v2++;
}
#endif
*ptr_a = sum;
ptr_a++;
}
}
}
if (bias_data)
{
if (unit_count == feature_batches)
{
for (int i = 0; i < input_batches; i++)
{
q31_t *output_temp = buffer_b + i * feature_batches;
const q31_t *ptr_a = buffer_a + i * feature_batches;
const int32_t *bi = bias_data;
for (int j = 0; j < feature_batches; j++)
{
output_temp[j] = ptr_a[j] + bi[j];
}
}
}
else
{
for (int i_batch = 0; i_batch < input_batches; i_batch++)
{
q31_t *output_data_temp = buffer_b + i_batch * unit_count;
q31_t *ptr_a = buffer_a + i_batch * feature_batches;
for (int i = 0; i < unit_count; i++)
{
int32_t sum = bias_data[i];
for (int j = 0; j < rank; j++)
{
sum += *ptr_a;
ptr_a++;
}
output_data_temp[i] = sum;
}
}
}
}
else
{
for (int i_batch = 0; i_batch < input_batches; i_batch++)
{
q31_t *output_data_temp = buffer_b + i_batch * unit_count;
q31_t *ptr_a = buffer_a + i_batch * feature_batches;
for (int i = 0; i < unit_count; i++)
{
int32_t sum = 0;
for (int j = 0; j < rank; j++)
{
sum += *ptr_a;
ptr_a++;
}
output_data_temp[i] = sum;
}
}
}
#if defined(ARM_MATH_MVEI)
int32_t num_elements = input_batches * unit_count;
const int32_t loop_count = (num_elements + 3) / 4;
for (int i_op = 0; i_op < loop_count; i_op++)
{
mve_pred16_t p = vctp32q((uint32_t)num_elements);
int32x4_t op = vldrwq_z_s32(buffer_b, p);
op = arm_requantize_mve(op, multiplier_out, shift_2);
op = vaddq_n_s32(op, zp_out);
const int32x4_t min_vec = vdupq_n_s32((int8_t)out_activation_min);
const int32x4_t max_vec = vdupq_n_s32((int8_t)out_activation_max);
op = vmaxq_s32(op, min_vec);
op = vminq_s32(op, max_vec);
vstrbq_p_s32(output_data, op, p);
output_data += 4;
buffer_b += 4;
num_elements -= 4;
}
#else
for (int i = 0; i < input_batches * unit_count; i++)
{
output_data[i] = (q7_t)CLAMP(
arm_nn_requantize(buffer_b[i], multiplier_out, shift_2) + zp_out, out_activation_max, out_activation_min);
}
#endif
return (ARM_MATH_SUCCESS);
}
/**
* @} end of SVDF group
*/
|