Guest User

Untitled

a guest
Jan 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. import argparse
  2. import os
  3. import subprocess
  4. import sys
  5. import torch
  6.  
  7. def main():
  8. parser = argparse.ArgumentParser()
  9. parser.add_argument("--batch", type=int, default=256)
  10. parser.add_argument("--img-size", type=int, default=32)
  11. parser.add_argument("--in-channels", type=int, default=128)
  12. parser.add_argument("--out-channels", type=int, default=128)
  13. parser.add_argument("--kernel-size", type=int, default=3)
  14. parser.add_argument("--groups", type=bool, default=True)
  15. parser.add_argument("--internal", action="store_true")
  16.  
  17. args = parser.parse_args()
  18.  
  19. if args.internal:
  20. internal(args)
  21. else:
  22. wrapper(args)
  23.  
  24. def internal(args):
  25. # Profiling is switched on: do convolution.
  26. assert torch.cuda.is_available()
  27. torch.backends.cudnn.benchmark = True
  28.  
  29. groups = args.in_channels if args.groups else 1
  30. conv = torch.nn.Conv2d(args.in_channels, args.out_channels,
  31. args.kernel_size, groups = groups)
  32. conv = conv.cuda()
  33. data = torch.Tensor(args.batch, args.in_channels, args.img_size,
  34. args.img_size)
  35. data = torch.autograd.Variable(data.cuda())
  36.  
  37. with torch.cuda.profiler.profile():
  38. conv(data) # Warmup CUDA memory allocator and profiler
  39. with torch.autograd.profiler.emit_nvtx():
  40. conv(data)
  41.  
  42. def wrapper(args):
  43. # Switch profiling on, run this script again, and interpret the results.
  44. command = "nvprof --profile-from-start off -o trace.prof -- "
  45. command += "python3 " + " ".join(sys.argv[:]) + " --internal"
  46.  
  47. print(command)
  48.  
  49. subprocess.run(command, shell=True)
  50.  
  51. # Now read the trace file.
  52. profile = torch.autograd.profiler.load_nvprof("trace.prof")
  53. print(profile)
  54.  
  55. os.remove("trace.prof")
  56.  
  57. if __name__ == "__main__":
  58. main()
Add Comment
Please, Sign In to add comment