Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /* ---------------------------------------------------------------------- |
2 | * Copyright (C) 2010-2018 Arm Limited. All rights reserved. |
||
3 | * |
||
4 | * |
||
5 | * Project: CMSIS NN Library |
||
6 | * Title: arm_nnexamples_gru.cpp |
||
7 | * |
||
8 | * Description: Gated Recurrent Unit Example |
||
9 | * |
||
10 | * Target Processor: Cortex-M4/Cortex-M7 |
||
11 | * |
||
12 | * Redistribution and use in source and binary forms, with or without |
||
13 | * modification, are permitted provided that the following conditions |
||
14 | * are met: |
||
15 | * - Redistributions of source code must retain the above copyright |
||
16 | * notice, this list of conditions and the following disclaimer. |
||
17 | * - Redistributions in binary form must reproduce the above copyright |
||
18 | * notice, this list of conditions and the following disclaimer in |
||
19 | * the documentation and/or other materials provided with the |
||
20 | * distribution. |
||
21 | * - Neither the name of Arm LIMITED nor the names of its contributors |
||
22 | * may be used to endorse or promote products derived from this |
||
23 | * software without specific prior written permission. |
||
24 | * |
||
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
26 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
27 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
||
28 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
||
29 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
30 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
||
31 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
32 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||
33 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
||
34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
||
35 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
36 | * POSSIBILITY OF SUCH DAMAGE. |
||
37 | * -------------------------------------------------------------------- */ |
||
38 | |||
39 | /** |
||
40 | * @ingroup groupExamples |
||
41 | */ |
||
42 | |||
43 | /** |
||
44 | * @defgroup GRUExample Gated Recurrent Unit Example |
||
45 | * |
||
46 | * \par Description: |
||
47 | * \par |
||
48 | * Demonstrates a gated recurrent unit (GRU) example with the use of fully-connected, |
||
49 | * Tanh/Sigmoid activation functions. |
||
50 | * |
||
51 | * \par Model definition: |
||
52 | * \par |
||
53 | * GRU is a type of recurrent neural network (RNN). It contains two sigmoid gates and one hidden |
||
54 | * state. |
||
55 | * \par |
||
56 | * The computation can be summarized as: |
||
57 | * <pre>z[t] = sigmoid( W_z ⋅ {h[t-1],x[t]} ) |
||
58 | * r[t] = sigmoid( W_r ⋅ {h[t-1],x[t]} ) |
||
59 | * n[t] = tanh( W_n ⋅ [r[t] × {h[t-1], x[t]} ) |
||
60 | * h[t] = (1 - z[t]) × h[t-1] + z[t] × n[t] </pre> |
||
61 | * \image html GRU.gif "Gate Recurrent Unit Diagram" |
||
62 | * |
||
63 | * \par Variables Description: |
||
64 | * \par |
||
65 | * \li \c update_gate_weights, \c reset_gate_weights, \c hidden_state_weights are weights corresponding to update gate (W_z), reset gate (W_r), and hidden state (W_n). |
||
66 | * \li \c update_gate_bias, \c reset_gate_bias, \c hidden_state_bias are layer bias arrays |
||
67 | * \li \c test_input1, \c test_input2, \c test_history are the inputs and initial history |
||
68 | * |
||
69 | * \par |
||
70 | * The buffer is allocated as: |
||
71 | * \par |
||
72 | * | reset | input | history | update | hidden_state | |
||
73 | * \par |
||
74 | * In this way, the concatination is automatically done since (reset, input) and (input, history) |
||
75 | * are physically concatinated in memory. |
||
76 | * \par |
||
77 | * The ordering of the weight matrix should be adjusted accordingly. |
||
78 | * |
||
79 | * |
||
80 | * |
||
81 | * \par CMSIS DSP Software Library Functions Used: |
||
82 | * \par |
||
83 | * - arm_fully_connected_mat_q7_vec_q15_opt() |
||
84 | * - arm_nn_activations_direct_q15() |
||
85 | * - arm_mult_q15() |
||
86 | * - arm_offset_q15() |
||
87 | * - arm_sub_q15() |
||
88 | * - arm_copy_q15() |
||
89 | * |
||
90 | * <b> Refer </b> |
||
91 | * \link arm_nnexamples_gru.cpp \endlink |
||
92 | * |
||
93 | */ |
||
94 | |||
95 | #include <stdio.h> |
||
96 | #include <stdlib.h> |
||
97 | #include <math.h> |
||
98 | #include "arm_nnexamples_gru_test_data.h" |
||
99 | #include "arm_math.h" |
||
100 | #include "arm_nnfunctions.h" |
||
101 | |||
102 | #ifdef _RTE_ |
||
103 | #include "RTE_Components.h" |
||
104 | #ifdef RTE_Compiler_EventRecorder |
||
105 | #include "EventRecorder.h" |
||
106 | #endif |
||
107 | #endif |
||
108 | |||
109 | #define DIM_HISTORY 32 |
||
110 | #define DIM_INPUT 32 |
||
111 | #define DIM_VEC 64 |
||
112 | |||
113 | #define USE_X4 |
||
114 | |||
115 | #ifndef USE_X4 |
||
116 | static q7_t update_gate_weights[DIM_VEC * DIM_HISTORY] = UPDATE_GATE_WEIGHT_X2; |
||
117 | static q7_t reset_gate_weights[DIM_VEC * DIM_HISTORY] = RESET_GATE_WEIGHT_X2; |
||
118 | static q7_t hidden_state_weights[DIM_VEC * DIM_HISTORY] = HIDDEN_STATE_WEIGHT_X2; |
||
119 | #else |
||
120 | static q7_t update_gate_weights[DIM_VEC * DIM_HISTORY] = UPDATE_GATE_WEIGHT_X4; |
||
121 | static q7_t reset_gate_weights[DIM_VEC * DIM_HISTORY] = RESET_GATE_WEIGHT_X4; |
||
122 | static q7_t hidden_state_weights[DIM_VEC * DIM_HISTORY] = HIDDEN_STATE_WEIGHT_X4; |
||
123 | #endif |
||
124 | |||
125 | static q7_t update_gate_bias[DIM_HISTORY] = UPDATE_GATE_BIAS; |
||
126 | static q7_t reset_gate_bias[DIM_HISTORY] = RESET_GATE_BIAS; |
||
127 | static q7_t hidden_state_bias[DIM_HISTORY] = HIDDEN_STATE_BIAS; |
||
128 | |||
129 | static q15_t test_input1[DIM_INPUT] = INPUT_DATA1; |
||
130 | static q15_t test_input2[DIM_INPUT] = INPUT_DATA2; |
||
131 | static q15_t test_history[DIM_HISTORY] = HISTORY_DATA; |
||
132 | |||
133 | q15_t scratch_buffer[DIM_HISTORY * 4 + DIM_INPUT]; |
||
134 | |||
135 | void gru_example(q15_t * scratch_input, uint16_t input_size, uint16_t history_size, |
||
136 | q7_t * weights_update, q7_t * weights_reset, q7_t * weights_hidden_state, |
||
137 | q7_t * bias_update, q7_t * bias_reset, q7_t * bias_hidden_state) |
||
138 | { |
||
139 | q15_t *reset = scratch_input; |
||
140 | q15_t *input = scratch_input + history_size; |
||
141 | q15_t *history = scratch_input + history_size + input_size; |
||
142 | q15_t *update = scratch_input + 2 * history_size + input_size; |
||
143 | q15_t *hidden_state = scratch_input + 3 * history_size + input_size; |
||
144 | |||
145 | // reset gate calculation |
||
146 | // the range of the output can be adjusted with bias_shift and output_shift |
||
147 | #ifndef USE_X4 |
||
148 | arm_fully_connected_mat_q7_vec_q15(input, weights_reset, input_size + history_size, history_size, 0, 15, bias_reset, |
||
149 | reset, NULL); |
||
150 | #else |
||
151 | arm_fully_connected_mat_q7_vec_q15_opt(input, weights_reset, input_size + history_size, history_size, 0, 15, |
||
152 | bias_reset, reset, NULL); |
||
153 | #endif |
||
154 | // sigmoid function, the size of the integer bit-width should be consistent with out_shift |
||
155 | arm_nn_activations_direct_q15(reset, history_size, 0, ARM_SIGMOID); |
||
156 | arm_mult_q15(history, reset, reset, history_size); |
||
157 | |||
158 | // update gate calculation |
||
159 | // the range of the output can be adjusted with bias_shift and output_shift |
||
160 | #ifndef USE_X4 |
||
161 | arm_fully_connected_mat_q7_vec_q15(input, weights_update, input_size + history_size, history_size, 0, 15, |
||
162 | bias_update, update, NULL); |
||
163 | #else |
||
164 | arm_fully_connected_mat_q7_vec_q15_opt(input, weights_update, input_size + history_size, history_size, 0, 15, |
||
165 | bias_update, update, NULL); |
||
166 | #endif |
||
167 | |||
168 | // sigmoid function, the size of the integer bit-width should be consistent with out_shift |
||
169 | arm_nn_activations_direct_q15(update, history_size, 0, ARM_SIGMOID); |
||
170 | |||
171 | // hidden state calculation |
||
172 | #ifndef USE_X4 |
||
173 | arm_fully_connected_mat_q7_vec_q15(reset, weights_hidden_state, input_size + history_size, history_size, 0, 15, |
||
174 | bias_hidden_state, hidden_state, NULL); |
||
175 | #else |
||
176 | arm_fully_connected_mat_q7_vec_q15_opt(reset, weights_hidden_state, input_size + history_size, history_size, 0, 15, |
||
177 | bias_hidden_state, hidden_state, NULL); |
||
178 | #endif |
||
179 | |||
180 | // tanh function, the size of the integer bit-width should be consistent with out_shift |
||
181 | arm_nn_activations_direct_q15(hidden_state, history_size, 0, ARM_TANH); |
||
182 | arm_mult_q15(update, hidden_state, hidden_state, history_size); |
||
183 | |||
184 | // we calculate z - 1 here |
||
185 | // so final addition becomes substraction |
||
186 | arm_offset_q15(update, 0x8000, update, history_size); |
||
187 | // multiply history |
||
188 | arm_mult_q15(history, update, update, history_size); |
||
189 | // calculate history_out |
||
190 | arm_sub_q15(hidden_state, update, history, history_size); |
||
191 | |||
192 | return; |
||
193 | } |
||
194 | |||
195 | int main() |
||
196 | { |
||
197 | #ifdef RTE_Compiler_EventRecorder |
||
198 | EventRecorderInitialize (EventRecordAll, 1); // initialize and start Event Recorder |
||
199 | #endif |
||
200 | |||
201 | printf("Start GRU execution\n"); |
||
202 | int input_size = DIM_INPUT; |
||
203 | int history_size = DIM_HISTORY; |
||
204 | |||
205 | // copy over the input data |
||
206 | arm_copy_q15(test_input1, scratch_buffer + history_size, input_size); |
||
207 | arm_copy_q15(test_history, scratch_buffer + history_size + input_size, history_size); |
||
208 | |||
209 | gru_example(scratch_buffer, input_size, history_size, |
||
210 | update_gate_weights, reset_gate_weights, hidden_state_weights, |
||
211 | update_gate_bias, reset_gate_bias, hidden_state_bias); |
||
212 | printf("Complete first iteration on GRU\n"); |
||
213 | |||
214 | arm_copy_q15(test_input2, scratch_buffer + history_size, input_size); |
||
215 | gru_example(scratch_buffer, input_size, history_size, |
||
216 | update_gate_weights, reset_gate_weights, hidden_state_weights, |
||
217 | update_gate_bias, reset_gate_bias, hidden_state_bias); |
||
218 | printf("Complete second iteration on GRU\n"); |
||
219 | |||
220 | return 0; |
||
221 | } |