Changesets · Branches · Tags · Compare
| Changeset | 8e190bc8d0b588cd2a624eb8ca9342b422820959c2881a9db1f51b0de76eb968 |
|---|---|
| Author | nihui <nihuini@tencent.com> · 2024-08-30 12:27 +08:00 |
| Committer | GitHub <noreply@github.com> · 2024-08-30 12:27 +08:00 |
| Parents | ea2b092788e9 |
3 files changed
modified tools/pnnx/src/pass_level2/F_hardswish.cpp +26 −0
| @@ -343,4 +343,30 @@ | ||
| 343 | 343 | |
| 344 | 344 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_onnx_2, 9) |
| 345 | 345 | |
| 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 | + | |
| 346 | 372 | } // namespace pnnx |
modified tools/pnnx/src/pass_level2/F_linear.cpp +64 −2
| @@ -129,7 +129,7 @@ | ||
| 129 | 129 | pnnx.Input input_0 0 1 input |
| 130 | 130 | pnnx.Input input_1 0 1 weight |
| 131 | 131 | 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 %*=%* | |
| 133 | 133 | pnnx.Output output 1 0 out |
| 134 | 134 | )PNNXIR"; |
| 135 | 135 | } |
| @@ -138,6 +138,39 @@ | ||
| 138 | 138 | { |
| 139 | 139 | return "F.linear"; |
| 140 | 140 | } |
| 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 | + } | |
| 141 | 174 | }; |
| 142 | 175 | |
| 143 | 176 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_onnx, 10) |
| @@ -152,7 +185,7 @@ | ||
| 152 | 185 | pnnx.Input input_0 0 1 input |
| 153 | 186 | pnnx.Input input_1 0 1 bias |
| 154 | 187 | 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 %*=%* | |
| 156 | 189 | pnnx.Output output 1 0 out |
| 157 | 190 | )PNNXIR"; |
| 158 | 191 | } |
| @@ -169,6 +202,35 @@ | ||
| 169 | 202 | )PNNXIR"; |
| 170 | 203 | } |
| 171 | 204 | |
| 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 | + | |
| 172 | 234 | 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 |
| 173 | 235 | { |
| 174 | 236 | 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 @@ | ||
| 702 | 702 | } |
| 703 | 703 | }; |
| 704 | 704 | |
| 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 | + | |
| 705 | 756 | class fuse_multiheadattention_pass_1_2 : public fuse_multiheadattention_pass_qkv |
| 706 | 757 | { |
| 707 | 758 | public: |
| @@ -2082,6 +2133,7 @@ | ||
| 2082 | 2133 | fuse_multiheadattention_pass_q_samekv d; |
| 2083 | 2134 | fuse_multiheadattention_pass_1 b1; |
| 2084 | 2135 | fuse_multiheadattention_pass_1_1 b11; |
| 2136 | + fuse_multiheadattention_pass_1_1_1 b111; | |
| 2085 | 2137 | fuse_multiheadattention_pass_1_2 b12; |
| 2086 | 2138 | fuse_multiheadattention_pass_2 c1; |
| 2087 | 2139 | fuse_multiheadattention_pass_3 d1; |
| @@ -2122,6 +2174,7 @@ | ||
| 2122 | 2174 | pnnx_graph_rewrite(graph, &d, opindex); |
| 2123 | 2175 | pnnx_graph_rewrite(graph, &b1, opindex); |
| 2124 | 2176 | pnnx_graph_rewrite(graph, &b11, opindex); |
| 2177 | + pnnx_graph_rewrite(graph, &b111, opindex); | |
| 2125 | 2178 | pnnx_graph_rewrite(graph, &b12, opindex); |
| 2126 | 2179 | pnnx_graph_rewrite(graph, &c1, opindex); |
| 2127 | 2180 | pnnx_graph_rewrite(graph, &d1, opindex); |