Elffuss BraidHub

kikocis/ncnn public

master · 5.1 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 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
17namespace pnnx {
18
19namespace ncnn {
20
21class torch_addmm : public GraphRewriterPass
22{
23public:
24 const char* match_pattern_graph() const
25 {
26 return R"PNNXIR(7767517
275 4
28pnnx.Input input_0 0 1 mat1
29pnnx.Attribute op_bias 0 1 bias @data
30pnnx.Attribute op_weight 0 1 weight @data
31torch.addmm op_0 3 1 bias mat1 weight out alpha=%alpha beta=%beta
32pnnx.Output output 1 0 out
33)PNNXIR";
34 }
35
36 const char* type_str() const
37 {
38 return "InnerProduct";
39 }
40
41 const char* name_str() const
42 {
43 return "addmm";
44 }
45
46 bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
47 {
48 float alpha = 1.f;
49 float beta = 1.f;
50
51 if (captured_params.at("alpha").type == 2)
52 {
53 alpha = (float)captured_params.at("alpha").i;
54 }
55 if (captured_params.at("alpha").type == 3)
56 {
57 alpha = captured_params.at("alpha").f;
58 }
59
60 if (captured_params.at("beta").type == 2)
61 {
62 beta = (float)captured_params.at("beta").i;
63 }
64 if (captured_params.at("beta").type == 3)
65 {
66 beta = captured_params.at("beta").f;
67 }
68
69 if (alpha != 1.f || beta != 1.f)
70 return false;
71
72 Attribute weight = captured_attrs.at("op_weight.data");
73 Attribute bias = captured_attrs.at("op_bias.data");
74
75 if (weight.shape.size() != 2 || bias.shape.size() != 1)
76 return false;
77
78 if (weight.shape[1] != bias.shape[0])
79 return false;
80
81 return true;
82 }
83
84 void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/, const std::map<std::string, Attribute>& captured_attrs) const
85 {
86 Attribute weight = captured_attrs.at("op_weight.data");
87 Attribute bias = captured_attrs.at("op_bias.data");
88
89 // transpose weight inch-outch to outch-inch
90 const int inch = weight.shape[0];
91 const int outch = weight.shape[1];
92 std::vector<float> new_weight;
93 {
94 auto w = weight.get_float32_data();
95
96 new_weight.resize(outch * inch);
97 float* w2 = (float*)new_weight.data();
98
99 // reorder weight from inch-outch to outch-inch
100 for (int i = 0; i < outch; i++)
101 {
102 for (int j = 0; j < inch; j++)
103 {
104 w2[i * inch + j] = w[j * outch + i];
105 }
106 }
107 }
108
109 op->params["0"] = outch;
110 op->params["1"] = 1;
111 op->params["2"] = weight.elemcount();
112
113 op->attrs["0"] = Attribute();
114 op->attrs["0"].data = {0, 0, 0, 0};
115 op->attrs["1"] = Attribute({outch, inch}, new_weight);
116 op->attrs["2"] = bias;
117 }
118};
119
120REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(torch_addmm, 20)
121
122class torch_addmm_1 : public GraphRewriterPass
123{
124public:
125 const char* match_pattern_graph() const
126 {
127 return R"PNNXIR(7767517
1285 4
129pnnx.Input input_0 0 1 input
130pnnx.Input input_1 0 1 mat1
131pnnx.Input input_2 0 1 mat2
132torch.addmm op_0 3 1 input mat1 mat2 out alpha=%alpha beta=%beta
133pnnx.Output output 1 0 out
134)PNNXIR";
135 }
136
137 const char* type_str() const
138 {
139 return "Gemm";
140 }
141
142 const char* name_str() const
143 {
144 return "addmm";
145 }
146
147 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
148 {
149 std::swap(op->inputs[0], op->inputs[1]);
150 std::swap(op->inputs[1], op->inputs[2]);
151
152 float alpha = 1.f;
153 float beta = 1.f;
154
155 if (captured_params.at("alpha").type == 2)
156 {
157 alpha = (float)captured_params.at("alpha").i;
158 }
159 if (captured_params.at("alpha").type == 3)
160 {
161 alpha = captured_params.at("alpha").f;
162 }
163
164 if (captured_params.at("beta").type == 2)
165 {
166 beta = (float)captured_params.at("beta").i;
167 }
168 if (captured_params.at("beta").type == 3)
169 {
170 beta = captured_params.at("beta").f;
171 }
172
173 op->params["0"] = alpha;
174 op->params["1"] = beta / alpha;
175 }
176};
177
178REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(torch_addmm_1, 22)
179
180} // namespace ncnn
181
182} // namespace pnnx