Elffuss BraidHub

kikocis/ncnn public

pnnx fuse mobilevit style selfattention, onnx2pnnx handle more general gemm (#5659)

Changeset8e190bc8d0b588cd2a624eb8ca9342b422820959c2881a9db1f51b0de76eb968
Authornihui <nihuini@tencent.com> · 2024-08-30 12:27 +08:00
CommitterGitHub <noreply@github.com> · 2024-08-30 12:27 +08:00
Parentsea2b092788e9

Browse the files at this changeset

3 files changed

modified tools/pnnx/src/pass_level2/F_hardswish.cpp +26 −0
@@ -343,4 +343,30 @@
343343
344344 REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_onnx_2, 9)
345345
346+class F_hardswish_onnx_3 : public GraphRewriterPass
347+{
348+public:
349+ const char* match_pattern_graph() const
350+ {
351+ return R"PNNXIR(7767517
352+8 7
353+pnnx.Input input 0 1 input
354+prim::Constant op_0 0 1 v3 value=3
355+aten::add op_1 2 1 input v3 a
356+aten::clamp op_2 1 1 a b max=6 min=0
357+aten::mul op_3 2 1 input b c
358+prim::Constant op_4 0 1 v6 value=6
359+aten::div op_5 2 1 c v6 out
360+pnnx.Output output 1 0 out
361+)PNNXIR";
362+ }
363+
364+ const char* type_str() const
365+ {
366+ return "F.hardswish";
367+ }
368+};
369+
370+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_onnx_3, 9)
371+
346372 } // namespace pnnx
modified tools/pnnx/src/pass_level2/F_linear.cpp +64 −2
@@ -129,7 +129,7 @@
129129 pnnx.Input input_0 0 1 input
130130 pnnx.Input input_1 0 1 weight
131131 pnnx.Input input_2 0 1 bias
132-Gemm op_0 3 1 input weight bias out alpha=1.000000e+00 beta=1.000000e+00 transB=1
132+Gemm gemm 3 1 input weight bias out %*=%*
133133 pnnx.Output output 1 0 out
134134 )PNNXIR";
135135 }
@@ -138,6 +138,39 @@
138138 {
139139 return "F.linear";
140140 }
141+
142+ bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const
143+ {
144+ if (captured_params.find("gemm.alpha") != captured_params.end())
145+ {
146+ if (captured_params.at("gemm.alpha").type != 3 || captured_params.at("gemm.alpha").f != 1.f)
147+ return false;
148+ }
149+
150+ if (captured_params.find("gemm.beta") != captured_params.end())
151+ {
152+ if (captured_params.at("gemm.beta").type != 3 || captured_params.at("gemm.beta").f != 1.f)
153+ return false;
154+ }
155+
156+ if (captured_params.find("gemm.transA") != captured_params.end())
157+ {
158+ if (captured_params.at("gemm.transA").type != 2 || captured_params.at("gemm.transA").i != 0)
159+ return false;
160+ }
161+
162+ if (captured_params.find("gemm.transB") == captured_params.end())
163+ return false;
164+
165+ if (captured_params.at("gemm.transB").type != 2 || captured_params.at("gemm.transB").i != 1)
166+ return false;
167+
168+ return true;
169+ }
170+
171+ void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const
172+ {
173+ }
141174 };
142175
143176 REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_onnx, 10)
@@ -152,7 +185,7 @@
152185 pnnx.Input input_0 0 1 input
153186 pnnx.Input input_1 0 1 bias
154187 pnnx.Attribute weight 0 1 weight @data=(%in_features,%out_features)f32
155-Gemm gemm 3 1 input weight bias out alpha=1.000000e+00 beta=1.000000e+00
188+Gemm gemm 3 1 input weight bias out %*=%*
156189 pnnx.Output output 1 0 out
157190 )PNNXIR";
158191 }
@@ -169,6 +202,35 @@
169202 )PNNXIR";
170203 }
171204
205+ bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const
206+ {
207+ if (captured_params.find("gemm.alpha") != captured_params.end())
208+ {
209+ if (captured_params.at("gemm.alpha").type != 3 || captured_params.at("gemm.alpha").f != 1.f)
210+ return false;
211+ }
212+
213+ if (captured_params.find("gemm.beta") != captured_params.end())
214+ {
215+ if (captured_params.at("gemm.beta").type != 3 || captured_params.at("gemm.beta").f != 1.f)
216+ return false;
217+ }
218+
219+ if (captured_params.find("gemm.transA") != captured_params.end())
220+ {
221+ if (captured_params.at("gemm.transA").type != 2 || captured_params.at("gemm.transA").i != 0)
222+ return false;
223+ }
224+
225+ if (captured_params.find("gemm.transB") != captured_params.end())
226+ {
227+ if (captured_params.at("gemm.transB").type != 2 || captured_params.at("gemm.transB").i != 0)
228+ return false;
229+ }
230+
231+ return true;
232+ }
233+
172234 void write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
173235 {
174236 const int in_features = captured_params.at("in_features").i;
modified tools/pnnx/src/pass_level5/fuse_multiheadattention.cpp +53 −0
@@ -702,6 +702,57 @@
702702 }
703703 };
704704
705+class fuse_multiheadattention_pass_1_1_1 : public fuse_multiheadattention_pass_sameqkv
706+{
707+public:
708+ const char* match_pattern_graph() const
709+ {
710+ return R"PNNXIR(7767517
711+19 18
712+pnnx.Input input 0 1 input
713+nn.Linear op_0 1 1 input 256 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
714+nn.Linear op_1 1 1 input 257 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight
715+nn.Linear op_2 1 1 input 260 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight
716+Tensor.view op_3 1 1 256 263 shape=(%batch,%size,%num_heads,%feat_per_head)
717+Tensor.view op_4 1 1 257 258 shape=(%batch,%size,%num_heads,%feat_per_head)
718+Tensor.view op_5 1 1 260 261 shape=(%batch,%size,%num_heads,%feat_per_head)
719+Tensor.permute op_6 1 1 263 264 dims=(0,2,1,3)
720+Tensor.permute op_7 1 1 258 259 dims=(0,2,1,3)
721+Tensor.permute op_8 1 1 261 262 dims=(0,2,1,3)
722+torch.transpose op_9 1 1 259 265 dim0=-1 dim1=-2
723+torch.matmul op_10 2 1 264 265 266
724+pnnx.Expression op_11 1 1 266 267 expr=div(@0,%sqrt_feat_per_head)
725+F.softmax softmax 1 1 267 268 dim=%softmax_dim
726+torch.matmul op_13 2 1 268 262 269
727+Tensor.permute op_14 1 1 269 270 dims=(0,2,1,3)
728+Tensor.reshape op_15 1 1 270 271 shape=(%batch,%size,%embed_dim)
729+nn.Linear out_proj 1 1 271 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
730+pnnx.Output output 1 0 out
731+)PNNXIR";
732+ }
733+
734+ bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const
735+ {
736+ const int embed_dim = captured_params.at("embed_dim").i;
737+ const int num_heads = captured_params.at("num_heads").i;
738+ const int feat_per_head = captured_params.at("feat_per_head").i;
739+ const float sqrt_feat_per_head = captured_params.at("sqrt_feat_per_head").f;
740+ const int softmax_dim = captured_params.at("softmax_dim").i;
741+
742+ if (embed_dim != num_heads * feat_per_head)
743+ return false;
744+
745+ if (!NearlyEqual(sqrt_feat_per_head, sqrt(feat_per_head), 0.001))
746+ return false;
747+
748+ int softmax_input_rank = (int)matched_operators.at("softmax")->inputs[0]->shape.size();
749+ if (softmax_dim != -1 && softmax_dim != softmax_input_rank - 1)
750+ return false;
751+
752+ return true;
753+ }
754+};
755+
705756 class fuse_multiheadattention_pass_1_2 : public fuse_multiheadattention_pass_qkv
706757 {
707758 public:
@@ -2082,6 +2133,7 @@
20822133 fuse_multiheadattention_pass_q_samekv d;
20832134 fuse_multiheadattention_pass_1 b1;
20842135 fuse_multiheadattention_pass_1_1 b11;
2136+ fuse_multiheadattention_pass_1_1_1 b111;
20852137 fuse_multiheadattention_pass_1_2 b12;
20862138 fuse_multiheadattention_pass_2 c1;
20872139 fuse_multiheadattention_pass_3 d1;
@@ -2122,6 +2174,7 @@
21222174 pnnx_graph_rewrite(graph, &d, opindex);
21232175 pnnx_graph_rewrite(graph, &b1, opindex);
21242176 pnnx_graph_rewrite(graph, &b11, opindex);
2177+ pnnx_graph_rewrite(graph, &b111, opindex);
21252178 pnnx_graph_rewrite(graph, &b12, opindex);
21262179 pnnx_graph_rewrite(graph, &c1, opindex);
21272180 pnnx_graph_rewrite(graph, &d1, opindex);