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.

test_profiler.py 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 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. import os
  11. import tempfile
  12. import pytest
  13. from megengine import Parameter
  14. from megengine import distributed as dist
  15. from megengine import tensor
  16. from megengine.core import option
  17. from megengine.jit import trace
  18. from megengine.module import Module
  19. from megengine.utils.profiler import Profiler, scope
  20. class Simple(Module):
  21. def __init__(self):
  22. super().__init__()
  23. self.a = Parameter([1.23], dtype="float32")
  24. def forward(self, x):
  25. x = x * self.a
  26. return x
  27. @pytest.mark.parametrize("format", ["chrome_timeline.json", "memory_flow.svg"])
  28. @pytest.mark.parametrize(
  29. "trace_mode", [True, False, None], ids=["symbolic", "no-symbolic", "no-trace"]
  30. )
  31. @pytest.mark.require_ngpu(1)
  32. def test_profiler(format, trace_mode):
  33. tempdir = tempfile.TemporaryDirectory()
  34. profile_prefix = tempdir.name
  35. profile_path = os.path.join(profile_prefix, "{}.{}".format(os.getpid(), format))
  36. def infer():
  37. with scope("my_scope"):
  38. oup = Simple()(tensor([1.23], dtype="float32"))
  39. return oup
  40. if trace_mode:
  41. infer = trace(symbolic=trace_mode)(infer)
  42. with Profiler(profile_prefix, format=format):
  43. infer()
  44. print(profile_path)
  45. assert os.path.exists(profile_path), "profiling results not found"
  46. if format == "chrome_timeline.json":
  47. with open(profile_path, "r") as f:
  48. events = json.load(f)
  49. if isinstance(events, dict):
  50. assert "traceEvents" in events
  51. events = events["traceEvents"]
  52. prev_ts = {}
  53. scope_count = 0
  54. for event in events:
  55. if "dur" in event:
  56. assert event["dur"] >= 0
  57. elif "ts" in event and "tid" in event:
  58. ts = event["ts"]
  59. tid = event["tid"]
  60. if ts != 0:
  61. assert (tid not in prev_ts) or prev_ts[tid] <= ts
  62. prev_ts[tid] = ts
  63. if "name" in event and event["name"] == "my_scope":
  64. scope_count += 1
  65. assert scope_count > 0 and scope_count % 2 == 0
  66. @pytest.mark.parametrize("format", ["chrome_timeline.json", "memory_flow.svg"])
  67. @pytest.mark.parametrize(
  68. "trace_mode", [True, False, None], ids=["symbolic", "no-symbolic", "no-trace"]
  69. )
  70. @pytest.mark.isolated_distributed
  71. @pytest.mark.require_ngpu(2)
  72. def test_profiler_dist(format, trace_mode):
  73. n_gpus = 2
  74. tempdir = tempfile.TemporaryDirectory()
  75. profile_prefix = tempdir.name
  76. profile_path = os.path.join(profile_prefix, "{}.{}".format(os.getpid(), format))
  77. def infer():
  78. with scope("my_scope"):
  79. oup = Simple()(tensor([1.23], dtype="float32"))
  80. return oup
  81. if trace_mode:
  82. infer = trace(symbolic=trace_mode)(infer)
  83. @dist.launcher(n_gpus=2)
  84. def worker():
  85. infer()
  86. with Profiler(profile_prefix, format=format):
  87. worker()
  88. assert os.path.exists(profile_path), "profiling results not found"
  89. assert len(os.listdir(tempdir.name)) == n_gpus + 1

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