Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame^] | 1 | /* |
| 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 | |
| 31 | using namespace arm_compute::utils; |
| 32 | using namespace arm_compute::graph; |
| 33 | using namespace arm_compute::graph::frontend; |
| 34 | using 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 */ |
| 37 | class GraphDeepSpeechExample : public Example |
| 38 | { |
| 39 | public: |
| 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); |
| 48 | |
| 49 | // Consume common parameters |
| 50 | common_params = consume_common_graph_parameters(common_opts); |
| 51 | |
| 52 | // Return when help menu is requested |
| 53 | if(common_params.help) |
| 54 | { |
| 55 | cmd_parser.print_help(argv[0]); |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | // Checks |
| 60 | ARM_COMPUTE_EXIT_ON_MSG(arm_compute::is_data_type_quantized_asymmetric(common_params.data_type), "QASYMM8 not supported for this graph"); |
| 61 | |
| 62 | // Print parameter values |
| 63 | std::cout << common_params << std::endl; |
| 64 | |
| 65 | // Get trainable parameters data path |
| 66 | std::string data_path = common_params.data_path; |
| 67 | const std::string model_path = "/cnn_data/deepspeech_model/"; |
| 68 | |
| 69 | if(!data_path.empty()) |
| 70 | { |
| 71 | data_path += model_path; |
| 72 | } |
| 73 | |
| 74 | // How many timesteps to process at once, higher values mean more latency |
| 75 | // Notice that this corresponds to the number of LSTM cells that will be instantiated |
| 76 | const unsigned int n_steps = 16; |
| 77 | |
| 78 | // ReLU clipping value for non-recurrent layers |
| 79 | const float cell_clip = 20.f; |
| 80 | |
| 81 | // Create input descriptor |
| 82 | const TensorShape tensor_shape = permute_shape(TensorShape(26U, 19U, n_steps, 1U), DataLayout::NHWC, common_params.data_layout); |
| 83 | TensorDescriptor input_descriptor = TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout); |
| 84 | |
| 85 | // Set weights trained layout |
| 86 | const DataLayout weights_layout = DataLayout::NHWC; |
| 87 | |
| 88 | graph << common_params.target |
| 89 | << common_params.fast_math_hint |
| 90 | << InputLayer(input_descriptor, |
| 91 | get_weights_accessor(data_path, "input_values_x" + std::to_string(n_steps) + ".npy", weights_layout)) |
| 92 | .set_name("input_node"); |
| 93 | |
| 94 | if(common_params.data_layout == DataLayout::NCHW) |
| 95 | { |
| 96 | graph << PermuteLayer(PermutationVector(2U, 0U, 1U), common_params.data_layout).set_name("permute_to_nhwc"); |
| 97 | } |
| 98 | |
| 99 | graph << ReshapeLayer(TensorShape(494U, n_steps)).set_name("Reshape_input") |
| 100 | // Layer 1 |
| 101 | << FullyConnectedLayer( |
| 102 | 2048U, |
| 103 | get_weights_accessor(data_path, "h1_transpose.npy", weights_layout), |
| 104 | get_weights_accessor(data_path, "MatMul_bias.npy")) |
| 105 | .set_name("fc0") |
| 106 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, cell_clip)) |
| 107 | .set_name("Relu") |
| 108 | // Layer 2 |
| 109 | << FullyConnectedLayer( |
| 110 | 2048U, |
| 111 | get_weights_accessor(data_path, "h2_transpose.npy", weights_layout), |
| 112 | get_weights_accessor(data_path, "MatMul_1_bias.npy")) |
| 113 | .set_name("fc1") |
| 114 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, cell_clip)) |
| 115 | .set_name("Relu_1") |
| 116 | // Layer 3 |
| 117 | << FullyConnectedLayer( |
| 118 | 2048U, |
| 119 | get_weights_accessor(data_path, "h3_transpose.npy", weights_layout), |
| 120 | get_weights_accessor(data_path, "MatMul_2_bias.npy")) |
| 121 | .set_name("fc2") |
| 122 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, cell_clip)) |
| 123 | .set_name("Relu_2") |
| 124 | // Layer 4 |
| 125 | << ReshapeLayer(TensorShape(2048U, 1U, n_steps)).set_name("Reshape_1"); |
| 126 | |
| 127 | // Unstack Layer (using SplitLayerNode) |
| 128 | NodeParams unstack_params = { "unstack", graph.hints().target_hint }; |
| 129 | NodeID unstack_nid = GraphBuilder::add_split_node(graph.graph(), unstack_params, { graph.tail_node(), 0 }, n_steps, 2); |
| 130 | |
| 131 | // Create input state descriptor |
| 132 | TensorDescriptor state_descriptor = TensorDescriptor(TensorShape(2048U), common_params.data_type).set_layout(common_params.data_layout); |
| 133 | SubStream previous_state(graph); |
| 134 | SubStream add_y(graph); |
| 135 | |
| 136 | // Initial state for LSTM is all zeroes for both state_h and state_c, therefore only one input is created |
| 137 | previous_state << InputLayer(state_descriptor, |
| 138 | get_weights_accessor(data_path, "zeros.npy")) |
| 139 | .set_name("previous_state_c_h"); |
| 140 | add_y << InputLayer(state_descriptor, |
| 141 | get_weights_accessor(data_path, "ones.npy")) |
| 142 | .set_name("add_y"); |
| 143 | |
| 144 | // TODO(COMPMID-2103): Use sub stream for FC weights and bias in LSTM cells |
| 145 | // Create LSTM Fully Connected weights and bias descriptors |
| 146 | //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 | |
| 151 | //lstm_fc_weights << InputLayer(lstm_weights_descriptor, |
| 152 | // get_weights_accessor(data_path, "rnn_lstm_cell_kernel_transpose.npy", weights_layout)) |
| 153 | // .set_name("h5/transpose"); |
| 154 | //lstm_fc_bias << InputLayer(lstm_bias_descriptor, |
| 155 | // get_weights_accessor(data_path, "rnn_lstm_cell_MatMul_bias.npy")) |
| 156 | // .set_name("MatMul_3_bias"); |
| 157 | |
| 158 | // LSTM Block |
| 159 | std::pair<SubStream, SubStream> new_state_1 = add_lstm_cell(data_path, unstack_nid, 0, previous_state, previous_state, add_y); |
| 160 | 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); |
| 161 | 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); |
| 162 | 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); |
| 163 | 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); |
| 164 | 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); |
| 165 | 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); |
| 166 | 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); |
| 167 | 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); |
| 168 | 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); |
| 169 | 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); |
| 170 | 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); |
| 171 | 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); |
| 172 | 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); |
| 173 | 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); |
| 174 | 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); |
| 175 | |
| 176 | if(n_steps > 1) |
| 177 | { |
| 178 | // Concatenate new states on height |
| 179 | const int axis = 1; |
| 180 | graph << StackLayer(axis, |
| 181 | std::move(new_state_1.second), |
| 182 | std::move(new_state_2.second), |
| 183 | std::move(new_state_3.second), |
| 184 | std::move(new_state_4.second), |
| 185 | std::move(new_state_5.second), |
| 186 | std::move(new_state_6.second), |
| 187 | std::move(new_state_7.second), |
| 188 | std::move(new_state_8.second), |
| 189 | std::move(new_state_9.second), |
| 190 | std::move(new_state_10.second), |
| 191 | std::move(new_state_11.second), |
| 192 | std::move(new_state_12.second), |
| 193 | std::move(new_state_13.second), |
| 194 | std::move(new_state_14.second), |
| 195 | std::move(new_state_15.second), |
| 196 | std::move(new_state_16.second)) |
| 197 | .set_name("concat"); |
| 198 | } |
| 199 | |
| 200 | graph << FullyConnectedLayer( |
| 201 | 2048U, |
| 202 | get_weights_accessor(data_path, "h5_transpose.npy", weights_layout), |
| 203 | get_weights_accessor(data_path, "MatMul_3_bias.npy")) |
| 204 | .set_name("fc3") |
| 205 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, cell_clip)) |
| 206 | .set_name("Relu3") |
| 207 | << FullyConnectedLayer( |
| 208 | 29U, |
| 209 | get_weights_accessor(data_path, "h6_transpose.npy", weights_layout), |
| 210 | get_weights_accessor(data_path, "MatMul_4_bias.npy")) |
| 211 | .set_name("fc3") |
| 212 | << SoftmaxLayer().set_name("logits"); |
| 213 | |
| 214 | graph << OutputLayer(get_output_accessor(common_params, 5)); |
| 215 | |
| 216 | // Finalize graph |
| 217 | GraphConfig config; |
| 218 | config.num_threads = common_params.threads; |
| 219 | config.use_tuner = common_params.enable_tuner; |
| 220 | config.tuner_file = common_params.tuner_file; |
| 221 | |
| 222 | graph.finalize(common_params.target, config); |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | void do_run() override |
| 227 | { |
| 228 | // Run graph |
| 229 | graph.run(); |
| 230 | } |
| 231 | |
| 232 | private: |
| 233 | CommandLineParser cmd_parser; |
| 234 | CommonGraphOptions common_opts; |
| 235 | CommonGraphParams common_params; |
| 236 | Stream graph; |
| 237 | |
| 238 | Status set_node_params(Graph &g, NodeID nid, NodeParams ¶ms) |
| 239 | { |
| 240 | INode *node = g.node(nid); |
| 241 | ARM_COMPUTE_RETURN_ERROR_ON(!node); |
| 242 | |
| 243 | node->set_common_node_parameters(params); |
| 244 | |
| 245 | return Status{}; |
| 246 | } |
| 247 | |
| 248 | std::pair<SubStream, SubStream> add_lstm_cell(const std::string &data_path, |
| 249 | NodeID unstack_nid, |
| 250 | unsigned int unstack_idx, |
| 251 | SubStream previous_state_c, |
| 252 | SubStream previous_state_h, |
| 253 | SubStream add_y) |
| 254 | // TODO(COMPMID-2103): Use sub streams for FC weights and bias |
| 255 | //SubStream lstm_fc_weights, |
| 256 | //SubStream lstm_fc_bias) |
| 257 | { |
| 258 | const std::string cell_name("rnn/lstm_cell_" + std::to_string(unstack_idx)); |
| 259 | const DataLayoutDimension concat_dim = (common_params.data_layout == DataLayout::NHWC) ? DataLayoutDimension::CHANNEL : DataLayoutDimension::WIDTH; |
| 260 | |
| 261 | // Concatenate result of Unstack with previous_state_h |
| 262 | NodeParams concat_params = { cell_name + "/concat", graph.hints().target_hint }; |
| 263 | NodeID concat_nid = graph.graph().add_node<ConcatenateLayerNode>(2, concat_dim); |
| 264 | graph.graph().add_connection(unstack_nid, unstack_idx, concat_nid, 0); |
| 265 | graph.graph().add_connection(previous_state_h.tail_node(), 0, concat_nid, 1); |
| 266 | set_node_params(graph.graph(), concat_nid, concat_params); |
| 267 | graph.forward_tail(concat_nid); |
| 268 | |
| 269 | graph << FullyConnectedLayer( |
| 270 | 8192U, |
| 271 | get_weights_accessor(data_path, "rnn_lstm_cell_kernel_transpose.npy", DataLayout::NHWC), |
| 272 | get_weights_accessor(data_path, "rnn_lstm_cell_MatMul_bias.npy")) |
| 273 | .set_name(cell_name + "/BiasAdd"); |
| 274 | |
| 275 | // Split Layer |
| 276 | const unsigned int num_splits = 4; |
| 277 | const unsigned int split_axis = 0; |
| 278 | |
| 279 | NodeParams split_params = { cell_name + "/split", graph.hints().target_hint }; |
| 280 | NodeID split_nid = GraphBuilder::add_split_node(graph.graph(), split_params, { graph.tail_node(), 0 }, num_splits, split_axis); |
| 281 | |
| 282 | NodeParams sigmoid_1_params = { cell_name + "/Sigmoid_1", graph.hints().target_hint }; |
| 283 | NodeParams add_params = { cell_name + "/add", graph.hints().target_hint }; |
| 284 | NodeParams sigmoid_2_params = { cell_name + "/Sigmoid_2", graph.hints().target_hint }; |
| 285 | NodeParams tanh_params = { cell_name + "/Tanh", graph.hints().target_hint }; |
| 286 | |
| 287 | // Sigmoid 1 (first split) |
| 288 | NodeID sigmoid_1_nid = graph.graph().add_node<ActivationLayerNode>(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
| 289 | graph.graph().add_connection(split_nid, 0, sigmoid_1_nid, 0); |
| 290 | set_node_params(graph.graph(), sigmoid_1_nid, sigmoid_1_params); |
| 291 | |
| 292 | // Tanh (second split) |
| 293 | NodeID tanh_nid = graph.graph().add_node<ActivationLayerNode>(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)); |
| 294 | graph.graph().add_connection(split_nid, 1, tanh_nid, 0); |
| 295 | set_node_params(graph.graph(), tanh_nid, tanh_params); |
| 296 | |
| 297 | SubStream tanh_ss(graph); |
| 298 | tanh_ss.forward_tail(tanh_nid); |
| 299 | |
| 300 | // Add (third split) |
| 301 | NodeID add_nid = graph.graph().add_node<EltwiseLayerNode>(EltwiseOperation::Add); |
| 302 | graph.graph().add_connection(split_nid, 2, add_nid, 0); |
| 303 | graph.graph().add_connection(add_y.tail_node(), 0, add_nid, 1); |
| 304 | set_node_params(graph.graph(), add_nid, add_params); |
| 305 | |
| 306 | // Sigmoid 2 (fourth split) |
| 307 | NodeID sigmoid_2_nid = graph.graph().add_node<ActivationLayerNode>(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
| 308 | graph.graph().add_connection(split_nid, 3, sigmoid_2_nid, 0); |
| 309 | set_node_params(graph.graph(), sigmoid_2_nid, sigmoid_2_params); |
| 310 | |
| 311 | SubStream mul_1_ss(graph); |
| 312 | mul_1_ss.forward_tail(sigmoid_1_nid); |
| 313 | mul_1_ss << EltwiseLayer(std::move(mul_1_ss), std::move(tanh_ss), EltwiseOperation::Mul) |
| 314 | .set_name(cell_name + "/mul_1"); |
| 315 | |
| 316 | SubStream tanh_1_ss(graph); |
| 317 | tanh_1_ss.forward_tail(add_nid); |
| 318 | tanh_1_ss << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)) |
| 319 | .set_name(cell_name + "/Sigmoid"); |
| 320 | tanh_1_ss << EltwiseLayer(std::move(tanh_1_ss), std::move(previous_state_c), EltwiseOperation::Mul) |
| 321 | .set_name(cell_name + "/mul"); |
| 322 | |
| 323 | tanh_1_ss << EltwiseLayer(std::move(tanh_1_ss), std::move(mul_1_ss), EltwiseOperation::Add) |
| 324 | .set_name(cell_name + "/new_state_c"); |
| 325 | SubStream new_state_c(tanh_1_ss); |
| 326 | |
| 327 | tanh_1_ss << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)) |
| 328 | .set_name(cell_name + "/Tanh_1"); |
| 329 | |
| 330 | SubStream sigmoid_2_ss(graph); |
| 331 | sigmoid_2_ss.forward_tail(sigmoid_2_nid); |
| 332 | graph << EltwiseLayer(std::move(sigmoid_2_ss), std::move(tanh_1_ss), EltwiseOperation::Mul) |
| 333 | .set_name(cell_name + "/new_state_h"); |
| 334 | |
| 335 | SubStream new_state_h(graph); |
| 336 | return std::pair<SubStream, SubStream>(new_state_c, new_state_h); |
| 337 | } |
| 338 | }; |
| 339 | |
| 340 | /** Main program for DeepSpeech v0.4.1 |
| 341 | * |
| 342 | * Model is based on: |
| 343 | * https://arxiv.org/abs/1412.5567 |
| 344 | * "Deep Speech: Scaling up end-to-end speech recognition" |
| 345 | * Awni Hannun, Carl Case, Jared Casper, Bryan Catanzaro, Greg Diamos, Erich Elsen, Ryan Prenger, Sanjeev Satheesh, Shubho Sengupta, Adam Coates, Andrew Y. Ng |
| 346 | * |
| 347 | * Provenance: https://github.com/mozilla/DeepSpeech |
| 348 | * |
| 349 | * @note To list all the possible arguments execute the binary appended with the --help option |
| 350 | * |
| 351 | * @param[in] argc Number of arguments |
| 352 | * @param[in] argv Arguments |
| 353 | * |
| 354 | * @return Return code |
| 355 | */ |
| 356 | int main(int argc, char **argv) |
| 357 | { |
| 358 | return arm_compute::utils::run_example<GraphDeepSpeechExample>(argc, argv); |
| 359 | } |