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_new_empty.py 1.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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):
  21. out0 = x.new_empty((2,2))
  22. out1 = x.new_empty(3)
  23. out2 = x.new_empty((4,5,6,7,8))
  24. out3 = x.new_empty((1,2,1))
  25. out4 = x.new_empty((3,3,3,3), dtype=torch.long)
  26. return out0, out1, out2, out3, out4
  27. def test():
  28. net = Model()
  29. net.eval()
  30. torch.manual_seed(0)
  31. x = torch.rand(1, 16)
  32. a = net(x)
  33. # export torchscript
  34. mod = torch.jit.trace(net, x)
  35. mod.save("test_Tensor_new_empty.pt")
  36. # torchscript to pnnx
  37. import os
  38. os.system("../src/pnnx test_Tensor_new_empty.pt inputshape=[1,16]")
  39. # pnnx inference
  40. import test_Tensor_new_empty_pnnx
  41. b = test_Tensor_new_empty_pnnx.test_inference()
  42. # test shape only for uninitialized data
  43. for a0, b0 in zip(a, b):
  44. if not a0.shape == b0.shape:
  45. return False
  46. return True
  47. if __name__ == "__main__":
  48. if test():
  49. exit(0)
  50. else:
  51. exit(1)