2
0

backend.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python
  2. ''' Expermiental Python Server backend test '''
  3. import os
  4. import sys
  5. root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  6. sys.path.append(root_dir)
  7. sys.pycache_prefix = os.path.join(root_dir, 'dist', 'pycache', 'test', 'backend')
  8. netron = __import__('source')
  9. third_party_dir = os.path.join(root_dir, 'third_party')
  10. test_data_dir = os.path.join(third_party_dir, 'test')
  11. def _test_onnx():
  12. file = os.path.join(test_data_dir, 'onnx', 'candy.onnx')
  13. onnx = __import__('onnx')
  14. model = onnx.load(file)
  15. netron.serve(None, model)
  16. def _test_onnx_iterate():
  17. folder = os.path.join(test_data_dir, 'onnx')
  18. for item in os.listdir(folder):
  19. file = os.path.join(folder, item)
  20. if file.endswith('.onnx') and \
  21. item != 'super_resolution.onnx' and \
  22. item != 'arcface-resnet100.onnx':
  23. print(item)
  24. onnx = __import__('onnx')
  25. model = onnx.load(file)
  26. address = netron.serve(file, model, verbosity='quiet')
  27. netron.stop(address)
  28. def _test_torchscript(file):
  29. torch = __import__('torch')
  30. model = torch.load(os.path.join(test_data_dir, 'pytorch', file))
  31. torch._C._jit_pass_inline(model.graph) # pylint: disable=protected-access
  32. netron.serve(file, model)
  33. def _test_torchscript_transformer():
  34. torch = __import__('torch')
  35. model = torch.nn.Transformer(nhead=16, num_encoder_layers=12)
  36. module = torch.jit.trace(model, (torch.rand(10, 32, 512), torch.rand(20, 32, 512)))
  37. # module = torch.jit.script(model)
  38. torch._C._jit_pass_inline(module.graph) # pylint: disable=protected-access
  39. netron.serve('transformer', module)
  40. def _test_torchscript_resnet34():
  41. torch = __import__('torch')
  42. torchvision = __import__('torchvision')
  43. model = torchvision.models.resnet34()
  44. # model = torchvision.models.alexnet(weights=torchvision.models.AlexNet_Weights.DEFAULT)
  45. # model = torchvision.models.resnet34(weights=torchvision.models.ResNet34_Weights.DEFAULT)
  46. state_dict = torch.load(os.path.join(test_data_dir, 'pytorch', 'resnet34-333f7ec4.pth'))
  47. model.load_state_dict(state_dict)
  48. trace = torch.jit.trace(model, torch.zeros([1, 3, 224, 224]), strict=True)
  49. torch._C._jit_pass_inline(trace.graph) # pylint: disable=protected-access
  50. netron.serve('resnet34', trace)
  51. def _test_torchscript_quantized():
  52. torch = __import__('torch')
  53. __import__('torchvision')
  54. torch.backends.quantized.engine = 'qnnpack'
  55. trace = torch.jit.load(os.path.join(test_data_dir, 'pytorch', 'd2go.pt'))
  56. torch._C._jit_pass_inline(trace.graph) # pylint: disable=protected-access
  57. netron.serve('d2go', trace)
  58. # _test_onnx()
  59. # _test_onnx_iterate()
  60. # _test_torchscript('alexnet.pt')
  61. _test_torchscript('gpt2.pt')
  62. # _test_torchscript('inception_v3_traced.pt')
  63. # _test_torchscript('netron_issue_920.pt') # scalar
  64. # _test_torchscript('fasterrcnn_resnet50_fpn.pt') # tuple
  65. # _test_torchscript('mobilenetv2-quant_full-nnapi.pt') # nnapi
  66. # _test_torchscript_quantized()
  67. # _test_torchscript_resnet34()
  68. # _test_torchscript_transformer()