Changesets · Branches · Tags · Compare
| Changeset | 00a1e1f87e7d7d240d42e36e4ad8608872218ef8660169e6881c637682a92743 |
|---|---|
| Author | nihui <nihuini@tencent.com> · 2024-07-18 14:20 +08:00 |
| Committer | GitHub <noreply@github.com> · 2024-07-18 14:20 +08:00 |
| Parents | 423fec64c3e4 |
3 files changed
modified tools/pnnx/src/CMakeLists.txt +1 −0
| @@ -472,6 +472,7 @@ | ||
| 472 | 472 | pass_ncnn/F_prelu.cpp |
| 473 | 473 | pass_ncnn/F_relu.cpp |
| 474 | 474 | pass_ncnn/F_relu6.cpp |
| 475 | + pass_ncnn/F_scaled_dot_product_attention.cpp | |
| 475 | 476 | pass_ncnn/F_selu.cpp |
| 476 | 477 | pass_ncnn/F_sigmoid.cpp |
| 477 | 478 | pass_ncnn/F_silu.cpp |
modified tools/pnnx/src/pass_level5/fuse_scaled_dot_product_attention.cpp +82 −2
| @@ -62,7 +62,7 @@ | ||
| 62 | 62 | pnnx.Input input_0 0 1 query |
| 63 | 63 | pnnx.Input input_1 0 1 key |
| 64 | 64 | pnnx.Input input_2 0 1 value |
| 65 | -F.scaled_dot_product_attention op_0 3 1 query key value out attn_mask=None dropout_p=0.0 is_causal=False | |
| 65 | +F.scaled_dot_product_attention sdpa 3 1 query key value out attn_mask=None dropout_p=0.0 is_causal=False | |
| 66 | 66 | pnnx.Output output 1 0 out |
| 67 | 67 | )PNNXIR"; |
| 68 | 68 | } |
| @@ -114,7 +114,7 @@ | ||
| 114 | 114 | pnnx.Input input_Rw 0 1 Rw |
| 115 | 115 | pnnx.Expression RhRw 2 1 Rh Rw RhRw expr=add(@0,@1) #RhRw=(%batch,%h,%w,%h,%w)f32 |
| 116 | 116 | Tensor.reshape attn_mask 1 1 RhRw attn_mask shape=(%batch,%qsize,%qsize) #attn_mask=(%batch,%qsize,%qsize)f32 |
| 117 | -F.scaled_dot_product_attention op_0 4 1 query key value attn_mask out dropout_p=0.0 is_causal=False $attn_mask=attn_mask | |
| 117 | +F.scaled_dot_product_attention sdpa 4 1 query key value attn_mask out dropout_p=0.0 is_causal=False $attn_mask=attn_mask | |
| 118 | 118 | pnnx.Output output 1 0 out |
| 119 | 119 | )PNNXIR"; |
| 120 | 120 | } |
| @@ -137,15 +137,95 @@ | ||
| 137 | 137 | } |
| 138 | 138 | }; |
| 139 | 139 | |
| 140 | +class fuse_scaled_dot_product_attention_pass_onnx : public GraphRewriterPass | |
| 141 | +{ | |
| 142 | +public: | |
| 143 | + const char* match_pattern_graph() const | |
| 144 | + { | |
| 145 | + return R"PNNXIR(7767517 | |
| 146 | +12 11 | |
| 147 | +pnnx.Input input_0 0 1 query | |
| 148 | +pnnx.Input input_1 0 1 key | |
| 149 | +pnnx.Input input_2 0 1 value | |
| 150 | +pnnx.Input input_3 0 1 attn_mask | |
| 151 | +Tensor.permute op_0 1 1 query 13 dims=(0,2,1,3) | |
| 152 | +Tensor.permute op_1 1 1 key 20 dims=(0,2,3,1) | |
| 153 | +Tensor.permute op_2 1 1 value 19 dims=(0,2,1,3) | |
| 154 | +torch.matmul op_3 2 1 13 20 21 | |
| 155 | +pnnx.Expression op_4 2 1 21 attn_mask 23 expr=add(@0,@1) | |
| 156 | +F.softmax softmax 1 1 23 24 dim=%softmax_dim | |
| 157 | +torch.matmul op_6 2 1 24 19 out | |
| 158 | +pnnx.Output output 1 0 out | |
| 159 | +)PNNXIR"; | |
| 160 | + } | |
| 161 | + | |
| 162 | + const char* replace_pattern_graph() const | |
| 163 | + { | |
| 164 | + return R"PNNXIR(7767517 | |
| 165 | +9 8 | |
| 166 | +pnnx.Input input_0 0 1 query | |
| 167 | +pnnx.Input input_1 0 1 key | |
| 168 | +pnnx.Input input_2 0 1 value | |
| 169 | +pnnx.Input input_3 0 1 attn_mask | |
| 170 | +Tensor.permute op_0 1 1 query q dims=(0,2,1,3) | |
| 171 | +Tensor.permute op_1 1 1 key k dims=(0,2,1,3) | |
| 172 | +Tensor.permute op_2 1 1 value v dims=(0,2,1,3) | |
| 173 | +F.scaled_dot_product_attention sdpa 4 1 q k v attn_mask out dropout_p=0.0 is_causal=False $attn_mask=attn_mask | |
| 174 | +pnnx.Output output 1 0 out | |
| 175 | +)PNNXIR"; | |
| 176 | + } | |
| 177 | + | |
| 178 | + 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 | |
| 179 | + { | |
| 180 | + const int softmax_dim = captured_params.at("softmax_dim").i; | |
| 181 | + | |
| 182 | + int softmax_input_rank = (int)matched_operators.at("softmax")->inputs[0]->shape.size(); | |
| 183 | + if (softmax_dim != -1 && softmax_dim != softmax_input_rank - 1) | |
| 184 | + return false; | |
| 185 | + | |
| 186 | + return true; | |
| 187 | + } | |
| 188 | + | |
| 189 | + 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 | |
| 190 | + { | |
| 191 | + Operator* op = ops.at("sdpa"); | |
| 192 | + | |
| 193 | + op->params["scale"] = 1.f; | |
| 194 | + | |
| 195 | + // rewrite qkv shape | |
| 196 | + { | |
| 197 | + std::vector<int> q_shape = ops.at("op_0")->inputs[0]->shape; | |
| 198 | + std::vector<int> k_shape = ops.at("op_1")->inputs[0]->shape; | |
| 199 | + std::vector<int> v_shape = ops.at("op_2")->inputs[0]->shape; | |
| 200 | + | |
| 201 | + if (!q_shape.empty()) | |
| 202 | + std::swap(q_shape[1], q_shape[2]); | |
| 203 | + if (!k_shape.empty()) | |
| 204 | + std::swap(k_shape[1], k_shape[2]); | |
| 205 | + if (!v_shape.empty()) | |
| 206 | + std::swap(v_shape[1], v_shape[2]); | |
| 207 | + | |
| 208 | + ops.at("op_0")->outputs[0]->shape = q_shape; | |
| 209 | + ops.at("op_0")->outputs[0]->type = ops.at("op_0")->inputs[0]->type; | |
| 210 | + ops.at("op_1")->outputs[0]->shape = k_shape; | |
| 211 | + ops.at("op_1")->outputs[0]->type = ops.at("op_1")->inputs[0]->type; | |
| 212 | + ops.at("op_2")->outputs[0]->shape = v_shape; | |
| 213 | + ops.at("op_2")->outputs[0]->type = ops.at("op_2")->inputs[0]->type; | |
| 214 | + } | |
| 215 | + } | |
| 216 | +}; | |
| 217 | + | |
| 140 | 218 | void fuse_scaled_dot_product_attention(Graph& graph) |
| 141 | 219 | { |
| 142 | 220 | #if TORCH_VERSION_MAJOR >= 2 |
| 143 | 221 | fuse_scaled_dot_product_attention_pass a; |
| 144 | 222 | fuse_scaled_dot_product_attention_pass_1 b; |
| 223 | + fuse_scaled_dot_product_attention_pass_onnx onnx0; | |
| 145 | 224 | int opindex = 0; |
| 146 | 225 | |
| 147 | 226 | pnnx_graph_rewrite(graph, &a, opindex); |
| 148 | 227 | pnnx_graph_rewrite(graph, &b, opindex); |
| 228 | + pnnx_graph_rewrite(graph, &onnx0, opindex); | |
| 149 | 229 | #endif |
| 150 | 230 | } |
| 151 | 231 |
added tools/pnnx/src/pass_ncnn/F_scaled_dot_product_attention.cpp +223 −0
| @@ -0,0 +1,223 @@ | ||
| 1 | +// Tencent is pleased to support the open source community by making ncnn available. | |
| 2 | +// | |
| 3 | +// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. | |
| 4 | +// | |
| 5 | +// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except | |
| 6 | +// in compliance with the License. You may obtain a copy of the License at | |
| 7 | +// | |
| 8 | +// https://opensource.org/licenses/BSD-3-Clause | |
| 9 | +// | |
| 10 | +// Unless required by applicable law or agreed to in writing, software distributed | |
| 11 | +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | |
| 12 | +// CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
| 13 | +// specific language governing permissions and limitations under the License. | |
| 14 | + | |
| 15 | +#include "pass_ncnn.h" | |
| 16 | + | |
| 17 | +namespace pnnx { | |
| 18 | + | |
| 19 | +namespace ncnn { | |
| 20 | + | |
| 21 | +class F_scaled_dot_product_attention : public GraphRewriterPass | |
| 22 | +{ | |
| 23 | +public: | |
| 24 | + const char* match_pattern_graph() const | |
| 25 | + { | |
| 26 | + return R"PNNXIR(7767517 | |
| 27 | +16 15 | |
| 28 | +pnnx.Input input_0 0 1 input | |
| 29 | +pnnx.Input input_1 0 1 attn_mask | |
| 30 | +nn.Linear op_0 1 1 input q bias=%qbias in_features=%qdim out_features=%embed_dim @bias @weight | |
| 31 | +nn.Linear op_1 1 1 input k bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight | |
| 32 | +nn.Linear op_2 1 1 input v bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight | |
| 33 | +Tensor.reshape op_3 1 1 q 10 shape=(%batch,%size,%num_heads,%feat_per_head) | |
| 34 | +Tensor.reshape op_4 1 1 k 12 shape=(%batch,%size,%num_heads,%feat_per_head) | |
| 35 | +Tensor.reshape op_5 1 1 v 14 shape=(%batch,%size,%num_heads,%feat_per_head) | |
| 36 | +Tensor.permute op_6 1 1 10 16 dims=(0,2,1,3) | |
| 37 | +Tensor.permute op_7 1 1 12 17 dims=(0,2,1,3) | |
| 38 | +Tensor.permute op_8 1 1 14 18 dims=(0,2,1,3) | |
| 39 | +F.scaled_dot_product_attention op_9 4 1 16 17 18 attn_mask 19 dropout_p=0.0 is_causal=False scale=%scale | |
| 40 | +Tensor.permute op_10 1 1 19 20 dims=(0,2,1,3) | |
| 41 | +Tensor.reshape op_11 1 1 20 21 shape=(%batch,%size,%embed_dim) | |
| 42 | +nn.Linear out_proj 1 1 21 out bias=%outbias in_features=%embed_dim out_features=%qdim @bias @weight | |
| 43 | +pnnx.Output output 1 0 out | |
| 44 | +)PNNXIR"; | |
| 45 | + } | |
| 46 | + | |
| 47 | + const char* type_str() const | |
| 48 | + { | |
| 49 | + return "MultiHeadAttention"; | |
| 50 | + } | |
| 51 | + | |
| 52 | + const char* name_str() const | |
| 53 | + { | |
| 54 | + return "sdpa_attention"; | |
| 55 | + } | |
| 56 | + | |
| 57 | + void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const | |
| 58 | + { | |
| 59 | + op->params["0"] = captured_params.at("embed_dim"); | |
| 60 | + op->params["1"] = captured_params.at("num_heads"); | |
| 61 | + | |
| 62 | + const int embed_dim = captured_params.at("embed_dim").i; | |
| 63 | + const int qdim = captured_params.at("qdim").i; | |
| 64 | + const int kdim = captured_params.at("kdim").i; | |
| 65 | + const int vdim = captured_params.at("vdim").i; | |
| 66 | + | |
| 67 | + op->params["2"] = embed_dim * qdim; | |
| 68 | + op->params["3"] = kdim; | |
| 69 | + op->params["4"] = vdim; | |
| 70 | + op->params["5"] = 1; | |
| 71 | + op->params["6"] = captured_params.at("scale"); | |
| 72 | + | |
| 73 | + op->attrs["0"] = Attribute(); | |
| 74 | + op->attrs["0"].data = {0, 0, 0, 0}; | |
| 75 | + op->attrs["1"] = captured_attrs.at("op_0.weight"); | |
| 76 | + if (captured_params.at("qbias").b) | |
| 77 | + { | |
| 78 | + op->attrs["2"] = captured_attrs.at("op_0.bias"); | |
| 79 | + } | |
| 80 | + else | |
| 81 | + { | |
| 82 | + op->attrs["2"] = Attribute({embed_dim}, std::vector<float>(embed_dim, 0.f)); | |
| 83 | + } | |
| 84 | + op->attrs["3"] = Attribute(); | |
| 85 | + op->attrs["3"].data = {0, 0, 0, 0}; | |
| 86 | + op->attrs["4"] = captured_attrs.at("op_1.weight"); | |
| 87 | + if (captured_params.at("kbias").b) | |
| 88 | + { | |
| 89 | + op->attrs["5"] = captured_attrs.at("op_1.bias"); | |
| 90 | + } | |
| 91 | + else | |
| 92 | + { | |
| 93 | + op->attrs["5"] = Attribute({embed_dim}, std::vector<float>(embed_dim, 0.f)); | |
| 94 | + } | |
| 95 | + op->attrs["6"] = Attribute(); | |
| 96 | + op->attrs["6"].data = {0, 0, 0, 0}; | |
| 97 | + op->attrs["7"] = captured_attrs.at("op_2.weight"); | |
| 98 | + if (captured_params.at("vbias").b) | |
| 99 | + { | |
| 100 | + op->attrs["8"] = captured_attrs.at("op_2.bias"); | |
| 101 | + } | |
| 102 | + else | |
| 103 | + { | |
| 104 | + op->attrs["8"] = Attribute({embed_dim}, std::vector<float>(embed_dim, 0.f)); | |
| 105 | + } | |
| 106 | + op->attrs["9"] = Attribute(); | |
| 107 | + op->attrs["9"].data = {0, 0, 0, 0}; | |
| 108 | + op->attrs["a"] = captured_attrs.at("out_proj.weight"); | |
| 109 | + if (captured_params.at("outbias").b) | |
| 110 | + { | |
| 111 | + op->attrs["b"] = captured_attrs.at("out_proj.bias"); | |
| 112 | + } | |
| 113 | + else | |
| 114 | + { | |
| 115 | + op->attrs["b"] = Attribute({qdim}, std::vector<float>(qdim, 0.f)); | |
| 116 | + } | |
| 117 | + } | |
| 118 | +}; | |
| 119 | + | |
| 120 | +REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_scaled_dot_product_attention, 10) | |
| 121 | + | |
| 122 | +class F_scaled_dot_product_attention_1 : public F_scaled_dot_product_attention | |
| 123 | +{ | |
| 124 | +public: | |
| 125 | + const char* match_pattern_graph() const | |
| 126 | + { | |
| 127 | + return R"PNNXIR(7767517 | |
| 128 | +17 16 | |
| 129 | +pnnx.Input input_0 0 1 input | |
| 130 | +pnnx.Input input_1 0 1 kv | |
| 131 | +pnnx.Input input_2 0 1 attn_mask | |
| 132 | +nn.Linear op_0 1 1 input q bias=%qbias in_features=%qdim out_features=%embed_dim @bias @weight | |
| 133 | +nn.Linear op_1 1 1 kv k bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight | |
| 134 | +nn.Linear op_2 1 1 kv v bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight | |
| 135 | +Tensor.reshape op_3 1 1 q 10 shape=(%batch,%qsize,%num_heads,%feat_per_head) | |
| 136 | +Tensor.reshape op_4 1 1 k 12 shape=(%batch,%size,%num_heads,%feat_per_head) | |
| 137 | +Tensor.reshape op_5 1 1 v 14 shape=(%batch,%size,%num_heads,%feat_per_head) | |
| 138 | +Tensor.permute op_6 1 1 10 16 dims=(0,2,1,3) | |
| 139 | +Tensor.permute op_7 1 1 12 17 dims=(0,2,1,3) | |
| 140 | +Tensor.permute op_8 1 1 14 18 dims=(0,2,1,3) | |
| 141 | +F.scaled_dot_product_attention op_9 4 1 16 17 18 attn_mask 19 dropout_p=0.0 is_causal=False scale=%scale | |
| 142 | +Tensor.permute op_10 1 1 19 20 dims=(0,2,1,3) | |
| 143 | +Tensor.reshape op_11 1 1 20 21 shape=(%batch,%qsize,%embed_dim) | |
| 144 | +nn.Linear out_proj 1 1 21 out bias=%outbias in_features=%embed_dim out_features=%qdim @bias @weight | |
| 145 | +pnnx.Output output 1 0 out | |
| 146 | +)PNNXIR"; | |
| 147 | + } | |
| 148 | +}; | |
| 149 | + | |
| 150 | +REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_scaled_dot_product_attention_1, 10) | |
| 151 | + | |
| 152 | +class F_scaled_dot_product_attention_2 : public F_scaled_dot_product_attention | |
| 153 | +{ | |
| 154 | +public: | |
| 155 | + const char* match_pattern_graph() const | |
| 156 | + { | |
| 157 | + return R"PNNXIR(7767517 | |
| 158 | +15 14 | |
| 159 | +pnnx.Input input 0 1 input | |
| 160 | +nn.Linear op_0 1 1 input q bias=%qbias in_features=%qdim out_features=%embed_dim @bias @weight | |
| 161 | +nn.Linear op_1 1 1 input k bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight | |
| 162 | +nn.Linear op_2 1 1 input v bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight | |
| 163 | +Tensor.reshape op_3 1 1 q 10 shape=(%batch,%size,%num_heads,%feat_per_head) | |
| 164 | +Tensor.reshape op_4 1 1 k 12 shape=(%batch,%size,%num_heads,%feat_per_head) | |
| 165 | +Tensor.reshape op_5 1 1 v 14 shape=(%batch,%size,%num_heads,%feat_per_head) | |
| 166 | +Tensor.permute op_6 1 1 10 16 dims=(0,2,1,3) | |
| 167 | +Tensor.permute op_7 1 1 12 17 dims=(0,2,1,3) | |
| 168 | +Tensor.permute op_8 1 1 14 18 dims=(0,2,1,3) | |
| 169 | +F.scaled_dot_product_attention op_9 3 1 16 17 18 19 dropout_p=0.0 is_causal=False attn_mask=None scale=%scale | |
| 170 | +Tensor.permute op_10 1 1 19 20 dims=(0,2,1,3) | |
| 171 | +Tensor.reshape op_11 1 1 20 21 shape=(%batch,%size,%embed_dim) | |
| 172 | +nn.Linear out_proj 1 1 21 out bias=%outbias in_features=%embed_dim out_features=%qdim @bias @weight | |
| 173 | +pnnx.Output output 1 0 out | |
| 174 | +)PNNXIR"; | |
| 175 | + } | |
| 176 | + | |
| 177 | + void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const | |
| 178 | + { | |
| 179 | + F_scaled_dot_product_attention::write(op, captured_params, captured_attrs); | |
| 180 | + op->params["5"] = 0; | |
| 181 | + } | |
| 182 | +}; | |
| 183 | + | |
| 184 | +REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_scaled_dot_product_attention_2, 10) | |
| 185 | + | |
| 186 | +class F_scaled_dot_product_attention_3 : public F_scaled_dot_product_attention | |
| 187 | +{ | |
| 188 | +public: | |
| 189 | + const char* match_pattern_graph() const | |
| 190 | + { | |
| 191 | + return R"PNNXIR(7767517 | |
| 192 | +16 15 | |
| 193 | +pnnx.Input input_0 0 1 input | |
| 194 | +pnnx.Input input_1 0 1 kv | |
| 195 | +nn.Linear op_0 1 1 input q bias=%qbias in_features=%qdim out_features=%embed_dim @bias @weight | |
| 196 | +nn.Linear op_1 1 1 kv k bias=%kbias in_features=%kdim out_features=%embed_dim @bias @weight | |
| 197 | +nn.Linear op_2 1 1 kv v bias=%vbias in_features=%vdim out_features=%embed_dim @bias @weight | |
| 198 | +Tensor.reshape op_3 1 1 q 10 shape=(%batch,%qsize,%num_heads,%feat_per_head) | |
| 199 | +Tensor.reshape op_4 1 1 k 12 shape=(%batch,%size,%num_heads,%feat_per_head) | |
| 200 | +Tensor.reshape op_5 1 1 v 14 shape=(%batch,%size,%num_heads,%feat_per_head) | |
| 201 | +Tensor.permute op_6 1 1 10 16 dims=(0,2,1,3) | |
| 202 | +Tensor.permute op_7 1 1 12 17 dims=(0,2,1,3) | |
| 203 | +Tensor.permute op_8 1 1 14 18 dims=(0,2,1,3) | |
| 204 | +F.scaled_dot_product_attention op_9 3 1 16 17 18 19 dropout_p=0.0 is_causal=False attn_mask=None scale=%scale | |
| 205 | +Tensor.permute op_10 1 1 19 20 dims=(0,2,1,3) | |
| 206 | +Tensor.reshape op_11 1 1 20 21 shape=(%batch,%qsize,%embed_dim) | |
| 207 | +nn.Linear out_proj 1 1 21 out bias=%outbias in_features=%embed_dim out_features=%qdim @bias @weight | |
| 208 | +pnnx.Output output 1 0 out | |
| 209 | +)PNNXIR"; | |
| 210 | + } | |
| 211 | + | |
| 212 | + void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const | |
| 213 | + { | |
| 214 | + F_scaled_dot_product_attention::write(op, captured_params, captured_attrs); | |
| 215 | + op->params["5"] = 0; | |
| 216 | + } | |
| 217 | +}; | |
| 218 | + | |
| 219 | +REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(F_scaled_dot_product_attention_3, 10) | |
| 220 | + | |
| 221 | +} // namespace ncnn | |
| 222 | + | |
| 223 | +} // namespace pnnx |