Elffuss BraidHub

kikocis/ncnn public

master · 7.2 KiBRawPermalink
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 "pass_ncnn.h"
16#include <string.h>
17
18namespace pnnx {
19
20namespace ncnn {
21
22class nn_GRU : public GraphRewriterPass
23{
24public:
25 const char* match_pattern_graph() const
26 {
27 return R"PNNXIR(7767517
283 3
29pnnx.Input input 0 1 input
30nn.GRU op_0 1 2 input out out_hidden input_size=%input_size hidden_size=%hidden_size num_layers=1 bias=%bias batch_first=%batch_first bidirectional=%bidirectional @weight_ih_l0 @weight_hh_l0 @bias_ih_l0 @bias_hh_l0 @weight_ih_l0_reverse @weight_hh_l0_reverse @bias_ih_l0_reverse @bias_hh_l0_reverse
31pnnx.Output output 2 0 out out_hidden
32)PNNXIR";
33 }
34
35 const char* type_str() const
36 {
37 return "GRU";
38 }
39
40 const char* name_str() const
41 {
42 return "gru";
43 }
44
45 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
46 {
47 const bool bidirectional = captured_params.at("bidirectional").b;
48 const int num_directions = bidirectional ? 2 : 1;
49 const int num_output = captured_params.at("hidden_size").i;
50 const int input_size = captured_params.at("input_size").i;
51
52 int weight_data_size = num_directions * num_output * input_size * 3;
53
54 op->params["0"] = num_output;
55 op->params["1"] = weight_data_size;
56 op->params["2"] = bidirectional ? 2 : 0;
57
58 op->attrs["0"] = Attribute();
59 op->attrs["0"].data = {0, 0, 0, 0};
60
61 // RUN-hidden-input_size
62 {
63 if (bidirectional)
64 op->attrs["1"] = captured_attrs.at("op_0.weight_ih_l0") + captured_attrs.at("op_0.weight_ih_l0_reverse");
65 else
66 op->attrs["1"] = captured_attrs.at("op_0.weight_ih_l0");
67 }
68
69 op->attrs["2"] = Attribute();
70 op->attrs["2"].data = {0, 0, 0, 0};
71 if (captured_params.at("bias").b)
72 {
73 // reduce bias_ih and bias_hh
74 std::vector<float> new_bias;
75 {
76 auto bias_ih = captured_attrs.at("op_0.bias_ih_l0").get_float32_data();
77 auto bias_hh = captured_attrs.at("op_0.bias_hh_l0").get_float32_data();
78
79 new_bias.resize(4 * num_output);
80 float* bias = (float*)new_bias.data();
81 for (int i = 0; i < 2 * num_output; i++)
82 {
83 bias[i] = bias_ih[i] + bias_hh[i];
84 }
85 memcpy(bias + num_output * 2, (const float*)bias_ih.data() + num_output * 2, num_output * sizeof(float));
86 memcpy(bias + num_output * 3, (const float*)bias_hh.data() + num_output * 2, num_output * sizeof(float));
87 }
88
89 if (bidirectional)
90 {
91 std::vector<float> new_bias_reverse;
92 {
93 auto bias_ih = captured_attrs.at("op_0.bias_ih_l0_reverse").get_float32_data();
94 auto bias_hh = captured_attrs.at("op_0.bias_hh_l0_reverse").get_float32_data();
95
96 new_bias_reverse.resize(4 * num_output);
97 float* bias = (float*)new_bias_reverse.data();
98 for (int i = 0; i < 2 * num_output; i++)
99 {
100 bias[i] = bias_ih[i] + bias_hh[i];
101 }
102 memcpy(bias + num_output * 2, (const float*)bias_ih.data() + num_output * 2, num_output * sizeof(float));
103 memcpy(bias + num_output * 3, (const float*)bias_hh.data() + num_output * 2, num_output * sizeof(float));
104 }
105
106 op->attrs["3"] = Attribute({4, num_output}, new_bias) + Attribute({4, num_output}, new_bias_reverse);
107 }
108 else
109 {
110 op->attrs["3"] = Attribute({4, num_output}, new_bias);
111 }
112 }
113 else
114 {
115 std::vector<float> bias(4 * num_output, 0.f);
116
117 if (bidirectional)
118 op->attrs["3"] = Attribute({4, num_output}, bias) + Attribute({4, num_output}, bias);
119 else
120 op->attrs["3"] = Attribute({4, num_output}, bias);
121 }
122
123 op->attrs["4"] = Attribute();
124 op->attrs["4"].data = {0, 0, 0, 0};
125
126 // RUN-hidden-hidden
127 {
128 if (bidirectional)
129 op->attrs["5"] = captured_attrs.at("op_0.weight_hh_l0") + captured_attrs.at("op_0.weight_hh_l0_reverse");
130 else
131 op->attrs["5"] = captured_attrs.at("op_0.weight_hh_l0");
132 }
133 }
134};
135
136REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_GRU, 20)
137
138class nn_GRU_1 : public nn_GRU
139{
140public:
141 const char* match_pattern_graph() const
142 {
143 return R"PNNXIR(7767517
1444 4
145pnnx.Input input 0 1 input
146pnnx.Input in_hidden 0 1 in_hidden
147nn.GRU op_0 2 2 input in_hidden out out_hidden input_size=%input_size hidden_size=%hidden_size num_layers=1 bias=%bias batch_first=%batch_first bidirectional=%bidirectional @weight_ih_l0 @weight_hh_l0 @bias_ih_l0 @bias_hh_l0 @weight_ih_l0_reverse @weight_hh_l0_reverse @bias_ih_l0_reverse @bias_hh_l0_reverse
148pnnx.Output output 2 0 out out_hidden
149)PNNXIR";
150 }
151};
152
153REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_GRU_1, 20)
154
155class nn_GRU_2 : public nn_GRU
156{
157public:
158 const char* match_pattern_graph() const
159 {
160 return R"PNNXIR(7767517
1613 2
162pnnx.Input input 0 1 input
163nn.GRU op_0 1 1 input out input_size=%input_size hidden_size=%hidden_size num_layers=1 bias=%bias batch_first=%batch_first bidirectional=%bidirectional @weight_ih_l0 @weight_hh_l0 @bias_ih_l0 @bias_hh_l0 @weight_ih_l0_reverse @weight_hh_l0_reverse @bias_ih_l0_reverse @bias_hh_l0_reverse
164pnnx.Output output 1 0 out
165)PNNXIR";
166 }
167};
168
169REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_GRU_2, 20)
170
171class nn_GRU_3 : public nn_GRU
172{
173public:
174 const char* match_pattern_graph() const
175 {
176 return R"PNNXIR(7767517
1774 3
178pnnx.Input input 0 1 input
179pnnx.Input in_hidden 0 1 in_hidden
180nn.GRU op_0 2 1 input in_hidden out input_size=%input_size hidden_size=%hidden_size num_layers=1 bias=%bias batch_first=%batch_first bidirectional=%bidirectional @weight_ih_l0 @weight_hh_l0 @bias_ih_l0 @bias_hh_l0 @weight_ih_l0_reverse @weight_hh_l0_reverse @bias_ih_l0_reverse @bias_hh_l0_reverse
181pnnx.Output output 1 0 out
182)PNNXIR";
183 }
184};
185
186REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_GRU_3, 20)
187
188} // namespace ncnn
189
190} // namespace pnnx