blob: 84650a662777191906ef7d2d658bfc1c5a0b5131 [file] [log] [blame]
Michele Di Giorgio3418ba52019-03-01 17:19:55 +00001/*
2 * Copyright (c) 2019 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/graph.h"
25#include "arm_compute/graph/Types.h"
26#include "support/ToolchainSupport.h"
27#include "utils/CommonGraphOptions.h"
28#include "utils/GraphUtils.h"
29#include "utils/Utils.h"
30
31using namespace arm_compute::utils;
32using namespace arm_compute::graph;
33using namespace arm_compute::graph::frontend;
34using namespace arm_compute::graph_utils;
35
36/** Example demonstrating how to implement DeepSpeech v0.4.1's network using the Compute Library's graph API */
37class GraphDeepSpeechExample : public Example
38{
39public:
40 GraphDeepSpeechExample()
41 : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "DeepSpeech v0.4.1")
42 {
43 }
44 bool do_setup(int argc, char **argv) override
45 {
46 // Parse arguments
47 cmd_parser.parse(argc, argv);
Georgios Pinitascd60a5f2019-08-21 17:06:54 +010048 cmd_parser.validate();
Michele Di Giorgio3418ba52019-03-01 17:19:55 +000049
50 // Consume common parameters
51 common_params = consume_common_graph_parameters(common_opts);
52
53 // Return when help menu is requested
54 if(common_params.help)
55 {
56 cmd_parser.print_help(argv[0]);
57 return false;
58 }
59
60 // Checks
61 ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph");
62
63 // Print parameter values
64 std::cout << common_params << std::endl;
65
66 // Get trainable parameters data path
67 std::string data_path = common_params.data_path;
68 const std::string model_path = "/cnn_data/deepspeech_model/";
69
70 if(!data_path.empty())
71 {
72 data_path += model_path;
73 }
74
75 // How many timesteps to process at once, higher values mean more latency
76 // Notice that this corresponds to the number of LSTM cells that will be instantiated
77 const unsigned int n_steps = 16;
78
79 // ReLU clipping value for non-recurrent layers
80 const float cell_clip = 20.f;
81
82 // Create input descriptor
83 const TensorShape tensor_shape = permute_shape(TensorShape(26U, 19U, n_steps, 1U), DataLayout::NHWC, common_params.data_layout);
84 TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout);
85
86 // Set weights trained layout
87 const DataLayout weights_layout = DataLayout::NHWC;
88
89 graph << common_params.target
90 << common_params.fast_math_hint
91 << InputLayer(input_descriptor,
92 get_weights_accessor(data_path, "input_values_x" + std::to_string(n_steps) + ".npy", weights_layout))
93 .set_name("input_node");
94
95 if(common_params.data_layout == DataLayout::NCHW)
96 {
97 graph << PermuteLayer(PermutationVector(2U, 0U, 1U), common_params.data_layout).set_name("permute_to_nhwc");
98 }
99
100 graph << ReshapeLayer(TensorShape(494U, n_steps)).set_name("Reshape_input")
101 // Layer 1
102 << FullyConnectedLayer(
103 2048U,
104 get_weights_accessor(data_path, "h1_transpose.npy", weights_layout),
105 get_weights_accessor(data_path, "MatMul_bias.npy"))
106 .set_name("fc0")
107 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, cell_clip))
108 .set_name("Relu")
109 // Layer 2
110 << FullyConnectedLayer(
111 2048U,
112 get_weights_accessor(data_path, "h2_transpose.npy", weights_layout),
113 get_weights_accessor(data_path, "MatMul_1_bias.npy"))
114 .set_name("fc1")
115 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, cell_clip))
116 .set_name("Relu_1")
117 // Layer 3
118 << FullyConnectedLayer(
119 2048U,
120 get_weights_accessor(data_path, "h3_transpose.npy", weights_layout),
121 get_weights_accessor(data_path, "MatMul_2_bias.npy"))
122 .set_name("fc2")
123 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, cell_clip))
124 .set_name("Relu_2")
125 // Layer 4
126 << ReshapeLayer(TensorShape(2048U, 1U, n_steps)).set_name("Reshape_1");
127
128 // Unstack Layer (using SplitLayerNode)
129 NodeParams unstack_params = { "unstack", graph.hints().target_hint };
130 NodeID unstack_nid = GraphBuilder::add_split_node(graph.graph(), unstack_params, { graph.tail_node(), 0 }, n_steps, 2);
131
132 // Create input state descriptor
133 TensorDescriptor state_descriptor = TensorDescriptor(TensorShape(2048U), common_params.data_type).set_layout(common_params.data_layout);
134 SubStream previous_state(graph);
135 SubStream add_y(graph);
136
137 // Initial state for LSTM is all zeroes for both state_h and state_c, therefore only one input is created
138 previous_state << InputLayer(state_descriptor,
139 get_weights_accessor(data_path, "zeros.npy"))
140 .set_name("previous_state_c_h");
141 add_y << InputLayer(state_descriptor,
142 get_weights_accessor(data_path, "ones.npy"))
143 .set_name("add_y");
144
Michele Di Giorgio3418ba52019-03-01 17:19:55 +0000145 // Create LSTM Fully Connected weights and bias descriptors
Michalis Spyroubd188112019-10-02 15:43:44 +0100146 TensorDescriptor lstm_weights_descriptor = TensorDescriptor(TensorShape(4096U, 8192U), common_params.data_type).set_layout(common_params.data_layout);
147 TensorDescriptor lstm_bias_descriptor = TensorDescriptor(TensorShape(8192U), common_params.data_type).set_layout(common_params.data_layout);
148 SubStream lstm_fc_weights(graph);
149 SubStream lstm_fc_bias(graph);
150 lstm_fc_weights << ConstantLayer(lstm_weights_descriptor,
151 get_weights_accessor(data_path, "rnn_lstm_cell_kernel_transpose.npy", weights_layout))
152 .set_name("h5/transpose");
153 lstm_fc_bias << ConstantLayer(lstm_bias_descriptor,
154 get_weights_accessor(data_path, "rnn_lstm_cell_MatMul_bias.npy"))
155 .set_name("MatMul_3_bias");
Michele Di Giorgio3418ba52019-03-01 17:19:55 +0000156
157 // LSTM Block
Michalis Spyroubd188112019-10-02 15:43:44 +0100158 std::pair<SubStream, SubStream> new_state_1 = add_lstm_cell(data_path, unstack_nid, 0, previous_state, previous_state, add_y, lstm_fc_weights, lstm_fc_bias);
159 std::pair<SubStream, SubStream> new_state_2 = add_lstm_cell(data_path, unstack_nid, 1, new_state_1.first, new_state_1.second, add_y, lstm_fc_weights, lstm_fc_bias);
160 std::pair<SubStream, SubStream> new_state_3 = add_lstm_cell(data_path, unstack_nid, 2, new_state_2.first, new_state_2.second, add_y, lstm_fc_weights, lstm_fc_bias);
161 std::pair<SubStream, SubStream> new_state_4 = add_lstm_cell(data_path, unstack_nid, 3, new_state_3.first, new_state_3.second, add_y, lstm_fc_weights, lstm_fc_bias);
162 std::pair<SubStream, SubStream> new_state_5 = add_lstm_cell(data_path, unstack_nid, 4, new_state_4.first, new_state_4.second, add_y, lstm_fc_weights, lstm_fc_bias);
163 std::pair<SubStream, SubStream> new_state_6 = add_lstm_cell(data_path, unstack_nid, 5, new_state_5.first, new_state_5.second, add_y, lstm_fc_weights, lstm_fc_bias);
164 std::pair<SubStream, SubStream> new_state_7 = add_lstm_cell(data_path, unstack_nid, 6, new_state_6.first, new_state_6.second, add_y, lstm_fc_weights, lstm_fc_bias);
165 std::pair<SubStream, SubStream> new_state_8 = add_lstm_cell(data_path, unstack_nid, 7, new_state_7.first, new_state_7.second, add_y, lstm_fc_weights, lstm_fc_bias);
166 std::pair<SubStream, SubStream> new_state_9 = add_lstm_cell(data_path, unstack_nid, 8, new_state_8.first, new_state_8.second, add_y, lstm_fc_weights, lstm_fc_bias);
167 std::pair<SubStream, SubStream> new_state_10 = add_lstm_cell(data_path, unstack_nid, 9, new_state_9.first, new_state_9.second, add_y, lstm_fc_weights, lstm_fc_bias);
168 std::pair<SubStream, SubStream> new_state_11 = add_lstm_cell(data_path, unstack_nid, 10, new_state_10.first, new_state_10.second, add_y, lstm_fc_weights, lstm_fc_bias);
169 std::pair<SubStream, SubStream> new_state_12 = add_lstm_cell(data_path, unstack_nid, 11, new_state_11.first, new_state_11.second, add_y, lstm_fc_weights, lstm_fc_bias);
170 std::pair<SubStream, SubStream> new_state_13 = add_lstm_cell(data_path, unstack_nid, 12, new_state_12.first, new_state_12.second, add_y, lstm_fc_weights, lstm_fc_bias);
171 std::pair<SubStream, SubStream> new_state_14 = add_lstm_cell(data_path, unstack_nid, 13, new_state_13.first, new_state_13.second, add_y, lstm_fc_weights, lstm_fc_bias);
172 std::pair<SubStream, SubStream> new_state_15 = add_lstm_cell(data_path, unstack_nid, 14, new_state_14.first, new_state_14.second, add_y, lstm_fc_weights, lstm_fc_bias);
173 std::pair<SubStream, SubStream> new_state_16 = add_lstm_cell(data_path, unstack_nid, 15, new_state_15.first, new_state_15.second, add_y, lstm_fc_weights, lstm_fc_bias);
Michele Di Giorgio3418ba52019-03-01 17:19:55 +0000174
Michalis Spyroubd188112019-10-02 15:43:44 +0100175 // Concatenate new states on height
176 const int axis = 1;
177 graph << StackLayer(axis,
178 std::move(new_state_1.second),
179 std::move(new_state_2.second),
180 std::move(new_state_3.second),
181 std::move(new_state_4.second),
182 std::move(new_state_5.second),
183 std::move(new_state_6.second),
184 std::move(new_state_7.second),
185 std::move(new_state_8.second),
186 std::move(new_state_9.second),
187 std::move(new_state_10.second),
188 std::move(new_state_11.second),
189 std::move(new_state_12.second),
190 std::move(new_state_13.second),
191 std::move(new_state_14.second),
192 std::move(new_state_15.second),
193 std::move(new_state_16.second))
194 .set_name("concat");
Michele Di Giorgio3418ba52019-03-01 17:19:55 +0000195
196 graph << FullyConnectedLayer(
197 2048U,
198 get_weights_accessor(data_path, "h5_transpose.npy", weights_layout),
199 get_weights_accessor(data_path, "MatMul_3_bias.npy"))
200 .set_name("fc3")
201 << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, cell_clip))
202 .set_name("Relu3")
203 << FullyConnectedLayer(
204 29U,
205 get_weights_accessor(data_path, "h6_transpose.npy", weights_layout),
206 get_weights_accessor(data_path, "MatMul_4_bias.npy"))
207 .set_name("fc3")
208 << SoftmaxLayer().set_name("logits");
209
210 graph << OutputLayer(get_output_accessor(common_params, 5));
211
212 // Finalize graph
213 GraphConfig config;
214 config.num_threads = common_params.threads;
215 config.use_tuner = common_params.enable_tuner;
216 config.tuner_file = common_params.tuner_file;
217
218 graph.finalize(common_params.target, config);
219
220 return true;
221 }
222 void do_run() override
223 {
224 // Run graph
225 graph.run();
226 }
227
228private:
229 CommandLineParser cmd_parser;
230 CommonGraphOptions common_opts;
231 CommonGraphParams common_params;
232 Stream graph;
233
234 Status set_node_params(Graph &g, NodeID nid, NodeParams &params)
235 {
236 INode *node = g.node(nid);
237 ARM_COMPUTE_RETURN_ERROR_ON(!node);
238
239 node->set_common_node_parameters(params);
240
241 return Status{};
242 }
243
244 std::pair<SubStream, SubStream> add_lstm_cell(const std::string &data_path,
245 NodeID unstack_nid,
246 unsigned int unstack_idx,
247 SubStream previous_state_c,
248 SubStream previous_state_h,
Michalis Spyroubd188112019-10-02 15:43:44 +0100249 SubStream add_y,
250 SubStream lstm_fc_weights,
251 SubStream lstm_fc_bias)
Michele Di Giorgio3418ba52019-03-01 17:19:55 +0000252 {
253 const std::string cell_name("rnn/lstm_cell_" + std::to_string(unstack_idx));
254 const DataLayoutDimension concat_dim = (common_params.data_layout == DataLayout::NHWC) ? DataLayoutDimension::CHANNEL : DataLayoutDimension::WIDTH;
255
256 // Concatenate result of Unstack with previous_state_h
257 NodeParams concat_params = { cell_name + "/concat", graph.hints().target_hint };
258 NodeID concat_nid = graph.graph().add_node<ConcatenateLayerNode>(2, concat_dim);
259 graph.graph().add_connection(unstack_nid, unstack_idx, concat_nid, 0);
260 graph.graph().add_connection(previous_state_h.tail_node(), 0, concat_nid, 1);
261 set_node_params(graph.graph(), concat_nid, concat_params);
262 graph.forward_tail(concat_nid);
263
264 graph << FullyConnectedLayer(
265 8192U,
Michalis Spyroubd188112019-10-02 15:43:44 +0100266 lstm_fc_weights,
267 lstm_fc_bias)
Michele Di Giorgio3418ba52019-03-01 17:19:55 +0000268 .set_name(cell_name + "/BiasAdd");
269
270 // Split Layer
271 const unsigned int num_splits = 4;
272 const unsigned int split_axis = 0;
273
274 NodeParams split_params = { cell_name + "/split", graph.hints().target_hint };
275 NodeID split_nid = GraphBuilder::add_split_node(graph.graph(), split_params, { graph.tail_node(), 0 }, num_splits, split_axis);
276
277 NodeParams sigmoid_1_params = { cell_name + "/Sigmoid_1", graph.hints().target_hint };
278 NodeParams add_params = { cell_name + "/add", graph.hints().target_hint };
279 NodeParams sigmoid_2_params = { cell_name + "/Sigmoid_2", graph.hints().target_hint };
280 NodeParams tanh_params = { cell_name + "/Tanh", graph.hints().target_hint };
281
282 // Sigmoid 1 (first split)
283 NodeID sigmoid_1_nid = graph.graph().add_node<ActivationLayerNode>(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
284 graph.graph().add_connection(split_nid, 0, sigmoid_1_nid, 0);
285 set_node_params(graph.graph(), sigmoid_1_nid, sigmoid_1_params);
286
287 // Tanh (second split)
288 NodeID tanh_nid = graph.graph().add_node<ActivationLayerNode>(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f));
289 graph.graph().add_connection(split_nid, 1, tanh_nid, 0);
290 set_node_params(graph.graph(), tanh_nid, tanh_params);
291
292 SubStream tanh_ss(graph);
293 tanh_ss.forward_tail(tanh_nid);
294
295 // Add (third split)
296 NodeID add_nid = graph.graph().add_node<EltwiseLayerNode>(EltwiseOperation::Add);
297 graph.graph().add_connection(split_nid, 2, add_nid, 0);
298 graph.graph().add_connection(add_y.tail_node(), 0, add_nid, 1);
299 set_node_params(graph.graph(), add_nid, add_params);
300
301 // Sigmoid 2 (fourth split)
302 NodeID sigmoid_2_nid = graph.graph().add_node<ActivationLayerNode>(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC));
303 graph.graph().add_connection(split_nid, 3, sigmoid_2_nid, 0);
304 set_node_params(graph.graph(), sigmoid_2_nid, sigmoid_2_params);
305
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100306 SubStream sigmoid_1_ss(graph);
307 sigmoid_1_ss.forward_tail(sigmoid_1_nid);
308 SubStream mul_1_ss(sigmoid_1_ss);
309 mul_1_ss << EltwiseLayer(std::move(sigmoid_1_ss), std::move(tanh_ss), EltwiseOperation::Mul)
Michele Di Giorgio3418ba52019-03-01 17:19:55 +0000310 .set_name(cell_name + "/mul_1");
311
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100312 SubStream tanh_1_ss_tmp(graph);
313 tanh_1_ss_tmp.forward_tail(add_nid);
Michele Di Giorgio3418ba52019-03-01 17:19:55 +0000314
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100315 tanh_1_ss_tmp << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC))
316 .set_name(cell_name + "/Sigmoid");
317 SubStream tanh_1_ss_tmp2(tanh_1_ss_tmp);
318 tanh_1_ss_tmp2 << EltwiseLayer(std::move(tanh_1_ss_tmp), std::move(previous_state_c), EltwiseOperation::Mul)
319 .set_name(cell_name + "/mul");
320 SubStream tanh_1_ss(tanh_1_ss_tmp2);
321 tanh_1_ss << EltwiseLayer(std::move(tanh_1_ss_tmp2), std::move(mul_1_ss), EltwiseOperation::Add)
Michele Di Giorgio3418ba52019-03-01 17:19:55 +0000322 .set_name(cell_name + "/new_state_c");
323 SubStream new_state_c(tanh_1_ss);
324
325 tanh_1_ss << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f))
326 .set_name(cell_name + "/Tanh_1");
327
328 SubStream sigmoid_2_ss(graph);
329 sigmoid_2_ss.forward_tail(sigmoid_2_nid);
330 graph << EltwiseLayer(std::move(sigmoid_2_ss), std::move(tanh_1_ss), EltwiseOperation::Mul)
331 .set_name(cell_name + "/new_state_h");
332
333 SubStream new_state_h(graph);
334 return std::pair<SubStream, SubStream>(new_state_c, new_state_h);
335 }
336};
337
338/** Main program for DeepSpeech v0.4.1
339 *
340 * Model is based on:
341 * https://arxiv.org/abs/1412.5567
342 * "Deep Speech: Scaling up end-to-end speech recognition"
343 * Awni Hannun, Carl Case, Jared Casper, Bryan Catanzaro, Greg Diamos, Erich Elsen, Ryan Prenger, Sanjeev Satheesh, Shubho Sengupta, Adam Coates, Andrew Y. Ng
344 *
345 * Provenance: https://github.com/mozilla/DeepSpeech
346 *
347 * @note To list all the possible arguments execute the binary appended with the --help option
348 *
349 * @param[in] argc Number of arguments
350 * @param[in] argv Arguments
351 *
352 * @return Return code
353 */
354int main(int argc, char **argv)
355{
356 return arm_compute::utils::run_example<GraphDeepSpeechExample>(argc, argv);
357}