| 1 | # Tencent is pleased to support the open source community by making ncnn available. |
| 2 | # |
| 3 | # Copyright (C) 2021 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 | import torch |
| 16 | import torch.nn as nn |
| 17 | import torch.nn.functional as F |
| 18 | from packaging import version |
| 19 | |
| 20 | class Model(nn.Module): |
| 21 | def __init__(self): |
| 22 | super(Model, self).__init__() |
| 23 | |
| 24 | self.attention_0_0 = nn.MultiheadAttention(embed_dim=64, num_heads=4) |
| 25 | self.attention_0_1 = nn.MultiheadAttention(embed_dim=64, num_heads=8, bias=False, add_bias_kv=False, add_zero_attn=False) |
| 26 | self.attention_0_2 = nn.MultiheadAttention(embed_dim=64, num_heads=16, bias=True, add_bias_kv=True, add_zero_attn=True) |
| 27 | |
| 28 | self.attention_0_3 = nn.MultiheadAttention(embed_dim=32, num_heads=8, bias=True) |
| 29 | |
| 30 | self.attention_0_4 = nn.MultiheadAttention(embed_dim=40, num_heads=4, kdim=30, vdim=20) |
| 31 | self.attention_0_5 = nn.MultiheadAttention(embed_dim=40, num_heads=8, kdim=30, vdim=20, bias=False, add_bias_kv=False, add_zero_attn=False) |
| 32 | self.attention_0_6 = nn.MultiheadAttention(embed_dim=40, num_heads=10, kdim=30, vdim=20, bias=True, add_bias_kv=True, add_zero_attn=True) |
| 33 | |
| 34 | if version.parse(torch.__version__) >= version.parse('1.9'): |
| 35 | self.attention_1_0 = nn.MultiheadAttention(embed_dim=64, num_heads=4, batch_first=True) |
| 36 | self.attention_1_1 = nn.MultiheadAttention(embed_dim=64, num_heads=8, bias=False, add_bias_kv=False, add_zero_attn=False, batch_first=True) |
| 37 | self.attention_1_2 = nn.MultiheadAttention(embed_dim=64, num_heads=16, bias=True, add_bias_kv=True, add_zero_attn=True, batch_first=True) |
| 38 | |
| 39 | self.attention_1_3 = nn.MultiheadAttention(embed_dim=32, num_heads=8, bias=True, batch_first=True) |
| 40 | |
| 41 | self.attention_1_4 = nn.MultiheadAttention(embed_dim=40, num_heads=4, kdim=30, vdim=20, batch_first=True) |
| 42 | self.attention_1_5 = nn.MultiheadAttention(embed_dim=40, num_heads=8, kdim=30, vdim=20, bias=False, add_bias_kv=False, add_zero_attn=False, batch_first=True) |
| 43 | self.attention_1_6 = nn.MultiheadAttention(embed_dim=40, num_heads=10, kdim=30, vdim=20, bias=True, add_bias_kv=True, add_zero_attn=True, batch_first=True) |
| 44 | |
| 45 | def forward(self, xq, xk, xv, z, zmask, yq, yk, yv, ymask, ymask2): |
| 46 | x0, x0w = self.attention_0_0(xq, xk, xv) |
| 47 | x1, x1w = self.attention_0_1(xq, xk, xv) |
| 48 | x2, x2w = self.attention_0_2(xq, xk, xk) |
| 49 | |
| 50 | x3, _ = self.attention_0_3(z, z, z, need_weights=False) |
| 51 | x33, _ = self.attention_0_3(z, z, z, attn_mask=zmask) |
| 52 | |
| 53 | x4, x4w = self.attention_0_4(yq, yk, yv) |
| 54 | x5, x5w = self.attention_0_5(yq, yk, yv, attn_mask=ymask) |
| 55 | x6, x6w = self.attention_0_6(yq, yk, yv, attn_mask=ymask2) |
| 56 | |
| 57 | if version.parse(torch.__version__) < version.parse('1.9'): |
| 58 | return x0, x0w, x1, x1w, x2, x2w, x3, x33, x4, x4w, x5, x5w, x6, x6w |
| 59 | |
| 60 | xq = xq.transpose(0, 1) |
| 61 | xk = xk.transpose(0, 1) |
| 62 | xv = xv.transpose(0, 1) |
| 63 | z = z.transpose(0, 1) |
| 64 | yq = yq.transpose(0, 1) |
| 65 | yk = yk.transpose(0, 1) |
| 66 | yv = yv.transpose(0, 1) |
| 67 | |
| 68 | y0, y0w = self.attention_1_0(xq, xk, xv) |
| 69 | y1, y1w = self.attention_1_1(xq, xk, xv) |
| 70 | y2, y2w = self.attention_1_2(xq, xk, xk) |
| 71 | |
| 72 | y3, _ = self.attention_1_3(z, z, z) |
| 73 | if version.parse(torch.__version__) >= version.parse('1.12') and version.parse(torch.__version__) < version.parse('1.13'): |
| 74 | # HACK pytorch 1.12 breaks 2-dim zmask |
| 75 | # https://github.com/pytorch/pytorch/issues/97409 |
| 76 | # zmask2 = zmask.reshape(1, 1, 30, 30).expand(1, 8, 30, 30) |
| 77 | # y33, _ = self.attention_1_3(z, z, z, attn_mask=zmask2) |
| 78 | # but it produce all nan then, skip test :( |
| 79 | y33 = y3 |
| 80 | elif version.parse(torch.__version__) >= version.parse('2.0') and version.parse(torch.__version__) < version.parse('2.1'): |
| 81 | # HACK pytorch 2.0 produce all nan, skip test :( |
| 82 | y33 = y3 |
| 83 | else: |
| 84 | y33, _ = self.attention_1_3(z, z, z, attn_mask=zmask) |
| 85 | |
| 86 | y4, y4w = self.attention_1_4(yq, yk, yv) |
| 87 | y5, y5w = self.attention_1_5(yq, yk, yv, attn_mask=ymask) |
| 88 | y6, y6w = self.attention_1_6(yq, yk, yv, attn_mask=ymask2) |
| 89 | |
| 90 | return x0, x0w, x1, x1w, x2, x2w, x3, x33, x4, x4w, x5, x5w, x6, x6w, y0, y0w, y1, y1w, y2, y2w, y3, y33, y4, y4w, y5, y5w, y6, y6w |
| 91 | |
| 92 | def test(): |
| 93 | torch.set_grad_enabled(False) |
| 94 | |
| 95 | net = Model() |
| 96 | net.eval() |
| 97 | |
| 98 | torch.manual_seed(0) |
| 99 | xq = torch.rand(20, 1, 64) |
| 100 | xk = torch.rand(20, 1, 64) |
| 101 | xv = torch.rand(20, 1, 64) |
| 102 | z = torch.rand(30, 1, 32) |
| 103 | zmask = torch.rand(30, 30) |
| 104 | yq = torch.rand(15, 1, 40) |
| 105 | yk = torch.rand(24, 1, 30) |
| 106 | yv = torch.rand(24, 1, 20) |
| 107 | ymask = torch.rand(15, 24) |
| 108 | ymask2 = torch.rand(10, 15, 24) |
| 109 | |
| 110 | a = net(xq, xk, xv, z, zmask, yq, yk, yv, ymask, ymask2) |
| 111 | |
| 112 | # export torchscript |
| 113 | print(torch.__version__) |
| 114 | if version.parse(torch.__version__) >= version.parse('1.12.0'): |
| 115 | mod = torch.jit.trace(net, (xq, xk, xv, z, zmask, yq, yk, yv, ymask, ymask2), check_trace=False) |
| 116 | else: |
| 117 | mod = torch.jit.trace(net, (xq, xk, xv, z, zmask, yq, yk, yv, ymask, ymask2)) |
| 118 | mod.save("test_nn_MultiheadAttention.pt") |
| 119 | |
| 120 | # torchscript to pnnx |
| 121 | import os |
| 122 | os.system("../src/pnnx test_nn_MultiheadAttention.pt inputshape=[20,1,64],[20,1,64],[20,1,64],[30,1,32],[30,30],[15,1,40],[24,1,30],[24,1,20],[15,24],[10,15,24]") |
| 123 | |
| 124 | # pnnx inference |
| 125 | import test_nn_MultiheadAttention_pnnx |
| 126 | b = test_nn_MultiheadAttention_pnnx.test_inference() |
| 127 | |
| 128 | for a0, b0 in zip(a, b): |
| 129 | if not torch.allclose(a0, b0, 1e-4, 1e-4): |
| 130 | return False |
| 131 | return True |
| 132 | |
| 133 | if __name__ == "__main__": |
| 134 | if test(): |
| 135 | exit(0) |
| 136 | else: |
| 137 | exit(1) |