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

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