| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "unroll_rnn_op.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | |
| 19 | namespace pnnx { |
| 20 | |
| 21 | void unroll_rnn_op(Graph& graph) |
| 22 | { |
| 23 | while (1) |
| 24 | { |
| 25 | bool matched = false; |
| 26 | |
| 27 | for (size_t i = 0; i < graph.ops.size(); i++) |
| 28 | { |
| 29 | Operator* op = graph.ops[i]; |
| 30 | |
| 31 | if (op->type != "nn.RNN" && op->type != "nn.LSTM" && op->type != "nn.GRU") |
| 32 | continue; |
| 33 | |
| 34 | int num_layers = op->params["num_layers"].i; |
| 35 | if (num_layers == 1) |
| 36 | continue; |
| 37 | |
| 38 | matched = true; |
| 39 | |
| 40 | bool has_input_hidden = op->inputs.size() >= 2; |
| 41 | bool has_input_cell = op->inputs.size() == 3; |
| 42 | bool has_output_hidden = op->outputs.size() >= 2; |
| 43 | bool has_output_cell = op->outputs.size() == 3; |
| 44 | const int hidden_size = op->params["hidden_size"].i; |
| 45 | const int proj_size = (op->type == "nn.LSTM") ? op->params["proj_size"].i : 0; |
| 46 | bool has_bias = op->params["bias"].b; |
| 47 | bool is_bidirectional = op->params["bidirectional"].b; |
| 48 | |
| 49 | std::vector<Operand*> input_hiddens(num_layers); |
| 50 | std::vector<Operand*> input_cells(num_layers); |
| 51 | std::vector<Operand*> output_hiddens(num_layers); |
| 52 | std::vector<Operand*> output_cells(num_layers); |
| 53 | |
| 54 | // slice input hidden cell |
| 55 | if (has_input_hidden) |
| 56 | { |
| 57 | std::string opname = op->name + "_chunk_in_hidden"; |
| 58 | |
| 59 | Operator* op1 = graph.new_operator_before("torch.chunk", opname, op); |
| 60 | |
| 61 | op1->params["chunks"] = num_layers; |
| 62 | op1->params["dim"] = 0; |
| 63 | |
| 64 | op1->inputs.push_back(op->inputs[1]); |
| 65 | op->inputs[1]->remove_consumer(op); |
| 66 | op->inputs[1]->consumers.push_back(op1); |
| 67 | |
| 68 | for (int j = 0; j < num_layers; j++) |
| 69 | { |
| 70 | Operand* r0 = graph.new_operand(op1->name + "_in_hidden_" + std::to_string(j)); |
| 71 | r0->producer = op1; |
| 72 | op1->outputs.push_back(r0); |
| 73 | |
| 74 | input_hiddens[j] = r0; |
| 75 | } |
| 76 | } |
| 77 | if (has_input_cell) |
| 78 | { |
| 79 | std::string opname = op->name + "_chunk_in_cell"; |
| 80 | |
| 81 | Operator* op1 = graph.new_operator_before("torch.chunk", opname, op); |
| 82 | |
| 83 | op1->params["chunks"] = num_layers; |
| 84 | op1->params["dim"] = 0; |
| 85 | |
| 86 | op1->inputs.push_back(op->inputs[2]); |
| 87 | op->inputs[2]->remove_consumer(op); |
| 88 | op->inputs[2]->consumers.push_back(op1); |
| 89 | |
| 90 | for (int j = 0; j < num_layers; j++) |
| 91 | { |
| 92 | Operand* r0 = graph.new_operand(op1->name + "_in_cell_" + std::to_string(j)); |
| 93 | r0->producer = op1; |
| 94 | op1->outputs.push_back(r0); |
| 95 | |
| 96 | input_cells[j] = r0; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // unroll |
| 101 | std::vector<Operator*> unrolled_ops(num_layers); |
| 102 | for (int j = 0; j < num_layers; j++) |
| 103 | { |
| 104 | std::string opname = op->name + "_unroll_" + std::to_string(j); |
| 105 | |
| 106 | Operator* op1 = graph.new_operator_before(op->type, opname, op); |
| 107 | |
| 108 | op1->params = op->params; |
| 109 | op1->params["num_layers"] = 1; |
| 110 | |
| 111 | // link |
| 112 | if (j == 0) |
| 113 | { |
| 114 | op1->inputs.push_back(op->inputs[0]); |
| 115 | op1->inputs[0]->remove_consumer(op); |
| 116 | op1->inputs[0]->consumers.push_back(op1); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | if (proj_size) |
| 121 | { |
| 122 | op1->params["input_size"] = is_bidirectional ? proj_size * 2 : proj_size; |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | op1->params["input_size"] = is_bidirectional ? hidden_size * 2 : hidden_size; |
| 127 | } |
| 128 | |
| 129 | op1->inputs.push_back(unrolled_ops[j - 1]->outputs[0]); |
| 130 | op1->inputs[0]->consumers.push_back(op1); |
| 131 | } |
| 132 | |
| 133 | if (has_input_hidden) |
| 134 | { |
| 135 | op1->inputs.push_back(input_hiddens[j]); |
| 136 | op1->inputs[1]->consumers.push_back(op1); |
| 137 | } |
| 138 | if (has_input_cell) |
| 139 | { |
| 140 | op1->inputs.push_back(input_cells[j]); |
| 141 | op1->inputs[2]->consumers.push_back(op1); |
| 142 | } |
| 143 | |
| 144 | if (j == num_layers - 1) |
| 145 | { |
| 146 | op1->outputs.push_back(op->outputs[0]); |
| 147 | op1->outputs[0]->producer = op1; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | Operand* r0 = graph.new_operand(op1->name + "_out"); |
| 152 | r0->producer = op1; |
| 153 | op1->outputs.push_back(r0); |
| 154 | } |
| 155 | |
| 156 | if (has_output_hidden) |
| 157 | { |
| 158 | Operand* r1 = graph.new_operand(op1->name + "_out_hidden"); |
| 159 | r1->producer = op1; |
| 160 | op1->outputs.push_back(r1); |
| 161 | |
| 162 | output_hiddens[j] = r1; |
| 163 | } |
| 164 | if (has_output_cell) |
| 165 | { |
| 166 | Operand* r1 = graph.new_operand(op1->name + "_out_cell"); |
| 167 | r1->producer = op1; |
| 168 | op1->outputs.push_back(r1); |
| 169 | |
| 170 | output_cells[j] = r1; |
| 171 | } |
| 172 | |
| 173 | op1->attrs["weight_hh_l0"] = op->attrs["weight_hh_l" + std::to_string(j)]; |
| 174 | op1->attrs["weight_ih_l0"] = op->attrs["weight_ih_l" + std::to_string(j)]; |
| 175 | |
| 176 | if (has_bias) |
| 177 | { |
| 178 | op1->attrs["bias_hh_l0"] = op->attrs["bias_hh_l" + std::to_string(j)]; |
| 179 | op1->attrs["bias_ih_l0"] = op->attrs["bias_ih_l" + std::to_string(j)]; |
| 180 | } |
| 181 | |
| 182 | if (proj_size) |
| 183 | { |
| 184 | op1->attrs["weight_hr_l0"] = op->attrs["weight_hr_l" + std::to_string(j)]; |
| 185 | } |
| 186 | |
| 187 | if (is_bidirectional) |
| 188 | { |
| 189 | op1->attrs["weight_hh_l0_reverse"] = op->attrs["weight_hh_l" + std::to_string(j) + "_reverse"]; |
| 190 | op1->attrs["weight_ih_l0_reverse"] = op->attrs["weight_ih_l" + std::to_string(j) + "_reverse"]; |
| 191 | |
| 192 | if (has_bias) |
| 193 | { |
| 194 | op1->attrs["bias_hh_l0_reverse"] = op->attrs["bias_hh_l" + std::to_string(j) + "_reverse"]; |
| 195 | op1->attrs["bias_ih_l0_reverse"] = op->attrs["bias_ih_l" + std::to_string(j) + "_reverse"]; |
| 196 | } |
| 197 | |
| 198 | if (proj_size) |
| 199 | { |
| 200 | op1->attrs["weight_hr_l0_reverse"] = op->attrs["weight_hr_l" + std::to_string(j) + "_reverse"]; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | unrolled_ops[j] = op1; |
| 205 | } |
| 206 | |
| 207 | // concat output hidden cell |
| 208 | if (has_output_hidden) |
| 209 | { |
| 210 | std::string opname = op->name + "_cat_out_hidden"; |
| 211 | |
| 212 | Operator* op1 = graph.new_operator_before("torch.cat", opname, op); |
| 213 | |
| 214 | op1->params["dim"] = 0; |
| 215 | |
| 216 | for (int j = 0; j < num_layers; j++) |
| 217 | { |
| 218 | Operand* r0 = output_hiddens[j]; |
| 219 | r0->consumers.push_back(op1); |
| 220 | op1->inputs.push_back(r0); |
| 221 | } |
| 222 | |
| 223 | op1->outputs.push_back(op->outputs[1]); |
| 224 | op1->outputs[0]->producer = op1; |
| 225 | } |
| 226 | if (has_output_cell) |
| 227 | { |
| 228 | std::string opname = op->name + "_cat_out_cell"; |
| 229 | |
| 230 | Operator* op1 = graph.new_operator_before("torch.cat", opname, op); |
| 231 | |
| 232 | op1->params["dim"] = 0; |
| 233 | |
| 234 | for (int j = 0; j < num_layers; j++) |
| 235 | { |
| 236 | Operand* r0 = output_cells[j]; |
| 237 | r0->consumers.push_back(op1); |
| 238 | op1->inputs.push_back(r0); |
| 239 | } |
| 240 | |
| 241 | op1->outputs.push_back(op->outputs[2]); |
| 242 | op1->outputs[0]->producer = op1; |
| 243 | } |
| 244 | |
| 245 | op->inputs.clear(); |
| 246 | op->outputs.clear(); |
| 247 | |
| 248 | graph.ops.erase(std::find(graph.ops.begin(), graph.ops.end(), op)); |
| 249 | |
| 250 | delete op; |
| 251 | |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | if (!matched) |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | } // namespace pnnx |