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.

profiler.py 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #! /usr/bin/env python3
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import argparse
  10. import getopt
  11. import os
  12. import runpy
  13. import sys
  14. from megengine.logger import get_logger
  15. from megengine.utils.profiler import Profiler, merge_trace_events
  16. def main():
  17. parser = argparse.ArgumentParser(
  18. prog="megengine.tools.profiler", description="Profiling megengine program"
  19. )
  20. parser.add_argument(
  21. "-m", "--module", action="store_true", help="whether launch program as module"
  22. )
  23. parser.add_argument("-o", "--output", type=str, help="output file location")
  24. parser.add_argument(
  25. "-f",
  26. "--format",
  27. action="append",
  28. type=str,
  29. help="output file format",
  30. choices=Profiler.valid_formats,
  31. )
  32. parser.add_argument(
  33. "--merge_trace_events", action="store_true",
  34. )
  35. parser.add_argument(
  36. "--clean", action="store_true",
  37. )
  38. for opt in Profiler.valid_options:
  39. parser.add_argument("--" + opt, type=int, default=None)
  40. args, extras = parser.parse_known_args(sys.argv[1:])
  41. prof_args = {}
  42. for opt in Profiler.valid_options:
  43. optval = getattr(args, opt, None)
  44. if optval is not None:
  45. prof_args[opt] = optval
  46. if args.output is not None:
  47. prof_args["path"] = args.output
  48. if args.format:
  49. prof_args["formats"] = args.format
  50. if args.clean:
  51. for file in os.listdir(profiler.directory):
  52. os.remove(os.path.join(profiler.directory, file))
  53. if len(extras) == 0:
  54. if not args.merge_trace_events:
  55. parser.print_usage()
  56. exit(1)
  57. else:
  58. filename = extras[0]
  59. if not args.module:
  60. if not os.path.exists(filename):
  61. get_logger().fatal("cannot find file {}".format(filename))
  62. exit(1)
  63. filename = os.path.realpath(filename)
  64. # Replace profiler's dir with script's dir in front of module search path.
  65. sys.path[0] = os.path.dirname(filename)
  66. sys.argv[:] = [filename, *extras[1:]]
  67. profiler = Profiler(**prof_args)
  68. with profiler:
  69. if args.module:
  70. runpy.run_module(filename)
  71. else:
  72. run_script(filename)
  73. profiler.dump()
  74. if args.merge_trace_events:
  75. merge_trace_events(profiler.directory)
  76. def run_module(modulename):
  77. runpy.run_module(modulename, None, "__main__", True)
  78. def run_script(filename):
  79. runpy.run_path(filename, None, "__main__")
  80. if __name__ == "__main__":
  81. main()

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台