1 /*
2  * SPDX-FileCopyrightText: Copyright 2022, 2024 Arm Limited and/or its affiliates <open-source-office.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_nn_lstm_calculate_gate_s8_s16.c
22  * Description:  Update single gate for an incremental step of LSTM function.
23  *
24  * $Date:        19 January 2024
25  * $Revision:    V.2.0.0
26  *
27  * Target Processor:  Cortex-M cores
28  *
29  * -------------------------------------------------------------------- */
30 
31 #include "arm_nn_tables.h"
32 #include "arm_nnfunctions.h"
33 #include "arm_nnsupportfunctions.h"
34 /**
35  * @ingroup groupSupport
36  */
37 
38 /**
39  * @defgroup supportLSTM LSTM
40  *
41  * Support functions for LSTM
42  *
43  */
44 
45 /**
46  * @addtogroup supportLSTM
47  * @{
48  */
49 
50 /*
51  * Calculates a single LSTM gate, int8x8_16 version.
52  * Refer to header file for details
53  */
arm_nn_lstm_calculate_gate_s8_s16(const int8_t * data_in,const int8_t * hidden_in,const cmsis_nn_lstm_gate * gate,const cmsis_nn_lstm_params * params,int16_t * output,const int32_t batch_offset)54 arm_cmsis_nn_status arm_nn_lstm_calculate_gate_s8_s16(const int8_t *data_in,
55                                                       const int8_t *hidden_in,
56                                                       const cmsis_nn_lstm_gate *gate,
57                                                       const cmsis_nn_lstm_params *params,
58                                                       int16_t *output,
59                                                       const int32_t batch_offset)
60 {
61 
62     memset(output, 0, params->hidden_size * params->batch_size * sizeof(int16_t));
63 
64     arm_nn_vec_mat_mul_result_acc_s8_s16(data_in,
65                                          gate->input_weights,
66                                          gate->input_effective_bias,
67                                          output,
68                                          gate->input_multiplier,
69                                          gate->input_shift,
70                                          params->input_size,
71                                          params->hidden_size,
72                                          params->batch_size,
73                                          batch_offset);
74 
75     if (hidden_in)
76     {
77         arm_nn_vec_mat_mul_result_acc_s8_s16(hidden_in,
78                                              gate->hidden_weights,
79                                              gate->hidden_effective_bias,
80                                              output,
81                                              gate->hidden_multiplier,
82                                              gate->hidden_shift,
83                                              params->hidden_size,
84                                              params->hidden_size,
85                                              params->batch_size,
86                                              batch_offset);
87     }
88 
89     arm_nn_activation_s16(output, output, params->hidden_size * params->batch_size, 0, gate->activation_type);
90 
91     return ARM_CMSIS_NN_SUCCESS;
92 }
93 /**
94  * @} end of supportLSTM group
95  */