| 1 | # Tencent is pleased to support the open source community by making ncnn available. |
| 2 | # |
| 3 | # Copyright (C) 2022 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 | |
| 19 | class Model(nn.Module): |
| 20 | def __init__(self): |
| 21 | super(Model, self).__init__() |
| 22 | |
| 23 | def forward(self, x, y0, y1, z0, z1, w, r0, r1, r2, s0, s1): |
| 24 | # identity |
| 25 | a0 = torch.einsum('i', y0) |
| 26 | a1 = torch.einsum('ij', x) |
| 27 | a2 = torch.einsum('ijk', z0) |
| 28 | a3 = torch.einsum('ijkl', w) |
| 29 | |
| 30 | # permute |
| 31 | b0 = torch.einsum('ij->ji', x) |
| 32 | b1 = torch.einsum('ba', x) |
| 33 | b2 = torch.einsum('jki', z0) |
| 34 | b3 = torch.einsum('ijk->kij', z0) |
| 35 | b4 = torch.einsum('kjil', w) |
| 36 | b5 = torch.einsum('ijkl->jilk', w) |
| 37 | b6 = torch.einsum('...ij->...ji', w) |
| 38 | b7 = torch.einsum('abc...->cba...', w) |
| 39 | |
| 40 | b8 = torch.einsum('ikj', z0) |
| 41 | b9 = torch.einsum('jik', z0) |
| 42 | b10 = torch.einsum('kij', z0) |
| 43 | b11 = torch.einsum('jki', z0) |
| 44 | b12 = torch.einsum('kji', z0) |
| 45 | |
| 46 | b13 = torch.einsum('ijlk', w) |
| 47 | b14 = torch.einsum('ikjl', w) |
| 48 | b15 = torch.einsum('iljk', w) |
| 49 | b16 = torch.einsum('iklj', w) |
| 50 | b17 = torch.einsum('ilkj', w) |
| 51 | b18 = torch.einsum('jikl', w) |
| 52 | b19 = torch.einsum('jilk', w) |
| 53 | b20 = torch.einsum('kijl', w) |
| 54 | b21 = torch.einsum('lijk', w) |
| 55 | b22 = torch.einsum('kilj', w) |
| 56 | b23 = torch.einsum('likj', w) |
| 57 | b24 = torch.einsum('jkil', w) |
| 58 | b25 = torch.einsum('jlik', w) |
| 59 | b26 = torch.einsum('kjil', w) |
| 60 | b27 = torch.einsum('ljik', w) |
| 61 | b28 = torch.einsum('klij', w) |
| 62 | b29 = torch.einsum('lkij', w) |
| 63 | b30 = torch.einsum('jkli', w) |
| 64 | b31 = torch.einsum('jlki', w) |
| 65 | b32 = torch.einsum('kjli', w) |
| 66 | b33 = torch.einsum('ljki', w) |
| 67 | b34 = torch.einsum('klji', w) |
| 68 | b35 = torch.einsum('lkji', w) |
| 69 | |
| 70 | # trace |
| 71 | c = torch.einsum('ii', x) |
| 72 | |
| 73 | # sum |
| 74 | d0 = torch.einsum('ij->', x) |
| 75 | d1 = torch.einsum('xyz->', z0) |
| 76 | d2 = torch.einsum('ijkl->', w) |
| 77 | |
| 78 | # sum axis |
| 79 | e0 = torch.einsum('ij->i', x) |
| 80 | e1 = torch.einsum('ij->j', x) |
| 81 | e2 = torch.einsum('ijk->i', z0) |
| 82 | e3 = torch.einsum('ijk->j', z0) |
| 83 | e4 = torch.einsum('ijk->k', z0) |
| 84 | e5 = torch.einsum('ijk->ij', z0) |
| 85 | e6 = torch.einsum('ijk->jk', z0) |
| 86 | e7 = torch.einsum('ijk->ik', z0) |
| 87 | e8 = torch.einsum('ijkl->i', w) |
| 88 | e9 = torch.einsum('ijkl->j', w) |
| 89 | e10 = torch.einsum('ijkl->k', w) |
| 90 | e11 = torch.einsum('ijkl->l', w) |
| 91 | e12 = torch.einsum('ijkl->ij', w) |
| 92 | e13 = torch.einsum('ijkl->jk', w) |
| 93 | e14 = torch.einsum('ijkl->kl', w) |
| 94 | e15 = torch.einsum('ijkl->il', w) |
| 95 | e16 = torch.einsum('ijkl->ijk', w) |
| 96 | e17 = torch.einsum('ijkl->jkl', w) |
| 97 | e18 = torch.einsum('ijkl->ijl', w) |
| 98 | |
| 99 | # matrix-vector |
| 100 | f0 = torch.einsum('ij,j->i', r0, y0) |
| 101 | f1 = torch.einsum('i,jki->jk', y1, r1) |
| 102 | |
| 103 | # vector-vector outer product |
| 104 | g0 = torch.einsum('i,j->ij', y0, y1) |
| 105 | g1 = torch.einsum('a,b,c,d->abcd', y0, y1, y0, y1) |
| 106 | |
| 107 | # batch mm |
| 108 | h0 = torch.einsum('bij,bjk->bik', z0, z1) |
| 109 | h1 = torch.einsum('bjk,bij->bik', z1, z0) |
| 110 | |
| 111 | # bilinear |
| 112 | i = torch.einsum('bn,anm,bm->ba', r0, r1, r2) |
| 113 | |
| 114 | # tensor contraction |
| 115 | j = torch.einsum('pqrs,tqvr->pstv', s0, s1) |
| 116 | |
| 117 | return a0, a1, a2, a3, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, c, d0, d1, d2, e0, e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18, f0, f1, g0, g1, h0, h1, i, j |
| 118 | |
| 119 | def test(): |
| 120 | net = Model() |
| 121 | net.eval() |
| 122 | |
| 123 | torch.manual_seed(0) |
| 124 | x = torch.rand(4, 4) |
| 125 | y0 = torch.rand(5) |
| 126 | y1 = torch.rand(4) |
| 127 | z0 = torch.rand(3, 2, 5) |
| 128 | z1 = torch.rand(3, 5, 4) |
| 129 | w = torch.rand(2, 3, 4, 5) |
| 130 | r0 = torch.rand(2, 5) |
| 131 | r1 = torch.rand(3, 5, 4) |
| 132 | r2 = torch.rand(2, 4) |
| 133 | s0 = torch.rand(2, 3, 5, 7) |
| 134 | s1 = torch.rand(11, 3, 17, 5) |
| 135 | |
| 136 | a = net(x, y0, y1, z0, z1, w, r0, r1, r2, s0, s1) |
| 137 | |
| 138 | # export torchscript |
| 139 | mod = torch.jit.trace(net, (x, y0, y1, z0, z1, w, r0, r1, r2, s0, s1)) |
| 140 | mod.save("test_torch_einsum.pt") |
| 141 | |
| 142 | # torchscript to pnnx |
| 143 | import os |
| 144 | os.system("../src/pnnx test_torch_einsum.pt inputshape=[4,4],[5],[4],[3,2,5],[3,5,4],[2,3,4,5],[2,5],[3,5,4],[2,4],[2,3,5,7],[11,3,17,5]") |
| 145 | |
| 146 | # pnnx inference |
| 147 | import test_torch_einsum_pnnx |
| 148 | b = test_torch_einsum_pnnx.test_inference() |
| 149 | |
| 150 | for a0, b0 in zip(a, b): |
| 151 | # allclose may auto broadcast compare |
| 152 | if a0.shape != b0.shape: |
| 153 | return False |
| 154 | if not torch.allclose(a0, b0, 1e-4, 1e-4): |
| 155 | return False |
| 156 | return True |
| 157 | |
| 158 | if __name__ == "__main__": |
| 159 | if test(): |
| 160 | exit(0) |
| 161 | else: |
| 162 | exit(1) |