Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 1 | /* |
SiCong Li | 4841c97 | 2021-02-03 12:17:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 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" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 26 | |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 27 | #include "support/ToolchainSupport.h" |
| 28 | #include "utils/CommonGraphOptions.h" |
| 29 | #include "utils/GraphUtils.h" |
| 30 | #include "utils/Utils.h" |
| 31 | |
| 32 | using namespace arm_compute::utils; |
| 33 | using namespace arm_compute::graph; |
| 34 | using namespace arm_compute::graph::frontend; |
| 35 | using namespace arm_compute::graph_utils; |
| 36 | |
| 37 | /** Example demonstrating how to implement DeepSpeech v0.4.1's network using the Compute Library's graph API */ |
| 38 | class GraphDeepSpeechExample : public Example |
| 39 | { |
| 40 | public: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 41 | GraphDeepSpeechExample() : cmd_parser(), common_opts(cmd_parser), common_params(), graph(0, "DeepSpeech v0.4.1") |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 42 | { |
| 43 | } |
| 44 | bool do_setup(int argc, char **argv) override |
| 45 | { |
| 46 | // Parse arguments |
| 47 | cmd_parser.parse(argc, argv); |
Georgios Pinitas | cd60a5f | 2019-08-21 17:06:54 +0100 | [diff] [blame] | 48 | cmd_parser.validate(); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 49 | |
| 50 | // Consume common parameters |
| 51 | common_params = consume_common_graph_parameters(common_opts); |
| 52 | |
| 53 | // Return when help menu is requested |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | if (common_params.help) |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 55 | { |
| 56 | cmd_parser.print_help(argv[0]); |
| 57 | return false; |
| 58 | } |
| 59 | |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 60 | // Print parameter values |
| 61 | std::cout << common_params << std::endl; |
| 62 | |
| 63 | // Get trainable parameters data path |
| 64 | std::string data_path = common_params.data_path; |
| 65 | const std::string model_path = "/cnn_data/deepspeech_model/"; |
| 66 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 67 | if (!data_path.empty()) |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 68 | { |
| 69 | data_path += model_path; |
| 70 | } |
| 71 | |
| 72 | // How many timesteps to process at once, higher values mean more latency |
| 73 | // Notice that this corresponds to the number of LSTM cells that will be instantiated |
| 74 | const unsigned int n_steps = 16; |
| 75 | |
| 76 | // ReLU clipping value for non-recurrent layers |
| 77 | const float cell_clip = 20.f; |
| 78 | |
| 79 | // Create input descriptor |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | const TensorShape tensor_shape = |
| 81 | permute_shape(TensorShape(26U, 19U, n_steps, 1U), DataLayout::NHWC, common_params.data_layout); |
| 82 | TensorDescriptor input_descriptor = |
| 83 | TensorDescriptor(tensor_shape, common_params.data_type).set_layout(common_params.data_layout); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 84 | |
| 85 | // Set weights trained layout |
| 86 | const DataLayout weights_layout = DataLayout::NHWC; |
| 87 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | graph << common_params.target << common_params.fast_math_hint |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 89 | << InputLayer(input_descriptor, |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 90 | get_weights_accessor(data_path, "input_values_x" + std::to_string(n_steps) + ".npy", |
| 91 | weights_layout)) |
| 92 | .set_name("input_node"); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 93 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 94 | if (common_params.data_layout == DataLayout::NCHW) |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 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 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 101 | << FullyConnectedLayer(2048U, get_weights_accessor(data_path, "h1_transpose.npy", weights_layout), |
| 102 | get_weights_accessor(data_path, "MatMul_bias.npy")) |
| 103 | .set_name("fc0") |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 104 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, cell_clip)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 105 | .set_name("Relu") |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 106 | // Layer 2 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | << FullyConnectedLayer(2048U, get_weights_accessor(data_path, "h2_transpose.npy", weights_layout), |
| 108 | get_weights_accessor(data_path, "MatMul_1_bias.npy")) |
| 109 | .set_name("fc1") |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 110 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, cell_clip)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 111 | .set_name("Relu_1") |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 112 | // Layer 3 |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 113 | << FullyConnectedLayer(2048U, get_weights_accessor(data_path, "h3_transpose.npy", weights_layout), |
| 114 | get_weights_accessor(data_path, "MatMul_2_bias.npy")) |
| 115 | .set_name("fc2") |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 116 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, cell_clip)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 117 | .set_name("Relu_2") |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 118 | // Layer 4 |
| 119 | << ReshapeLayer(TensorShape(2048U, 1U, n_steps)).set_name("Reshape_1"); |
| 120 | |
| 121 | // Unstack Layer (using SplitLayerNode) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 122 | NodeParams unstack_params = {"unstack", graph.hints().target_hint}; |
| 123 | NodeID unstack_nid = |
| 124 | GraphBuilder::add_split_node(graph.graph(), unstack_params, {graph.tail_node(), 0}, n_steps, 2); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 125 | |
| 126 | // Create input state descriptor |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 127 | TensorDescriptor state_descriptor = |
| 128 | TensorDescriptor(TensorShape(2048U), common_params.data_type).set_layout(common_params.data_layout); |
| 129 | SubStream previous_state(graph); |
| 130 | SubStream add_y(graph); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 131 | |
| 132 | // Initial state for LSTM is all zeroes for both state_h and state_c, therefore only one input is created |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 133 | previous_state << InputLayer(state_descriptor, get_weights_accessor(data_path, "zeros.npy")) |
| 134 | .set_name("previous_state_c_h"); |
| 135 | add_y << InputLayer(state_descriptor, get_weights_accessor(data_path, "ones.npy")).set_name("add_y"); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 136 | |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 137 | // Create LSTM Fully Connected weights and bias descriptors |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 138 | TensorDescriptor lstm_weights_descriptor = |
| 139 | TensorDescriptor(TensorShape(4096U, 8192U), common_params.data_type).set_layout(common_params.data_layout); |
| 140 | TensorDescriptor lstm_bias_descriptor = |
| 141 | TensorDescriptor(TensorShape(8192U), common_params.data_type).set_layout(common_params.data_layout); |
| 142 | SubStream lstm_fc_weights(graph); |
| 143 | SubStream lstm_fc_bias(graph); |
| 144 | lstm_fc_weights << ConstantLayer( |
| 145 | lstm_weights_descriptor, |
| 146 | get_weights_accessor(data_path, "rnn_lstm_cell_kernel_transpose.npy", weights_layout)) |
| 147 | .set_name("h5/transpose"); |
Michalis Spyrou | bd18811 | 2019-10-02 15:43:44 +0100 | [diff] [blame] | 148 | lstm_fc_bias << ConstantLayer(lstm_bias_descriptor, |
| 149 | get_weights_accessor(data_path, "rnn_lstm_cell_MatMul_bias.npy")) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 150 | .set_name("MatMul_3_bias"); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 151 | |
| 152 | // LSTM Block |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 153 | std::pair<SubStream, SubStream> new_state_1 = |
| 154 | add_lstm_cell(unstack_nid, 0, previous_state, previous_state, add_y, lstm_fc_weights, lstm_fc_bias); |
| 155 | std::pair<SubStream, SubStream> new_state_2 = |
| 156 | add_lstm_cell(unstack_nid, 1, new_state_1.first, new_state_1.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 157 | std::pair<SubStream, SubStream> new_state_3 = |
| 158 | add_lstm_cell(unstack_nid, 2, new_state_2.first, new_state_2.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 159 | std::pair<SubStream, SubStream> new_state_4 = |
| 160 | add_lstm_cell(unstack_nid, 3, new_state_3.first, new_state_3.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 161 | std::pair<SubStream, SubStream> new_state_5 = |
| 162 | add_lstm_cell(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 = |
| 164 | add_lstm_cell(unstack_nid, 5, new_state_5.first, new_state_5.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 165 | std::pair<SubStream, SubStream> new_state_7 = |
| 166 | add_lstm_cell(unstack_nid, 6, new_state_6.first, new_state_6.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 167 | std::pair<SubStream, SubStream> new_state_8 = |
| 168 | add_lstm_cell(unstack_nid, 7, new_state_7.first, new_state_7.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 169 | std::pair<SubStream, SubStream> new_state_9 = |
| 170 | add_lstm_cell(unstack_nid, 8, new_state_8.first, new_state_8.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 171 | std::pair<SubStream, SubStream> new_state_10 = |
| 172 | add_lstm_cell(unstack_nid, 9, new_state_9.first, new_state_9.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 173 | std::pair<SubStream, SubStream> new_state_11 = add_lstm_cell( |
| 174 | unstack_nid, 10, new_state_10.first, new_state_10.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 175 | std::pair<SubStream, SubStream> new_state_12 = add_lstm_cell( |
| 176 | unstack_nid, 11, new_state_11.first, new_state_11.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 177 | std::pair<SubStream, SubStream> new_state_13 = add_lstm_cell( |
| 178 | unstack_nid, 12, new_state_12.first, new_state_12.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 179 | std::pair<SubStream, SubStream> new_state_14 = add_lstm_cell( |
| 180 | unstack_nid, 13, new_state_13.first, new_state_13.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 181 | std::pair<SubStream, SubStream> new_state_15 = add_lstm_cell( |
| 182 | unstack_nid, 14, new_state_14.first, new_state_14.second, add_y, lstm_fc_weights, lstm_fc_bias); |
| 183 | std::pair<SubStream, SubStream> new_state_16 = add_lstm_cell( |
| 184 | unstack_nid, 15, new_state_15.first, new_state_15.second, add_y, lstm_fc_weights, lstm_fc_bias); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 185 | |
Michalis Spyrou | bd18811 | 2019-10-02 15:43:44 +0100 | [diff] [blame] | 186 | // Concatenate new states on height |
| 187 | const int axis = 1; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 188 | graph << StackLayer(axis, std::move(new_state_1.second), std::move(new_state_2.second), |
| 189 | std::move(new_state_3.second), std::move(new_state_4.second), std::move(new_state_5.second), |
| 190 | std::move(new_state_6.second), std::move(new_state_7.second), std::move(new_state_8.second), |
| 191 | std::move(new_state_9.second), std::move(new_state_10.second), |
| 192 | std::move(new_state_11.second), std::move(new_state_12.second), |
| 193 | std::move(new_state_13.second), std::move(new_state_14.second), |
| 194 | std::move(new_state_15.second), std::move(new_state_16.second)) |
| 195 | .set_name("concat"); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 196 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 197 | graph << FullyConnectedLayer(2048U, get_weights_accessor(data_path, "h5_transpose.npy", weights_layout), |
| 198 | get_weights_accessor(data_path, "MatMul_3_bias.npy")) |
| 199 | .set_name("fc3") |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 200 | << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::BOUNDED_RELU, cell_clip)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 201 | .set_name("Relu3") |
| 202 | << FullyConnectedLayer(29U, get_weights_accessor(data_path, "h6_transpose.npy", weights_layout), |
| 203 | get_weights_accessor(data_path, "MatMul_4_bias.npy")) |
| 204 | .set_name("fc3") |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 205 | << SoftmaxLayer().set_name("logits"); |
| 206 | |
| 207 | graph << OutputLayer(get_output_accessor(common_params, 5)); |
| 208 | |
| 209 | // Finalize graph |
| 210 | GraphConfig config; |
SiCongLi | f466d75 | 2021-03-01 15:26:18 +0000 | [diff] [blame] | 211 | config.num_threads = common_params.threads; |
| 212 | config.use_tuner = common_params.enable_tuner; |
| 213 | config.tuner_file = common_params.tuner_file; |
| 214 | config.mlgo_file = common_params.mlgo_file; |
| 215 | config.use_synthetic_type = arm_compute::is_data_type_quantized(common_params.data_type); |
| 216 | config.synthetic_type = common_params.data_type; |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 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 | |
| 228 | private: |
| 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 ¶ms) |
| 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 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 244 | std::pair<SubStream, SubStream> add_lstm_cell(NodeID unstack_nid, |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 245 | unsigned int unstack_idx, |
| 246 | SubStream previous_state_c, |
| 247 | SubStream previous_state_h, |
Michalis Spyrou | bd18811 | 2019-10-02 15:43:44 +0100 | [diff] [blame] | 248 | SubStream add_y, |
| 249 | SubStream lstm_fc_weights, |
| 250 | SubStream lstm_fc_bias) |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 251 | { |
| 252 | const std::string cell_name("rnn/lstm_cell_" + std::to_string(unstack_idx)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 253 | const DataLayoutDimension concat_dim = |
| 254 | (common_params.data_layout == DataLayout::NHWC) ? DataLayoutDimension::CHANNEL : DataLayoutDimension::WIDTH; |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 255 | |
| 256 | // Concatenate result of Unstack with previous_state_h |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 257 | NodeParams concat_params = {cell_name + "/concat", graph.hints().target_hint}; |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 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 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 264 | graph << FullyConnectedLayer(8192U, lstm_fc_weights, lstm_fc_bias).set_name(cell_name + "/BiasAdd"); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 265 | |
| 266 | // Split Layer |
| 267 | const unsigned int num_splits = 4; |
| 268 | const unsigned int split_axis = 0; |
| 269 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 270 | NodeParams split_params = {cell_name + "/split", graph.hints().target_hint}; |
| 271 | NodeID split_nid = |
| 272 | GraphBuilder::add_split_node(graph.graph(), split_params, {graph.tail_node(), 0}, num_splits, split_axis); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 273 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 274 | NodeParams sigmoid_1_params = {cell_name + "/Sigmoid_1", graph.hints().target_hint}; |
| 275 | NodeParams add_params = {cell_name + "/add", graph.hints().target_hint}; |
| 276 | NodeParams sigmoid_2_params = {cell_name + "/Sigmoid_2", graph.hints().target_hint}; |
| 277 | NodeParams tanh_params = {cell_name + "/Tanh", graph.hints().target_hint}; |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 278 | |
| 279 | // Sigmoid 1 (first split) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 280 | NodeID sigmoid_1_nid = graph.graph().add_node<ActivationLayerNode>( |
| 281 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 282 | graph.graph().add_connection(split_nid, 0, sigmoid_1_nid, 0); |
| 283 | set_node_params(graph.graph(), sigmoid_1_nid, sigmoid_1_params); |
| 284 | |
| 285 | // Tanh (second split) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 286 | NodeID tanh_nid = graph.graph().add_node<ActivationLayerNode>( |
| 287 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 288 | graph.graph().add_connection(split_nid, 1, tanh_nid, 0); |
| 289 | set_node_params(graph.graph(), tanh_nid, tanh_params); |
| 290 | |
| 291 | SubStream tanh_ss(graph); |
| 292 | tanh_ss.forward_tail(tanh_nid); |
| 293 | |
| 294 | // Add (third split) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 295 | NodeID add_nid = |
| 296 | graph.graph().add_node<EltwiseLayerNode>(descriptors::EltwiseLayerDescriptor{EltwiseOperation::Add}); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 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) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 302 | NodeID sigmoid_2_nid = graph.graph().add_node<ActivationLayerNode>( |
| 303 | ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 304 | graph.graph().add_connection(split_nid, 3, sigmoid_2_nid, 0); |
| 305 | set_node_params(graph.graph(), sigmoid_2_nid, sigmoid_2_params); |
| 306 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 307 | SubStream sigmoid_1_ss(graph); |
| 308 | sigmoid_1_ss.forward_tail(sigmoid_1_nid); |
| 309 | SubStream mul_1_ss(sigmoid_1_ss); |
| 310 | mul_1_ss << EltwiseLayer(std::move(sigmoid_1_ss), std::move(tanh_ss), EltwiseOperation::Mul) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 311 | .set_name(cell_name + "/mul_1"); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 312 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 313 | SubStream tanh_1_ss_tmp(graph); |
| 314 | tanh_1_ss_tmp.forward_tail(add_nid); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 315 | |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 316 | tanh_1_ss_tmp << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::LOGISTIC)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 317 | .set_name(cell_name + "/Sigmoid"); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 318 | SubStream tanh_1_ss_tmp2(tanh_1_ss_tmp); |
| 319 | tanh_1_ss_tmp2 << EltwiseLayer(std::move(tanh_1_ss_tmp), std::move(previous_state_c), EltwiseOperation::Mul) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 320 | .set_name(cell_name + "/mul"); |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 321 | SubStream tanh_1_ss(tanh_1_ss_tmp2); |
| 322 | tanh_1_ss << EltwiseLayer(std::move(tanh_1_ss_tmp2), std::move(mul_1_ss), EltwiseOperation::Add) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 323 | .set_name(cell_name + "/new_state_c"); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 324 | SubStream new_state_c(tanh_1_ss); |
| 325 | |
| 326 | tanh_1_ss << ActivationLayer(ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::TANH, 1.f, 1.f)) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 327 | .set_name(cell_name + "/Tanh_1"); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 328 | |
| 329 | SubStream sigmoid_2_ss(graph); |
| 330 | sigmoid_2_ss.forward_tail(sigmoid_2_nid); |
| 331 | graph << EltwiseLayer(std::move(sigmoid_2_ss), std::move(tanh_1_ss), EltwiseOperation::Mul) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 332 | .set_name(cell_name + "/new_state_h"); |
Michele Di Giorgio | 3418ba5 | 2019-03-01 17:19:55 +0000 | [diff] [blame] | 333 | |
| 334 | SubStream new_state_h(graph); |
| 335 | return std::pair<SubStream, SubStream>(new_state_c, new_state_h); |
| 336 | } |
| 337 | }; |
| 338 | |
| 339 | /** Main program for DeepSpeech v0.4.1 |
| 340 | * |
| 341 | * Model is based on: |
| 342 | * https://arxiv.org/abs/1412.5567 |
| 343 | * "Deep Speech: Scaling up end-to-end speech recognition" |
| 344 | * Awni Hannun, Carl Case, Jared Casper, Bryan Catanzaro, Greg Diamos, Erich Elsen, Ryan Prenger, Sanjeev Satheesh, Shubho Sengupta, Adam Coates, Andrew Y. Ng |
| 345 | * |
| 346 | * Provenance: https://github.com/mozilla/DeepSpeech |
| 347 | * |
| 348 | * @note To list all the possible arguments execute the binary appended with the --help option |
| 349 | * |
| 350 | * @param[in] argc Number of arguments |
| 351 | * @param[in] argv Arguments |
| 352 | * |
| 353 | * @return Return code |
| 354 | */ |
| 355 | int main(int argc, char **argv) |
| 356 | { |
| 357 | return arm_compute::utils::run_example<GraphDeepSpeechExample>(argc, argv); |
| 358 | } |