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 1.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 pytest
  12. from megengine import Parameter, tensor
  13. from megengine.core import option
  14. from megengine.module import Module
  15. from megengine.utils.profiler import Profiler, scope
  16. class Simple(Module):
  17. def __init__(self):
  18. super().__init__()
  19. self.a = Parameter([1.23], dtype="float32")
  20. def forward(self, x):
  21. x = x * self.a
  22. return x
  23. def test_profiler():
  24. profile_prefix = "pytest_profile"
  25. profile_format = "chrome_timeline.json"
  26. profile_path = "{}.{}".format(profile_prefix, profile_format)
  27. with Profiler(profile_prefix, format=profile_format):
  28. with scope("my_scope"):
  29. oup = Simple()(tensor([1.23], dtype="float32"))
  30. with open(profile_path, "r") as f:
  31. events = json.load(f)
  32. os.remove(profile_path)
  33. prev_ts = {}
  34. scope_count = 0
  35. for event in events:
  36. if "dur" in event:
  37. assert event["dur"] >= 0
  38. elif "ts" in event and "tid" in event:
  39. ts = event["ts"]
  40. tid = event["tid"]
  41. if ts == 0:
  42. continue
  43. assert (tid not in prev_ts) or prev_ts[tid] <= ts
  44. prev_ts[tid] = ts
  45. if "name" in event and event["name"] == "my_scope":
  46. scope_count += 1
  47. assert scope_count > 0 and scope_count % 2 == 0

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