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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 len(extras) == 0:
  51. if not args.merge_trace_events:
  52. parser.print_usage()
  53. exit(1)
  54. else:
  55. filename = extras[0]
  56. if not args.module:
  57. if not os.path.exists(filename):
  58. get_logger().fatal("cannot find file {}".format(filename))
  59. exit(1)
  60. filename = os.path.realpath(filename)
  61. # Replace profiler's dir with script's dir in front of module search path.
  62. sys.path[0] = os.path.dirname(filename)
  63. sys.argv[:] = [filename, *extras[1:]]
  64. profiler = Profiler(**prof_args)
  65. if args.clean:
  66. for file in os.listdir(profiler.directory):
  67. os.remove(os.path.join(profiler.directory, file))
  68. with profiler:
  69. if args.module:
  70. 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()