You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_Tensor_repeat.py 1.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. import torch
  15. import torch.nn as nn
  16. import torch.nn.functional as F
  17. class Model(nn.Module):
  18. def __init__(self):
  19. super(Model, self).__init__()
  20. def forward(self, x, y, z):
  21. x = x.repeat(1, 2, 3)
  22. x = x.repeat(2, 3, 4)
  23. y = y.repeat(1, 2, 1, 4)
  24. y = y.repeat(3, 4, 5, 1)
  25. z = z.repeat(1, 2, 3, 1, 5)
  26. z = z.repeat(2, 3, 3, 1, 1)
  27. return x, y, z
  28. def test():
  29. net = Model()
  30. net.eval()
  31. torch.manual_seed(0)
  32. x = torch.rand(1, 3, 16)
  33. y = torch.rand(1, 5, 9, 11)
  34. z = torch.rand(14, 8, 5, 9, 10)
  35. a = net(x, y, z)
  36. # export torchscript
  37. mod = torch.jit.trace(net, (x, y, z))
  38. mod.save("test_Tensor_repeat.pt")
  39. # torchscript to pnnx
  40. import os
  41. os.system("../src/pnnx test_Tensor_repeat.pt inputshape=[1,3,16],[1,5,9,11],[14,8,5,9,10]")
  42. # pnnx inference
  43. import test_Tensor_repeat_pnnx
  44. b = test_Tensor_repeat_pnnx.test_inference()
  45. for a0, b0 in zip(a, b):
  46. if not torch.equal(a0, b0):
  47. return False
  48. return True
  49. if __name__ == "__main__":
  50. if test():
  51. exit(0)
  52. else:
  53. exit(1)