2020-02-07 03:01:21 +00:00
|
|
|
import time
|
2020-02-09 22:48:37 +00:00
|
|
|
|
2020-02-07 03:01:21 +00:00
|
|
|
import numpy as np
|
|
|
|
|
2020-02-09 22:48:37 +00:00
|
|
|
from pytorch_lightning.profiler import Profiler, AdvancedProfiler
|
|
|
|
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
def test_simple_profiler():
|
|
|
|
p = Profiler()
|
|
|
|
|
|
|
|
with p.profile("a"):
|
|
|
|
time.sleep(3)
|
|
|
|
|
|
|
|
with p.profile("a"):
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
with p.profile("b"):
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
with p.profile("c"):
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
# different environments have different precision when it comes to time.sleep()
|
2020-02-09 22:48:37 +00:00
|
|
|
# see: https://github.com/PyTorchLightning/pytorch-lightning/issues/796
|
|
|
|
np.testing.assert_allclose(p.recorded_durations["a"], [3, 1], rtol=0.2)
|
|
|
|
np.testing.assert_allclose(p.recorded_durations["b"], [2], rtol=0.2)
|
|
|
|
np.testing.assert_allclose(p.recorded_durations["c"], [1], rtol=0.2)
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_advanced_profiler():
|
2020-02-09 22:48:37 +00:00
|
|
|
def _get_duration(profile):
|
2020-02-07 03:01:21 +00:00
|
|
|
return sum([x.totaltime for x in profile.getstats()])
|
|
|
|
|
|
|
|
p = AdvancedProfiler()
|
|
|
|
|
|
|
|
with p.profile("a"):
|
|
|
|
time.sleep(3)
|
|
|
|
|
|
|
|
with p.profile("a"):
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
with p.profile("b"):
|
|
|
|
time.sleep(2)
|
|
|
|
|
|
|
|
with p.profile("c"):
|
|
|
|
time.sleep(1)
|
|
|
|
|
2020-02-09 22:48:37 +00:00
|
|
|
# different environments have different precision when it comes to time.sleep()
|
|
|
|
# see: https://github.com/PyTorchLightning/pytorch-lightning/issues/796
|
|
|
|
a_duration = _get_duration(p.profiled_actions["a"])
|
|
|
|
np.testing.assert_allclose(a_duration, [4], rtol=0.2)
|
|
|
|
b_duration = _get_duration(p.profiled_actions["b"])
|
|
|
|
np.testing.assert_allclose(b_duration, [2], rtol=0.2)
|
|
|
|
c_duration = _get_duration(p.profiled_actions["c"])
|
|
|
|
np.testing.assert_allclose(c_duration, [1], rtol=0.2)
|