2020-02-07 03:01:21 +00:00
|
|
|
import time
|
|
|
|
import numpy as np
|
2020-02-16 04:43:43 +00:00
|
|
|
import pytest
|
2020-02-07 03:01:21 +00:00
|
|
|
|
2020-02-09 22:48:37 +00:00
|
|
|
from pytorch_lightning.profiler import Profiler, AdvancedProfiler
|
|
|
|
|
2020-02-19 12:09:28 +00:00
|
|
|
PROFILER_OVERHEAD_MAX_TOLERANCE = 0.001
|
2020-02-16 04:43:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def simple_profiler():
|
2020-02-19 12:09:28 +00:00
|
|
|
"""Creates a new profiler for every test with `simple_profiler` as an arg."""
|
2020-02-16 04:43:43 +00:00
|
|
|
profiler = Profiler()
|
|
|
|
return profiler
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
|
2020-02-16 04:43:43 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def advanced_profiler():
|
2020-02-19 12:09:28 +00:00
|
|
|
"""Creates a new profiler for every test with `advanced_profiler` as an arg."""
|
2020-02-16 04:43:43 +00:00
|
|
|
profiler = AdvancedProfiler()
|
|
|
|
return profiler
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
|
2020-02-16 04:43:43 +00:00
|
|
|
@pytest.mark.parametrize("action,expected", [("a", [3, 1]), ("b", [2]), ("c", [1])])
|
|
|
|
def test_simple_profiler_durations(simple_profiler, action, expected):
|
2020-02-19 12:09:28 +00:00
|
|
|
"""Ensure the reported durations are reasonably accurate."""
|
2020-02-07 03:01:21 +00:00
|
|
|
|
2020-02-16 04:43:43 +00:00
|
|
|
for duration in expected:
|
|
|
|
with simple_profiler.profile(action):
|
|
|
|
time.sleep(duration)
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
# 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
|
2020-02-16 04:43:43 +00:00
|
|
|
np.testing.assert_allclose(
|
|
|
|
simple_profiler.recorded_durations[action], expected, rtol=0.2
|
|
|
|
)
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
|
2020-02-16 04:43:43 +00:00
|
|
|
def test_simple_profiler_overhead(simple_profiler, n_iter=5):
|
2020-02-19 12:09:28 +00:00
|
|
|
"""Ensure that the profiler doesn't introduce too much overhead during training."""
|
2020-02-16 04:43:43 +00:00
|
|
|
for _ in range(n_iter):
|
|
|
|
with simple_profiler.profile("no-op"):
|
|
|
|
pass
|
|
|
|
|
|
|
|
durations = np.array(simple_profiler.recorded_durations["no-op"])
|
|
|
|
assert all(durations < PROFILER_OVERHEAD_MAX_TOLERANCE)
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
|
2020-02-16 04:43:43 +00:00
|
|
|
def test_simple_profiler_describe(simple_profiler):
|
2020-02-19 12:09:28 +00:00
|
|
|
"""Ensure the profiler won't fail when reporting the summary."""
|
2020-02-16 04:43:43 +00:00
|
|
|
simple_profiler.describe()
|
2020-02-07 03:01:21 +00:00
|
|
|
|
|
|
|
|
2020-02-19 12:09:28 +00:00
|
|
|
def _get_total_cprofile_duration(profile):
|
|
|
|
return sum([x.totaltime for x in profile.getstats()])
|
|
|
|
|
|
|
|
|
2020-02-16 04:43:43 +00:00
|
|
|
@pytest.mark.parametrize("action,expected", [("a", [3, 1]), ("b", [2]), ("c", [1])])
|
|
|
|
def test_advanced_profiler_durations(advanced_profiler, action, expected):
|
2020-02-19 12:09:28 +00:00
|
|
|
"""Ensure the reported durations are reasonably accurate."""
|
2020-02-07 03:01:21 +00:00
|
|
|
|
2020-02-16 04:43:43 +00:00
|
|
|
for duration in expected:
|
|
|
|
with advanced_profiler.profile(action):
|
|
|
|
time.sleep(duration)
|
2020-02-07 03:01:21 +00:00
|
|
|
|
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
|
2020-02-19 12:09:28 +00:00
|
|
|
recored_total_duration = _get_total_cprofile_duration(
|
2020-02-16 04:43:43 +00:00
|
|
|
advanced_profiler.profiled_actions[action]
|
|
|
|
)
|
|
|
|
expected_total_duration = np.sum(expected)
|
|
|
|
np.testing.assert_allclose(
|
|
|
|
recored_total_duration, expected_total_duration, rtol=0.2
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_advanced_profiler_overhead(advanced_profiler, n_iter=5):
|
2020-02-19 12:09:28 +00:00
|
|
|
"""Ensure that the profiler doesn't introduce too much overhead during training."""
|
2020-02-16 04:43:43 +00:00
|
|
|
for _ in range(n_iter):
|
|
|
|
with advanced_profiler.profile("no-op"):
|
|
|
|
pass
|
|
|
|
|
|
|
|
action_profile = advanced_profiler.profiled_actions["no-op"]
|
2020-02-19 12:09:28 +00:00
|
|
|
total_duration = _get_total_cprofile_duration(action_profile)
|
2020-02-16 04:43:43 +00:00
|
|
|
average_duration = total_duration / n_iter
|
|
|
|
assert average_duration < PROFILER_OVERHEAD_MAX_TOLERANCE
|
|
|
|
|
|
|
|
|
|
|
|
def test_advanced_profiler_describe(advanced_profiler):
|
2020-02-19 12:09:28 +00:00
|
|
|
"""Ensure the profiler won't fail when reporting the summary."""
|
2020-02-16 04:43:43 +00:00
|
|
|
advanced_profiler.describe()
|