| 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 | |
| 24 | namespace pnnx { |
| 25 | |
| 26 | static 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 | |
| 39 | class fuse_multiheadattention_pass : public GraphRewriterPass |
| 40 | { |
| 41 | public: |
| 42 | const char* match_pattern_graph() const |
| 43 | { |
| 44 | return R"PNNXIR(7767517 |
| 45 | 14 13 |
| 46 | pnnx.Input input 0 1 input |
| 47 | nn.Linear op_0 1 1 input 1 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 48 | Tensor.reshape op_1 1 1 1 2 shape=(%batch,%size,3,%num_heads,%feat_per_head) |
| 49 | Tensor.permute op_2 1 1 2 3 dims=(2,0,3,1,4) |
| 50 | torch.unbind op_3 1 3 3 4 5 6 dim=0 |
| 51 | pnnx.Expression op_4 1 1 4 7 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 52 | Tensor.permute op_5 1 1 5 8 dims=(0,1,3,2) |
| 53 | torch.matmul op_6 2 1 7 8 9 |
| 54 | F.softmax softmax 1 1 9 10 dim=%softmax_dim |
| 55 | torch.matmul op_8 2 1 10 6 11 |
| 56 | Tensor.permute op_9 1 1 11 12 dims=(0,2,1,3) |
| 57 | Tensor.reshape op_10 1 1 12 13 shape=(%batch,%size,%embed_dim) |
| 58 | nn.Linear out_proj 1 1 13 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 59 | pnnx.Output output 1 0 out |
| 60 | )PNNXIR"; |
| 61 | } |
| 62 | |
| 63 | const char* replace_pattern_graph() const |
| 64 | { |
| 65 | return R"PNNXIR(7767517 |
| 66 | 3 2 |
| 67 | pnnx.Input input 0 1 input |
| 68 | nn.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 |
| 69 | pnnx.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 | |
| 147 | class fuse_multiheadattention_pass_11 : public fuse_multiheadattention_pass |
| 148 | { |
| 149 | public: |
| 150 | const char* match_pattern_graph() const |
| 151 | { |
| 152 | return R"PNNXIR(7767517 |
| 153 | 18 17 |
| 154 | pnnx.Input input 0 1 input |
| 155 | nn.Linear op_0 1 1 input 1 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 156 | torch.chunk op_1 1 3 1 2 3 4 chunks=3 dim=-1 |
| 157 | Tensor.reshape op_2 1 1 2 5 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 158 | Tensor.reshape op_3 1 1 3 6 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 159 | Tensor.reshape op_4 1 1 4 7 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 160 | Tensor.permute op_5 1 1 6 8 dims=(0,2,1,3) |
| 161 | Tensor.permute op_6 1 1 5 9 dims=(0,2,1,3) |
| 162 | torch.transpose op_7 1 1 8 10 dim0=-1 dim1=-2 |
| 163 | torch.matmul op_8 2 1 9 10 11 |
| 164 | pnnx.Expression op_9 1 1 11 12 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 165 | nn.Softmax softmax 1 1 12 13 dim=%softmax_dim |
| 166 | Tensor.permute op_11 1 1 7 14 dims=(0,2,1,3) |
| 167 | torch.matmul op_12 2 1 13 14 15 |
| 168 | Tensor.permute op_13 1 1 15 16 dims=(0,2,1,3) |
| 169 | Tensor.reshape op_14 1 1 16 17 shape=(%batch,%size,%embed_dim) |
| 170 | nn.Linear out_proj 1 1 17 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 171 | pnnx.Output output 1 0 out |
| 172 | )PNNXIR"; |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | class fuse_multiheadattention_pass_11_0 : public fuse_multiheadattention_pass |
| 177 | { |
| 178 | public: |
| 179 | const char* match_pattern_graph() const |
| 180 | { |
| 181 | return R"PNNXIR(7767517 |
| 182 | 22 21 |
| 183 | pnnx.Input input 0 1 input |
| 184 | Tensor.permute op_1 1 1 input 13 dims=(1,0,2) |
| 185 | nn.Linear op_0 1 1 13 14 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 186 | Tensor.slice op_2 1 1 14 15 dim=-1 end=%embed_dim start=0 step=1 |
| 187 | Tensor.slice op_3 1 1 14 16 dim=-1 end=%embed_dim2 start=%embed_dim step=1 |
| 188 | Tensor.slice op_4 1 1 14 17 dim=-1 end=%qkv_out_features start=%embed_dim2 step=1 |
| 189 | Tensor.reshape op_5 1 1 15 18 shape=(%size,%num_heads,%feat_per_head) |
| 190 | Tensor.permute op_6 1 1 18 19 dims=(1,0,2) |
| 191 | Tensor.reshape op_7 1 1 16 20 shape=(%size,%num_heads,%feat_per_head) |
| 192 | Tensor.reshape op_8 1 1 17 21 shape=(%size,%num_heads,%feat_per_head) |
| 193 | Tensor.permute op_9 1 1 21 22 dims=(1,0,2) |
| 194 | pnnx.Expression op_10 1 1 19 23 expr=div(@0,%sqrt_embed_dim_per_head) |
| 195 | Tensor.permute op_11 1 1 20 24 dims=(1,2,0) |
| 196 | torch.matmul op_12 2 1 23 24 25 |
| 197 | F.softmax softmax 1 1 25 26 dim=%softmax_dim |
| 198 | torch.matmul op_14 2 1 26 22 27 |
| 199 | Tensor.permute op_15 1 1 27 28 dims=(1,0,2) |
| 200 | Tensor.reshape op_16 1 1 28 29 shape=(%size,%embed_dim) |
| 201 | nn.Linear out_proj 1 1 29 30 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 202 | Tensor.reshape op_24 1 1 30 31 shape=(%size,%batch,%embed_dim) |
| 203 | Tensor.permute op_25 1 1 31 out dims=(1,0,2) |
| 204 | pnnx.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 | |
| 235 | class fuse_multiheadattention_pass_11_1 : public fuse_multiheadattention_pass |
| 236 | { |
| 237 | public: |
| 238 | const char* match_pattern_graph() const |
| 239 | { |
| 240 | return R"PNNXIR(7767517 |
| 241 | 28 27 |
| 242 | pnnx.Input input 0 1 input |
| 243 | Tensor.permute op_1 1 1 input 9 dims=(1,0,2) |
| 244 | nn.Linear op_0 1 1 9 10 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 245 | Tensor.reshape op_2 1 1 10 11 shape=(1,%size,%batch,3,%embed_dim) |
| 246 | Tensor.permute op_3 1 1 11 12 dims=(3,1,2,0,4) |
| 247 | torch.squeeze op_4 1 1 12 13 dim=3 |
| 248 | torch.unbind op_5 1 3 13 14 15 16 dim=0 |
| 249 | Tensor.reshape op_6 1 1 14 17 shape=(%size,%num_heads,%feat_per_head) |
| 250 | Tensor.permute op_7 1 1 17 18 dims=(1,0,2) |
| 251 | Tensor.reshape op_8 1 1 15 19 shape=(%size,%num_heads,%feat_per_head) |
| 252 | Tensor.permute op_9 1 1 19 20 dims=(1,0,2) |
| 253 | Tensor.reshape op_10 1 1 16 21 shape=(%size,%num_heads,%feat_per_head) |
| 254 | Tensor.permute op_11 1 1 21 22 dims=(1,0,2) |
| 255 | Tensor.reshape op_12 1 1 18 23 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 256 | Tensor.reshape op_13 1 1 20 24 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 257 | Tensor.reshape op_14 1 1 22 25 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 258 | Tensor.permute op_15 1 1 24 26 dims=(0,1,3,2) |
| 259 | pnnx.Expression op_16 1 1 23 27 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 260 | pnnx.Expression op_17 1 1 26 28 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 261 | torch.matmul op_18 2 1 27 28 29 |
| 262 | F.softmax softmax 1 1 29 30 dim=%softmax_dim |
| 263 | torch.matmul op_20 2 1 30 25 31 |
| 264 | Tensor.permute op_21 1 1 31 32 dims=(2,0,1,3) |
| 265 | Tensor.reshape op_22 1 1 32 33 shape=(%size,%embed_dim) |
| 266 | nn.Linear out_proj 1 1 33 34 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 267 | Tensor.reshape op_24 1 1 34 35 shape=(%size,%batch,%embed_dim) |
| 268 | Tensor.permute op_25 1 1 35 out dims=(1,0,2) |
| 269 | pnnx.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 | |
| 299 | class fuse_multiheadattention_pass_sameqkv : public GraphRewriterPass |
| 300 | { |
| 301 | public: |
| 302 | const char* match_pattern_graph() const |
| 303 | { |
| 304 | return R"PNNXIR(7767517 |
| 305 | 23 22 |
| 306 | pnnx.Input input 0 1 input |
| 307 | nn.Linear op_0 1 1 input 31 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 308 | nn.Linear op_1 1 1 input 32 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 309 | nn.Linear op_2 1 1 input 33 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 310 | pnnx.Expression op_3 1 1 32 34 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 311 | Tensor.reshape op_4 1 1 31 35 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 312 | Tensor.reshape op_5 1 1 34 36 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 313 | Tensor.reshape op_6 1 1 33 37 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 314 | Tensor.permute op_7 1 1 36 38 dims=(0,2,1,3) |
| 315 | Tensor.reshape op_8 1 1 38 39 shape=(%num_heads,%size,%feat_per_head) |
| 316 | Tensor.permute op_9 1 1 35 40 dims=(0,2,1,3) |
| 317 | Tensor.reshape op_10 1 1 40 41 shape=(%num_heads,%size,%feat_per_head) |
| 318 | Tensor.permute op_11 1 1 39 42 dims=(0,2,1) |
| 319 | torch.matmul op_12 2 1 41 42 43 |
| 320 | F.softmax softmax 1 1 43 44 dim=%softmax_dim |
| 321 | Tensor.permute op_14 1 1 37 45 dims=(0,2,1,3) |
| 322 | Tensor.reshape op_15 1 1 45 46 shape=(%num_heads,%size,%feat_per_head) |
| 323 | torch.matmul op_16 2 1 44 46 47 |
| 324 | Tensor.reshape op_17 1 1 47 48 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 325 | Tensor.permute op_18 1 1 48 49 dims=(0,2,1,3) |
| 326 | Tensor.reshape op_19 1 1 49 50 shape=(%batch,%size,%embed_dim) |
| 327 | nn.Linear out_proj 1 1 50 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 328 | pnnx.Output output 1 0 out |
| 329 | )PNNXIR"; |
| 330 | } |
| 331 | |
| 332 | const char* replace_pattern_graph() const |
| 333 | { |
| 334 | return R"PNNXIR(7767517 |
| 335 | 3 2 |
| 336 | pnnx.Input input 0 1 input |
| 337 | nn.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 |
| 338 | pnnx.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 | |
| 439 | class fuse_multiheadattention_pass_qkv : public fuse_multiheadattention_pass_sameqkv |
| 440 | { |
| 441 | public: |
| 442 | const char* match_pattern_graph() const |
| 443 | { |
| 444 | return R"PNNXIR(7767517 |
| 445 | 25 24 |
| 446 | pnnx.Input input_q 0 1 query |
| 447 | pnnx.Input input_k 0 1 key |
| 448 | pnnx.Input input_v 0 1 value |
| 449 | nn.Linear op_0 1 1 query 32 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 450 | nn.Linear op_1 1 1 key 33 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight |
| 451 | nn.Linear op_2 1 1 value 34 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight |
| 452 | pnnx.Expression op_3 1 1 33 35 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 453 | Tensor.reshape op_4 1 1 32 36 shape=(%batch,%qsize,%num_heads,%feat_per_head) |
| 454 | Tensor.reshape op_5 1 1 35 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 455 | Tensor.reshape op_6 1 1 34 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 456 | Tensor.permute op_7 1 1 37 39 dims=(0,2,1,3) |
| 457 | Tensor.reshape op_8 1 1 39 40 shape=(%num_heads,%kvsize,%feat_per_head) |
| 458 | Tensor.permute op_9 1 1 36 41 dims=(0,2,1,3) |
| 459 | Tensor.reshape op_10 1 1 41 42 shape=(%num_heads,%qsize,%feat_per_head) |
| 460 | Tensor.permute op_11 1 1 40 43 dims=(0,2,1) |
| 461 | torch.matmul op_12 2 1 42 43 44 |
| 462 | F.softmax softmax 1 1 44 45 dim=%softmax_dim |
| 463 | Tensor.permute op_14 1 1 38 46 dims=(0,2,1,3) |
| 464 | Tensor.reshape op_15 1 1 46 47 shape=(%num_heads,%kvsize,%feat_per_head) |
| 465 | torch.matmul op_16 2 1 45 47 48 |
| 466 | Tensor.reshape op_17 1 1 48 49 shape=(%batch,%num_heads,%qsize,%feat_per_head) |
| 467 | Tensor.permute op_18 1 1 49 50 dims=(0,2,1,3) |
| 468 | Tensor.reshape op_19 1 1 50 51 shape=(%batch,%qsize,%embed_dim) |
| 469 | nn.Linear out_proj 1 1 51 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 470 | pnnx.Output output 1 0 out |
| 471 | )PNNXIR"; |
| 472 | } |
| 473 | |
| 474 | const char* replace_pattern_graph() const |
| 475 | { |
| 476 | return R"PNNXIR(7767517 |
| 477 | 5 4 |
| 478 | pnnx.Input input_0 0 1 query |
| 479 | pnnx.Input input_1 0 1 key |
| 480 | pnnx.Input input_2 0 1 value |
| 481 | nn.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 |
| 482 | pnnx.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 | |
| 575 | class fuse_multiheadattention_pass_q_samekv : public fuse_multiheadattention_pass_qkv |
| 576 | { |
| 577 | public: |
| 578 | const char* match_pattern_graph() const |
| 579 | { |
| 580 | return R"PNNXIR(7767517 |
| 581 | 24 23 |
| 582 | pnnx.Input input_q 0 1 query |
| 583 | pnnx.Input input_kv 0 1 kv |
| 584 | nn.Linear op_0 1 1 query 32 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 585 | nn.Linear op_1 1 1 kv 33 bias=%kbias in_features=%kvdim out_features=%embed_dim @bias @weight |
| 586 | nn.Linear op_2 1 1 kv 34 bias=%vbias in_features=%kvdim out_features=%embed_dim @bias @weight |
| 587 | pnnx.Expression op_3 1 1 33 35 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 588 | Tensor.reshape op_4 1 1 32 36 shape=(%batch,%qsize,%num_heads,%feat_per_head) |
| 589 | Tensor.reshape op_5 1 1 35 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 590 | Tensor.reshape op_6 1 1 34 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 591 | Tensor.permute op_7 1 1 37 39 dims=(0,2,1,3) |
| 592 | Tensor.reshape op_8 1 1 39 40 shape=(%num_heads,%kvsize,%feat_per_head) |
| 593 | Tensor.permute op_9 1 1 36 41 dims=(0,2,1,3) |
| 594 | Tensor.reshape op_10 1 1 41 42 shape=(%num_heads,%qsize,%feat_per_head) |
| 595 | Tensor.permute op_11 1 1 40 43 dims=(0,2,1) |
| 596 | torch.matmul op_12 2 1 42 43 44 |
| 597 | F.softmax softmax 1 1 44 45 dim=%softmax_dim |
| 598 | Tensor.permute op_14 1 1 38 46 dims=(0,2,1,3) |
| 599 | Tensor.reshape op_15 1 1 46 47 shape=(%num_heads,%kvsize,%feat_per_head) |
| 600 | torch.matmul op_16 2 1 45 47 48 |
| 601 | Tensor.reshape op_17 1 1 48 49 shape=(%batch,%num_heads,%qsize,%feat_per_head) |
| 602 | Tensor.permute op_18 1 1 49 50 dims=(0,2,1,3) |
| 603 | Tensor.reshape op_19 1 1 50 51 shape=(%batch,%qsize,%embed_dim) |
| 604 | nn.Linear out_proj 1 1 51 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 605 | pnnx.Output output 1 0 out |
| 606 | )PNNXIR"; |
| 607 | } |
| 608 | |
| 609 | const char* replace_pattern_graph() const |
| 610 | { |
| 611 | return R"PNNXIR(7767517 |
| 612 | 4 3 |
| 613 | pnnx.Input input_0 0 1 query |
| 614 | pnnx.Input input_1 0 1 kv |
| 615 | nn.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 |
| 616 | pnnx.Output output 1 0 out |
| 617 | )PNNXIR"; |
| 618 | } |
| 619 | }; |
| 620 | |
| 621 | class fuse_multiheadattention_pass_1 : public fuse_multiheadattention_pass_sameqkv |
| 622 | { |
| 623 | public: |
| 624 | const char* match_pattern_graph() const |
| 625 | { |
| 626 | return R"PNNXIR(7767517 |
| 627 | 22 21 |
| 628 | pnnx.Input input 0 1 input |
| 629 | nn.Linear op_0 1 1 input 31 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 630 | nn.Linear op_1 1 1 input 32 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight |
| 631 | nn.Linear op_2 1 1 input 33 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight |
| 632 | Tensor.reshape op_3 1 1 31 35 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 633 | Tensor.reshape op_4 1 1 32 36 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 634 | Tensor.reshape op_5 1 1 33 37 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 635 | Tensor.permute op_6 1 1 36 38 dims=(0,2,1,3) |
| 636 | Tensor.reshape op_7 1 1 38 39 shape=(%num_heads,%size,%feat_per_head) |
| 637 | Tensor.permute op_8 1 1 35 40 dims=(0,2,1,3) |
| 638 | Tensor.reshape op_9 1 1 40 41 shape=(%num_heads,%size,%feat_per_head) |
| 639 | torch.einsum op_10 2 1 41 39 42 equation=ijl,ikl->ijk |
| 640 | pnnx.Expression op_11 1 1 42 43 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 641 | F.softmax softmax 1 1 43 44 dim=%softmax_dim |
| 642 | Tensor.permute op_13 1 1 37 45 dims=(0,2,1,3) |
| 643 | Tensor.reshape op_14 1 1 45 46 shape=(%num_heads,%size,%feat_per_head) |
| 644 | torch.einsum op_15 2 1 44 46 47 equation=ijl,ilk->ijk |
| 645 | Tensor.reshape op_16 1 1 47 48 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 646 | Tensor.permute op_17 1 1 48 49 dims=(0,2,1,3) |
| 647 | Tensor.reshape op_18 1 1 49 50 shape=(%batch,%size,%embed_dim) |
| 648 | nn.Linear out_proj 1 1 50 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 649 | pnnx.Output output 1 0 out |
| 650 | )PNNXIR"; |
| 651 | } |
| 652 | }; |
| 653 | |
| 654 | class fuse_multiheadattention_pass_1_1 : public fuse_multiheadattention_pass_sameqkv |
| 655 | { |
| 656 | public: |
| 657 | const char* match_pattern_graph() const |
| 658 | { |
| 659 | return R"PNNXIR(7767517 |
| 660 | 19 18 |
| 661 | pnnx.Input input 0 1 input |
| 662 | nn.Linear op_0 1 1 input 47 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 663 | nn.Linear op_1 1 1 input 48 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 664 | nn.Linear op_2 1 1 input 49 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 665 | Tensor.reshape op_3 1 1 47 50 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 666 | Tensor.reshape op_4 1 1 48 51 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 667 | Tensor.reshape op_5 1 1 49 52 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 668 | torch.transpose op_6 1 1 51 53 dim0=1 dim1=2 |
| 669 | Tensor.permute op_7 1 1 53 54 dims=(0,1,3,2) |
| 670 | torch.transpose op_8 1 1 50 55 dim0=1 dim1=2 |
| 671 | torch.matmul op_9 2 1 55 54 56 |
| 672 | pnnx.Expression op_10 1 1 56 57 expr=div(@0,%sqrt_feat_per_head) |
| 673 | F.softmax softmax 1 1 57 58 dim=%softmax_dim |
| 674 | torch.transpose op_12 1 1 52 59 dim0=1 dim1=2 |
| 675 | torch.matmul op_13 2 1 58 59 60 |
| 676 | torch.transpose op_14 1 1 60 61 dim0=1 dim1=2 |
| 677 | Tensor.reshape op_15 1 1 61 62 shape=(%batch,%size,%embed_dim) |
| 678 | nn.Linear out_proj 1 1 62 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 679 | pnnx.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 | |
| 705 | class fuse_multiheadattention_pass_1_1_1 : public fuse_multiheadattention_pass_sameqkv |
| 706 | { |
| 707 | public: |
| 708 | const char* match_pattern_graph() const |
| 709 | { |
| 710 | return R"PNNXIR(7767517 |
| 711 | 19 18 |
| 712 | pnnx.Input input 0 1 input |
| 713 | nn.Linear op_0 1 1 input 256 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 714 | nn.Linear op_1 1 1 input 257 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 715 | nn.Linear op_2 1 1 input 260 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 716 | Tensor.view op_3 1 1 256 263 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 717 | Tensor.view op_4 1 1 257 258 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 718 | Tensor.view op_5 1 1 260 261 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 719 | Tensor.permute op_6 1 1 263 264 dims=(0,2,1,3) |
| 720 | Tensor.permute op_7 1 1 258 259 dims=(0,2,1,3) |
| 721 | Tensor.permute op_8 1 1 261 262 dims=(0,2,1,3) |
| 722 | torch.transpose op_9 1 1 259 265 dim0=-1 dim1=-2 |
| 723 | torch.matmul op_10 2 1 264 265 266 |
| 724 | pnnx.Expression op_11 1 1 266 267 expr=div(@0,%sqrt_feat_per_head) |
| 725 | F.softmax softmax 1 1 267 268 dim=%softmax_dim |
| 726 | torch.matmul op_13 2 1 268 262 269 |
| 727 | Tensor.permute op_14 1 1 269 270 dims=(0,2,1,3) |
| 728 | Tensor.reshape op_15 1 1 270 271 shape=(%batch,%size,%embed_dim) |
| 729 | nn.Linear out_proj 1 1 271 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 730 | pnnx.Output output 1 0 out |
| 731 | )PNNXIR"; |
| 732 | } |
| 733 | |
| 734 | bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const |
| 735 | { |
| 736 | const int embed_dim = captured_params.at("embed_dim").i; |
| 737 | const int num_heads = captured_params.at("num_heads").i; |
| 738 | const int feat_per_head = captured_params.at("feat_per_head").i; |
| 739 | const float sqrt_feat_per_head = captured_params.at("sqrt_feat_per_head").f; |
| 740 | const int softmax_dim = captured_params.at("softmax_dim").i; |
| 741 | |
| 742 | if (embed_dim != num_heads * feat_per_head) |
| 743 | return false; |
| 744 | |
| 745 | if (!NearlyEqual(sqrt_feat_per_head, sqrt(feat_per_head), 0.001)) |
| 746 | return false; |
| 747 | |
| 748 | int softmax_input_rank = (int)matched_operators.at("softmax")->inputs[0]->shape.size(); |
| 749 | if (softmax_dim != -1 && softmax_dim != softmax_input_rank - 1) |
| 750 | return false; |
| 751 | |
| 752 | return true; |
| 753 | } |
| 754 | }; |
| 755 | |
| 756 | class fuse_multiheadattention_pass_1_2 : public fuse_multiheadattention_pass_qkv |
| 757 | { |
| 758 | public: |
| 759 | const char* match_pattern_graph() const |
| 760 | { |
| 761 | return R"PNNXIR(7767517 |
| 762 | 21 20 |
| 763 | pnnx.Input input_0 0 1 query #query=(%batch,%qsize,%embed_dim)f32 |
| 764 | pnnx.Input input_1 0 1 key #key=(%batch,%kvsize,%kdim)f32 |
| 765 | pnnx.Input input_2 0 1 value #value=(%batch,%kvsize,%kdim)f32 |
| 766 | nn.Linear op_0 1 1 query 47 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 767 | nn.Linear op_1 1 1 key 48 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight |
| 768 | nn.Linear op_2 1 1 value 49 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight |
| 769 | Tensor.reshape op_3 1 1 47 50 shape=(%batch,%qsize,%num_heads,%feat_per_head) |
| 770 | Tensor.reshape op_4 1 1 48 51 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 771 | Tensor.reshape op_5 1 1 49 52 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 772 | torch.transpose op_6 1 1 51 53 dim0=1 dim1=2 |
| 773 | Tensor.permute op_7 1 1 53 54 dims=(0,1,3,2) |
| 774 | torch.transpose op_8 1 1 50 55 dim0=1 dim1=2 |
| 775 | torch.matmul op_9 2 1 55 54 56 |
| 776 | pnnx.Expression op_10 1 1 56 57 expr=div(@0,%sqrt_feat_per_head) |
| 777 | F.softmax softmax 1 1 57 58 dim=%softmax_dim |
| 778 | torch.transpose op_12 1 1 52 59 dim0=1 dim1=2 |
| 779 | torch.matmul op_13 2 1 58 59 60 |
| 780 | torch.transpose op_14 1 1 60 61 dim0=1 dim1=2 |
| 781 | Tensor.reshape op_15 1 1 61 62 shape=(%batch,%qsize,%embed_dim) |
| 782 | nn.Linear out_proj 1 1 62 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 783 | pnnx.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 | |
| 809 | class fuse_multiheadattention_pass_2 : public fuse_multiheadattention_pass_qkv |
| 810 | { |
| 811 | public: |
| 812 | const char* match_pattern_graph() const |
| 813 | { |
| 814 | return R"PNNXIR(7767517 |
| 815 | 24 23 |
| 816 | pnnx.Input input_q 0 1 query |
| 817 | pnnx.Input input_k 0 1 key |
| 818 | pnnx.Input input_v 0 1 value |
| 819 | nn.Linear op_0 1 1 query 32 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 820 | nn.Linear op_1 1 1 key 33 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight |
| 821 | nn.Linear op_2 1 1 value 34 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight |
| 822 | Tensor.reshape op_3 1 1 32 36 shape=(%batch,%qsize,%num_heads,%feat_per_head) |
| 823 | Tensor.reshape op_4 1 1 33 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 824 | Tensor.reshape op_5 1 1 34 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 825 | Tensor.permute op_6 1 1 37 39 dims=(0,2,1,3) |
| 826 | Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%kvsize,%feat_per_head) |
| 827 | Tensor.permute op_8 1 1 36 41 dims=(0,2,1,3) |
| 828 | Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%qsize,%feat_per_head) |
| 829 | torch.einsum op_10 2 1 42 40 43 equation=ijl,ikl->ijk |
| 830 | pnnx.Expression op_11 1 1 43 44 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 831 | F.softmax softmax 1 1 44 45 dim=%softmax_dim |
| 832 | Tensor.permute op_13 1 1 38 46 dims=(0,2,1,3) |
| 833 | Tensor.reshape op_14 1 1 46 47 shape=(%num_heads,%kvsize,%feat_per_head) |
| 834 | torch.einsum op_15 2 1 45 47 48 equation=ijl,ilk->ijk |
| 835 | Tensor.reshape op_16 1 1 48 49 shape=(%batch,%num_heads,%qsize,%feat_per_head) |
| 836 | Tensor.permute op_17 1 1 49 50 dims=(0,2,1,3) |
| 837 | Tensor.reshape op_18 1 1 50 51 shape=(%batch,%qsize,%embed_dim) |
| 838 | nn.Linear out_proj 1 1 51 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 839 | pnnx.Output output 1 0 out |
| 840 | )PNNXIR"; |
| 841 | } |
| 842 | }; |
| 843 | |
| 844 | class fuse_multiheadattention_pass_3 : public fuse_multiheadattention_pass_q_samekv |
| 845 | { |
| 846 | public: |
| 847 | const char* match_pattern_graph() const |
| 848 | { |
| 849 | return R"PNNXIR(7767517 |
| 850 | 23 22 |
| 851 | pnnx.Input input_q 0 1 query |
| 852 | pnnx.Input input_kv 0 1 kv |
| 853 | nn.Linear op_0 1 1 query 32 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 854 | nn.Linear op_1 1 1 kv 33 bias=%kbias in_features=%kvdim out_features=%embed_dim @bias @weight |
| 855 | nn.Linear op_2 1 1 kv 34 bias=%vbias in_features=%kvdim out_features=%embed_dim @bias @weight |
| 856 | Tensor.reshape op_3 1 1 32 36 shape=(%batch,%qsize,%num_heads,%feat_per_head) |
| 857 | Tensor.reshape op_4 1 1 33 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 858 | Tensor.reshape op_5 1 1 34 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 859 | Tensor.permute op_6 1 1 37 39 dims=(0,2,1,3) |
| 860 | Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%kvsize,%feat_per_head) |
| 861 | Tensor.permute op_8 1 1 36 41 dims=(0,2,1,3) |
| 862 | Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%qsize,%feat_per_head) |
| 863 | torch.einsum op_10 2 1 42 40 43 equation=ijl,ikl->ijk |
| 864 | pnnx.Expression op_11 1 1 43 44 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 865 | F.softmax softmax 1 1 44 45 dim=%softmax_dim |
| 866 | Tensor.permute op_13 1 1 38 46 dims=(0,2,1,3) |
| 867 | Tensor.reshape op_14 1 1 46 47 shape=(%num_heads,%kvsize,%feat_per_head) |
| 868 | torch.einsum op_15 2 1 45 47 48 equation=ijl,ilk->ijk |
| 869 | Tensor.reshape op_16 1 1 48 49 shape=(%batch,%num_heads,%qsize,%feat_per_head) |
| 870 | Tensor.permute op_17 1 1 49 50 dims=(0,2,1,3) |
| 871 | Tensor.reshape op_18 1 1 50 51 shape=(%batch,%qsize,%embed_dim) |
| 872 | nn.Linear out_proj 1 1 51 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 873 | pnnx.Output output 1 0 out |
| 874 | )PNNXIR"; |
| 875 | } |
| 876 | }; |
| 877 | |
| 878 | class fuse_multiheadattention_pass_5 : public fuse_multiheadattention_pass_sameqkv |
| 879 | { |
| 880 | public: |
| 881 | const char* match_pattern_graph() const |
| 882 | { |
| 883 | return R"PNNXIR(7767517 |
| 884 | 23 22 |
| 885 | pnnx.Input input 0 1 input |
| 886 | nn.Linear op_0 1 1 input 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 887 | nn.Linear op_1 1 1 input 34 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 888 | nn.Linear op_2 1 1 input 35 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 889 | Tensor.reshape op_3 1 1 33 36 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 890 | Tensor.reshape op_4 1 1 34 37 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 891 | Tensor.reshape op_5 1 1 35 38 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 892 | Tensor.permute op_6 1 1 36 39 dims=(0,2,1,3) |
| 893 | Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%size,%feat_per_head) |
| 894 | Tensor.permute op_8 1 1 37 41 dims=(0,2,1,3) |
| 895 | Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%size,%feat_per_head) |
| 896 | pnnx.Attribute op_10 0 1 43 @data |
| 897 | torch.transpose op_11 1 1 42 44 dim0=-1 dim1=-2 |
| 898 | torch.baddbmm op_12 3 1 43 40 44 45 alpha=%inv_sqrt_embed_dim_per_head beta=0 |
| 899 | F.softmax softmax 1 1 45 46 dim=%softmax_dim |
| 900 | Tensor.permute op_14 1 1 38 47 dims=(0,2,1,3) |
| 901 | Tensor.reshape op_15 1 1 47 48 shape=(%num_heads,%size,%feat_per_head) |
| 902 | torch.bmm op_16 2 1 46 48 49 |
| 903 | Tensor.reshape op_17 1 1 49 50 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 904 | Tensor.permute op_18 1 1 50 51 dims=(0,2,1,3) |
| 905 | Tensor.reshape op_19 1 1 51 52 shape=(%batch,%size,%embed_dim) |
| 906 | nn.Linear out_proj 1 1 52 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 907 | pnnx.Output output 1 0 out |
| 908 | )PNNXIR"; |
| 909 | } |
| 910 | |
| 911 | // TODO match data zero |
| 912 | }; |
| 913 | |
| 914 | class fuse_multiheadattention_pass_6 : public fuse_multiheadattention_pass_sameqkv |
| 915 | { |
| 916 | public: |
| 917 | const char* match_pattern_graph() const |
| 918 | { |
| 919 | return R"PNNXIR(7767517 |
| 920 | 24 23 |
| 921 | pnnx.Input input 0 1 input |
| 922 | nn.Linear op_0 1 1 input 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 923 | nn.Linear op_1 1 1 input 34 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 924 | nn.Linear op_2 1 1 input 35 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 925 | Tensor.reshape op_3 1 1 33 36 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 926 | Tensor.reshape op_4 1 1 34 37 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 927 | Tensor.reshape op_5 1 1 35 38 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 928 | Tensor.permute op_6 1 1 36 39 dims=(0,2,1,3) |
| 929 | Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%size,%feat_per_head) |
| 930 | Tensor.permute op_8 1 1 37 41 dims=(0,2,1,3) |
| 931 | Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%size,%feat_per_head) |
| 932 | pnnx.Expression op_10 2 1 40 42 43 expr=%expr_zero_shape |
| 933 | torch.empty op_11 1 1 43 zeros |
| 934 | torch.transpose op_12 1 1 42 44 dim0=-1 dim1=-2 |
| 935 | torch.baddbmm op_13 3 1 zeros 40 44 45 alpha=%inv_sqrt_embed_dim_per_head beta=0 |
| 936 | F.softmax softmax 1 1 45 46 dim=%softmax_dim |
| 937 | Tensor.permute op_15 1 1 38 47 dims=(0,2,1,3) |
| 938 | Tensor.reshape op_16 1 1 47 48 shape=(%num_heads,%size,%feat_per_head) |
| 939 | torch.bmm op_17 2 1 46 48 49 |
| 940 | Tensor.reshape op_18 1 1 49 50 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 941 | Tensor.permute op_19 1 1 50 51 dims=(0,2,1,3) |
| 942 | Tensor.reshape op_20 1 1 51 52 shape=(%batch,%size,%embed_dim) |
| 943 | nn.Linear out_proj 1 1 52 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 944 | pnnx.Output output 1 0 out |
| 945 | )PNNXIR"; |
| 946 | } |
| 947 | |
| 948 | // TODO match expr_zero_shape |
| 949 | }; |
| 950 | |
| 951 | class fuse_multiheadattention_pass_7 : public fuse_multiheadattention_pass_qkv |
| 952 | { |
| 953 | public: |
| 954 | const char* match_pattern_graph() const |
| 955 | { |
| 956 | return R"PNNXIR(7767517 |
| 957 | 25 24 |
| 958 | pnnx.Input input_q 0 1 query |
| 959 | pnnx.Input input_k 0 1 key |
| 960 | pnnx.Input input_v 0 1 value |
| 961 | nn.Linear op_0 1 1 query 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 962 | nn.Linear op_1 1 1 key 34 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight |
| 963 | nn.Linear op_2 1 1 value 35 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight |
| 964 | Tensor.reshape op_3 1 1 33 36 shape=(%batch,%qsize,%num_heads,%feat_per_head) |
| 965 | Tensor.reshape op_4 1 1 34 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 966 | Tensor.reshape op_5 1 1 35 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 967 | Tensor.permute op_6 1 1 36 39 dims=(0,2,1,3) |
| 968 | Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%qsize,%feat_per_head) |
| 969 | Tensor.permute op_8 1 1 37 41 dims=(0,2,1,3) |
| 970 | Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%kvsize,%feat_per_head) |
| 971 | pnnx.Attribute op_10 0 1 43 @data |
| 972 | torch.transpose op_11 1 1 42 44 dim0=-1 dim1=-2 |
| 973 | torch.baddbmm op_12 3 1 43 40 44 45 alpha=%inv_sqrt_embed_dim_per_head beta=0 |
| 974 | F.softmax softmax 1 1 45 46 dim=%softmax_dim |
| 975 | Tensor.permute op_14 1 1 38 47 dims=(0,2,1,3) |
| 976 | Tensor.reshape op_15 1 1 47 48 shape=(%num_heads,%kvsize,%feat_per_head) |
| 977 | torch.bmm op_16 2 1 46 48 49 |
| 978 | Tensor.reshape op_17 1 1 49 50 shape=(%batch,%num_heads,%qsize,%feat_per_head) |
| 979 | Tensor.permute op_18 1 1 50 51 dims=(0,2,1,3) |
| 980 | Tensor.reshape op_19 1 1 51 52 shape=(%batch,%qsize,%embed_dim) |
| 981 | nn.Linear out_proj 1 1 52 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 982 | pnnx.Output output 1 0 out |
| 983 | )PNNXIR"; |
| 984 | } |
| 985 | |
| 986 | // TODO match data zero |
| 987 | }; |
| 988 | |
| 989 | class fuse_multiheadattention_pass_8 : public fuse_multiheadattention_pass_q_samekv |
| 990 | { |
| 991 | public: |
| 992 | const char* match_pattern_graph() const |
| 993 | { |
| 994 | return R"PNNXIR(7767517 |
| 995 | 24 23 |
| 996 | pnnx.Input input_q 0 1 query |
| 997 | pnnx.Input input_kv 0 1 kv |
| 998 | nn.Linear op_0 1 1 query 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 999 | nn.Linear op_1 1 1 kv 34 bias=%kbias in_features=%kvdim out_features=%embed_dim @bias @weight |
| 1000 | nn.Linear op_2 1 1 kv 35 bias=%vbias in_features=%kvdim out_features=%embed_dim @bias @weight |
| 1001 | Tensor.reshape op_3 1 1 33 36 shape=(%batch,%qsize,%num_heads,%feat_per_head) |
| 1002 | Tensor.reshape op_4 1 1 34 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 1003 | Tensor.reshape op_5 1 1 35 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 1004 | Tensor.permute op_6 1 1 36 39 dims=(0,2,1,3) |
| 1005 | Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%qsize,%feat_per_head) |
| 1006 | Tensor.permute op_8 1 1 37 41 dims=(0,2,1,3) |
| 1007 | Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%kvsize,%feat_per_head) |
| 1008 | pnnx.Attribute op_10 0 1 43 @data |
| 1009 | torch.transpose op_11 1 1 42 44 dim0=-1 dim1=-2 |
| 1010 | torch.baddbmm op_12 3 1 43 40 44 45 alpha=%inv_sqrt_embed_dim_per_head beta=0 |
| 1011 | F.softmax softmax 1 1 45 46 dim=%softmax_dim |
| 1012 | Tensor.permute op_14 1 1 38 47 dims=(0,2,1,3) |
| 1013 | Tensor.reshape op_15 1 1 47 48 shape=(%num_heads,%kvsize,%feat_per_head) |
| 1014 | torch.bmm op_16 2 1 46 48 49 |
| 1015 | Tensor.reshape op_17 1 1 49 50 shape=(%batch,%num_heads,%qsize,%feat_per_head) |
| 1016 | Tensor.permute op_18 1 1 50 51 dims=(0,2,1,3) |
| 1017 | Tensor.reshape op_19 1 1 51 52 shape=(%batch,%qsize,%embed_dim) |
| 1018 | nn.Linear out_proj 1 1 52 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1019 | pnnx.Output output 1 0 out |
| 1020 | )PNNXIR"; |
| 1021 | } |
| 1022 | |
| 1023 | // TODO match data zero |
| 1024 | }; |
| 1025 | |
| 1026 | class fuse_multiheadattention_pass_9 : public fuse_multiheadattention_pass_qkv |
| 1027 | { |
| 1028 | public: |
| 1029 | const char* match_pattern_graph() const |
| 1030 | { |
| 1031 | return R"PNNXIR(7767517 |
| 1032 | 26 25 |
| 1033 | pnnx.Input input_q 0 1 query |
| 1034 | pnnx.Input input_k 0 1 key |
| 1035 | pnnx.Input input_v 0 1 value |
| 1036 | nn.Linear op_0 1 1 query 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1037 | nn.Linear op_1 1 1 key 34 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight |
| 1038 | nn.Linear op_2 1 1 value 35 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight |
| 1039 | Tensor.reshape op_3 1 1 33 36 shape=(%batch,%qsize,%num_heads,%feat_per_head) |
| 1040 | Tensor.reshape op_4 1 1 34 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 1041 | Tensor.reshape op_5 1 1 35 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 1042 | Tensor.permute op_6 1 1 36 39 dims=(0,2,1,3) |
| 1043 | Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%qsize,%feat_per_head) |
| 1044 | Tensor.permute op_8 1 1 37 41 dims=(0,2,1,3) |
| 1045 | Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%kvsize,%feat_per_head) |
| 1046 | pnnx.Expression op_10 1 1 40 43 expr=%expr_zero_shape |
| 1047 | torch.empty op_11 1 1 43 zeros |
| 1048 | torch.transpose op_12 1 1 42 44 dim0=-1 dim1=-2 |
| 1049 | torch.baddbmm op_13 3 1 zeros 40 44 45 alpha=%inv_sqrt_embed_dim_per_head beta=0 |
| 1050 | F.softmax softmax 1 1 45 46 dim=%softmax_dim |
| 1051 | Tensor.permute op_15 1 1 38 47 dims=(0,2,1,3) |
| 1052 | Tensor.reshape op_16 1 1 47 48 shape=(%num_heads,%kvsize,%feat_per_head) |
| 1053 | torch.bmm op_17 2 1 46 48 49 |
| 1054 | Tensor.reshape op_18 1 1 49 50 shape=(%batch,%num_heads,%qsize,%feat_per_head) |
| 1055 | Tensor.permute op_19 1 1 50 51 dims=(0,2,1,3) |
| 1056 | Tensor.reshape op_20 1 1 51 52 shape=(%batch,%qsize,%embed_dim) |
| 1057 | nn.Linear out_proj 1 1 52 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1058 | pnnx.Output output 1 0 out |
| 1059 | )PNNXIR"; |
| 1060 | } |
| 1061 | |
| 1062 | // TODO match expr_zero_shape |
| 1063 | }; |
| 1064 | |
| 1065 | class fuse_multiheadattention_pass_10 : public fuse_multiheadattention_pass_q_samekv |
| 1066 | { |
| 1067 | public: |
| 1068 | const char* match_pattern_graph() const |
| 1069 | { |
| 1070 | return R"PNNXIR(7767517 |
| 1071 | 25 24 |
| 1072 | pnnx.Input input_q 0 1 query |
| 1073 | pnnx.Input input_kv 0 1 kv |
| 1074 | nn.Linear op_0 1 1 query 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1075 | nn.Linear op_1 1 1 kv 34 bias=%kbias in_features=%kvdim out_features=%embed_dim @bias @weight |
| 1076 | nn.Linear op_2 1 1 kv 35 bias=%vbias in_features=%kvdim out_features=%embed_dim @bias @weight |
| 1077 | Tensor.reshape op_3 1 1 33 36 shape=(%batch,%qsize,%num_heads,%feat_per_head) |
| 1078 | Tensor.reshape op_4 1 1 34 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 1079 | Tensor.reshape op_5 1 1 35 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 1080 | Tensor.permute op_6 1 1 36 39 dims=(0,2,1,3) |
| 1081 | Tensor.reshape op_7 1 1 39 40 shape=(%num_heads,%qsize,%feat_per_head) |
| 1082 | Tensor.permute op_8 1 1 37 41 dims=(0,2,1,3) |
| 1083 | Tensor.reshape op_9 1 1 41 42 shape=(%num_heads,%kvsize,%feat_per_head) |
| 1084 | pnnx.Expression op_10 1 1 40 43 expr=%expr_zero_shape |
| 1085 | torch.empty op_11 1 1 43 zeros |
| 1086 | torch.transpose op_12 1 1 42 44 dim0=-1 dim1=-2 |
| 1087 | torch.baddbmm op_13 3 1 zeros 40 44 45 alpha=%alpha beta=0 |
| 1088 | F.softmax softmax 1 1 45 46 dim=%softmax_dim |
| 1089 | Tensor.permute op_15 1 1 38 47 dims=(0,2,1,3) |
| 1090 | Tensor.reshape op_16 1 1 47 48 shape=(%num_heads,%kvsize,%feat_per_head) |
| 1091 | torch.bmm op_17 2 1 46 48 49 |
| 1092 | Tensor.reshape op_18 1 1 49 50 shape=(%batch,%num_heads,%qsize,%feat_per_head) |
| 1093 | Tensor.permute op_19 1 1 50 51 dims=(0,2,1,3) |
| 1094 | Tensor.reshape op_20 1 1 51 52 shape=(%batch,%qsize,%embed_dim) |
| 1095 | nn.Linear out_proj 1 1 52 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1096 | pnnx.Output output 1 0 out |
| 1097 | )PNNXIR"; |
| 1098 | } |
| 1099 | }; |
| 1100 | |
| 1101 | class fuse_multiheadattention_pass_12 : public fuse_multiheadattention_pass_sameqkv |
| 1102 | { |
| 1103 | public: |
| 1104 | const char* match_pattern_graph() const |
| 1105 | { |
| 1106 | return R"PNNXIR(7767517 |
| 1107 | 15 14 |
| 1108 | pnnx.Input input_0 0 1 input |
| 1109 | nn.Linear op_0 1 1 input 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1110 | nn.Linear op_1 1 1 input 34 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1111 | nn.Linear op_2 1 1 input 35 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1112 | Tensor.view op_3 1 1 33 36 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1113 | Tensor.view op_4 1 1 34 37 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1114 | Tensor.view op_5 1 1 35 38 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1115 | torch.transpose op_6 1 1 38 39 dim0=1 dim1=2 |
| 1116 | torch.transpose op_7 1 1 37 40 dim0=1 dim1=2 |
| 1117 | torch.transpose op_8 1 1 36 41 dim0=1 dim1=2 |
| 1118 | F.scaled_dot_product_attention op_9 3 1 41 40 39 42 attn_mask=None dropout_p=0.000000e+00 is_causal=False |
| 1119 | torch.transpose op_10 1 1 42 43 dim0=1 dim1=2 |
| 1120 | Tensor.reshape op_11 1 1 43 44 shape=(%batch,%size,%embed_dim) |
| 1121 | nn.Linear out_proj 1 1 44 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1122 | pnnx.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 | |
| 1139 | class fuse_multiheadattention_pass_12_1 : public fuse_multiheadattention_pass_12 |
| 1140 | { |
| 1141 | public: |
| 1142 | const char* match_pattern_graph() const |
| 1143 | { |
| 1144 | return R"PNNXIR(7767517 |
| 1145 | 15 14 |
| 1146 | pnnx.Input input_0 0 1 input |
| 1147 | nn.Linear op_0 1 1 input 14 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1148 | nn.Linear op_1 1 1 input 15 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1149 | nn.Linear op_2 1 1 input 16 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1150 | Tensor.reshape op_3 1 1 14 17 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1151 | Tensor.reshape op_4 1 1 15 18 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1152 | Tensor.reshape op_5 1 1 16 19 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1153 | Tensor.permute op_6 1 1 19 20 dims=(0,2,1,3) |
| 1154 | Tensor.permute op_7 1 1 18 21 dims=(0,2,1,3) |
| 1155 | Tensor.permute op_8 1 1 17 22 dims=(0,2,1,3) |
| 1156 | F.scaled_dot_product_attention op_9 3 1 22 21 20 23 attn_mask=None dropout_p=0.000000e+00 is_causal=False |
| 1157 | Tensor.permute op_10 1 1 23 24 dims=(0,2,1,3) |
| 1158 | Tensor.reshape op_11 1 1 24 25 shape=(%batch,%size,%embed_dim) |
| 1159 | nn.Linear out_proj 1 1 25 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1160 | pnnx.Output output 1 0 out |
| 1161 | )PNNXIR"; |
| 1162 | } |
| 1163 | }; |
| 1164 | |
| 1165 | class fuse_multiheadattention_pass_13 : public fuse_multiheadattention_pass_qkv |
| 1166 | { |
| 1167 | public: |
| 1168 | const char* match_pattern_graph() const |
| 1169 | { |
| 1170 | return R"PNNXIR(7767517 |
| 1171 | 17 16 |
| 1172 | pnnx.Input input_0 0 1 query |
| 1173 | pnnx.Input input_1 0 1 key |
| 1174 | pnnx.Input input_2 0 1 value |
| 1175 | nn.Linear op_0 1 1 query 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1176 | nn.Linear op_1 1 1 key 34 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight |
| 1177 | nn.Linear op_2 1 1 value 35 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight |
| 1178 | Tensor.view op_3 1 1 33 36 shape=(%batch,%qsize,%num_heads,%feat_per_head) |
| 1179 | Tensor.view op_4 1 1 34 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 1180 | Tensor.view op_5 1 1 35 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 1181 | torch.transpose op_6 1 1 38 39 dim0=1 dim1=2 |
| 1182 | torch.transpose op_7 1 1 37 40 dim0=1 dim1=2 |
| 1183 | torch.transpose op_8 1 1 36 41 dim0=1 dim1=2 |
| 1184 | F.scaled_dot_product_attention op_9 3 1 41 40 39 42 attn_mask=None dropout_p=0.000000e+00 is_causal=False |
| 1185 | torch.transpose op_10 1 1 42 43 dim0=1 dim1=2 |
| 1186 | Tensor.reshape op_11 1 1 43 44 shape=(%batch,%qsize,%embed_dim) |
| 1187 | nn.Linear out_proj 1 1 44 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1188 | pnnx.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 | |
| 1205 | class fuse_multiheadattention_pass_14 : public fuse_multiheadattention_pass_q_samekv |
| 1206 | { |
| 1207 | public: |
| 1208 | const char* match_pattern_graph() const |
| 1209 | { |
| 1210 | return R"PNNXIR(7767517 |
| 1211 | 16 15 |
| 1212 | pnnx.Input input_0 0 1 query |
| 1213 | pnnx.Input input_1 0 1 kv |
| 1214 | nn.Linear op_0 1 1 query 33 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1215 | nn.Linear op_1 1 1 kv 34 bias=%kbias in_features=%kvdim out_features=%embed_dim @bias @weight |
| 1216 | nn.Linear op_2 1 1 kv 35 bias=%vbias in_features=%kvdim out_features=%embed_dim @bias @weight |
| 1217 | Tensor.view op_3 1 1 33 36 shape=(%batch,%qsize,%num_heads,%feat_per_head) |
| 1218 | Tensor.view op_4 1 1 34 37 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 1219 | Tensor.view op_5 1 1 35 38 shape=(%batch,%kvsize,%num_heads,%feat_per_head) |
| 1220 | torch.transpose op_6 1 1 38 39 dim0=1 dim1=2 |
| 1221 | torch.transpose op_7 1 1 37 40 dim0=1 dim1=2 |
| 1222 | torch.transpose op_8 1 1 36 41 dim0=1 dim1=2 |
| 1223 | F.scaled_dot_product_attention op_9 3 1 41 40 39 42 attn_mask=None dropout_p=0.000000e+00 is_causal=False |
| 1224 | torch.transpose op_10 1 1 42 43 dim0=1 dim1=2 |
| 1225 | Tensor.reshape op_11 1 1 43 44 shape=(%batch,%qsize,%embed_dim) |
| 1226 | nn.Linear out_proj 1 1 44 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1227 | pnnx.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 | |
| 1244 | class fuse_multiheadattention_pass_15 : public fuse_multiheadattention_pass_sameqkv |
| 1245 | { |
| 1246 | public: |
| 1247 | const char* match_pattern_graph() const |
| 1248 | { |
| 1249 | return R"PNNXIR(7767517 |
| 1250 | 23 22 |
| 1251 | pnnx.Input input 0 1 input |
| 1252 | nn.Linear op_0 1 1 input 2 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1253 | nn.Linear op_1 1 1 input 4 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1254 | nn.Linear op_2 1 1 input 6 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1255 | pnnx.Expression op_3 1 1 2 3 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 1256 | Tensor.view op_4 1 1 3 8 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1257 | Tensor.view op_5 1 1 4 5 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1258 | Tensor.view op_6 1 1 6 7 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1259 | torch.transpose op_7 1 1 8 9 dim0=1 dim1=2 |
| 1260 | torch.transpose op_8 1 1 5 10 dim0=1 dim1=2 |
| 1261 | torch.transpose op_9 1 1 7 11 dim0=1 dim1=2 |
| 1262 | Tensor.reshape op_10 1 1 9 14 shape=(%num_heads,%batch_mul_size,%feat_per_head) |
| 1263 | Tensor.reshape op_11 1 1 10 12 shape=(%num_heads,%batch_mul_size,%feat_per_head) |
| 1264 | Tensor.reshape op_12 1 1 11 17 shape=(%num_heads,%batch_mul_size,%feat_per_head) |
| 1265 | torch.transpose op_13 1 1 12 13 dim0=1 dim1=2 |
| 1266 | torch.bmm op_14 2 1 14 13 15 |
| 1267 | F.softmax softmax 1 1 15 16 dim=%softmax_dim |
| 1268 | torch.bmm op_16 2 1 16 17 18 |
| 1269 | Tensor.view op_17 1 1 18 19 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 1270 | torch.transpose op_18 1 1 19 20 dim0=1 dim1=2 |
| 1271 | Tensor.reshape op_19 1 1 20 21 shape=(%batch,%size,%embed_dim) |
| 1272 | nn.Linear out_proj 1 1 21 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1273 | pnnx.Output output 1 0 out |
| 1274 | )PNNXIR"; |
| 1275 | } |
| 1276 | }; |
| 1277 | |
| 1278 | class fuse_multiheadattention_pass_16 : public fuse_multiheadattention_pass_sameqkv |
| 1279 | { |
| 1280 | public: |
| 1281 | const char* match_pattern_graph() const |
| 1282 | { |
| 1283 | return R"PNNXIR(7767517 |
| 1284 | 27 26 |
| 1285 | pnnx.Input input 0 1 input |
| 1286 | nn.Linear op_0 1 1 input 3 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1287 | nn.Linear op_1 1 1 input 5 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1288 | nn.Linear op_2 1 1 input 7 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1289 | pnnx.Expression op_3 1 1 3 4 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 1290 | Tensor.view op_4 1 1 4 9 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1291 | Tensor.view op_5 1 1 5 6 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1292 | Tensor.view op_6 1 1 7 8 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1293 | torch.transpose op_7 1 1 9 10 dim0=1 dim1=2 |
| 1294 | torch.transpose op_8 1 1 6 11 dim0=1 dim1=2 |
| 1295 | torch.transpose op_9 1 1 8 12 dim0=1 dim1=2 |
| 1296 | Tensor.reshape op_10 1 1 10 15 shape=(%num_heads,%batch_mul_size,%feat_per_head) |
| 1297 | Tensor.reshape op_11 1 1 11 13 shape=(%num_heads,%batch_mul_size,%feat_per_head) |
| 1298 | Tensor.reshape op_12 1 1 12 21 shape=(%num_heads,%batch_mul_size,%feat_per_head) |
| 1299 | torch.transpose op_13 1 1 13 14 dim0=1 dim1=2 |
| 1300 | torch.bmm op_14 2 1 15 14 16 |
| 1301 | Tensor.view op_15 1 1 16 17 shape=(%batch,%num_heads,%size,%size) |
| 1302 | pnnx.Attribute attn_mask 0 1 attn_mask @data=(1,1,%size,%size)f32 |
| 1303 | pnnx.Expression op_16 2 1 17 attn_mask 18 expr=add(@0,@1) |
| 1304 | Tensor.view op_17 1 1 18 19 shape=(%num_heads,%size,%size) |
| 1305 | F.softmax softmax 1 1 19 20 dim=%softmax_dim |
| 1306 | torch.bmm op_19 2 1 20 21 22 |
| 1307 | Tensor.view op_20 1 1 22 23 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 1308 | torch.transpose op_21 1 1 23 24 dim0=1 dim1=2 |
| 1309 | Tensor.reshape op_22 1 1 24 25 shape=(%batch,%size,%embed_dim) |
| 1310 | nn.Linear out_proj 1 1 25 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1311 | pnnx.Output output 1 0 out |
| 1312 | )PNNXIR"; |
| 1313 | } |
| 1314 | |
| 1315 | const char* replace_pattern_graph() const |
| 1316 | { |
| 1317 | return R"PNNXIR(7767517 |
| 1318 | 4 3 |
| 1319 | pnnx.Input input 0 1 input |
| 1320 | pnnx.Attribute attn_mask 0 1 attn_mask @data=%attn_mask.data |
| 1321 | nn.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 |
| 1322 | pnnx.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 | |
| 1336 | class fuse_multiheadattention_pass_16_1 : public fuse_multiheadattention_pass_sameqkv |
| 1337 | { |
| 1338 | public: |
| 1339 | const char* match_pattern_graph() const |
| 1340 | { |
| 1341 | return R"PNNXIR(7767517 |
| 1342 | 20 19 |
| 1343 | pnnx.Input input_0 0 1 input |
| 1344 | nn.Linear op_0 1 1 input 31 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1345 | nn.Linear op_1 1 1 input 32 bias=%kbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1346 | nn.Linear op_2 1 1 input 34 bias=%vbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1347 | Tensor.view op_3 1 1 31 36 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1348 | Tensor.view op_4 1 1 32 33 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1349 | Tensor.view op_5 1 1 34 35 shape=(%batch,%size,%num_heads,%feat_per_head) |
| 1350 | Tensor.permute op_6 1 1 36 38 dims=(0,2,1,3) |
| 1351 | Tensor.permute op_7 1 1 33 37 dims=(0,2,1,3) |
| 1352 | Tensor.permute op_8 1 1 35 43 dims=(0,2,1,3) |
| 1353 | torch.transpose op_9 1 1 37 39 dim0=-1 dim1=-2 |
| 1354 | torch.matmul op_10 2 1 38 39 40 |
| 1355 | pnnx.Attribute attn_mask 0 1 attn_mask @data=(1,1,1,%size)f32 |
| 1356 | pnnx.Expression op_11 2 1 40 attn_mask 41 expr=add(div(@0,%sqrt_feat_per_head),@1) |
| 1357 | F.softmax softmax 1 1 41 42 dim=%softmax_dim |
| 1358 | torch.matmul op_13 2 1 42 43 44 |
| 1359 | Tensor.permute op_14 1 1 44 45 dims=(0,2,1,3) |
| 1360 | Tensor.reshape op_15 1 1 45 46 shape=(%batch,%size,%embed_dim) |
| 1361 | nn.Linear out_proj 1 1 46 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1362 | pnnx.Output output 1 0 out |
| 1363 | )PNNXIR"; |
| 1364 | } |
| 1365 | |
| 1366 | const char* replace_pattern_graph() const |
| 1367 | { |
| 1368 | return R"PNNXIR(7767517 |
| 1369 | 4 3 |
| 1370 | pnnx.Input input_0 0 1 input |
| 1371 | pnnx.Attribute attn_mask 0 1 attn_mask @data=%attn_mask.data |
| 1372 | nn.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 |
| 1373 | pnnx.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 | |
| 1422 | class fuse_multiheadattention_pass_17 : public fuse_multiheadattention_pass |
| 1423 | { |
| 1424 | public: |
| 1425 | const char* match_pattern_graph() const |
| 1426 | { |
| 1427 | return R"PNNXIR(7767517 |
| 1428 | 17 16 |
| 1429 | pnnx.Input input_0 0 1 input |
| 1430 | nn.Linear op_0 1 1 input 8 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 1431 | Tensor.reshape op_1 1 1 8 9 shape=(%batch,%size,3,%num_heads,%feat_per_head) |
| 1432 | Tensor.permute op_2 1 1 9 10 dims=(2,0,3,1,4) |
| 1433 | torch.unbind op_3 1 3 10 11 12 13 dim=0 |
| 1434 | pnnx.Expression op_4 1 1 11 14 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 1435 | torch.transpose op_5 1 1 12 15 dim0=-2 dim1=-1 |
| 1436 | torch.matmul op_6 2 1 14 15 16 |
| 1437 | pnnx.Attribute attn_mask 0 1 attn_mask @data=(1,%num_heads,%size,%size)f32 |
| 1438 | pnnx.Expression op_7 2 1 16 attn_mask 18 expr=add(@0,@1) |
| 1439 | F.softmax softmax 1 1 18 19 dim=%softmax_dim |
| 1440 | torch.matmul op_9 2 1 19 13 20 |
| 1441 | torch.transpose op_10 1 1 20 21 dim0=1 dim1=2 |
| 1442 | Tensor.reshape op_11 1 1 21 22 shape=(%batch,%size,%embed_dim) |
| 1443 | nn.Linear out_proj 1 1 22 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1444 | pnnx.Output output 1 0 out |
| 1445 | )PNNXIR"; |
| 1446 | } |
| 1447 | |
| 1448 | const char* replace_pattern_graph() const |
| 1449 | { |
| 1450 | return R"PNNXIR(7767517 |
| 1451 | 4 3 |
| 1452 | pnnx.Input input_0 0 1 input |
| 1453 | pnnx.Attribute attn_mask 0 1 attn_mask @data=%attn_mask.data |
| 1454 | nn.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 |
| 1455 | pnnx.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 | |
| 1483 | class fuse_multiheadattention_pass_17_1 : public fuse_multiheadattention_pass_17 |
| 1484 | { |
| 1485 | public: |
| 1486 | const char* match_pattern_graph() const |
| 1487 | { |
| 1488 | return R"PNNXIR(7767517 |
| 1489 | 17 16 |
| 1490 | pnnx.Input input_0 0 1 input |
| 1491 | nn.Linear op_0 1 1 input 8 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 1492 | Tensor.reshape op_1 1 1 8 9 shape=(%batch,%size,3,%num_heads,%feat_per_head) |
| 1493 | Tensor.permute op_2 1 1 9 10 dims=(2,0,3,1,4) |
| 1494 | torch.unbind op_3 1 3 10 11 12 13 dim=0 |
| 1495 | pnnx.Expression op_4 1 1 11 14 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 1496 | Tensor.permute op_5 1 1 12 15 dims=(0,1,3,2) |
| 1497 | torch.matmul op_6 2 1 14 15 16 |
| 1498 | pnnx.Attribute attn_mask 0 1 attn_mask @data=(1,%num_heads,%size,%size)f32 |
| 1499 | pnnx.Expression op_7 2 1 16 attn_mask 18 expr=add(@0,@1) |
| 1500 | F.softmax softmax 1 1 18 19 dim=%softmax_dim |
| 1501 | torch.matmul op_9 2 1 19 13 20 |
| 1502 | Tensor.permute op_10 1 1 20 21 dims=(0,2,1,3) |
| 1503 | Tensor.reshape op_11 1 1 21 22 shape=(%batch,%size,%embed_dim) |
| 1504 | nn.Linear out_proj 1 1 22 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1505 | pnnx.Output output 1 0 out |
| 1506 | )PNNXIR"; |
| 1507 | } |
| 1508 | }; |
| 1509 | |
| 1510 | class fuse_multiheadattention_pass_18 : public fuse_multiheadattention_pass |
| 1511 | { |
| 1512 | public: |
| 1513 | const char* match_pattern_graph() const |
| 1514 | { |
| 1515 | return R"PNNXIR(7767517 |
| 1516 | 20 19 |
| 1517 | pnnx.Input input_0 0 1 input |
| 1518 | nn.Linear op_0 1 1 input 25 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 1519 | Tensor.reshape op_1 1 1 25 26 shape=(%batch,%size,3,%num_heads,%feat_per_head) |
| 1520 | Tensor.permute op_2 1 1 26 27 dims=(2,0,3,1,4) |
| 1521 | torch.unbind op_3 1 3 27 28 29 30 dim=0 |
| 1522 | pnnx.Expression op_4 1 1 28 31 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 1523 | torch.transpose op_5 1 1 29 32 dim0=-2 dim1=-1 |
| 1524 | torch.matmul op_6 2 1 31 32 33 |
| 1525 | pnnx.Attribute attn_mask 0 1 attn_mask @data=(1,%num_heads,%size,%size)f32 |
| 1526 | pnnx.Expression op_7 2 1 33 attn_mask 35 expr=add(@0,@1) |
| 1527 | Tensor.view op_8 1 1 35 36 shape=(1,%batch,%num_heads,%size,%size) |
| 1528 | pnnx.Attribute op_9 0 1 37 @data=(1,%batch,1,%size,%size)f32 |
| 1529 | pnnx.Expression op_10 2 1 36 37 38 expr=add(@0,@1) |
| 1530 | Tensor.view op_11 1 1 38 39 shape=(%batch,%num_heads,%size,%size) |
| 1531 | F.softmax softmax 1 1 39 40 dim=%softmax_dim |
| 1532 | torch.matmul op_13 2 1 40 30 41 |
| 1533 | torch.transpose op_14 1 1 41 42 dim0=1 dim1=2 |
| 1534 | Tensor.reshape op_15 1 1 42 43 shape=(%batch,%size,%embed_dim) |
| 1535 | nn.Linear out_proj 1 1 43 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1536 | pnnx.Output output 1 0 out |
| 1537 | )PNNXIR"; |
| 1538 | } |
| 1539 | |
| 1540 | const char* replace_pattern_graph() const |
| 1541 | { |
| 1542 | return R"PNNXIR(7767517 |
| 1543 | 4 3 |
| 1544 | pnnx.Input input_0 0 1 input |
| 1545 | pnnx.Attribute attn_mask 0 1 attn_mask @data=%attn_mask.data |
| 1546 | nn.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 |
| 1547 | pnnx.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 | |
| 1597 | class fuse_multiheadattention_pass_18_1 : public fuse_multiheadattention_pass_18 |
| 1598 | { |
| 1599 | public: |
| 1600 | const char* match_pattern_graph() const |
| 1601 | { |
| 1602 | return R"PNNXIR(7767517 |
| 1603 | 20 19 |
| 1604 | pnnx.Input input_0 0 1 input |
| 1605 | nn.Linear op_0 1 1 input 25 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 1606 | Tensor.reshape op_1 1 1 25 26 shape=(%batch,%size,3,%num_heads,%feat_per_head) |
| 1607 | Tensor.permute op_2 1 1 26 27 dims=(2,0,3,1,4) |
| 1608 | torch.unbind op_3 1 3 27 28 29 30 dim=0 |
| 1609 | pnnx.Expression op_4 1 1 28 31 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 1610 | Tensor.permute op_5 1 1 29 32 dims=(0,1,3,2) |
| 1611 | torch.matmul op_6 2 1 31 32 33 |
| 1612 | pnnx.Attribute attn_mask 0 1 attn_mask @data=(1,%num_heads,%size,%size)f32 |
| 1613 | pnnx.Expression op_7 2 1 33 attn_mask 35 expr=add(@0,@1) |
| 1614 | Tensor.reshape op_8 1 1 35 36 shape=(1,%batch,%num_heads,%size,%size) |
| 1615 | pnnx.Attribute op_9 0 1 37 @data=(1,%batch,1,%size,%size)f32 |
| 1616 | pnnx.Expression op_10 2 1 36 37 38 expr=add(@0,@1) |
| 1617 | Tensor.reshape op_11 1 1 38 39 shape=(%batch,%num_heads,%size,%size) |
| 1618 | F.softmax softmax 1 1 39 40 dim=%softmax_dim |
| 1619 | torch.matmul op_13 2 1 40 30 41 |
| 1620 | Tensor.permute op_14 1 1 41 42 dims=(0,2,1,3) |
| 1621 | Tensor.reshape op_15 1 1 42 43 shape=(%batch,%size,%embed_dim) |
| 1622 | nn.Linear out_proj 1 1 43 out bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1623 | pnnx.Output output 1 0 out |
| 1624 | )PNNXIR"; |
| 1625 | } |
| 1626 | }; |
| 1627 | |
| 1628 | class fuse_multiheadattention_pass_onnx : public fuse_multiheadattention_pass_qkv |
| 1629 | { |
| 1630 | public: |
| 1631 | const char* match_pattern_graph() const |
| 1632 | { |
| 1633 | return R"PNNXIR(7767517 |
| 1634 | 23 22 |
| 1635 | pnnx.Input input_q 0 1 query |
| 1636 | pnnx.Input input_k 0 1 key |
| 1637 | pnnx.Input input_v 0 1 value |
| 1638 | nn.Linear op_0 1 1 query 10 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1639 | nn.Linear op_1 1 1 key 11 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight |
| 1640 | nn.Linear op_2 1 1 value 12 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight |
| 1641 | Tensor.reshape op_3 1 1 10 13 shape=(%qsize,%num_heads,%feat_per_head) |
| 1642 | Tensor.reshape op_4 1 1 11 15 shape=(%kvsize,%num_heads,%feat_per_head) |
| 1643 | Tensor.reshape op_5 1 1 12 16 shape=(%kvsize,%num_heads,%feat_per_head) |
| 1644 | Tensor.permute op_6 1 1 13 14 dims=(1,0,2) |
| 1645 | Tensor.permute op_7 1 1 15 19 dims=(1,2,0) |
| 1646 | Tensor.permute op_8 1 1 16 17 dims=(1,0,2) |
| 1647 | pnnx.Expression op_9 1 1 14 18 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 1648 | torch.matmul op_10 2 1 18 19 20 |
| 1649 | F.softmax softmax 1 1 20 21 dim=%softmax_dim |
| 1650 | torch.matmul op_12 2 1 21 17 22 |
| 1651 | Tensor.permute op_13 1 1 22 23 dims=(1,0,2) |
| 1652 | Tensor.reshape op_14 1 1 23 24 shape=(%qsize,%embed_dim) |
| 1653 | nn.Linear out_proj 1 1 24 25 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1654 | Tensor.reshape op_16 1 1 25 out shape=(%qsize,%batch,%embed_dim) |
| 1655 | Tensor.reshape op_17 1 1 21 27 shape=(%batch,%num_heads,%qsize,%kvsize) |
| 1656 | torch.mean op_18 1 1 27 outweight dim=(1) keepdim=False |
| 1657 | pnnx.Output output 2 0 out outweight |
| 1658 | )PNNXIR"; |
| 1659 | } |
| 1660 | |
| 1661 | const char* replace_pattern_graph() const |
| 1662 | { |
| 1663 | return R"PNNXIR(7767517 |
| 1664 | 5 5 |
| 1665 | pnnx.Input input_0 0 1 query |
| 1666 | pnnx.Input input_1 0 1 key |
| 1667 | pnnx.Input input_2 0 1 value |
| 1668 | nn.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 |
| 1669 | pnnx.Output output 2 0 out outweight |
| 1670 | )PNNXIR"; |
| 1671 | } |
| 1672 | }; |
| 1673 | |
| 1674 | class fuse_multiheadattention_pass_onnx_1 : public fuse_multiheadattention_pass |
| 1675 | { |
| 1676 | public: |
| 1677 | const char* match_pattern_graph() const |
| 1678 | { |
| 1679 | return R"PNNXIR(7767517 |
| 1680 | 26 25 |
| 1681 | pnnx.Input input_q 0 1 input |
| 1682 | nn.Linear op_0 1 1 input 14 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 1683 | Tensor.reshape op_1 1 1 14 15 shape=(%batch,%size,1,3,%embed_dim) |
| 1684 | Tensor.permute op_2 1 1 15 16 dims=(3,1,2,0,4) |
| 1685 | torch.squeeze op_3 1 1 16 17 dim=3 |
| 1686 | torch.unbind op_4 1 3 17 18 19 20 dim=0 |
| 1687 | Tensor.reshape op_5 1 1 18 21 shape=(%size,%num_heads,%feat_per_head) |
| 1688 | Tensor.reshape op_6 1 1 19 23 shape=(%size,%num_heads,%feat_per_head) |
| 1689 | Tensor.reshape op_7 1 1 20 25 shape=(%size,%num_heads,%feat_per_head) |
| 1690 | Tensor.permute op_8 1 1 21 22 dims=(1,0,2) |
| 1691 | Tensor.permute op_9 1 1 23 24 dims=(1,0,2) |
| 1692 | Tensor.permute op_10 1 1 25 26 dims=(1,0,2) |
| 1693 | Tensor.reshape op_11 1 1 22 27 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 1694 | Tensor.reshape op_12 1 1 24 28 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 1695 | Tensor.reshape op_13 1 1 26 29 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 1696 | Tensor.permute op_14 1 1 28 30 dims=(0,1,3,2) |
| 1697 | pnnx.Expression op_15 1 1 27 31 expr=mul(@0,%sqrt_inv_sqrt_embed_dim_per_head) |
| 1698 | pnnx.Expression op_16 1 1 30 32 expr=mul(@0,%sqrt_inv_sqrt_embed_dim_per_head) |
| 1699 | torch.matmul op_17 2 1 31 32 33 |
| 1700 | F.softmax softmax 1 1 33 34 dim=%softmax_dim |
| 1701 | torch.matmul op_19 2 1 34 29 35 |
| 1702 | Tensor.permute op_20 1 1 35 36 dims=(2,0,1,3) |
| 1703 | Tensor.reshape op_21 1 1 36 37 shape=(%size,%embed_dim) |
| 1704 | nn.Linear out_proj 1 1 37 38 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1705 | Tensor.reshape op_23 1 1 38 out shape=(%size,%batch,%embed_dim) |
| 1706 | pnnx.Output output 1 0 out |
| 1707 | )PNNXIR"; |
| 1708 | } |
| 1709 | |
| 1710 | const char* replace_pattern_graph() const |
| 1711 | { |
| 1712 | return R"PNNXIR(7767517 |
| 1713 | 3 2 |
| 1714 | pnnx.Input input 0 1 input |
| 1715 | nn.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 |
| 1716 | pnnx.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 | |
| 1746 | class fuse_multiheadattention_pass_onnx_1_1 : public fuse_multiheadattention_pass |
| 1747 | { |
| 1748 | public: |
| 1749 | const char* match_pattern_graph() const |
| 1750 | { |
| 1751 | return R"PNNXIR(7767517 |
| 1752 | 21 20 |
| 1753 | pnnx.Input input_q 0 1 input |
| 1754 | nn.Linear op_0 1 1 input 33 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 1755 | Tensor.reshape op_1 1 1 33 34 shape=(%batch,%size,1,3,%embed_dim) |
| 1756 | Tensor.permute op_2 1 1 34 35 dims=(3,1,2,0,4) |
| 1757 | torch.squeeze op_3 1 1 35 36 dim=3 |
| 1758 | torch.unbind op_4 1 3 36 37 38 39 dim=0 |
| 1759 | Tensor.reshape op_5 1 1 37 40 shape=(%size,%num_heads,%feat_per_head) |
| 1760 | Tensor.reshape op_6 1 1 38 42 shape=(%size,%num_heads,%feat_per_head) |
| 1761 | Tensor.reshape op_7 1 1 39 43 shape=(%size,%num_heads,%feat_per_head) |
| 1762 | Tensor.permute op_8 1 1 40 41 dims=(1,0,2) |
| 1763 | Tensor.permute op_9 1 1 42 46 dims=(1,2,0) |
| 1764 | Tensor.permute op_10 1 1 43 44 dims=(1,0,2) |
| 1765 | pnnx.Expression op_11 1 1 41 45 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 1766 | torch.matmul op_12 2 1 45 46 47 |
| 1767 | F.softmax softmax 1 1 47 48 dim=%softmax_dim |
| 1768 | torch.matmul op_14 2 1 48 44 49 |
| 1769 | Tensor.permute op_15 1 1 49 50 dims=(1,0,2) |
| 1770 | Tensor.reshape op_16 1 1 50 51 shape=(%size,%embed_dim) |
| 1771 | nn.Linear out_proj 1 1 51 52 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1772 | Tensor.reshape op_18 1 1 52 out shape=(%size,%batch,%embed_dim) |
| 1773 | pnnx.Output output 1 0 out |
| 1774 | )PNNXIR"; |
| 1775 | } |
| 1776 | |
| 1777 | const char* replace_pattern_graph() const |
| 1778 | { |
| 1779 | return R"PNNXIR(7767517 |
| 1780 | 3 2 |
| 1781 | pnnx.Input input 0 1 input |
| 1782 | nn.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 |
| 1783 | pnnx.Output output 1 0 out |
| 1784 | )PNNXIR"; |
| 1785 | } |
| 1786 | }; |
| 1787 | |
| 1788 | class fuse_multiheadattention_pass_onnx_1_2 : public fuse_multiheadattention_pass |
| 1789 | { |
| 1790 | public: |
| 1791 | const char* match_pattern_graph() const |
| 1792 | { |
| 1793 | return R"PNNXIR(7767517 |
| 1794 | 21 20 |
| 1795 | pnnx.Input input_q 0 1 input |
| 1796 | nn.Linear op_0 1 1 input 14 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 1797 | Tensor.reshape op_1 1 1 14 15 shape=(%batch,%size,1,3,%embed_dim) |
| 1798 | Tensor.permute op_2 1 1 15 16 dims=(3,1,2,0,4) |
| 1799 | torch.squeeze op_3 1 1 16 17 dim=3 |
| 1800 | torch.unbind op_4 1 3 17 18 19 20 dim=0 |
| 1801 | Tensor.reshape op_5 1 1 18 21 shape=(%size,%num_heads,%feat_per_head) |
| 1802 | Tensor.reshape op_6 1 1 19 23 shape=(%size,%num_heads,%feat_per_head) |
| 1803 | Tensor.reshape op_7 1 1 20 25 shape=(%size,%num_heads,%feat_per_head) |
| 1804 | Tensor.permute op_8 1 1 21 22 dims=(1,0,2) |
| 1805 | Tensor.permute op_9 1 1 23 24 dims=(1,0,2) |
| 1806 | Tensor.permute op_10 1 1 25 26 dims=(1,0,2) |
| 1807 | Tensor.reshape op_11 1 1 22 27 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 1808 | Tensor.reshape op_12 1 1 24 28 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 1809 | Tensor.reshape op_13 1 1 26 29 shape=(%batch,%num_heads,%size,%feat_per_head) |
| 1810 | F.scaled_dot_product_attention op_14 3 1 27 28 29 35 dropout_p=0.000000e+00 is_causal=False |
| 1811 | Tensor.permute op_15 1 1 35 36 dims=(2,0,1,3) |
| 1812 | Tensor.reshape op_16 1 1 36 37 shape=(%size,%embed_dim) |
| 1813 | nn.Linear out_proj 1 1 37 38 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1814 | Tensor.reshape op_18 1 1 38 out shape=(%size,%batch,%embed_dim) |
| 1815 | pnnx.Output output 1 0 out |
| 1816 | )PNNXIR"; |
| 1817 | } |
| 1818 | |
| 1819 | const char* replace_pattern_graph() const |
| 1820 | { |
| 1821 | return R"PNNXIR(7767517 |
| 1822 | 3 2 |
| 1823 | pnnx.Input input 0 1 input |
| 1824 | nn.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 |
| 1825 | pnnx.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 | |
| 1846 | class fuse_multiheadattention_pass_onnx_2 : public fuse_multiheadattention_pass |
| 1847 | { |
| 1848 | public: |
| 1849 | const char* match_pattern_graph() const |
| 1850 | { |
| 1851 | return R"PNNXIR(7767517 |
| 1852 | 24 23 |
| 1853 | pnnx.Input input_0 0 1 input |
| 1854 | pnnx.Input input_1 0 1 attn_mask |
| 1855 | nn.Linear op_0 1 1 input 15 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 1856 | Tensor.reshape op_1 1 1 15 16 shape=(%batch,%size,1,3,%embed_dim) |
| 1857 | Tensor.permute op_2 1 1 16 17 dims=(3,1,2,0,4) |
| 1858 | torch.squeeze op_3 1 1 17 18 dim=3 |
| 1859 | torch.unbind op_4 1 3 18 19 20 21 dim=0 |
| 1860 | Tensor.reshape op_5 1 1 19 23 shape=(%size,%num_heads,%feat_per_head) |
| 1861 | Tensor.reshape op_7 1 1 20 25 shape=(%size,%num_heads,%feat_per_head) |
| 1862 | Tensor.reshape op_8 1 1 21 26 shape=(%size,%num_heads,%feat_per_head) |
| 1863 | Tensor.permute op_6 1 1 23 24 dims=(1,0,2) |
| 1864 | Tensor.permute op_11 1 1 25 29 dims=(1,2,0) |
| 1865 | Tensor.permute op_9 1 1 26 27 dims=(1,0,2) |
| 1866 | pnnx.Expression op_10 1 1 24 28 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 1867 | torch.matmul op_12 2 1 28 29 30 |
| 1868 | torch.unsqueeze op_13 1 1 attn_mask 22 dim=0 |
| 1869 | pnnx.Expression op_14 2 1 30 22 31 expr=add(@0,@1) |
| 1870 | F.softmax softmax 1 1 31 32 dim=%softmax_dim |
| 1871 | torch.matmul op_16 2 1 32 27 33 |
| 1872 | Tensor.permute op_17 1 1 33 34 dims=(1,0,2) |
| 1873 | Tensor.reshape op_18 1 1 34 35 shape=(%size,%embed_dim) |
| 1874 | nn.Linear out_proj 1 1 35 36 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1875 | Tensor.reshape op_20 1 1 36 out shape=(%size,%batch,%embed_dim) |
| 1876 | pnnx.Output output 1 0 out |
| 1877 | )PNNXIR"; |
| 1878 | } |
| 1879 | |
| 1880 | const char* replace_pattern_graph() const |
| 1881 | { |
| 1882 | return R"PNNXIR(7767517 |
| 1883 | 4 3 |
| 1884 | pnnx.Input input_0 0 1 input |
| 1885 | pnnx.Input input_1 0 1 attn_mask |
| 1886 | nn.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 |
| 1887 | pnnx.Output output 1 0 out |
| 1888 | )PNNXIR"; |
| 1889 | } |
| 1890 | }; |
| 1891 | |
| 1892 | class fuse_multiheadattention_pass_onnx_2_1 : public fuse_multiheadattention_pass_onnx_2 |
| 1893 | { |
| 1894 | public: |
| 1895 | const char* match_pattern_graph() const |
| 1896 | { |
| 1897 | return R"PNNXIR(7767517 |
| 1898 | 24 23 |
| 1899 | pnnx.Input input_0 0 1 input |
| 1900 | pnnx.Input input_1 0 1 attn_mask |
| 1901 | nn.Linear op_0 1 1 input 15 bias=%qkvbias in_features=%embed_dim out_features=%qkv_out_features @bias @weight |
| 1902 | Tensor.reshape op_1 1 1 15 16 shape=(%batch,%size,1,3,%embed_dim) |
| 1903 | Tensor.permute op_2 1 1 16 17 dims=(3,1,2,0,4) |
| 1904 | torch.squeeze op_3 1 1 17 18 dim=3 |
| 1905 | torch.unbind op_4 1 3 18 19 20 21 dim=0 |
| 1906 | Tensor.reshape op_5 1 1 19 23 shape=(%size,%num_heads,%feat_per_head) |
| 1907 | Tensor.reshape op_7 1 1 20 25 shape=(%size,%num_heads,%feat_per_head) |
| 1908 | Tensor.reshape op_8 1 1 21 26 shape=(%size,%num_heads,%feat_per_head) |
| 1909 | Tensor.permute op_6 1 1 23 24 dims=(1,0,2) |
| 1910 | Tensor.permute op_11 1 1 25 29 dims=(1,2,0) |
| 1911 | Tensor.permute op_9 1 1 26 27 dims=(1,0,2) |
| 1912 | pnnx.Expression op_10 1 1 24 28 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 1913 | torch.matmul op_12 2 1 28 29 30 |
| 1914 | torch.unsqueeze op_13 1 1 attn_mask 22 dim=0 |
| 1915 | pnnx.Expression op_14 2 1 30 22 31 expr=add(@0,@1) |
| 1916 | F.softmax softmax 1 1 31 32 dim=%softmax_dim |
| 1917 | torch.matmul op_16 2 1 32 27 33 |
| 1918 | Tensor.permute op_17 1 1 33 34 dims=(1,0,2) |
| 1919 | Tensor.reshape op_18 1 1 34 35 shape=(%size,%embed_dim) |
| 1920 | nn.Linear out_proj 1 1 35 36 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1921 | Tensor.reshape op_20 1 1 36 out shape=(%size,%batch,%embed_dim) |
| 1922 | pnnx.Output output 1 0 out |
| 1923 | )PNNXIR"; |
| 1924 | } |
| 1925 | }; |
| 1926 | |
| 1927 | class fuse_multiheadattention_pass_onnx_3 : public fuse_multiheadattention_pass_qkv |
| 1928 | { |
| 1929 | public: |
| 1930 | const char* match_pattern_graph() const |
| 1931 | { |
| 1932 | return R"PNNXIR(7767517 |
| 1933 | 25 24 |
| 1934 | pnnx.Input input_q 0 1 query |
| 1935 | pnnx.Input input_kv 0 1 kv |
| 1936 | nn.Linear op_0 1 1 query 14 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1937 | nn.Linear op_1 1 1 kv 15 bias=%kvbias in_features=%kvdim out_features=%kv_embed_dim @bias @weight |
| 1938 | Tensor.reshape op_2 1 1 15 16 shape=(%batch,%kvsize,1,2,%embed_dim) |
| 1939 | Tensor.permute op_3 1 1 16 17 dims=(3,1,2,0,4) |
| 1940 | torch.squeeze op_4 1 1 17 18 dim=3 |
| 1941 | torch.unbind op_5 1 2 18 19 20 dim=0 |
| 1942 | Tensor.reshape op_6 1 1 14 21 shape=(%qsize,%num_heads,%feat_per_head) |
| 1943 | Tensor.reshape op_7 1 1 19 23 shape=(%kvsize,%num_heads,%feat_per_head) |
| 1944 | Tensor.reshape op_8 1 1 20 24 shape=(%kvsize,%num_heads,%feat_per_head) |
| 1945 | Tensor.permute op_9 1 1 21 22 dims=(1,0,2) |
| 1946 | Tensor.permute op_10 1 1 24 25 dims=(1,0,2) |
| 1947 | Tensor.permute op_11 1 1 23 27 dims=(1,2,0) |
| 1948 | pnnx.Expression op_12 1 1 22 26 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 1949 | torch.matmul op_13 2 1 26 27 28 |
| 1950 | F.softmax softmax 1 1 28 29 dim=%softmax_dim |
| 1951 | torch.matmul op_15 2 1 29 25 30 |
| 1952 | Tensor.permute op_16 1 1 30 31 dims=(1,0,2) |
| 1953 | Tensor.reshape op_17 1 1 31 32 shape=(%qsize,%embed_dim) |
| 1954 | nn.Linear out_proj 1 1 32 33 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 1955 | Tensor.reshape op_19 1 1 33 out shape=(%qsize,1,%embed_dim) |
| 1956 | Tensor.reshape op_20 1 1 29 35 shape=(1,%num_heads,%qsize,%kvsize) |
| 1957 | torch.mean op_21 1 1 35 outweight dim=(1) keepdim=False |
| 1958 | pnnx.Output output 2 0 out outweight |
| 1959 | )PNNXIR"; |
| 1960 | } |
| 1961 | |
| 1962 | const char* replace_pattern_graph() const |
| 1963 | { |
| 1964 | return R"PNNXIR(7767517 |
| 1965 | 4 4 |
| 1966 | pnnx.Input input_0 0 1 query |
| 1967 | pnnx.Input input_1 0 1 kv |
| 1968 | nn.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 |
| 1969 | pnnx.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 | |
| 2038 | class fuse_multiheadattention_pass_onnx_4 : public fuse_multiheadattention_pass_qkv |
| 2039 | { |
| 2040 | public: |
| 2041 | const char* match_pattern_graph() const |
| 2042 | { |
| 2043 | return R"PNNXIR(7767517 |
| 2044 | 26 25 |
| 2045 | pnnx.Input input_q 0 1 query |
| 2046 | pnnx.Input input_k 0 1 key |
| 2047 | pnnx.Input input_v 0 1 value |
| 2048 | pnnx.Input input_3 0 1 attn_mask |
| 2049 | nn.Linear op_0 1 1 query 20 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 2050 | nn.Linear op_1 1 1 key 21 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight |
| 2051 | nn.Linear op_2 1 1 value 22 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight |
| 2052 | Tensor.reshape op_3 1 1 20 24 shape=(%qsize,%num_heads,%feat_per_head) |
| 2053 | Tensor.reshape op_4 1 1 21 26 shape=(%kvsize,%num_heads,%feat_per_head) |
| 2054 | Tensor.reshape op_5 1 1 22 27 shape=(%kvsize,%num_heads,%feat_per_head) |
| 2055 | Tensor.permute op_6 1 1 24 25 dims=(1,0,2) |
| 2056 | Tensor.permute op_7 1 1 26 30 dims=(1,2,0) |
| 2057 | Tensor.permute op_8 1 1 27 28 dims=(1,0,2) |
| 2058 | pnnx.Expression op_9 1 1 25 29 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 2059 | torch.matmul op_10 2 1 29 30 31 |
| 2060 | torch.unsqueeze op_11 1 1 attn_mask 23 dim=0 |
| 2061 | pnnx.Expression op_12 2 1 31 23 32 expr=add(@0,@1) |
| 2062 | F.softmax softmax 1 1 32 33 dim=%softmax_dim |
| 2063 | torch.matmul op_14 2 1 33 28 34 |
| 2064 | Tensor.permute op_15 1 1 34 35 dims=(1,0,2) |
| 2065 | Tensor.reshape op_16 1 1 35 36 shape=(%qsize,%embed_dim) |
| 2066 | nn.Linear out_proj 1 1 36 37 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 2067 | Tensor.reshape op_18 1 1 37 out shape=(%qsize,%batch,%embed_dim) |
| 2068 | Tensor.reshape op_19 1 1 33 39 shape=(%batch,%num_heads,%qsize,%kvsize) |
| 2069 | torch.mean op_20 1 1 39 outweight dim=(1) keepdim=False |
| 2070 | pnnx.Output output 2 0 out outweight |
| 2071 | )PNNXIR"; |
| 2072 | } |
| 2073 | |
| 2074 | const char* replace_pattern_graph() const |
| 2075 | { |
| 2076 | return R"PNNXIR(7767517 |
| 2077 | 6 6 |
| 2078 | pnnx.Input input_0 0 1 query |
| 2079 | pnnx.Input input_1 0 1 key |
| 2080 | pnnx.Input input_2 0 1 value |
| 2081 | pnnx.Input input_3 0 1 attn_mask |
| 2082 | nn.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 |
| 2083 | pnnx.Output output 2 0 out outweight |
| 2084 | )PNNXIR"; |
| 2085 | } |
| 2086 | }; |
| 2087 | |
| 2088 | class fuse_multiheadattention_pass_onnx_4_1 : public fuse_multiheadattention_pass_onnx_4 |
| 2089 | { |
| 2090 | public: |
| 2091 | const char* match_pattern_graph() const |
| 2092 | { |
| 2093 | return R"PNNXIR(7767517 |
| 2094 | 25 24 |
| 2095 | pnnx.Input input_q 0 1 query |
| 2096 | pnnx.Input input_k 0 1 key |
| 2097 | pnnx.Input input_v 0 1 value |
| 2098 | pnnx.Input input_3 0 1 attn_mask |
| 2099 | nn.Linear op_0 1 1 query 22 bias=%qbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 2100 | nn.Linear op_1 1 1 key 23 bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight |
| 2101 | nn.Linear op_2 1 1 value 24 bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight |
| 2102 | Tensor.reshape op_3 1 1 22 25 shape=(%qsize,%num_heads,%feat_per_head) |
| 2103 | Tensor.reshape op_4 1 1 23 27 shape=(%kvsize,%num_heads,%feat_per_head) |
| 2104 | Tensor.reshape op_5 1 1 24 28 shape=(%kvsize,%num_heads,%feat_per_head) |
| 2105 | Tensor.permute op_6 1 1 25 26 dims=(1,0,2) |
| 2106 | Tensor.permute op_7 1 1 28 29 dims=(1,0,2) |
| 2107 | Tensor.permute op_8 1 1 27 31 dims=(1,2,0) |
| 2108 | pnnx.Expression op_9 1 1 26 30 expr=mul(@0,%inv_sqrt_embed_dim_per_head) |
| 2109 | torch.matmul op_10 2 1 30 31 32 |
| 2110 | pnnx.Expression op_11 2 1 32 attn_mask 33 expr=add(@0,@1) |
| 2111 | F.softmax softmax 1 1 33 34 dim=%softmax_dim |
| 2112 | torch.matmul op_13 2 1 34 29 35 |
| 2113 | Tensor.permute op_14 1 1 35 36 dims=(1,0,2) |
| 2114 | Tensor.reshape op_15 1 1 36 37 shape=(%qsize,%embed_dim) |
| 2115 | nn.Linear out_proj 1 1 37 38 bias=%outbias in_features=%embed_dim out_features=%embed_dim @bias @weight |
| 2116 | Tensor.reshape op_16 1 1 38 out shape=(%qsize,%batch,%embed_dim) |
| 2117 | Tensor.reshape op_18 1 1 34 40 shape=(%batch,%num_heads,%qsize,%kvsize) |
| 2118 | torch.mean op_19 1 1 40 outweight dim=(1) keepdim=False |
| 2119 | pnnx.Output output 2 0 out outweight |
| 2120 | )PNNXIR"; |
| 2121 | } |
| 2122 | }; |
| 2123 | |
| 2124 | void 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 |