Elffuss BraidHub

kikocis/ncnn public

master · 2.0 KiBRawPermalink
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
15import torch
16import torch.nn as nn
17import torch.nn.functional as F
18
19class Model(nn.Module):
20 def __init__(self):
21 super(Model, self).__init__()
22
23 def forward(self, x, y, z, w):
24 x = x.clone()
25 z = z.clone()
26 x = x.index_put(indices=[torch.tensor([10,2])], values=y, accumulate=False)
27 z.index_put_(indices=[torch.tensor([1,0,0]), torch.tensor([3,2,1])], values=w, accumulate=True)
28
29 x[torch.tensor([1], dtype=torch.int64)] = torch.tensor(45).float()
30 x[torch.tensor([], dtype=torch.int64)] = torch.tensor(233).float()
31 return x, z
32
33def test():
34 net = Model()
35 net.eval()
36
37 torch.manual_seed(0)
38 x = torch.rand(12)
39 y = torch.rand(2)
40 z = torch.rand(6,9)
41 w = torch.rand(3)
42
43 a = net(x, y, z, w)
44
45 # export torchscript
46 mod = torch.jit.trace(net, (x, y, z, w))
47 mod.save("test_Tensor_index_put.pt")
48
49 # torchscript to pnnx
50 import os
51 os.system("../src/pnnx test_Tensor_index_put.pt inputshape=[12],[2],[6,9],[3]")
52
53 # pnnx inference
54 import test_Tensor_index_put_pnnx
55 b = test_Tensor_index_put_pnnx.test_inference()
56
57 for a0, b0 in zip(a, b):
58 if not torch.equal(a0, b0):
59 return False
60 return True
61
62if __name__ == "__main__":
63 if test():
64 exit(0)
65 else:
66 exit(1)