Elffuss BraidHub

kikocis/ncnn public

master · 108.7 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2023 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 "fuse_multiheadattention.h"
16
17#include "pass_level2.h"
18
19#include <math.h>
20#include <string.h>
21
22#include <torch/csrc/api/include/torch/version.h>
23
24namespace pnnx {
25
26static bool NearlyEqual(float a, float b, float epsilon)
27{
28 if (a == b)
29 return true;
30
31 float diff = (float)fabs(a - b);
32 if (diff <= epsilon)
33 return true;
34
35 // relative error
36 return diff < epsilon * std::max(fabs(a), fabs(b));
37}
38
39class fuse_multiheadattention_pass : public GraphRewriterPass
40{
41public:
42 const char* match_pattern_graph() const
43 {
44 return R"PNNXIR(7767517
4514 13
46pnnx.Input input 0 1 input
47nn.Linear op_0 1 1 input 1 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
48Tensor.reshape op_1 1 1 1 2 shape=(%batch,%size,3,%num_heads,%feat_per_head)
49Tensor.permute op_2 1 1 2 3 dims=(2,0,3,1,4)
50torch.unbind op_3 1 3 3 4 5 6 dim=0
51pnnx.Expression op_4 1 1 4 7 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
52Tensor.permute op_5 1 1 5 8 dims=(0,1,3,2)
53torch.matmul op_6 2 1 7 8 9
54F.softmax softmax 1 1 9 10 dim=%softmax_dim
55torch.matmul op_8 2 1 10 6 11
56Tensor.permute op_9 1 1 11 12 dims=(0,2,1,3)
57Tensor.reshape op_10 1 1 12 13 shape=(%batch,%size,%embed_dim)
58nn.Linear out_proj 1 1 13 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
59pnnx.Output output 1 0 out
60)PNNXIR";
61 }
62
63 const char* replace_pattern_graph() const
64 {
65 return R"PNNXIR(7767517
663 2
67pnnx.Input input 0 1 input
68nn.MultiheadAttention attention 1 1 input out embed_dim=%embed_dim kdim=%embed_dim vdim=%embed_dim num_heads=%num_heads batch_first=True add_zero_attn=False add_bias_kv=False
69pnnx.Output output 1 0 out
70)PNNXIR";
71 }
72
73 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
74 {
75 const int embed_dim = captured_params.at("embed_dim").i;
76 const int qkv_out_features = captured_params.at("qkv_out_features").i;
77 const int num_heads = captured_params.at("num_heads").i;
78 const int feat_per_head = captured_params.at("feat_per_head").i;
79 const float inv_sqrt_embed_dim_per_head = captured_params.at("inv_sqrt_embed_dim_per_head").f;
80 const int softmax_dim = captured_params.at("softmax_dim").i;
81
82 if (qkv_out_features != embed_dim * 3)
83 return false;
84
85 if (embed_dim != num_heads * feat_per_head)
86 return false;
87
88 if (!NearlyEqual(inv_sqrt_embed_dim_per_head, 1.f / sqrt(feat_per_head), 0.001))
89 return false;
90
91 int softmax_input_rank = (int)matched_operators.at("softmax")->inputs[0]->shape.size();
92 if (softmax_dim != -1 && softmax_dim != softmax_input_rank - 1)
93 return false;
94
95 return true;
96 }
97
98 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
99 {
100 GraphRewriterPass::write(ops, captured_params, captured_attrs);
101
102 Operator* op = ops.at("attention");
103
104 const int embed_dim = captured_params.at("embed_dim").i;
105 const bool qkvbias = captured_params.at("qkvbias").b;
106 const bool outbias = captured_params.at("outbias").b;
107 const bool bias = qkvbias || outbias;
108
109 op->params["bias"] = bias;
110
111 op->attrs["in_proj_weight"] = captured_attrs.at("op_0.weight");
112 if (bias)
113 {
114 if (qkvbias)
115 {
116 op->attrs["in_proj_bias"] = captured_attrs.at("op_0.bias");
117 }
118 else
119 {
120 // init bias as zero
121 op->attrs["in_proj_bias"] = Attribute();
122 op->attrs["in_proj_bias"].type = op->attrs["in_proj_weight"].type;
123 op->attrs["in_proj_bias"].shape = {embed_dim * 3};
124 op->attrs["in_proj_bias"].set_float32_data(std::vector<float>(embed_dim * 3, 0.f));
125 }
126 }
127
128 op->attrs["out_proj.weight"] = captured_attrs.at("out_proj.weight");
129 if (bias)
130 {
131 if (outbias)
132 {
133 op->attrs["out_proj.bias"] = captured_attrs.at("out_proj.bias");
134 }
135 else
136 {
137 // init bias as zero
138 op->attrs["out_proj.bias"] = Attribute();
139 op->attrs["out_proj.bias"].type = op->attrs["out_proj.weight"].type;
140 op->attrs["out_proj.bias"].shape = {embed_dim};
141 op->attrs["out_proj.bias"].set_float32_data(std::vector<float>(embed_dim, 0.f));
142 }
143 }
144 }
145};
146
147class fuse_multiheadattention_pass_11 : public fuse_multiheadattention_pass
148{
149public:
150 const char* match_pattern_graph() const
151 {
152 return R"PNNXIR(7767517
15318 17
154pnnx.Input input 0 1 input
155nn.Linear op_0 1 1 input 1 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
156torch.chunk op_1 1 3 1 2 3 4 chunks=3 dim=-1
157Tensor.reshape op_2 1 1 2 5 shape=(%batch,%size,%num_heads,%feat_per_head)
158Tensor.reshape op_3 1 1 3 6 shape=(%batch,%size,%num_heads,%feat_per_head)
159Tensor.reshape op_4 1 1 4 7 shape=(%batch,%size,%num_heads,%feat_per_head)
160Tensor.permute op_5 1 1 6 8 dims=(0,2,1,3)
161Tensor.permute op_6 1 1 5 9 dims=(0,2,1,3)
162torch.transpose op_7 1 1 8 10 dim0=-1 dim1=-2
163torch.matmul op_8 2 1 9 10 11
164pnnx.Expression op_9 1 1 11 12 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
165nn.Softmax softmax 1 1 12 13 dim=%softmax_dim
166Tensor.permute op_11 1 1 7 14 dims=(0,2,1,3)
167torch.matmul op_12 2 1 13 14 15
168Tensor.permute op_13 1 1 15 16 dims=(0,2,1,3)
169Tensor.reshape op_14 1 1 16 17 shape=(%batch,%size,%embed_dim)
170nn.Linear out_proj 1 1 17 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
171pnnx.Output output 1 0 out
172)PNNXIR";
173 }
174};
175
176class fuse_multiheadattention_pass_11_0 : public fuse_multiheadattention_pass
177{
178public:
179 const char* match_pattern_graph() const
180 {
181 return R"PNNXIR(7767517
18222 21
183pnnx.Input input 0 1 input
184Tensor.permute op_1 1 1 input 13 dims=(1,0,2)
185nn.Linear op_0 1 1 13 14 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
186Tensor.slice op_2 1 1 14 15 dim=-1 end=%embed_dim start=0 step=1
187Tensor.slice op_3 1 1 14 16 dim=-1 end=%embed_dim2 start=%embed_dim step=1
188Tensor.slice op_4 1 1 14 17 dim=-1 end=%qkv_out_features start=%embed_dim2 step=1
189Tensor.reshape op_5 1 1 15 18 shape=(%size,%num_heads,%feat_per_head)
190Tensor.permute op_6 1 1 18 19 dims=(1,0,2)
191Tensor.reshape op_7 1 1 16 20 shape=(%size,%num_heads,%feat_per_head)
192Tensor.reshape op_8 1 1 17 21 shape=(%size,%num_heads,%feat_per_head)
193Tensor.permute op_9 1 1 21 22 dims=(1,0,2)
194pnnx.Expression op_10 1 1 19 23 expr=div(@0,%sqrt_embed_dim_per_head)
195Tensor.permute op_11 1 1 20 24 dims=(1,2,0)
196torch.matmul op_12 2 1 23 24 25
197F.softmax softmax 1 1 25 26 dim=%softmax_dim
198torch.matmul op_14 2 1 26 22 27
199Tensor.permute op_15 1 1 27 28 dims=(1,0,2)
200Tensor.reshape op_16 1 1 28 29 shape=(%size,%embed_dim)
201nn.Linear out_proj 1 1 29 30 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
202Tensor.reshape op_24 1 1 30 31 shape=(%size,%batch,%embed_dim)
203Tensor.permute op_25 1 1 31 out dims=(1,0,2)
204pnnx.Output output 1 0 out
205)PNNXIR";
206 }
207
208 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
209 {
210 const int embed_dim = captured_params.at("embed_dim").i;
211 const int embed_dim2 = captured_params.at("embed_dim2").i;
212 const int qkv_out_features = captured_params.at("qkv_out_features").i;
213 const int num_heads = captured_params.at("num_heads").i;
214 const int feat_per_head = captured_params.at("feat_per_head").i;
215 const float sqrt_embed_dim_per_head = captured_params.at("sqrt_embed_dim_per_head").f;
216 const int softmax_dim = captured_params.at("softmax_dim").i;
217
218 if (qkv_out_features != embed_dim * 3 || embed_dim2 != embed_dim * 2)
219 return false;
220
221 if (embed_dim != num_heads * feat_per_head)
222 return false;
223
224 if (!NearlyEqual(sqrt_embed_dim_per_head, sqrt(feat_per_head), 0.001))
225 return false;
226
227 int softmax_input_rank = (int)matched_operators.at("softmax")->inputs[0]->shape.size();
228 if (softmax_dim != -1 && softmax_dim != softmax_input_rank - 1)
229 return false;
230
231 return true;
232 }
233};
234
235class fuse_multiheadattention_pass_11_1 : public fuse_multiheadattention_pass
236{
237public:
238 const char* match_pattern_graph() const
239 {
240 return R"PNNXIR(7767517
24128 27
242pnnx.Input input 0 1 input
243Tensor.permute op_1 1 1 input 9 dims=(1,0,2)
244nn.Linear op_0 1 1 9 10 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
245Tensor.reshape op_2 1 1 10 11 shape=(1,%size,%batch,3,%embed_dim)
246Tensor.permute op_3 1 1 11 12 dims=(3,1,2,0,4)
247torch.squeeze op_4 1 1 12 13 dim=3
248torch.unbind op_5 1 3 13 14 15 16 dim=0
249Tensor.reshape op_6 1 1 14 17 shape=(%size,%num_heads,%feat_per_head)
250Tensor.permute op_7 1 1 17 18 dims=(1,0,2)
251Tensor.reshape op_8 1 1 15 19 shape=(%size,%num_heads,%feat_per_head)
252Tensor.permute op_9 1 1 19 20 dims=(1,0,2)
253Tensor.reshape op_10 1 1 16 21 shape=(%size,%num_heads,%feat_per_head)
254Tensor.permute op_11 1 1 21 22 dims=(1,0,2)
255Tensor.reshape op_12 1 1 18 23 shape=(%batch,%num_heads,%size,%feat_per_head)
256Tensor.reshape op_13 1 1 20 24 shape=(%batch,%num_heads,%size,%feat_per_head)
257Tensor.reshape op_14 1 1 22 25 shape=(%batch,%num_heads,%size,%feat_per_head)
258Tensor.permute op_15 1 1 24 26 dims=(0,1,3,2)
259pnnx.Expression op_16 1 1 23 27 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
260pnnx.Expression op_17 1 1 26 28 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
261torch.matmul op_18 2 1 27 28 29
262F.softmax softmax 1 1 29 30 dim=%softmax_dim
263torch.matmul op_20 2 1 30 25 31
264Tensor.permute op_21 1 1 31 32 dims=(2,0,1,3)
265Tensor.reshape op_22 1 1 32 33 shape=(%size,%embed_dim)
266nn.Linear out_proj 1 1 33 34 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
267Tensor.reshape op_24 1 1 34 35 shape=(%size,%batch,%embed_dim)
268Tensor.permute op_25 1 1 35 out dims=(1,0,2)
269pnnx.Output output 1 0 out
270)PNNXIR";
271 }
272
273 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
274 {
275 const int embed_dim = captured_params.at("embed_dim").i;
276 const int qkv_out_features = captured_params.at("qkv_out_features").i;
277 const int num_heads = captured_params.at("num_heads").i;
278 const int feat_per_head = captured_params.at("feat_per_head").i;
279 const float inv_sqrt_embed_dim_per_head = captured_params.at("inv_sqrt_embed_dim_per_head").f;
280 const int softmax_dim = captured_params.at("softmax_dim").i;
281
282 if (qkv_out_features != embed_dim * 3)
283 return false;
284
285 if (embed_dim != num_heads * feat_per_head)
286 return false;
287
288 if (!NearlyEqual(inv_sqrt_embed_dim_per_head, sqrt(1.f / sqrt(feat_per_head)), 0.001))
289 return false;
290
291 int softmax_input_rank = (int)matched_operators.at("softmax")->inputs[0]->shape.size();
292 if (softmax_dim != -1 && softmax_dim != softmax_input_rank - 1)
293 return false;
294
295 return true;
296 }
297};
298
299class fuse_multiheadattention_pass_sameqkv : public GraphRewriterPass
300{
301public:
302 const char* match_pattern_graph() const
303 {
304 return R"PNNXIR(7767517
30523 22
306pnnx.Input input 0 1 input
307nn.Linear op_0 1 1 input 31 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
308nn.Linear op_1 1 1 input 32 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight
309nn.Linear op_2 1 1 input 33 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight
310pnnx.Expression op_3 1 1 32 34 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
311Tensor.reshape op_4 1 1 31 35 shape=(%batch,%size,%num_heads,%feat_per_head)
312Tensor.reshape op_5 1 1 34 36 shape=(%batch,%size,%num_heads,%feat_per_head)
313Tensor.reshape op_6 1 1 33 37 shape=(%batch,%size,%num_heads,%feat_per_head)
314Tensor.permute op_7 1 1 36 38 dims=(0,2,1,3)
315Tensor.reshape op_8 1 1 38 39 shape=(%num_heads,%size,%feat_per_head)
316Tensor.permute op_9 1 1 35 40 dims=(0,2,1,3)
317Tensor.reshape op_10 1 1 40 41 shape=(%num_heads,%size,%feat_per_head)
318Tensor.permute op_11 1 1 39 42 dims=(0,2,1)
319torch.matmul op_12 2 1 41 42 43
320F.softmax softmax 1 1 43 44 dim=%softmax_dim
321Tensor.permute op_14 1 1 37 45 dims=(0,2,1,3)
322Tensor.reshape op_15 1 1 45 46 shape=(%num_heads,%size,%feat_per_head)
323torch.matmul op_16 2 1 44 46 47
324Tensor.reshape op_17 1 1 47 48 shape=(%batch,%num_heads,%size,%feat_per_head)
325Tensor.permute op_18 1 1 48 49 dims=(0,2,1,3)
326Tensor.reshape op_19 1 1 49 50 shape=(%batch,%size,%embed_dim)
327nn.Linear out_proj 1 1 50 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
328pnnx.Output output 1 0 out
329)PNNXIR";
330 }
331
332 const char* replace_pattern_graph() const
333 {
334 return R"PNNXIR(7767517
3353 2
336pnnx.Input input 0 1 input
337nn.MultiheadAttention attention 1 1 input out embed_dim=%embed_dim kdim=%embed_dim vdim=%embed_dim num_heads=%num_heads batch_first=True add_zero_attn=False add_bias_kv=False
338pnnx.Output output 1 0 out
339)PNNXIR";
340 }
341
342 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
343 {
344 const int embed_dim = captured_params.at("embed_dim").i;
345 const int num_heads = captured_params.at("num_heads").i;
346 const int feat_per_head = captured_params.at("feat_per_head").i;
347 const float inv_sqrt_embed_dim_per_head = captured_params.at("inv_sqrt_embed_dim_per_head").f;
348 const int softmax_dim = captured_params.at("softmax_dim").i;
349
350 if (embed_dim != num_heads * feat_per_head)
351 return false;
352
353 if (!NearlyEqual(inv_sqrt_embed_dim_per_head, 1.f / sqrt(feat_per_head), 0.001))
354 return false;
355
356 int softmax_input_rank = (int)matched_operators.at("softmax")->inputs[0]->shape.size();
357 if (softmax_dim != -1 && softmax_dim != softmax_input_rank - 1)
358 return false;
359
360 return true;
361 }
362
363 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
364 {
365 GraphRewriterPass::write(ops, captured_params, captured_attrs);
366
367 Operator* op = ops.at("attention");
368
369 const int embed_dim = captured_params.at("embed_dim").i;
370 const bool qbias = captured_params.at("qbias").b;
371 const bool kbias = captured_params.at("kbias").b;
372 const bool vbias = captured_params.at("vbias").b;
373 const bool outbias = captured_params.at("outbias").b;
374 const bool bias = qbias || kbias || vbias || outbias;
375
376 op->params["bias"] = bias;
377
378 op->attrs["in_proj_weight"] = captured_attrs.at("op_0.weight") + captured_attrs.at("op_1.weight") + captured_attrs.at("op_2.weight");
379
380 op->attrs["out_proj.weight"] = captured_attrs.at("out_proj.weight");
381
382 if (bias)
383 {
384 op->attrs["in_proj_bias"] = Attribute();
385 op->attrs["in_proj_bias"].type = op->attrs["in_proj_weight"].type;
386 op->attrs["in_proj_bias"].shape = {embed_dim * 3};
387 // combine qkv bias
388 std::vector<float> in_proj_bias(embed_dim * 3);
389 {
390 float* in_proj_bias_ptr = (float*)in_proj_bias.data();
391 if (qbias)
392 {
393 auto qb = captured_attrs.at("op_0.bias").get_float32_data();
394 memcpy(in_proj_bias_ptr, (const void*)qb.data(), embed_dim * sizeof(float));
395 }
396 else
397 {
398 memset(in_proj_bias_ptr, 0, embed_dim * sizeof(float));
399 }
400 in_proj_bias_ptr += embed_dim;
401 if (kbias)
402 {
403 auto kb = captured_attrs.at("op_1.bias").get_float32_data();
404 memcpy(in_proj_bias_ptr, (const void*)kb.data(), embed_dim * sizeof(float));
405 }
406 else
407 {
408 memset(in_proj_bias_ptr, 0, embed_dim * sizeof(float));
409 }
410 in_proj_bias_ptr += embed_dim;
411 if (vbias)
412 {
413 auto vb = captured_attrs.at("op_2.bias").get_float32_data();
414 memcpy(in_proj_bias_ptr, (const void*)vb.data(), embed_dim * sizeof(float));
415 }
416 else
417 {
418 memset(in_proj_bias_ptr, 0, embed_dim * sizeof(float));
419 }
420 }
421 op->attrs["in_proj_bias"].set_float32_data(in_proj_bias);
422
423 if (outbias)
424 {
425 op->attrs["out_proj.bias"] = captured_attrs.at("out_proj.bias");
426 }
427 else
428 {
429 // init bias as zero
430 op->attrs["out_proj.bias"] = Attribute();
431 op->attrs["out_proj.bias"].type = op->attrs["out_proj.weight"].type;
432 op->attrs["out_proj.bias"].shape = {embed_dim};
433 op->attrs["out_proj.bias"].set_float32_data(std::vector<float>(embed_dim, 0.f));
434 }
435 }
436 }
437};
438
439class fuse_multiheadattention_pass_qkv : public fuse_multiheadattention_pass_sameqkv
440{
441public:
442 const char* match_pattern_graph() const
443 {
444 return R"PNNXIR(7767517
44525 24
446pnnx.Input input_q 0 1 query
447pnnx.Input input_k 0 1 key
448pnnx.Input input_v 0 1 value
449nn.Linear op_0 1 1 query 32 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
450nn.Linear op_1 1 1 key 33 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight
451nn.Linear op_2 1 1 value 34 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight
452pnnx.Expression op_3 1 1 33 35 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
453Tensor.reshape op_4 1 1 32 36 shape=(%batch,%qsize,%num_heads,%feat_per_head)
454Tensor.reshape op_5 1 1 35 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
455Tensor.reshape op_6 1 1 34 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
456Tensor.permute op_7 1 1 37 39 dims=(0,2,1,3)
457Tensor.reshape op_8 1 1 39 40 shape=(%num_heads,%kvsize,%feat_per_head)
458Tensor.permute op_9 1 1 36 41 dims=(0,2,1,3)
459Tensor.reshape op_10 1 1 41 42 shape=(%num_heads,%qsize,%feat_per_head)
460Tensor.permute op_11 1 1 40 43 dims=(0,2,1)
461torch.matmul op_12 2 1 42 43 44
462F.softmax softmax 1 1 44 45 dim=%softmax_dim
463Tensor.permute op_14 1 1 38 46 dims=(0,2,1,3)
464Tensor.reshape op_15 1 1 46 47 shape=(%num_heads,%kvsize,%feat_per_head)
465torch.matmul op_16 2 1 45 47 48
466Tensor.reshape op_17 1 1 48 49 shape=(%batch,%num_heads,%qsize,%feat_per_head)
467Tensor.permute op_18 1 1 49 50 dims=(0,2,1,3)
468Tensor.reshape op_19 1 1 50 51 shape=(%batch,%qsize,%embed_dim)
469nn.Linear out_proj 1 1 51 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
470pnnx.Output output 1 0 out
471)PNNXIR";
472 }
473
474 const char* replace_pattern_graph() const
475 {
476 return R"PNNXIR(7767517
4775 4
478pnnx.Input input_0 0 1 query
479pnnx.Input input_1 0 1 key
480pnnx.Input input_2 0 1 value
481nn.MultiheadAttention attention 3 1 query key value out embed_dim=%embed_dim kdim=%kdim vdim=%vdim num_heads=%num_heads batch_first=True add_zero_attn=False add_bias_kv=False
482pnnx.Output output 1 0 out
483)PNNXIR";
484 }
485
486 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
487 {
488 GraphRewriterPass::write(ops, captured_params, captured_attrs);
489
490 Operator* op = ops.at("attention");
491
492 const int embed_dim = captured_params.at("embed_dim").i;
493 const int kdim = captured_params.at("kdim").i;
494 const int vdim = captured_params.at("vdim").i;
495 const bool qbias = captured_params.at("qbias").b;
496 const bool kbias = captured_params.at("kbias").b;
497 const bool vbias = captured_params.at("vbias").b;
498 const bool outbias = captured_params.at("outbias").b;
499 const bool bias = qbias || kbias || vbias || outbias;
500 const bool same_qkv_dim = (embed_dim == kdim && embed_dim == vdim);
501
502 op->params["bias"] = bias;
503
504 if (same_qkv_dim)
505 {
506 // same qkv dim, merge into in_proj_weight
507 op->attrs["in_proj_weight"] = captured_attrs.at("op_0.weight") + captured_attrs.at("op_1.weight") + captured_attrs.at("op_2.weight");
508 }
509 else
510 {
511 op->attrs["q_proj_weight"] = captured_attrs.at("op_0.weight");
512 op->attrs["k_proj_weight"] = captured_attrs.at("op_1.weight");
513 op->attrs["v_proj_weight"] = captured_attrs.at("op_2.weight");
514 }
515
516 op->attrs["out_proj.weight"] = captured_attrs.at("out_proj.weight");
517
518 if (bias)
519 {
520 op->attrs["in_proj_bias"] = Attribute();
521 op->attrs["in_proj_bias"].type = same_qkv_dim ? op->attrs["in_proj_weight"].type : op->attrs["q_proj_weight"].type;
522 op->attrs["in_proj_bias"].shape = {embed_dim * 3};
523 // combine qkv bias
524 std::vector<float> in_proj_bias(embed_dim * 3);
525 {
526 float* in_proj_bias_ptr = (float*)in_proj_bias.data();
527 if (qbias)
528 {
529 auto qb = captured_attrs.at("op_0.bias").get_float32_data();
530 memcpy(in_proj_bias_ptr, (const void*)qb.data(), embed_dim * sizeof(float));
531 }
532 else
533 {
534 memset(in_proj_bias_ptr, 0, embed_dim * sizeof(float));
535 }
536 in_proj_bias_ptr += embed_dim;
537 if (kbias)
538 {
539 auto kb = captured_attrs.at("op_1.bias").get_float32_data();
540 memcpy(in_proj_bias_ptr, (const void*)kb.data(), embed_dim * sizeof(float));
541 }
542 else
543 {
544 memset(in_proj_bias_ptr, 0, embed_dim * sizeof(float));
545 }
546 in_proj_bias_ptr += embed_dim;
547 if (vbias)
548 {
549 auto vb = captured_attrs.at("op_2.bias").get_float32_data();
550 memcpy(in_proj_bias_ptr, (const void*)vb.data(), embed_dim * sizeof(float));
551 }
552 else
553 {
554 memset(in_proj_bias_ptr, 0, embed_dim * sizeof(float));
555 }
556 }
557 op->attrs["in_proj_bias"].set_float32_data(in_proj_bias);
558
559 if (outbias)
560 {
561 op->attrs["out_proj.bias"] = captured_attrs.at("out_proj.bias");
562 }
563 else
564 {
565 // init bias as zero
566 op->attrs["out_proj.bias"] = Attribute();
567 op->attrs["out_proj.bias"].type = op->attrs["out_proj.weight"].type;
568 op->attrs["out_proj.bias"].shape = {embed_dim};
569 op->attrs["out_proj.bias"].set_float32_data(std::vector<float>(embed_dim, 0.f));
570 }
571 }
572 }
573};
574
575class fuse_multiheadattention_pass_q_samekv : public fuse_multiheadattention_pass_qkv
576{
577public:
578 const char* match_pattern_graph() const
579 {
580 return R"PNNXIR(7767517
58124 23
582pnnx.Input input_q 0 1 query
583pnnx.Input input_kv 0 1 kv
584nn.Linear op_0 1 1 query 32 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
585nn.Linear op_1 1 1 kv 33 bias=%kbias in_features=%kvdim out_features=%embed_dim @bias @weight
586nn.Linear op_2 1 1 kv 34 bias=%vbias in_features=%kvdim out_features=%embed_dim @bias @weight
587pnnx.Expression op_3 1 1 33 35 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
588Tensor.reshape op_4 1 1 32 36 shape=(%batch,%qsize,%num_heads,%feat_per_head)
589Tensor.reshape op_5 1 1 35 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
590Tensor.reshape op_6 1 1 34 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
591Tensor.permute op_7 1 1 37 39 dims=(0,2,1,3)
592Tensor.reshape op_8 1 1 39 40 shape=(%num_heads,%kvsize,%feat_per_head)
593Tensor.permute op_9 1 1 36 41 dims=(0,2,1,3)
594Tensor.reshape op_10 1 1 41 42 shape=(%num_heads,%qsize,%feat_per_head)
595Tensor.permute op_11 1 1 40 43 dims=(0,2,1)
596torch.matmul op_12 2 1 42 43 44
597F.softmax softmax 1 1 44 45 dim=%softmax_dim
598Tensor.permute op_14 1 1 38 46 dims=(0,2,1,3)
599Tensor.reshape op_15 1 1 46 47 shape=(%num_heads,%kvsize,%feat_per_head)
600torch.matmul op_16 2 1 45 47 48
601Tensor.reshape op_17 1 1 48 49 shape=(%batch,%num_heads,%qsize,%feat_per_head)
602Tensor.permute op_18 1 1 49 50 dims=(0,2,1,3)
603Tensor.reshape op_19 1 1 50 51 shape=(%batch,%qsize,%embed_dim)
604nn.Linear out_proj 1 1 51 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
605pnnx.Output output 1 0 out
606)PNNXIR";
607 }
608
609 const char* replace_pattern_graph() const
610 {
611 return R"PNNXIR(7767517
6124 3
613pnnx.Input input_0 0 1 query
614pnnx.Input input_1 0 1 kv
615nn.MultiheadAttention attention 2 1 query kv out embed_dim=%embed_dim kdim=%kvdim vdim=%kvdim num_heads=%num_heads batch_first=True add_zero_attn=False add_bias_kv=False
616pnnx.Output output 1 0 out
617)PNNXIR";
618 }
619};
620
621class fuse_multiheadattention_pass_1 : public fuse_multiheadattention_pass_sameqkv
622{
623public:
624 const char* match_pattern_graph() const
625 {
626 return R"PNNXIR(7767517
62722 21
628pnnx.Input input 0 1 input
629nn.Linear op_0 1 1 input 31 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
630nn.Linear op_1 1 1 input 32 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight
631nn.Linear op_2 1 1 input 33 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight
632Tensor.reshape op_3 1 1 31 35 shape=(%batch,%size,%num_heads,%feat_per_head)
633Tensor.reshape op_4 1 1 32 36 shape=(%batch,%size,%num_heads,%feat_per_head)
634Tensor.reshape op_5 1 1 33 37 shape=(%batch,%size,%num_heads,%feat_per_head)
635Tensor.permute op_6 1 1 36 38 dims=(0,2,1,3)
636Tensor.reshape op_7 1 1 38 39 shape=(%num_heads,%size,%feat_per_head)
637Tensor.permute op_8 1 1 35 40 dims=(0,2,1,3)
638Tensor.reshape op_9 1 1 40 41 shape=(%num_heads,%size,%feat_per_head)
639torch.einsum op_10 2 1 41 39 42 equation=ijl,ikl->ijk
640pnnx.Expression op_11 1 1 42 43 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
641F.softmax softmax 1 1 43 44 dim=%softmax_dim
642Tensor.permute op_13 1 1 37 45 dims=(0,2,1,3)
643Tensor.reshape op_14 1 1 45 46 shape=(%num_heads,%size,%feat_per_head)
644torch.einsum op_15 2 1 44 46 47 equation=ijl,ilk->ijk
645Tensor.reshape op_16 1 1 47 48 shape=(%batch,%num_heads,%size,%feat_per_head)
646Tensor.permute op_17 1 1 48 49 dims=(0,2,1,3)
647Tensor.reshape op_18 1 1 49 50 shape=(%batch,%size,%embed_dim)
648nn.Linear out_proj 1 1 50 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
649pnnx.Output output 1 0 out
650)PNNXIR";
651 }
652};
653
654class fuse_multiheadattention_pass_1_1 : public fuse_multiheadattention_pass_sameqkv
655{
656public:
657 const char* match_pattern_graph() const
658 {
659 return R"PNNXIR(7767517
66019 18
661pnnx.Input input 0 1 input
662nn.Linear op_0 1 1 input 47 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
663nn.Linear op_1 1 1 input 48 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight
664nn.Linear op_2 1 1 input 49 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight
665Tensor.reshape op_3 1 1 47 50 shape=(%batch,%size,%num_heads,%feat_per_head)
666Tensor.reshape op_4 1 1 48 51 shape=(%batch,%size,%num_heads,%feat_per_head)
667Tensor.reshape op_5 1 1 49 52 shape=(%batch,%size,%num_heads,%feat_per_head)
668torch.transpose op_6 1 1 51 53 dim0=1 dim1=2
669Tensor.permute op_7 1 1 53 54 dims=(0,1,3,2)
670torch.transpose op_8 1 1 50 55 dim0=1 dim1=2
671torch.matmul op_9 2 1 55 54 56
672pnnx.Expression op_10 1 1 56 57 expr=div(@0,%sqrt_feat_per_head)
673F.softmax softmax 1 1 57 58 dim=%softmax_dim
674torch.transpose op_12 1 1 52 59 dim0=1 dim1=2
675torch.matmul op_13 2 1 58 59 60
676torch.transpose op_14 1 1 60 61 dim0=1 dim1=2
677Tensor.reshape op_15 1 1 61 62 shape=(%batch,%size,%embed_dim)
678nn.Linear out_proj 1 1 62 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
679pnnx.Output output 1 0 out
680)PNNXIR";
681 }
682
683 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
684 {
685 const int embed_dim = captured_params.at("embed_dim").i;
686 const int num_heads = captured_params.at("num_heads").i;
687 const int feat_per_head = captured_params.at("feat_per_head").i;
688 const float sqrt_feat_per_head = captured_params.at("sqrt_feat_per_head").f;
689 const int softmax_dim = captured_params.at("softmax_dim").i;
690
691 if (embed_dim != num_heads * feat_per_head)
692 return false;
693
694 if (!NearlyEqual(sqrt_feat_per_head, sqrt(feat_per_head), 0.001))
695 return false;
696
697 int softmax_input_rank = (int)matched_operators.at("softmax")->inputs[0]->shape.size();
698 if (softmax_dim != -1 && softmax_dim != softmax_input_rank - 1)
699 return false;
700
701 return true;
702 }
703};
704
705class fuse_multiheadattention_pass_1_1_1 : public fuse_multiheadattention_pass_sameqkv
706{
707public:
708 const char* match_pattern_graph() const
709 {
710 return R"PNNXIR(7767517
71119 18
712pnnx.Input input 0 1 input
713nn.Linear op_0 1 1 input 256 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
714nn.Linear op_1 1 1 input 257 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight
715nn.Linear op_2 1 1 input 260 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight
716Tensor.view op_3 1 1 256 263 shape=(%batch,%size,%num_heads,%feat_per_head)
717Tensor.view op_4 1 1 257 258 shape=(%batch,%size,%num_heads,%feat_per_head)
718Tensor.view op_5 1 1 260 261 shape=(%batch,%size,%num_heads,%feat_per_head)
719Tensor.permute op_6 1 1 263 264 dims=(0,2,1,3)
720Tensor.permute op_7 1 1 258 259 dims=(0,2,1,3)
721Tensor.permute op_8 1 1 261 262 dims=(0,2,1,3)
722torch.transpose op_9 1 1 259 265 dim0=-1 dim1=-2
723torch.matmul op_10 2 1 264 265 266
724pnnx.Expression op_11 1 1 266 267 expr=div(@0,%sqrt_feat_per_head)
725F.softmax softmax 1 1 267 268 dim=%softmax_dim
726torch.matmul op_13 2 1 268 262 269
727Tensor.permute op_14 1 1 269 270 dims=(0,2,1,3)
728Tensor.reshape op_15 1 1 270 271 shape=(%batch,%size,%embed_dim)
729nn.Linear out_proj 1 1 271 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
730pnnx.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
756class fuse_multiheadattention_pass_1_2 : public fuse_multiheadattention_pass_qkv
757{
758public:
759 const char* match_pattern_graph() const
760 {
761 return R"PNNXIR(7767517
76221 20
763pnnx.Input input_0 0 1 query #query=(%batch,%qsize,%embed_dim)f32
764pnnx.Input input_1 0 1 key #key=(%batch,%kvsize,%kdim)f32
765pnnx.Input input_2 0 1 value #value=(%batch,%kvsize,%kdim)f32
766nn.Linear op_0 1 1 query 47 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
767nn.Linear op_1 1 1 key 48 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight
768nn.Linear op_2 1 1 value 49 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight
769Tensor.reshape op_3 1 1 47 50 shape=(%batch,%qsize,%num_heads,%feat_per_head)
770Tensor.reshape op_4 1 1 48 51 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
771Tensor.reshape op_5 1 1 49 52 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
772torch.transpose op_6 1 1 51 53 dim0=1 dim1=2
773Tensor.permute op_7 1 1 53 54 dims=(0,1,3,2)
774torch.transpose op_8 1 1 50 55 dim0=1 dim1=2
775torch.matmul op_9 2 1 55 54 56
776pnnx.Expression op_10 1 1 56 57 expr=div(@0,%sqrt_feat_per_head)
777F.softmax softmax 1 1 57 58 dim=%softmax_dim
778torch.transpose op_12 1 1 52 59 dim0=1 dim1=2
779torch.matmul op_13 2 1 58 59 60
780torch.transpose op_14 1 1 60 61 dim0=1 dim1=2
781Tensor.reshape op_15 1 1 61 62 shape=(%batch,%qsize,%embed_dim)
782nn.Linear out_proj 1 1 62 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
783pnnx.Output output 1 0 out
784)PNNXIR";
785 }
786
787 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
788 {
789 const int embed_dim = captured_params.at("embed_dim").i;
790 const int num_heads = captured_params.at("num_heads").i;
791 const int feat_per_head = captured_params.at("feat_per_head").i;
792 const float sqrt_feat_per_head = captured_params.at("sqrt_feat_per_head").f;
793 const int softmax_dim = captured_params.at("softmax_dim").i;
794
795 if (embed_dim != num_heads * feat_per_head)
796 return false;
797
798 if (!NearlyEqual(sqrt_feat_per_head, sqrt(feat_per_head), 0.001))
799 return false;
800
801 int softmax_input_rank = (int)matched_operators.at("softmax")->inputs[0]->shape.size();
802 if (softmax_dim != -1 && softmax_dim != softmax_input_rank - 1)
803 return false;
804
805 return true;
806 }
807};
808
809class fuse_multiheadattention_pass_2 : public fuse_multiheadattention_pass_qkv
810{
811public:
812 const char* match_pattern_graph() const
813 {
814 return R"PNNXIR(7767517
81524 23
816pnnx.Input input_q 0 1 query
817pnnx.Input input_k 0 1 key
818pnnx.Input input_v 0 1 value
819nn.Linear op_0 1 1 query 32 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
820nn.Linear op_1 1 1 key 33 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight
821nn.Linear op_2 1 1 value 34 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight
822Tensor.reshape op_3 1 1 32 36 shape=(%batch,%qsize,%num_heads,%feat_per_head)
823Tensor.reshape op_4 1 1 33 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
824Tensor.reshape op_5 1 1 34 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
825Tensor.permute op_6 1 1 37 39 dims=(0,2,1,3)
826Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%kvsize,%feat_per_head)
827Tensor.permute op_8 1 1 36 41 dims=(0,2,1,3)
828Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%qsize,%feat_per_head)
829torch.einsum op_10 2 1 42 40 43 equation=ijl,ikl->ijk
830pnnx.Expression op_11 1 1 43 44 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
831F.softmax softmax 1 1 44 45 dim=%softmax_dim
832Tensor.permute op_13 1 1 38 46 dims=(0,2,1,3)
833Tensor.reshape op_14 1 1 46 47 shape=(%num_heads,%kvsize,%feat_per_head)
834torch.einsum op_15 2 1 45 47 48 equation=ijl,ilk->ijk
835Tensor.reshape op_16 1 1 48 49 shape=(%batch,%num_heads,%qsize,%feat_per_head)
836Tensor.permute op_17 1 1 49 50 dims=(0,2,1,3)
837Tensor.reshape op_18 1 1 50 51 shape=(%batch,%qsize,%embed_dim)
838nn.Linear out_proj 1 1 51 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
839pnnx.Output output 1 0 out
840)PNNXIR";
841 }
842};
843
844class fuse_multiheadattention_pass_3 : public fuse_multiheadattention_pass_q_samekv
845{
846public:
847 const char* match_pattern_graph() const
848 {
849 return R"PNNXIR(7767517
85023 22
851pnnx.Input input_q 0 1 query
852pnnx.Input input_kv 0 1 kv
853nn.Linear op_0 1 1 query 32 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
854nn.Linear op_1 1 1 kv 33 bias=%kbias in_features=%kvdim out_features=%embed_dim @bias @weight
855nn.Linear op_2 1 1 kv 34 bias=%vbias in_features=%kvdim out_features=%embed_dim @bias @weight
856Tensor.reshape op_3 1 1 32 36 shape=(%batch,%qsize,%num_heads,%feat_per_head)
857Tensor.reshape op_4 1 1 33 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
858Tensor.reshape op_5 1 1 34 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
859Tensor.permute op_6 1 1 37 39 dims=(0,2,1,3)
860Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%kvsize,%feat_per_head)
861Tensor.permute op_8 1 1 36 41 dims=(0,2,1,3)
862Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%qsize,%feat_per_head)
863torch.einsum op_10 2 1 42 40 43 equation=ijl,ikl->ijk
864pnnx.Expression op_11 1 1 43 44 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
865F.softmax softmax 1 1 44 45 dim=%softmax_dim
866Tensor.permute op_13 1 1 38 46 dims=(0,2,1,3)
867Tensor.reshape op_14 1 1 46 47 shape=(%num_heads,%kvsize,%feat_per_head)
868torch.einsum op_15 2 1 45 47 48 equation=ijl,ilk->ijk
869Tensor.reshape op_16 1 1 48 49 shape=(%batch,%num_heads,%qsize,%feat_per_head)
870Tensor.permute op_17 1 1 49 50 dims=(0,2,1,3)
871Tensor.reshape op_18 1 1 50 51 shape=(%batch,%qsize,%embed_dim)
872nn.Linear out_proj 1 1 51 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
873pnnx.Output output 1 0 out
874)PNNXIR";
875 }
876};
877
878class fuse_multiheadattention_pass_5 : public fuse_multiheadattention_pass_sameqkv
879{
880public:
881 const char* match_pattern_graph() const
882 {
883 return R"PNNXIR(7767517
88423 22
885pnnx.Input input 0 1 input
886nn.Linear op_0 1 1 input 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
887nn.Linear op_1 1 1 input 34 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight
888nn.Linear op_2 1 1 input 35 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight
889Tensor.reshape op_3 1 1 33 36 shape=(%batch,%size,%num_heads,%feat_per_head)
890Tensor.reshape op_4 1 1 34 37 shape=(%batch,%size,%num_heads,%feat_per_head)
891Tensor.reshape op_5 1 1 35 38 shape=(%batch,%size,%num_heads,%feat_per_head)
892Tensor.permute op_6 1 1 36 39 dims=(0,2,1,3)
893Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%size,%feat_per_head)
894Tensor.permute op_8 1 1 37 41 dims=(0,2,1,3)
895Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%size,%feat_per_head)
896pnnx.Attribute op_10 0 1 43 @data
897torch.transpose op_11 1 1 42 44 dim0=-1 dim1=-2
898torch.baddbmm op_12 3 1 43 40 44 45 alpha=%inv_sqrt_embed_dim_per_head beta=0
899F.softmax softmax 1 1 45 46 dim=%softmax_dim
900Tensor.permute op_14 1 1 38 47 dims=(0,2,1,3)
901Tensor.reshape op_15 1 1 47 48 shape=(%num_heads,%size,%feat_per_head)
902torch.bmm op_16 2 1 46 48 49
903Tensor.reshape op_17 1 1 49 50 shape=(%batch,%num_heads,%size,%feat_per_head)
904Tensor.permute op_18 1 1 50 51 dims=(0,2,1,3)
905Tensor.reshape op_19 1 1 51 52 shape=(%batch,%size,%embed_dim)
906nn.Linear out_proj 1 1 52 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
907pnnx.Output output 1 0 out
908)PNNXIR";
909 }
910
911 // TODO match data zero
912};
913
914class fuse_multiheadattention_pass_6 : public fuse_multiheadattention_pass_sameqkv
915{
916public:
917 const char* match_pattern_graph() const
918 {
919 return R"PNNXIR(7767517
92024 23
921pnnx.Input input 0 1 input
922nn.Linear op_0 1 1 input 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
923nn.Linear op_1 1 1 input 34 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight
924nn.Linear op_2 1 1 input 35 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight
925Tensor.reshape op_3 1 1 33 36 shape=(%batch,%size,%num_heads,%feat_per_head)
926Tensor.reshape op_4 1 1 34 37 shape=(%batch,%size,%num_heads,%feat_per_head)
927Tensor.reshape op_5 1 1 35 38 shape=(%batch,%size,%num_heads,%feat_per_head)
928Tensor.permute op_6 1 1 36 39 dims=(0,2,1,3)
929Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%size,%feat_per_head)
930Tensor.permute op_8 1 1 37 41 dims=(0,2,1,3)
931Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%size,%feat_per_head)
932pnnx.Expression op_10 2 1 40 42 43 expr=%expr_zero_shape
933torch.empty op_11 1 1 43 zeros
934torch.transpose op_12 1 1 42 44 dim0=-1 dim1=-2
935torch.baddbmm op_13 3 1 zeros 40 44 45 alpha=%inv_sqrt_embed_dim_per_head beta=0
936F.softmax softmax 1 1 45 46 dim=%softmax_dim
937Tensor.permute op_15 1 1 38 47 dims=(0,2,1,3)
938Tensor.reshape op_16 1 1 47 48 shape=(%num_heads,%size,%feat_per_head)
939torch.bmm op_17 2 1 46 48 49
940Tensor.reshape op_18 1 1 49 50 shape=(%batch,%num_heads,%size,%feat_per_head)
941Tensor.permute op_19 1 1 50 51 dims=(0,2,1,3)
942Tensor.reshape op_20 1 1 51 52 shape=(%batch,%size,%embed_dim)
943nn.Linear out_proj 1 1 52 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
944pnnx.Output output 1 0 out
945)PNNXIR";
946 }
947
948 // TODO match expr_zero_shape
949};
950
951class fuse_multiheadattention_pass_7 : public fuse_multiheadattention_pass_qkv
952{
953public:
954 const char* match_pattern_graph() const
955 {
956 return R"PNNXIR(7767517
95725 24
958pnnx.Input input_q 0 1 query
959pnnx.Input input_k 0 1 key
960pnnx.Input input_v 0 1 value
961nn.Linear op_0 1 1 query 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
962nn.Linear op_1 1 1 key 34 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight
963nn.Linear op_2 1 1 value 35 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight
964Tensor.reshape op_3 1 1 33 36 shape=(%batch,%qsize,%num_heads,%feat_per_head)
965Tensor.reshape op_4 1 1 34 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
966Tensor.reshape op_5 1 1 35 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
967Tensor.permute op_6 1 1 36 39 dims=(0,2,1,3)
968Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%qsize,%feat_per_head)
969Tensor.permute op_8 1 1 37 41 dims=(0,2,1,3)
970Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%kvsize,%feat_per_head)
971pnnx.Attribute op_10 0 1 43 @data
972torch.transpose op_11 1 1 42 44 dim0=-1 dim1=-2
973torch.baddbmm op_12 3 1 43 40 44 45 alpha=%inv_sqrt_embed_dim_per_head beta=0
974F.softmax softmax 1 1 45 46 dim=%softmax_dim
975Tensor.permute op_14 1 1 38 47 dims=(0,2,1,3)
976Tensor.reshape op_15 1 1 47 48 shape=(%num_heads,%kvsize,%feat_per_head)
977torch.bmm op_16 2 1 46 48 49
978Tensor.reshape op_17 1 1 49 50 shape=(%batch,%num_heads,%qsize,%feat_per_head)
979Tensor.permute op_18 1 1 50 51 dims=(0,2,1,3)
980Tensor.reshape op_19 1 1 51 52 shape=(%batch,%qsize,%embed_dim)
981nn.Linear out_proj 1 1 52 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
982pnnx.Output output 1 0 out
983)PNNXIR";
984 }
985
986 // TODO match data zero
987};
988
989class fuse_multiheadattention_pass_8 : public fuse_multiheadattention_pass_q_samekv
990{
991public:
992 const char* match_pattern_graph() const
993 {
994 return R"PNNXIR(7767517
99524 23
996pnnx.Input input_q 0 1 query
997pnnx.Input input_kv 0 1 kv
998nn.Linear op_0 1 1 query 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
999nn.Linear op_1 1 1 kv 34 bias=%kbias in_features=%kvdim out_features=%embed_dim @bias @weight
1000nn.Linear op_2 1 1 kv 35 bias=%vbias in_features=%kvdim out_features=%embed_dim @bias @weight
1001Tensor.reshape op_3 1 1 33 36 shape=(%batch,%qsize,%num_heads,%feat_per_head)
1002Tensor.reshape op_4 1 1 34 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
1003Tensor.reshape op_5 1 1 35 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
1004Tensor.permute op_6 1 1 36 39 dims=(0,2,1,3)
1005Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%qsize,%feat_per_head)
1006Tensor.permute op_8 1 1 37 41 dims=(0,2,1,3)
1007Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%kvsize,%feat_per_head)
1008pnnx.Attribute op_10 0 1 43 @data
1009torch.transpose op_11 1 1 42 44 dim0=-1 dim1=-2
1010torch.baddbmm op_12 3 1 43 40 44 45 alpha=%inv_sqrt_embed_dim_per_head beta=0
1011F.softmax softmax 1 1 45 46 dim=%softmax_dim
1012Tensor.permute op_14 1 1 38 47 dims=(0,2,1,3)
1013Tensor.reshape op_15 1 1 47 48 shape=(%num_heads,%kvsize,%feat_per_head)
1014torch.bmm op_16 2 1 46 48 49
1015Tensor.reshape op_17 1 1 49 50 shape=(%batch,%num_heads,%qsize,%feat_per_head)
1016Tensor.permute op_18 1 1 50 51 dims=(0,2,1,3)
1017Tensor.reshape op_19 1 1 51 52 shape=(%batch,%qsize,%embed_dim)
1018nn.Linear out_proj 1 1 52 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1019pnnx.Output output 1 0 out
1020)PNNXIR";
1021 }
1022
1023 // TODO match data zero
1024};
1025
1026class fuse_multiheadattention_pass_9 : public fuse_multiheadattention_pass_qkv
1027{
1028public:
1029 const char* match_pattern_graph() const
1030 {
1031 return R"PNNXIR(7767517
103226 25
1033pnnx.Input input_q 0 1 query
1034pnnx.Input input_k 0 1 key
1035pnnx.Input input_v 0 1 value
1036nn.Linear op_0 1 1 query 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1037nn.Linear op_1 1 1 key 34 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight
1038nn.Linear op_2 1 1 value 35 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight
1039Tensor.reshape op_3 1 1 33 36 shape=(%batch,%qsize,%num_heads,%feat_per_head)
1040Tensor.reshape op_4 1 1 34 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
1041Tensor.reshape op_5 1 1 35 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
1042Tensor.permute op_6 1 1 36 39 dims=(0,2,1,3)
1043Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%qsize,%feat_per_head)
1044Tensor.permute op_8 1 1 37 41 dims=(0,2,1,3)
1045Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%kvsize,%feat_per_head)
1046pnnx.Expression op_10 1 1 40 43 expr=%expr_zero_shape
1047torch.empty op_11 1 1 43 zeros
1048torch.transpose op_12 1 1 42 44 dim0=-1 dim1=-2
1049torch.baddbmm op_13 3 1 zeros 40 44 45 alpha=%inv_sqrt_embed_dim_per_head beta=0
1050F.softmax softmax 1 1 45 46 dim=%softmax_dim
1051Tensor.permute op_15 1 1 38 47 dims=(0,2,1,3)
1052Tensor.reshape op_16 1 1 47 48 shape=(%num_heads,%kvsize,%feat_per_head)
1053torch.bmm op_17 2 1 46 48 49
1054Tensor.reshape op_18 1 1 49 50 shape=(%batch,%num_heads,%qsize,%feat_per_head)
1055Tensor.permute op_19 1 1 50 51 dims=(0,2,1,3)
1056Tensor.reshape op_20 1 1 51 52 shape=(%batch,%qsize,%embed_dim)
1057nn.Linear out_proj 1 1 52 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1058pnnx.Output output 1 0 out
1059)PNNXIR";
1060 }
1061
1062 // TODO match expr_zero_shape
1063};
1064
1065class fuse_multiheadattention_pass_10 : public fuse_multiheadattention_pass_q_samekv
1066{
1067public:
1068 const char* match_pattern_graph() const
1069 {
1070 return R"PNNXIR(7767517
107125 24
1072pnnx.Input input_q 0 1 query
1073pnnx.Input input_kv 0 1 kv
1074nn.Linear op_0 1 1 query 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1075nn.Linear op_1 1 1 kv 34 bias=%kbias in_features=%kvdim out_features=%embed_dim @bias @weight
1076nn.Linear op_2 1 1 kv 35 bias=%vbias in_features=%kvdim out_features=%embed_dim @bias @weight
1077Tensor.reshape op_3 1 1 33 36 shape=(%batch,%qsize,%num_heads,%feat_per_head)
1078Tensor.reshape op_4 1 1 34 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
1079Tensor.reshape op_5 1 1 35 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
1080Tensor.permute op_6 1 1 36 39 dims=(0,2,1,3)
1081Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%qsize,%feat_per_head)
1082Tensor.permute op_8 1 1 37 41 dims=(0,2,1,3)
1083Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%kvsize,%feat_per_head)
1084pnnx.Expression op_10 1 1 40 43 expr=%expr_zero_shape
1085torch.empty op_11 1 1 43 zeros
1086torch.transpose op_12 1 1 42 44 dim0=-1 dim1=-2
1087torch.baddbmm op_13 3 1 zeros 40 44 45 alpha=%alpha beta=0
1088F.softmax softmax 1 1 45 46 dim=%softmax_dim
1089Tensor.permute op_15 1 1 38 47 dims=(0,2,1,3)
1090Tensor.reshape op_16 1 1 47 48 shape=(%num_heads,%kvsize,%feat_per_head)
1091torch.bmm op_17 2 1 46 48 49
1092Tensor.reshape op_18 1 1 49 50 shape=(%batch,%num_heads,%qsize,%feat_per_head)
1093Tensor.permute op_19 1 1 50 51 dims=(0,2,1,3)
1094Tensor.reshape op_20 1 1 51 52 shape=(%batch,%qsize,%embed_dim)
1095nn.Linear out_proj 1 1 52 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1096pnnx.Output output 1 0 out
1097)PNNXIR";
1098 }
1099};
1100
1101class fuse_multiheadattention_pass_12 : public fuse_multiheadattention_pass_sameqkv
1102{
1103public:
1104 const char* match_pattern_graph() const
1105 {
1106 return R"PNNXIR(7767517
110715 14
1108pnnx.Input input_0 0 1 input
1109nn.Linear op_0 1 1 input 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1110nn.Linear op_1 1 1 input 34 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1111nn.Linear op_2 1 1 input 35 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1112Tensor.view op_3 1 1 33 36 shape=(%batch,%size,%num_heads,%feat_per_head)
1113Tensor.view op_4 1 1 34 37 shape=(%batch,%size,%num_heads,%feat_per_head)
1114Tensor.view op_5 1 1 35 38 shape=(%batch,%size,%num_heads,%feat_per_head)
1115torch.transpose op_6 1 1 38 39 dim0=1 dim1=2
1116torch.transpose op_7 1 1 37 40 dim0=1 dim1=2
1117torch.transpose op_8 1 1 36 41 dim0=1 dim1=2
1118F.scaled_dot_product_attention op_9 3 1 41 40 39 42 attn_mask=None dropout_p=0.000000e+00 is_causal=False
1119torch.transpose op_10 1 1 42 43 dim0=1 dim1=2
1120Tensor.reshape op_11 1 1 43 44 shape=(%batch,%size,%embed_dim)
1121nn.Linear out_proj 1 1 44 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1122pnnx.Output output 1 0 out
1123)PNNXIR";
1124 }
1125
1126 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
1127 {
1128 const int embed_dim = captured_params.at("embed_dim").i;
1129 const int num_heads = captured_params.at("num_heads").i;
1130 const int feat_per_head = captured_params.at("feat_per_head").i;
1131
1132 if (embed_dim != num_heads * feat_per_head)
1133 return false;
1134
1135 return true;
1136 }
1137};
1138
1139class fuse_multiheadattention_pass_12_1 : public fuse_multiheadattention_pass_12
1140{
1141public:
1142 const char* match_pattern_graph() const
1143 {
1144 return R"PNNXIR(7767517
114515 14
1146pnnx.Input input_0 0 1 input
1147nn.Linear op_0 1 1 input 14 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1148nn.Linear op_1 1 1 input 15 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1149nn.Linear op_2 1 1 input 16 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1150Tensor.reshape op_3 1 1 14 17 shape=(%batch,%size,%num_heads,%feat_per_head)
1151Tensor.reshape op_4 1 1 15 18 shape=(%batch,%size,%num_heads,%feat_per_head)
1152Tensor.reshape op_5 1 1 16 19 shape=(%batch,%size,%num_heads,%feat_per_head)
1153Tensor.permute op_6 1 1 19 20 dims=(0,2,1,3)
1154Tensor.permute op_7 1 1 18 21 dims=(0,2,1,3)
1155Tensor.permute op_8 1 1 17 22 dims=(0,2,1,3)
1156F.scaled_dot_product_attention op_9 3 1 22 21 20 23 attn_mask=None dropout_p=0.000000e+00 is_causal=False
1157Tensor.permute op_10 1 1 23 24 dims=(0,2,1,3)
1158Tensor.reshape op_11 1 1 24 25 shape=(%batch,%size,%embed_dim)
1159nn.Linear out_proj 1 1 25 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1160pnnx.Output output 1 0 out
1161)PNNXIR";
1162 }
1163};
1164
1165class fuse_multiheadattention_pass_13 : public fuse_multiheadattention_pass_qkv
1166{
1167public:
1168 const char* match_pattern_graph() const
1169 {
1170 return R"PNNXIR(7767517
117117 16
1172pnnx.Input input_0 0 1 query
1173pnnx.Input input_1 0 1 key
1174pnnx.Input input_2 0 1 value
1175nn.Linear op_0 1 1 query 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1176nn.Linear op_1 1 1 key 34 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight
1177nn.Linear op_2 1 1 value 35 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight
1178Tensor.view op_3 1 1 33 36 shape=(%batch,%qsize,%num_heads,%feat_per_head)
1179Tensor.view op_4 1 1 34 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
1180Tensor.view op_5 1 1 35 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
1181torch.transpose op_6 1 1 38 39 dim0=1 dim1=2
1182torch.transpose op_7 1 1 37 40 dim0=1 dim1=2
1183torch.transpose op_8 1 1 36 41 dim0=1 dim1=2
1184F.scaled_dot_product_attention op_9 3 1 41 40 39 42 attn_mask=None dropout_p=0.000000e+00 is_causal=False
1185torch.transpose op_10 1 1 42 43 dim0=1 dim1=2
1186Tensor.reshape op_11 1 1 43 44 shape=(%batch,%qsize,%embed_dim)
1187nn.Linear out_proj 1 1 44 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1188pnnx.Output output 1 0 out
1189)PNNXIR";
1190 }
1191
1192 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
1193 {
1194 const int embed_dim = captured_params.at("embed_dim").i;
1195 const int num_heads = captured_params.at("num_heads").i;
1196 const int feat_per_head = captured_params.at("feat_per_head").i;
1197
1198 if (embed_dim != num_heads * feat_per_head)
1199 return false;
1200
1201 return true;
1202 }
1203};
1204
1205class fuse_multiheadattention_pass_14 : public fuse_multiheadattention_pass_q_samekv
1206{
1207public:
1208 const char* match_pattern_graph() const
1209 {
1210 return R"PNNXIR(7767517
121116 15
1212pnnx.Input input_0 0 1 query
1213pnnx.Input input_1 0 1 kv
1214nn.Linear op_0 1 1 query 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1215nn.Linear op_1 1 1 kv 34 bias=%kbias in_features=%kvdim out_features=%embed_dim @bias @weight
1216nn.Linear op_2 1 1 kv 35 bias=%vbias in_features=%kvdim out_features=%embed_dim @bias @weight
1217Tensor.view op_3 1 1 33 36 shape=(%batch,%qsize,%num_heads,%feat_per_head)
1218Tensor.view op_4 1 1 34 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
1219Tensor.view op_5 1 1 35 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head)
1220torch.transpose op_6 1 1 38 39 dim0=1 dim1=2
1221torch.transpose op_7 1 1 37 40 dim0=1 dim1=2
1222torch.transpose op_8 1 1 36 41 dim0=1 dim1=2
1223F.scaled_dot_product_attention op_9 3 1 41 40 39 42 attn_mask=None dropout_p=0.000000e+00 is_causal=False
1224torch.transpose op_10 1 1 42 43 dim0=1 dim1=2
1225Tensor.reshape op_11 1 1 43 44 shape=(%batch,%qsize,%embed_dim)
1226nn.Linear out_proj 1 1 44 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1227pnnx.Output output 1 0 out
1228)PNNXIR";
1229 }
1230
1231 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
1232 {
1233 const int embed_dim = captured_params.at("embed_dim").i;
1234 const int num_heads = captured_params.at("num_heads").i;
1235 const int feat_per_head = captured_params.at("feat_per_head").i;
1236
1237 if (embed_dim != num_heads * feat_per_head)
1238 return false;
1239
1240 return true;
1241 }
1242};
1243
1244class fuse_multiheadattention_pass_15 : public fuse_multiheadattention_pass_sameqkv
1245{
1246public:
1247 const char* match_pattern_graph() const
1248 {
1249 return R"PNNXIR(7767517
125023 22
1251pnnx.Input input 0 1 input
1252nn.Linear op_0 1 1 input 2 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1253nn.Linear op_1 1 1 input 4 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1254nn.Linear op_2 1 1 input 6 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1255pnnx.Expression op_3 1 1 2 3 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
1256Tensor.view op_4 1 1 3 8 shape=(%batch,%size,%num_heads,%feat_per_head)
1257Tensor.view op_5 1 1 4 5 shape=(%batch,%size,%num_heads,%feat_per_head)
1258Tensor.view op_6 1 1 6 7 shape=(%batch,%size,%num_heads,%feat_per_head)
1259torch.transpose op_7 1 1 8 9 dim0=1 dim1=2
1260torch.transpose op_8 1 1 5 10 dim0=1 dim1=2
1261torch.transpose op_9 1 1 7 11 dim0=1 dim1=2
1262Tensor.reshape op_10 1 1 9 14 shape=(%num_heads,%batch_mul_size,%feat_per_head)
1263Tensor.reshape op_11 1 1 10 12 shape=(%num_heads,%batch_mul_size,%feat_per_head)
1264Tensor.reshape op_12 1 1 11 17 shape=(%num_heads,%batch_mul_size,%feat_per_head)
1265torch.transpose op_13 1 1 12 13 dim0=1 dim1=2
1266torch.bmm op_14 2 1 14 13 15
1267F.softmax softmax 1 1 15 16 dim=%softmax_dim
1268torch.bmm op_16 2 1 16 17 18
1269Tensor.view op_17 1 1 18 19 shape=(%batch,%num_heads,%size,%feat_per_head)
1270torch.transpose op_18 1 1 19 20 dim0=1 dim1=2
1271Tensor.reshape op_19 1 1 20 21 shape=(%batch,%size,%embed_dim)
1272nn.Linear out_proj 1 1 21 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1273pnnx.Output output 1 0 out
1274)PNNXIR";
1275 }
1276};
1277
1278class fuse_multiheadattention_pass_16 : public fuse_multiheadattention_pass_sameqkv
1279{
1280public:
1281 const char* match_pattern_graph() const
1282 {
1283 return R"PNNXIR(7767517
128427 26
1285pnnx.Input input 0 1 input
1286nn.Linear op_0 1 1 input 3 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1287nn.Linear op_1 1 1 input 5 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1288nn.Linear op_2 1 1 input 7 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1289pnnx.Expression op_3 1 1 3 4 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
1290Tensor.view op_4 1 1 4 9 shape=(%batch,%size,%num_heads,%feat_per_head)
1291Tensor.view op_5 1 1 5 6 shape=(%batch,%size,%num_heads,%feat_per_head)
1292Tensor.view op_6 1 1 7 8 shape=(%batch,%size,%num_heads,%feat_per_head)
1293torch.transpose op_7 1 1 9 10 dim0=1 dim1=2
1294torch.transpose op_8 1 1 6 11 dim0=1 dim1=2
1295torch.transpose op_9 1 1 8 12 dim0=1 dim1=2
1296Tensor.reshape op_10 1 1 10 15 shape=(%num_heads,%batch_mul_size,%feat_per_head)
1297Tensor.reshape op_11 1 1 11 13 shape=(%num_heads,%batch_mul_size,%feat_per_head)
1298Tensor.reshape op_12 1 1 12 21 shape=(%num_heads,%batch_mul_size,%feat_per_head)
1299torch.transpose op_13 1 1 13 14 dim0=1 dim1=2
1300torch.bmm op_14 2 1 15 14 16
1301Tensor.view op_15 1 1 16 17 shape=(%batch,%num_heads,%size,%size)
1302pnnx.Attribute attn_mask 0 1 attn_mask @data=(1,1,%size,%size)f32
1303pnnx.Expression op_16 2 1 17 attn_mask 18 expr=add(@0,@1)
1304Tensor.view op_17 1 1 18 19 shape=(%num_heads,%size,%size)
1305F.softmax softmax 1 1 19 20 dim=%softmax_dim
1306torch.bmm op_19 2 1 20 21 22
1307Tensor.view op_20 1 1 22 23 shape=(%batch,%num_heads,%size,%feat_per_head)
1308torch.transpose op_21 1 1 23 24 dim0=1 dim1=2
1309Tensor.reshape op_22 1 1 24 25 shape=(%batch,%size,%embed_dim)
1310nn.Linear out_proj 1 1 25 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1311pnnx.Output output 1 0 out
1312)PNNXIR";
1313 }
1314
1315 const char* replace_pattern_graph() const
1316 {
1317 return R"PNNXIR(7767517
13184 3
1319pnnx.Input input 0 1 input
1320pnnx.Attribute attn_mask 0 1 attn_mask @data=%attn_mask.data
1321nn.MultiheadAttention attention 2 1 input attn_mask out embed_dim=%embed_dim kdim=%embed_dim vdim=%embed_dim num_heads=%num_heads batch_first=True add_zero_attn=False add_bias_kv=False $attn_mask=attn_mask
1322pnnx.Output output 1 0 out
1323)PNNXIR";
1324 }
1325
1326 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
1327 {
1328 fuse_multiheadattention_pass_sameqkv::write(ops, captured_params, captured_attrs);
1329
1330 const int size = captured_params.at("size").i;
1331
1332 ops.at("attn_mask")->attrs["data"].shape = {size, size};
1333 }
1334};
1335
1336class fuse_multiheadattention_pass_16_1 : public fuse_multiheadattention_pass_sameqkv
1337{
1338public:
1339 const char* match_pattern_graph() const
1340 {
1341 return R"PNNXIR(7767517
134220 19
1343pnnx.Input input_0 0 1 input
1344nn.Linear op_0 1 1 input 31 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1345nn.Linear op_1 1 1 input 32 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1346nn.Linear op_2 1 1 input 34 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1347Tensor.view op_3 1 1 31 36 shape=(%batch,%size,%num_heads,%feat_per_head)
1348Tensor.view op_4 1 1 32 33 shape=(%batch,%size,%num_heads,%feat_per_head)
1349Tensor.view op_5 1 1 34 35 shape=(%batch,%size,%num_heads,%feat_per_head)
1350Tensor.permute op_6 1 1 36 38 dims=(0,2,1,3)
1351Tensor.permute op_7 1 1 33 37 dims=(0,2,1,3)
1352Tensor.permute op_8 1 1 35 43 dims=(0,2,1,3)
1353torch.transpose op_9 1 1 37 39 dim0=-1 dim1=-2
1354torch.matmul op_10 2 1 38 39 40
1355pnnx.Attribute attn_mask 0 1 attn_mask @data=(1,1,1,%size)f32
1356pnnx.Expression op_11 2 1 40 attn_mask 41 expr=add(div(@0,%sqrt_feat_per_head),@1)
1357F.softmax softmax 1 1 41 42 dim=%softmax_dim
1358torch.matmul op_13 2 1 42 43 44
1359Tensor.permute op_14 1 1 44 45 dims=(0,2,1,3)
1360Tensor.reshape op_15 1 1 45 46 shape=(%batch,%size,%embed_dim)
1361nn.Linear out_proj 1 1 46 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1362pnnx.Output output 1 0 out
1363)PNNXIR";
1364 }
1365
1366 const char* replace_pattern_graph() const
1367 {
1368 return R"PNNXIR(7767517
13694 3
1370pnnx.Input input_0 0 1 input
1371pnnx.Attribute attn_mask 0 1 attn_mask @data=%attn_mask.data
1372nn.MultiheadAttention attention 2 1 input attn_mask out embed_dim=%embed_dim kdim=%embed_dim vdim=%embed_dim num_heads=%num_heads batch_first=True add_zero_attn=False add_bias_kv=False $attn_mask=attn_mask
1373pnnx.Output output 1 0 out
1374)PNNXIR";
1375 }
1376
1377 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
1378 {
1379 const int embed_dim = captured_params.at("embed_dim").i;
1380 const int num_heads = captured_params.at("num_heads").i;
1381 const int feat_per_head = captured_params.at("feat_per_head").i;
1382 const float sqrt_feat_per_head = captured_params.at("sqrt_feat_per_head").f;
1383 const int softmax_dim = captured_params.at("softmax_dim").i;
1384
1385 if (embed_dim != num_heads * feat_per_head)
1386 return false;
1387
1388 if (!NearlyEqual(sqrt_feat_per_head, sqrt(feat_per_head), 0.001))
1389 return false;
1390
1391 int softmax_input_rank = (int)matched_operators.at("softmax")->inputs[0]->shape.size();
1392 if (softmax_dim != -1 && softmax_dim != softmax_input_rank - 1)
1393 return false;
1394
1395 return true;
1396 }
1397
1398 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
1399 {
1400 fuse_multiheadattention_pass_sameqkv::write(ops, captured_params, captured_attrs);
1401
1402 const int size = captured_params.at("size").i;
1403
1404 Operator* op_attr = ops.at("attn_mask");
1405
1406 fprintf(stderr, "op_attr->attrs[data] type %d\n", op_attr->attrs["data"].type);
1407
1408 // hack attn_mask shape
1409 op_attr->attrs["data"].shape = {size, size};
1410
1411 // hack attn_mask value
1412 std::vector<char>& data = op_attr->attrs["data"].data;
1413 size_t len = data.size();
1414 data.resize(len * size);
1415 for (int i = 1; i < size; i++)
1416 {
1417 memcpy(&data[len * i], &data[0], len);
1418 }
1419 }
1420};
1421
1422class fuse_multiheadattention_pass_17 : public fuse_multiheadattention_pass
1423{
1424public:
1425 const char* match_pattern_graph() const
1426 {
1427 return R"PNNXIR(7767517
142817 16
1429pnnx.Input input_0 0 1 input
1430nn.Linear op_0 1 1 input 8 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
1431Tensor.reshape op_1 1 1 8 9 shape=(%batch,%size,3,%num_heads,%feat_per_head)
1432Tensor.permute op_2 1 1 9 10 dims=(2,0,3,1,4)
1433torch.unbind op_3 1 3 10 11 12 13 dim=0
1434pnnx.Expression op_4 1 1 11 14 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
1435torch.transpose op_5 1 1 12 15 dim0=-2 dim1=-1
1436torch.matmul op_6 2 1 14 15 16
1437pnnx.Attribute attn_mask 0 1 attn_mask @data=(1,%num_heads,%size,%size)f32
1438pnnx.Expression op_7 2 1 16 attn_mask 18 expr=add(@0,@1)
1439F.softmax softmax 1 1 18 19 dim=%softmax_dim
1440torch.matmul op_9 2 1 19 13 20
1441torch.transpose op_10 1 1 20 21 dim0=1 dim1=2
1442Tensor.reshape op_11 1 1 21 22 shape=(%batch,%size,%embed_dim)
1443nn.Linear out_proj 1 1 22 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1444pnnx.Output output 1 0 out
1445)PNNXIR";
1446 }
1447
1448 const char* replace_pattern_graph() const
1449 {
1450 return R"PNNXIR(7767517
14514 3
1452pnnx.Input input_0 0 1 input
1453pnnx.Attribute attn_mask 0 1 attn_mask @data=%attn_mask.data
1454nn.MultiheadAttention attention 2 1 input attn_mask out embed_dim=%embed_dim kdim=%embed_dim vdim=%embed_dim num_heads=%num_heads batch_first=True add_zero_attn=False add_bias_kv=False $attn_mask=attn_mask
1455pnnx.Output output 1 0 out
1456)PNNXIR";
1457 }
1458
1459 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
1460 {
1461 fuse_multiheadattention_pass::write(ops, captured_params, captured_attrs);
1462
1463 const int batch = captured_params.at("batch").i;
1464 const int size = captured_params.at("size").i;
1465 const int num_heads = captured_params.at("num_heads").i;
1466
1467 Operator* op_attr = ops.at("attn_mask");
1468
1469 // hack attn_mask shape
1470 op_attr->attrs["data"].shape = {batch * num_heads, size, size};
1471
1472 // hack attn_mask value
1473 std::vector<char>& data = op_attr->attrs["data"].data;
1474 size_t len = data.size();
1475 data.resize(len * batch);
1476 for (int i = 1; i < batch; i++)
1477 {
1478 memcpy(&data[len * i], &data[0], len);
1479 }
1480 }
1481};
1482
1483class fuse_multiheadattention_pass_17_1 : public fuse_multiheadattention_pass_17
1484{
1485public:
1486 const char* match_pattern_graph() const
1487 {
1488 return R"PNNXIR(7767517
148917 16
1490pnnx.Input input_0 0 1 input
1491nn.Linear op_0 1 1 input 8 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
1492Tensor.reshape op_1 1 1 8 9 shape=(%batch,%size,3,%num_heads,%feat_per_head)
1493Tensor.permute op_2 1 1 9 10 dims=(2,0,3,1,4)
1494torch.unbind op_3 1 3 10 11 12 13 dim=0
1495pnnx.Expression op_4 1 1 11 14 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
1496Tensor.permute op_5 1 1 12 15 dims=(0,1,3,2)
1497torch.matmul op_6 2 1 14 15 16
1498pnnx.Attribute attn_mask 0 1 attn_mask @data=(1,%num_heads,%size,%size)f32
1499pnnx.Expression op_7 2 1 16 attn_mask 18 expr=add(@0,@1)
1500F.softmax softmax 1 1 18 19 dim=%softmax_dim
1501torch.matmul op_9 2 1 19 13 20
1502Tensor.permute op_10 1 1 20 21 dims=(0,2,1,3)
1503Tensor.reshape op_11 1 1 21 22 shape=(%batch,%size,%embed_dim)
1504nn.Linear out_proj 1 1 22 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1505pnnx.Output output 1 0 out
1506)PNNXIR";
1507 }
1508};
1509
1510class fuse_multiheadattention_pass_18 : public fuse_multiheadattention_pass
1511{
1512public:
1513 const char* match_pattern_graph() const
1514 {
1515 return R"PNNXIR(7767517
151620 19
1517pnnx.Input input_0 0 1 input
1518nn.Linear op_0 1 1 input 25 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
1519Tensor.reshape op_1 1 1 25 26 shape=(%batch,%size,3,%num_heads,%feat_per_head)
1520Tensor.permute op_2 1 1 26 27 dims=(2,0,3,1,4)
1521torch.unbind op_3 1 3 27 28 29 30 dim=0
1522pnnx.Expression op_4 1 1 28 31 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
1523torch.transpose op_5 1 1 29 32 dim0=-2 dim1=-1
1524torch.matmul op_6 2 1 31 32 33
1525pnnx.Attribute attn_mask 0 1 attn_mask @data=(1,%num_heads,%size,%size)f32
1526pnnx.Expression op_7 2 1 33 attn_mask 35 expr=add(@0,@1)
1527Tensor.view op_8 1 1 35 36 shape=(1,%batch,%num_heads,%size,%size)
1528pnnx.Attribute op_9 0 1 37 @data=(1,%batch,1,%size,%size)f32
1529pnnx.Expression op_10 2 1 36 37 38 expr=add(@0,@1)
1530Tensor.view op_11 1 1 38 39 shape=(%batch,%num_heads,%size,%size)
1531F.softmax softmax 1 1 39 40 dim=%softmax_dim
1532torch.matmul op_13 2 1 40 30 41
1533torch.transpose op_14 1 1 41 42 dim0=1 dim1=2
1534Tensor.reshape op_15 1 1 42 43 shape=(%batch,%size,%embed_dim)
1535nn.Linear out_proj 1 1 43 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1536pnnx.Output output 1 0 out
1537)PNNXIR";
1538 }
1539
1540 const char* replace_pattern_graph() const
1541 {
1542 return R"PNNXIR(7767517
15434 3
1544pnnx.Input input_0 0 1 input
1545pnnx.Attribute attn_mask 0 1 attn_mask @data=%attn_mask.data
1546nn.MultiheadAttention attention 2 1 input attn_mask out embed_dim=%embed_dim kdim=%embed_dim vdim=%embed_dim num_heads=%num_heads batch_first=True add_zero_attn=False add_bias_kv=False $attn_mask=attn_mask
1547pnnx.Output output 1 0 out
1548)PNNXIR";
1549 }
1550
1551 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
1552 {
1553 fuse_multiheadattention_pass::write(ops, captured_params, captured_attrs);
1554
1555 const int batch = captured_params.at("batch").i;
1556 const int size = captured_params.at("size").i;
1557 const int num_heads = captured_params.at("num_heads").i;
1558
1559 Operator* op_attr = ops.at("attn_mask");
1560
1561 // hack attn_mask shape
1562 op_attr->attrs["data"].shape = {batch * num_heads, size, size};
1563
1564 // hack attn_mask value
1565 std::vector<char>& data = op_attr->attrs["data"].data;
1566 size_t len = data.size();
1567 data.resize(len * batch);
1568 for (int i = 1; i < batch; i++)
1569 {
1570 memcpy(&data[len * i], &data[0], len);
1571 }
1572
1573 // add mask2
1574 {
1575 auto mask2 = captured_attrs.at("op_9.data");
1576 auto maskdata = op_attr->attrs["data"].get_float32_data();
1577 const int ls = mask2.shape[3] * mask2.shape[4];
1578
1579 for (int i = 0; i < batch; i++)
1580 {
1581 for (int n = 0; n < num_heads; n++)
1582 {
1583 float* p = (float*)maskdata.data() + ls * (i * num_heads + n);
1584 const float* p2 = ((float*)mask2.data.data()) + ls * i;
1585 for (int k = 0; k < ls; k++)
1586 {
1587 p[k] += p2[k];
1588 }
1589 }
1590 }
1591
1592 op_attr->attrs["data"].set_float32_data(maskdata);
1593 }
1594 }
1595};
1596
1597class fuse_multiheadattention_pass_18_1 : public fuse_multiheadattention_pass_18
1598{
1599public:
1600 const char* match_pattern_graph() const
1601 {
1602 return R"PNNXIR(7767517
160320 19
1604pnnx.Input input_0 0 1 input
1605nn.Linear op_0 1 1 input 25 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
1606Tensor.reshape op_1 1 1 25 26 shape=(%batch,%size,3,%num_heads,%feat_per_head)
1607Tensor.permute op_2 1 1 26 27 dims=(2,0,3,1,4)
1608torch.unbind op_3 1 3 27 28 29 30 dim=0
1609pnnx.Expression op_4 1 1 28 31 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
1610Tensor.permute op_5 1 1 29 32 dims=(0,1,3,2)
1611torch.matmul op_6 2 1 31 32 33
1612pnnx.Attribute attn_mask 0 1 attn_mask @data=(1,%num_heads,%size,%size)f32
1613pnnx.Expression op_7 2 1 33 attn_mask 35 expr=add(@0,@1)
1614Tensor.reshape op_8 1 1 35 36 shape=(1,%batch,%num_heads,%size,%size)
1615pnnx.Attribute op_9 0 1 37 @data=(1,%batch,1,%size,%size)f32
1616pnnx.Expression op_10 2 1 36 37 38 expr=add(@0,@1)
1617Tensor.reshape op_11 1 1 38 39 shape=(%batch,%num_heads,%size,%size)
1618F.softmax softmax 1 1 39 40 dim=%softmax_dim
1619torch.matmul op_13 2 1 40 30 41
1620Tensor.permute op_14 1 1 41 42 dims=(0,2,1,3)
1621Tensor.reshape op_15 1 1 42 43 shape=(%batch,%size,%embed_dim)
1622nn.Linear out_proj 1 1 43 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1623pnnx.Output output 1 0 out
1624)PNNXIR";
1625 }
1626};
1627
1628class fuse_multiheadattention_pass_onnx : public fuse_multiheadattention_pass_qkv
1629{
1630public:
1631 const char* match_pattern_graph() const
1632 {
1633 return R"PNNXIR(7767517
163423 22
1635pnnx.Input input_q 0 1 query
1636pnnx.Input input_k 0 1 key
1637pnnx.Input input_v 0 1 value
1638nn.Linear op_0 1 1 query 10 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1639nn.Linear op_1 1 1 key 11 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight
1640nn.Linear op_2 1 1 value 12 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight
1641Tensor.reshape op_3 1 1 10 13 shape=(%qsize,%num_heads,%feat_per_head)
1642Tensor.reshape op_4 1 1 11 15 shape=(%kvsize,%num_heads,%feat_per_head)
1643Tensor.reshape op_5 1 1 12 16 shape=(%kvsize,%num_heads,%feat_per_head)
1644Tensor.permute op_6 1 1 13 14 dims=(1,0,2)
1645Tensor.permute op_7 1 1 15 19 dims=(1,2,0)
1646Tensor.permute op_8 1 1 16 17 dims=(1,0,2)
1647pnnx.Expression op_9 1 1 14 18 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
1648torch.matmul op_10 2 1 18 19 20
1649F.softmax softmax 1 1 20 21 dim=%softmax_dim
1650torch.matmul op_12 2 1 21 17 22
1651Tensor.permute op_13 1 1 22 23 dims=(1,0,2)
1652Tensor.reshape op_14 1 1 23 24 shape=(%qsize,%embed_dim)
1653nn.Linear out_proj 1 1 24 25 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1654Tensor.reshape op_16 1 1 25 out shape=(%qsize,%batch,%embed_dim)
1655Tensor.reshape op_17 1 1 21 27 shape=(%batch,%num_heads,%qsize,%kvsize)
1656torch.mean op_18 1 1 27 outweight dim=(1) keepdim=False
1657pnnx.Output output 2 0 out outweight
1658)PNNXIR";
1659 }
1660
1661 const char* replace_pattern_graph() const
1662 {
1663 return R"PNNXIR(7767517
16645 5
1665pnnx.Input input_0 0 1 query
1666pnnx.Input input_1 0 1 key
1667pnnx.Input input_2 0 1 value
1668nn.MultiheadAttention attention 3 2 query key value out outweight embed_dim=%embed_dim kdim=%kdim vdim=%vdim num_heads=%num_heads batch_first=False add_zero_attn=False add_bias_kv=False
1669pnnx.Output output 2 0 out outweight
1670)PNNXIR";
1671 }
1672};
1673
1674class fuse_multiheadattention_pass_onnx_1 : public fuse_multiheadattention_pass
1675{
1676public:
1677 const char* match_pattern_graph() const
1678 {
1679 return R"PNNXIR(7767517
168026 25
1681pnnx.Input input_q 0 1 input
1682nn.Linear op_0 1 1 input 14 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
1683Tensor.reshape op_1 1 1 14 15 shape=(%batch,%size,1,3,%embed_dim)
1684Tensor.permute op_2 1 1 15 16 dims=(3,1,2,0,4)
1685torch.squeeze op_3 1 1 16 17 dim=3
1686torch.unbind op_4 1 3 17 18 19 20 dim=0
1687Tensor.reshape op_5 1 1 18 21 shape=(%size,%num_heads,%feat_per_head)
1688Tensor.reshape op_6 1 1 19 23 shape=(%size,%num_heads,%feat_per_head)
1689Tensor.reshape op_7 1 1 20 25 shape=(%size,%num_heads,%feat_per_head)
1690Tensor.permute op_8 1 1 21 22 dims=(1,0,2)
1691Tensor.permute op_9 1 1 23 24 dims=(1,0,2)
1692Tensor.permute op_10 1 1 25 26 dims=(1,0,2)
1693Tensor.reshape op_11 1 1 22 27 shape=(%batch,%num_heads,%size,%feat_per_head)
1694Tensor.reshape op_12 1 1 24 28 shape=(%batch,%num_heads,%size,%feat_per_head)
1695Tensor.reshape op_13 1 1 26 29 shape=(%batch,%num_heads,%size,%feat_per_head)
1696Tensor.permute op_14 1 1 28 30 dims=(0,1,3,2)
1697pnnx.Expression op_15 1 1 27 31 expr=mul(@0,%sqrt_inv_sqrt_embed_dim_per_head)
1698pnnx.Expression op_16 1 1 30 32 expr=mul(@0,%sqrt_inv_sqrt_embed_dim_per_head)
1699torch.matmul op_17 2 1 31 32 33
1700F.softmax softmax 1 1 33 34 dim=%softmax_dim
1701torch.matmul op_19 2 1 34 29 35
1702Tensor.permute op_20 1 1 35 36 dims=(2,0,1,3)
1703Tensor.reshape op_21 1 1 36 37 shape=(%size,%embed_dim)
1704nn.Linear out_proj 1 1 37 38 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1705Tensor.reshape op_23 1 1 38 out shape=(%size,%batch,%embed_dim)
1706pnnx.Output output 1 0 out
1707)PNNXIR";
1708 }
1709
1710 const char* replace_pattern_graph() const
1711 {
1712 return R"PNNXIR(7767517
17133 2
1714pnnx.Input input 0 1 input
1715nn.MultiheadAttention attention 1 1 input out embed_dim=%embed_dim kdim=%embed_dim vdim=%embed_dim num_heads=%num_heads batch_first=False add_zero_attn=False add_bias_kv=False
1716pnnx.Output output 1 0 out
1717)PNNXIR";
1718 }
1719
1720 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
1721 {
1722 const int embed_dim = captured_params.at("embed_dim").i;
1723 const int qkv_out_features = captured_params.at("qkv_out_features").i;
1724 const int num_heads = captured_params.at("num_heads").i;
1725 const int feat_per_head = captured_params.at("feat_per_head").i;
1726 const float sqrt_inv_sqrt_embed_dim_per_head = captured_params.at("sqrt_inv_sqrt_embed_dim_per_head").f;
1727 const int softmax_dim = captured_params.at("softmax_dim").i;
1728
1729 if (qkv_out_features != embed_dim * 3)
1730 return false;
1731
1732 if (embed_dim != num_heads * feat_per_head)
1733 return false;
1734
1735 if (!NearlyEqual(sqrt_inv_sqrt_embed_dim_per_head, sqrt(1.f / sqrt(feat_per_head)), 0.001))
1736 return false;
1737
1738 int softmax_input_rank = (int)matched_operators.at("softmax")->inputs[0]->shape.size();
1739 if (softmax_dim != -1 && softmax_dim != softmax_input_rank - 1)
1740 return false;
1741
1742 return true;
1743 }
1744};
1745
1746class fuse_multiheadattention_pass_onnx_1_1 : public fuse_multiheadattention_pass
1747{
1748public:
1749 const char* match_pattern_graph() const
1750 {
1751 return R"PNNXIR(7767517
175221 20
1753pnnx.Input input_q 0 1 input
1754nn.Linear op_0 1 1 input 33 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
1755Tensor.reshape op_1 1 1 33 34 shape=(%batch,%size,1,3,%embed_dim)
1756Tensor.permute op_2 1 1 34 35 dims=(3,1,2,0,4)
1757torch.squeeze op_3 1 1 35 36 dim=3
1758torch.unbind op_4 1 3 36 37 38 39 dim=0
1759Tensor.reshape op_5 1 1 37 40 shape=(%size,%num_heads,%feat_per_head)
1760Tensor.reshape op_6 1 1 38 42 shape=(%size,%num_heads,%feat_per_head)
1761Tensor.reshape op_7 1 1 39 43 shape=(%size,%num_heads,%feat_per_head)
1762Tensor.permute op_8 1 1 40 41 dims=(1,0,2)
1763Tensor.permute op_9 1 1 42 46 dims=(1,2,0)
1764Tensor.permute op_10 1 1 43 44 dims=(1,0,2)
1765pnnx.Expression op_11 1 1 41 45 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
1766torch.matmul op_12 2 1 45 46 47
1767F.softmax softmax 1 1 47 48 dim=%softmax_dim
1768torch.matmul op_14 2 1 48 44 49
1769Tensor.permute op_15 1 1 49 50 dims=(1,0,2)
1770Tensor.reshape op_16 1 1 50 51 shape=(%size,%embed_dim)
1771nn.Linear out_proj 1 1 51 52 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1772Tensor.reshape op_18 1 1 52 out shape=(%size,%batch,%embed_dim)
1773pnnx.Output output 1 0 out
1774)PNNXIR";
1775 }
1776
1777 const char* replace_pattern_graph() const
1778 {
1779 return R"PNNXIR(7767517
17803 2
1781pnnx.Input input 0 1 input
1782nn.MultiheadAttention attention 1 1 input out embed_dim=%embed_dim kdim=%embed_dim vdim=%embed_dim num_heads=%num_heads batch_first=False add_zero_attn=False add_bias_kv=False
1783pnnx.Output output 1 0 out
1784)PNNXIR";
1785 }
1786};
1787
1788class fuse_multiheadattention_pass_onnx_1_2 : public fuse_multiheadattention_pass
1789{
1790public:
1791 const char* match_pattern_graph() const
1792 {
1793 return R"PNNXIR(7767517
179421 20
1795pnnx.Input input_q 0 1 input
1796nn.Linear op_0 1 1 input 14 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
1797Tensor.reshape op_1 1 1 14 15 shape=(%batch,%size,1,3,%embed_dim)
1798Tensor.permute op_2 1 1 15 16 dims=(3,1,2,0,4)
1799torch.squeeze op_3 1 1 16 17 dim=3
1800torch.unbind op_4 1 3 17 18 19 20 dim=0
1801Tensor.reshape op_5 1 1 18 21 shape=(%size,%num_heads,%feat_per_head)
1802Tensor.reshape op_6 1 1 19 23 shape=(%size,%num_heads,%feat_per_head)
1803Tensor.reshape op_7 1 1 20 25 shape=(%size,%num_heads,%feat_per_head)
1804Tensor.permute op_8 1 1 21 22 dims=(1,0,2)
1805Tensor.permute op_9 1 1 23 24 dims=(1,0,2)
1806Tensor.permute op_10 1 1 25 26 dims=(1,0,2)
1807Tensor.reshape op_11 1 1 22 27 shape=(%batch,%num_heads,%size,%feat_per_head)
1808Tensor.reshape op_12 1 1 24 28 shape=(%batch,%num_heads,%size,%feat_per_head)
1809Tensor.reshape op_13 1 1 26 29 shape=(%batch,%num_heads,%size,%feat_per_head)
1810F.scaled_dot_product_attention op_14 3 1 27 28 29 35 dropout_p=0.000000e+00 is_causal=False
1811Tensor.permute op_15 1 1 35 36 dims=(2,0,1,3)
1812Tensor.reshape op_16 1 1 36 37 shape=(%size,%embed_dim)
1813nn.Linear out_proj 1 1 37 38 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1814Tensor.reshape op_18 1 1 38 out shape=(%size,%batch,%embed_dim)
1815pnnx.Output output 1 0 out
1816)PNNXIR";
1817 }
1818
1819 const char* replace_pattern_graph() const
1820 {
1821 return R"PNNXIR(7767517
18223 2
1823pnnx.Input input 0 1 input
1824nn.MultiheadAttention attention 1 1 input out embed_dim=%embed_dim kdim=%embed_dim vdim=%embed_dim num_heads=%num_heads batch_first=False add_zero_attn=False add_bias_kv=False
1825pnnx.Output output 1 0 out
1826)PNNXIR";
1827 }
1828
1829 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
1830 {
1831 const int embed_dim = captured_params.at("embed_dim").i;
1832 const int qkv_out_features = captured_params.at("qkv_out_features").i;
1833 const int num_heads = captured_params.at("num_heads").i;
1834 const int feat_per_head = captured_params.at("feat_per_head").i;
1835
1836 if (qkv_out_features != embed_dim * 3)
1837 return false;
1838
1839 if (embed_dim != num_heads * feat_per_head)
1840 return false;
1841
1842 return true;
1843 }
1844};
1845
1846class fuse_multiheadattention_pass_onnx_2 : public fuse_multiheadattention_pass
1847{
1848public:
1849 const char* match_pattern_graph() const
1850 {
1851 return R"PNNXIR(7767517
185224 23
1853pnnx.Input input_0 0 1 input
1854pnnx.Input input_1 0 1 attn_mask
1855nn.Linear op_0 1 1 input 15 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
1856Tensor.reshape op_1 1 1 15 16 shape=(%batch,%size,1,3,%embed_dim)
1857Tensor.permute op_2 1 1 16 17 dims=(3,1,2,0,4)
1858torch.squeeze op_3 1 1 17 18 dim=3
1859torch.unbind op_4 1 3 18 19 20 21 dim=0
1860Tensor.reshape op_5 1 1 19 23 shape=(%size,%num_heads,%feat_per_head)
1861Tensor.reshape op_7 1 1 20 25 shape=(%size,%num_heads,%feat_per_head)
1862Tensor.reshape op_8 1 1 21 26 shape=(%size,%num_heads,%feat_per_head)
1863Tensor.permute op_6 1 1 23 24 dims=(1,0,2)
1864Tensor.permute op_11 1 1 25 29 dims=(1,2,0)
1865Tensor.permute op_9 1 1 26 27 dims=(1,0,2)
1866pnnx.Expression op_10 1 1 24 28 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
1867torch.matmul op_12 2 1 28 29 30
1868torch.unsqueeze op_13 1 1 attn_mask 22 dim=0
1869pnnx.Expression op_14 2 1 30 22 31 expr=add(@0,@1)
1870F.softmax softmax 1 1 31 32 dim=%softmax_dim
1871torch.matmul op_16 2 1 32 27 33
1872Tensor.permute op_17 1 1 33 34 dims=(1,0,2)
1873Tensor.reshape op_18 1 1 34 35 shape=(%size,%embed_dim)
1874nn.Linear out_proj 1 1 35 36 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1875Tensor.reshape op_20 1 1 36 out shape=(%size,%batch,%embed_dim)
1876pnnx.Output output 1 0 out
1877)PNNXIR";
1878 }
1879
1880 const char* replace_pattern_graph() const
1881 {
1882 return R"PNNXIR(7767517
18834 3
1884pnnx.Input input_0 0 1 input
1885pnnx.Input input_1 0 1 attn_mask
1886nn.MultiheadAttention attention 2 1 input attn_mask out embed_dim=%embed_dim kdim=%embed_dim vdim=%embed_dim num_heads=%num_heads batch_first=False add_zero_attn=False add_bias_kv=False $attn_mask=attn_mask
1887pnnx.Output output 1 0 out
1888)PNNXIR";
1889 }
1890};
1891
1892class fuse_multiheadattention_pass_onnx_2_1 : public fuse_multiheadattention_pass_onnx_2
1893{
1894public:
1895 const char* match_pattern_graph() const
1896 {
1897 return R"PNNXIR(7767517
189824 23
1899pnnx.Input input_0 0 1 input
1900pnnx.Input input_1 0 1 attn_mask
1901nn.Linear op_0 1 1 input 15 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight
1902Tensor.reshape op_1 1 1 15 16 shape=(%batch,%size,1,3,%embed_dim)
1903Tensor.permute op_2 1 1 16 17 dims=(3,1,2,0,4)
1904torch.squeeze op_3 1 1 17 18 dim=3
1905torch.unbind op_4 1 3 18 19 20 21 dim=0
1906Tensor.reshape op_5 1 1 19 23 shape=(%size,%num_heads,%feat_per_head)
1907Tensor.reshape op_7 1 1 20 25 shape=(%size,%num_heads,%feat_per_head)
1908Tensor.reshape op_8 1 1 21 26 shape=(%size,%num_heads,%feat_per_head)
1909Tensor.permute op_6 1 1 23 24 dims=(1,0,2)
1910Tensor.permute op_11 1 1 25 29 dims=(1,2,0)
1911Tensor.permute op_9 1 1 26 27 dims=(1,0,2)
1912pnnx.Expression op_10 1 1 24 28 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
1913torch.matmul op_12 2 1 28 29 30
1914torch.unsqueeze op_13 1 1 attn_mask 22 dim=0
1915pnnx.Expression op_14 2 1 30 22 31 expr=add(@0,@1)
1916F.softmax softmax 1 1 31 32 dim=%softmax_dim
1917torch.matmul op_16 2 1 32 27 33
1918Tensor.permute op_17 1 1 33 34 dims=(1,0,2)
1919Tensor.reshape op_18 1 1 34 35 shape=(%size,%embed_dim)
1920nn.Linear out_proj 1 1 35 36 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1921Tensor.reshape op_20 1 1 36 out shape=(%size,%batch,%embed_dim)
1922pnnx.Output output 1 0 out
1923)PNNXIR";
1924 }
1925};
1926
1927class fuse_multiheadattention_pass_onnx_3 : public fuse_multiheadattention_pass_qkv
1928{
1929public:
1930 const char* match_pattern_graph() const
1931 {
1932 return R"PNNXIR(7767517
193325 24
1934pnnx.Input input_q 0 1 query
1935pnnx.Input input_kv 0 1 kv
1936nn.Linear op_0 1 1 query 14 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1937nn.Linear op_1 1 1 kv 15 bias=%kvbias in_features=%kvdim out_features=%kv_embed_dim @bias @weight
1938Tensor.reshape op_2 1 1 15 16 shape=(%batch,%kvsize,1,2,%embed_dim)
1939Tensor.permute op_3 1 1 16 17 dims=(3,1,2,0,4)
1940torch.squeeze op_4 1 1 17 18 dim=3
1941torch.unbind op_5 1 2 18 19 20 dim=0
1942Tensor.reshape op_6 1 1 14 21 shape=(%qsize,%num_heads,%feat_per_head)
1943Tensor.reshape op_7 1 1 19 23 shape=(%kvsize,%num_heads,%feat_per_head)
1944Tensor.reshape op_8 1 1 20 24 shape=(%kvsize,%num_heads,%feat_per_head)
1945Tensor.permute op_9 1 1 21 22 dims=(1,0,2)
1946Tensor.permute op_10 1 1 24 25 dims=(1,0,2)
1947Tensor.permute op_11 1 1 23 27 dims=(1,2,0)
1948pnnx.Expression op_12 1 1 22 26 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
1949torch.matmul op_13 2 1 26 27 28
1950F.softmax softmax 1 1 28 29 dim=%softmax_dim
1951torch.matmul op_15 2 1 29 25 30
1952Tensor.permute op_16 1 1 30 31 dims=(1,0,2)
1953Tensor.reshape op_17 1 1 31 32 shape=(%qsize,%embed_dim)
1954nn.Linear out_proj 1 1 32 33 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
1955Tensor.reshape op_19 1 1 33 out shape=(%qsize,1,%embed_dim)
1956Tensor.reshape op_20 1 1 29 35 shape=(1,%num_heads,%qsize,%kvsize)
1957torch.mean op_21 1 1 35 outweight dim=(1) keepdim=False
1958pnnx.Output output 2 0 out outweight
1959)PNNXIR";
1960 }
1961
1962 const char* replace_pattern_graph() const
1963 {
1964 return R"PNNXIR(7767517
19654 4
1966pnnx.Input input_0 0 1 query
1967pnnx.Input input_1 0 1 kv
1968nn.MultiheadAttention attention 2 2 query kv out outweight embed_dim=%embed_dim kdim=%kvdim vdim=%kvdim num_heads=%num_heads batch_first=False add_zero_attn=False add_bias_kv=False
1969pnnx.Output output 2 0 out outweight
1970)PNNXIR";
1971 }
1972
1973 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
1974 {
1975 GraphRewriterPass::write(ops, captured_params, captured_attrs);
1976
1977 Operator* op = ops.at("attention");
1978
1979 const int embed_dim = captured_params.at("embed_dim").i;
1980 const bool qbias = captured_params.at("qbias").b;
1981 const bool kvbias = captured_params.at("kvbias").b;
1982 const bool outbias = captured_params.at("outbias").b;
1983 const bool bias = qbias || kvbias || outbias;
1984
1985 op->params["bias"] = bias;
1986
1987 op->attrs["in_proj_weight"] = captured_attrs.at("op_0.weight") + captured_attrs.at("op_1.weight");
1988
1989 op->attrs["out_proj.weight"] = captured_attrs.at("out_proj.weight");
1990
1991 if (bias)
1992 {
1993 op->attrs["in_proj_bias"] = Attribute();
1994 op->attrs["in_proj_bias"].type = op->attrs["in_proj_weight"].type;
1995 op->attrs["in_proj_bias"].shape = {embed_dim * 3};
1996 // combine qkv bias
1997 std::vector<float> in_proj_bias(embed_dim * 3);
1998 {
1999 float* in_proj_bias_ptr = (float*)in_proj_bias.data();
2000 if (qbias)
2001 {
2002 auto qb = captured_attrs.at("op_0.bias").get_float32_data();
2003 memcpy(in_proj_bias_ptr, (const void*)qb.data(), embed_dim * sizeof(float));
2004 }
2005 else
2006 {
2007 memset(in_proj_bias_ptr, 0, embed_dim * sizeof(float));
2008 }
2009 in_proj_bias_ptr += embed_dim;
2010 if (kvbias)
2011 {
2012 auto kvb = captured_attrs.at("op_1.bias").get_float32_data();
2013 memcpy(in_proj_bias_ptr, (const void*)kvb.data(), embed_dim * 2 * sizeof(float));
2014 }
2015 else
2016 {
2017 memset(in_proj_bias_ptr, 0, embed_dim * 2 * sizeof(float));
2018 }
2019 }
2020 op->attrs["in_proj_bias"].set_float32_data(in_proj_bias);
2021
2022 if (outbias)
2023 {
2024 op->attrs["out_proj.bias"] = captured_attrs.at("out_proj.bias");
2025 }
2026 else
2027 {
2028 // init bias as zero
2029 op->attrs["out_proj.bias"] = Attribute();
2030 op->attrs["out_proj.bias"].type = op->attrs["out_proj.weight"].type;
2031 op->attrs["out_proj.bias"].shape = {embed_dim};
2032 op->attrs["out_proj.bias"].set_float32_data(std::vector<float>(embed_dim, 0.f));
2033 }
2034 }
2035 }
2036};
2037
2038class fuse_multiheadattention_pass_onnx_4 : public fuse_multiheadattention_pass_qkv
2039{
2040public:
2041 const char* match_pattern_graph() const
2042 {
2043 return R"PNNXIR(7767517
204426 25
2045pnnx.Input input_q 0 1 query
2046pnnx.Input input_k 0 1 key
2047pnnx.Input input_v 0 1 value
2048pnnx.Input input_3 0 1 attn_mask
2049nn.Linear op_0 1 1 query 20 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
2050nn.Linear op_1 1 1 key 21 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight
2051nn.Linear op_2 1 1 value 22 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight
2052Tensor.reshape op_3 1 1 20 24 shape=(%qsize,%num_heads,%feat_per_head)
2053Tensor.reshape op_4 1 1 21 26 shape=(%kvsize,%num_heads,%feat_per_head)
2054Tensor.reshape op_5 1 1 22 27 shape=(%kvsize,%num_heads,%feat_per_head)
2055Tensor.permute op_6 1 1 24 25 dims=(1,0,2)
2056Tensor.permute op_7 1 1 26 30 dims=(1,2,0)
2057Tensor.permute op_8 1 1 27 28 dims=(1,0,2)
2058pnnx.Expression op_9 1 1 25 29 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
2059torch.matmul op_10 2 1 29 30 31
2060torch.unsqueeze op_11 1 1 attn_mask 23 dim=0
2061pnnx.Expression op_12 2 1 31 23 32 expr=add(@0,@1)
2062F.softmax softmax 1 1 32 33 dim=%softmax_dim
2063torch.matmul op_14 2 1 33 28 34
2064Tensor.permute op_15 1 1 34 35 dims=(1,0,2)
2065Tensor.reshape op_16 1 1 35 36 shape=(%qsize,%embed_dim)
2066nn.Linear out_proj 1 1 36 37 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
2067Tensor.reshape op_18 1 1 37 out shape=(%qsize,%batch,%embed_dim)
2068Tensor.reshape op_19 1 1 33 39 shape=(%batch,%num_heads,%qsize,%kvsize)
2069torch.mean op_20 1 1 39 outweight dim=(1) keepdim=False
2070pnnx.Output output 2 0 out outweight
2071)PNNXIR";
2072 }
2073
2074 const char* replace_pattern_graph() const
2075 {
2076 return R"PNNXIR(7767517
20776 6
2078pnnx.Input input_0 0 1 query
2079pnnx.Input input_1 0 1 key
2080pnnx.Input input_2 0 1 value
2081pnnx.Input input_3 0 1 attn_mask
2082nn.MultiheadAttention attention 4 2 query key value attn_mask out outweight embed_dim=%embed_dim kdim=%kdim vdim=%vdim num_heads=%num_heads batch_first=False add_zero_attn=False add_bias_kv=False $attn_mask=attn_mask
2083pnnx.Output output 2 0 out outweight
2084)PNNXIR";
2085 }
2086};
2087
2088class fuse_multiheadattention_pass_onnx_4_1 : public fuse_multiheadattention_pass_onnx_4
2089{
2090public:
2091 const char* match_pattern_graph() const
2092 {
2093 return R"PNNXIR(7767517
209425 24
2095pnnx.Input input_q 0 1 query
2096pnnx.Input input_k 0 1 key
2097pnnx.Input input_v 0 1 value
2098pnnx.Input input_3 0 1 attn_mask
2099nn.Linear op_0 1 1 query 22 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight
2100nn.Linear op_1 1 1 key 23 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight
2101nn.Linear op_2 1 1 value 24 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight
2102Tensor.reshape op_3 1 1 22 25 shape=(%qsize,%num_heads,%feat_per_head)
2103Tensor.reshape op_4 1 1 23 27 shape=(%kvsize,%num_heads,%feat_per_head)
2104Tensor.reshape op_5 1 1 24 28 shape=(%kvsize,%num_heads,%feat_per_head)
2105Tensor.permute op_6 1 1 25 26 dims=(1,0,2)
2106Tensor.permute op_7 1 1 28 29 dims=(1,0,2)
2107Tensor.permute op_8 1 1 27 31 dims=(1,2,0)
2108pnnx.Expression op_9 1 1 26 30 expr=mul(@0,%inv_sqrt_embed_dim_per_head)
2109torch.matmul op_10 2 1 30 31 32
2110pnnx.Expression op_11 2 1 32 attn_mask 33 expr=add(@0,@1)
2111F.softmax softmax 1 1 33 34 dim=%softmax_dim
2112torch.matmul op_13 2 1 34 29 35
2113Tensor.permute op_14 1 1 35 36 dims=(1,0,2)
2114Tensor.reshape op_15 1 1 36 37 shape=(%qsize,%embed_dim)
2115nn.Linear out_proj 1 1 37 38 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight
2116Tensor.reshape op_16 1 1 38 out shape=(%qsize,%batch,%embed_dim)
2117Tensor.reshape op_18 1 1 34 40 shape=(%batch,%num_heads,%qsize,%kvsize)
2118torch.mean op_19 1 1 40 outweight dim=(1) keepdim=False
2119pnnx.Output output 2 0 out outweight
2120)PNNXIR";
2121 }
2122};
2123
2124void fuse_multiheadattention(Graph& graph)
2125{
2126#if TORCH_VERSION_MAJOR >= 2 || (TORCH_VERSION_MAJOR >= 1 && TORCH_VERSION_MINOR >= 9)
2127 fuse_multiheadattention_pass a;
2128 fuse_multiheadattention_pass_11 a1;
2129 fuse_multiheadattention_pass_11_0 a2;
2130 fuse_multiheadattention_pass_11_1 a3;
2131 fuse_multiheadattention_pass_sameqkv b;
2132 fuse_multiheadattention_pass_qkv c;
2133 fuse_multiheadattention_pass_q_samekv d;
2134 fuse_multiheadattention_pass_1 b1;
2135 fuse_multiheadattention_pass_1_1 b11;
2136 fuse_multiheadattention_pass_1_1_1 b111;
2137 fuse_multiheadattention_pass_1_2 b12;
2138 fuse_multiheadattention_pass_2 c1;
2139 fuse_multiheadattention_pass_3 d1;
2140 fuse_multiheadattention_pass_5 e;
2141 fuse_multiheadattention_pass_6 f;
2142 fuse_multiheadattention_pass_7 g;
2143 fuse_multiheadattention_pass_8 h;
2144 fuse_multiheadattention_pass_9 i;
2145 fuse_multiheadattention_pass_10 j;
2146 fuse_multiheadattention_pass_12 k;
2147 fuse_multiheadattention_pass_12_1 k1;
2148 fuse_multiheadattention_pass_13 l;
2149 fuse_multiheadattention_pass_14 m;
2150 fuse_multiheadattention_pass_15 n;
2151 fuse_multiheadattention_pass_16 o;
2152 fuse_multiheadattention_pass_16_1 o1;
2153 fuse_multiheadattention_pass_17 p;
2154 fuse_multiheadattention_pass_17_1 p1;
2155 fuse_multiheadattention_pass_18 q;
2156 fuse_multiheadattention_pass_18_1 q1;
2157
2158 fuse_multiheadattention_pass_onnx onnx0;
2159 fuse_multiheadattention_pass_onnx_1 onnx1;
2160 fuse_multiheadattention_pass_onnx_1_1 onnx1a;
2161 fuse_multiheadattention_pass_onnx_1_2 onnx1b;
2162 fuse_multiheadattention_pass_onnx_2 onnx2;
2163 fuse_multiheadattention_pass_onnx_3 onnx3;
2164 fuse_multiheadattention_pass_onnx_4 onnx4;
2165 fuse_multiheadattention_pass_onnx_4_1 onnx4a;
2166 int opindex = 0;
2167
2168 pnnx_graph_rewrite(graph, &a, opindex);
2169 pnnx_graph_rewrite(graph, &a1, opindex);
2170 pnnx_graph_rewrite(graph, &a2, opindex);
2171 pnnx_graph_rewrite(graph, &a3, opindex);
2172 pnnx_graph_rewrite(graph, &b, opindex);
2173 pnnx_graph_rewrite(graph, &c, opindex);
2174 pnnx_graph_rewrite(graph, &d, opindex);
2175 pnnx_graph_rewrite(graph, &b1, opindex);
2176 pnnx_graph_rewrite(graph, &b11, opindex);
2177 pnnx_graph_rewrite(graph, &b111, opindex);
2178 pnnx_graph_rewrite(graph, &b12, opindex);
2179 pnnx_graph_rewrite(graph, &c1, opindex);
2180 pnnx_graph_rewrite(graph, &d1, opindex);
2181 pnnx_graph_rewrite(graph, &e, opindex);
2182 pnnx_graph_rewrite(graph, &f, opindex);
2183 pnnx_graph_rewrite(graph, &g, opindex);
2184 pnnx_graph_rewrite(graph, &h, opindex);
2185 pnnx_graph_rewrite(graph, &i, opindex);
2186 pnnx_graph_rewrite(graph, &j, opindex);
2187 pnnx_graph_rewrite(graph, &k, opindex);
2188 pnnx_graph_rewrite(graph, &k1, opindex);
2189 pnnx_graph_rewrite(graph, &l, opindex);
2190 pnnx_graph_rewrite(graph, &m, opindex);
2191 pnnx_graph_rewrite(graph, &n, opindex);
2192 pnnx_graph_rewrite(graph, &o, opindex);
2193 pnnx_graph_rewrite(graph, &o1, opindex);
2194 pnnx_graph_rewrite(graph, &p, opindex);
2195 pnnx_graph_rewrite(graph, &p1, opindex);
2196 pnnx_graph_rewrite(graph, &q, opindex);
2197 pnnx_graph_rewrite(graph, &q1, opindex);
2198
2199 pnnx_graph_rewrite(graph, &onnx0, opindex);
2200 pnnx_graph_rewrite(graph, &onnx1, opindex);
2201 pnnx_graph_rewrite(graph, &onnx1a, opindex);
2202 pnnx_graph_rewrite(graph, &onnx1b, opindex);
2203 pnnx_graph_rewrite(graph, &onnx2, opindex);
2204 pnnx_graph_rewrite(graph, &onnx3, opindex);
2205 pnnx_graph_rewrite(graph, &onnx4, opindex);
2206 pnnx_graph_rewrite(graph, &onnx4a, opindex);
2207#endif
2208}
2209
2210} // namespace pnnx