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.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  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 json
  10. from contextlib import contextmanager
  11. from typing import List
  12. from ..core._imperative_rt.core2 import (
  13. pop_scope,
  14. push_scope,
  15. start_profile,
  16. stop_profile,
  17. sync,
  18. )
  19. class Profiler:
  20. r"""
  21. Profile graph execution in imperative mode.
  22. :type path: Optional[str]
  23. :param path: default path prefix for profiler to dump.
  24. Examples:
  25. .. code-block::
  26. import megengine as mge
  27. import megengine.module as M
  28. from megengine.utils.profiler import Profiler
  29. # With Learnable Parameters
  30. for iter in range(0, 10):
  31. # Only profile record of last iter would be saved
  32. with Profiler("profile"):
  33. # your code here
  34. # Then open the profile file in chrome timeline window
  35. """
  36. CHROME_TIMELINE = "chrome_timeline.json"
  37. COMMAND = 1 << 0
  38. OPERATOR = 1 << 1
  39. TENSOR_LIFETIME = 1 << 2
  40. TENSOR_PROP = 1 << 3
  41. SYNC = 1 << 4
  42. SCOPE = 1 << 5
  43. ALL = (1 << 6) - 1
  44. def __init__(
  45. self,
  46. path: str = "profile",
  47. format: str = CHROME_TIMELINE,
  48. *,
  49. topic=OPERATOR | SCOPE,
  50. align_time=True,
  51. show_operator_name=True
  52. ) -> None:
  53. self._path = path
  54. self._format = format
  55. self._options = {
  56. "topic": int(topic),
  57. "align_time": int(align_time),
  58. "show_operator_name": int(show_operator_name),
  59. }
  60. def __enter__(self):
  61. start_profile(self._options)
  62. return self
  63. def __exit__(self, val, tp, trace):
  64. stop_profile(self._path, self._format)
  65. # dump is async, so it's necessary to sync interpreter
  66. sync()
  67. def __call__(self, func):
  68. def wrapper(*args, **kwargs):
  69. with self:
  70. return func(*args, **kwargs)
  71. return wrapper
  72. @contextmanager
  73. def scope(name):
  74. push_scope(name)
  75. yield
  76. pop_scope(name)
  77. profile = Profiler
  78. def merge_trace_events(sources: List[str], target: str):
  79. names = list(map(lambda x: x + ".chrome_timeline.json", sources))
  80. result = []
  81. for name in names:
  82. with open(name, "r", encoding="utf-8") as f:
  83. content = json.load(f)
  84. for entry in content:
  85. result.append(entry)
  86. with open(target + ".chrome_timeline.json", "w") as f:
  87. json.dump(result, f, ensure_ascii=False, indent=4)

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