| 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_onnx.h" |
| 16 | #include "ir.h" |
| 17 | |
| 18 | #include "onnx-ml.pb.h" |
| 19 | |
| 20 | namespace pnnx { |
| 21 | |
| 22 | namespace onnx2pnnx { |
| 23 | |
| 24 | class MultiheadAttention : public FuseFunctionPass |
| 25 | { |
| 26 | public: |
| 27 | const char* match_type_str() const |
| 28 | { |
| 29 | return "nn.MultiheadAttention"; |
| 30 | } |
| 31 | |
| 32 | const char* type_str() const |
| 33 | { |
| 34 | return "nn.MultiheadAttention"; |
| 35 | } |
| 36 | |
| 37 | void write(Operator* op, const OnnxFunctionProxy& function) const |
| 38 | { |
| 39 | const OnnxNodeProxy attention_scale = function.typed_node("_attention_scale"); |
| 40 | |
| 41 | const OnnxNodeProxy reshape_heads = function.find_producer(attention_scale.node.input(0)); |
| 42 | |
| 43 | const OnnxNodeProxy constant_shape = function.find_producer(reshape_heads.node.input(1)); |
| 44 | |
| 45 | if (constant_shape.node.op_type() == "Constant") |
| 46 | { |
| 47 | std::vector<int64_t> shape = constant_shape.attribute("value"); |
| 48 | op->params["num_heads"] = shape[1]; |
| 49 | } |
| 50 | |
| 51 | const OnnxNodeProxy transpose = function.typed_node("Transpose"); |
| 52 | std::vector<int64_t> perm = transpose.attribute("perm"); |
| 53 | if (perm == std::vector<int64_t> {1, 0, 2}) |
| 54 | { |
| 55 | op->params["batch_first"] = true; |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | op->params["batch_first"] = false; |
| 60 | } |
| 61 | |
| 62 | op->params["add_zero_attn"] = false; // TODO |
| 63 | |
| 64 | if (function.has_typed_node("_aten_scaled_dot_product_attention_no_mask_onnx")) |
| 65 | { |
| 66 | // TODO handle attn_mask |
| 67 | } |
| 68 | |
| 69 | if (function.has_initializer("in_proj_weight")) |
| 70 | { |
| 71 | const onnx::TensorProto& in_proj_weight = function.initializer("in_proj_weight"); |
| 72 | |
| 73 | op->params["embed_dim"] = in_proj_weight.dims(1); |
| 74 | op->params["kdim"] = in_proj_weight.dims(1); |
| 75 | op->params["vdim"] = in_proj_weight.dims(1); |
| 76 | op->attrs["in_proj_weight"] = in_proj_weight; |
| 77 | } |
| 78 | else |
| 79 | { |
| 80 | const onnx::TensorProto& q_proj_weight = function.initializer("q_proj_weight"); |
| 81 | const onnx::TensorProto& k_proj_weight = function.initializer("k_proj_weight"); |
| 82 | const onnx::TensorProto& v_proj_weight = function.initializer("v_proj_weight"); |
| 83 | |
| 84 | op->params["embed_dim"] = q_proj_weight.dims(1); |
| 85 | op->params["kdim"] = k_proj_weight.dims(1); |
| 86 | op->params["vdim"] = v_proj_weight.dims(1); |
| 87 | op->attrs["q_proj_weight"] = q_proj_weight; |
| 88 | op->attrs["k_proj_weight"] = k_proj_weight; |
| 89 | op->attrs["v_proj_weight"] = v_proj_weight; |
| 90 | } |
| 91 | |
| 92 | op->attrs["out_proj.weight"] = function.initializer("weight"); |
| 93 | |
| 94 | if (function.has_initializer("in_proj_bias") && function.has_initializer("bias")) |
| 95 | { |
| 96 | op->params["bias"] = true; |
| 97 | op->attrs["in_proj_bias"] = function.initializer("in_proj_bias"); |
| 98 | op->attrs["out_proj.bias"] = function.initializer("bias"); |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | op->params["bias"] = false; |
| 103 | } |
| 104 | |
| 105 | if (function.has_initializer("bias_k") && function.has_initializer("bias_v")) |
| 106 | { |
| 107 | op->params["add_bias_kv"] = true; |
| 108 | op->attrs["bias_k"] = function.initializer("bias_k"); |
| 109 | op->attrs["bias_v"] = function.initializer("bias_v"); |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | op->params["add_bias_kv"] = false; |
| 114 | } |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | REGISTER_GLOBAL_PNNX_FUSE_FUNCTION_PASS(MultiheadAttention) |
| 119 | |
| 120 | } // namespace onnx2pnnx |
| 121 | |
| 122 | } // namespace pnnx |