2020-10-13 11:18:07 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-08-27 17:50:32 +00:00
|
|
|
import os
|
2021-07-19 11:42:43 +00:00
|
|
|
from unittest import mock
|
2021-01-14 18:15:34 +00:00
|
|
|
|
|
|
|
import numpy as np
|
2020-08-27 17:50:32 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
|
|
|
|
from pytorch_lightning import Trainer
|
|
|
|
from pytorch_lightning.callbacks import GPUStatsMonitor
|
|
|
|
from pytorch_lightning.loggers import CSVLogger
|
|
|
|
from pytorch_lightning.loggers.csv_logs import ExperimentWriter
|
|
|
|
from pytorch_lightning.utilities.exceptions import MisconfigurationException
|
2021-02-16 19:59:57 +00:00
|
|
|
from tests.helpers import BoringModel
|
2021-03-02 09:36:01 +00:00
|
|
|
from tests.helpers.runif import RunIf
|
2020-08-27 17:50:32 +00:00
|
|
|
|
|
|
|
|
2021-03-02 08:03:32 +00:00
|
|
|
@RunIf(min_gpus=1)
|
2020-08-27 17:50:32 +00:00
|
|
|
def test_gpu_stats_monitor(tmpdir):
|
|
|
|
"""
|
|
|
|
Test GPU stats are logged using a logger.
|
|
|
|
"""
|
2021-02-16 19:59:57 +00:00
|
|
|
model = BoringModel()
|
2020-10-22 11:08:03 +00:00
|
|
|
gpu_stats = GPUStatsMonitor(intra_step_time=True)
|
2020-08-27 17:50:32 +00:00
|
|
|
logger = CSVLogger(tmpdir)
|
2020-10-22 11:08:03 +00:00
|
|
|
log_every_n_steps = 2
|
2020-08-27 17:50:32 +00:00
|
|
|
|
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
2020-10-22 11:08:03 +00:00
|
|
|
max_epochs=2,
|
|
|
|
limit_train_batches=7,
|
|
|
|
log_every_n_steps=log_every_n_steps,
|
2020-08-27 17:50:32 +00:00
|
|
|
gpus=1,
|
|
|
|
callbacks=[gpu_stats],
|
2021-07-26 11:37:35 +00:00
|
|
|
logger=logger,
|
2020-08-27 17:50:32 +00:00
|
|
|
)
|
|
|
|
|
2021-01-12 00:36:48 +00:00
|
|
|
trainer.fit(model)
|
2021-05-04 10:50:56 +00:00
|
|
|
assert trainer.state.finished, f"Training failed with {trainer.state}"
|
2020-08-27 17:50:32 +00:00
|
|
|
|
|
|
|
path_csv = os.path.join(logger.log_dir, ExperimentWriter.NAME_METRICS_FILE)
|
2021-07-26 11:37:35 +00:00
|
|
|
met_data = np.genfromtxt(path_csv, delimiter=",", names=True, deletechars="", replace_space=" ")
|
2020-08-27 17:50:32 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
batch_time_data = met_data["batch_time/intra_step (ms)"]
|
2020-10-22 11:08:03 +00:00
|
|
|
batch_time_data = batch_time_data[~np.isnan(batch_time_data)]
|
|
|
|
assert batch_time_data.shape[0] == trainer.global_step // log_every_n_steps
|
2020-08-27 17:50:32 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
fields = ["utilization.gpu", "memory.used", "memory.free", "utilization.memory"]
|
2020-08-27 17:50:32 +00:00
|
|
|
|
|
|
|
for f in fields:
|
2021-07-19 08:30:24 +00:00
|
|
|
assert any(f in h for h in met_data.dtype.names)
|
2020-08-27 17:50:32 +00:00
|
|
|
|
|
|
|
|
2021-08-21 03:22:33 +00:00
|
|
|
@RunIf(min_gpus=1)
|
|
|
|
def test_gpu_stats_monitor_no_queries(tmpdir):
|
|
|
|
"""
|
|
|
|
Test GPU logger doesn't fail if no "nvidia-smi" queries are to be performed.
|
|
|
|
"""
|
|
|
|
model = BoringModel()
|
|
|
|
gpu_stats = GPUStatsMonitor(
|
|
|
|
memory_utilization=False,
|
|
|
|
gpu_utilization=False,
|
|
|
|
intra_step_time=True,
|
|
|
|
inter_step_time=True,
|
|
|
|
)
|
|
|
|
trainer = Trainer(
|
|
|
|
default_root_dir=tmpdir,
|
|
|
|
max_epochs=1,
|
|
|
|
limit_train_batches=2,
|
|
|
|
limit_val_batches=0,
|
|
|
|
log_every_n_steps=1,
|
|
|
|
gpus=1,
|
|
|
|
callbacks=[gpu_stats],
|
|
|
|
)
|
|
|
|
with mock.patch("pytorch_lightning.loggers.tensorboard.TensorBoardLogger.log_metrics") as log_metrics_mock:
|
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
assert log_metrics_mock.mock_calls[2:] == [
|
|
|
|
mock.call({"batch_time/intra_step (ms)": mock.ANY}, step=0),
|
|
|
|
mock.call({"batch_time/inter_step (ms)": mock.ANY}, step=1),
|
|
|
|
mock.call({"batch_time/intra_step (ms)": mock.ANY}, step=1),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-08-27 17:50:32 +00:00
|
|
|
@pytest.mark.skipif(torch.cuda.is_available(), reason="test requires CPU machine")
|
|
|
|
def test_gpu_stats_monitor_cpu_machine(tmpdir):
|
|
|
|
"""
|
|
|
|
Test GPUStatsMonitor on CPU machine.
|
|
|
|
"""
|
2021-07-26 11:37:35 +00:00
|
|
|
with pytest.raises(MisconfigurationException, match="NVIDIA driver is not installed"):
|
2020-12-21 05:40:55 +00:00
|
|
|
GPUStatsMonitor()
|
2020-08-27 17:50:32 +00:00
|
|
|
|
|
|
|
|
2021-03-02 08:03:32 +00:00
|
|
|
@RunIf(min_gpus=1)
|
2020-08-27 17:50:32 +00:00
|
|
|
def test_gpu_stats_monitor_no_logger(tmpdir):
|
|
|
|
"""
|
|
|
|
Test GPUStatsMonitor with no logger in Trainer.
|
|
|
|
"""
|
2021-02-16 19:59:57 +00:00
|
|
|
model = BoringModel()
|
2020-08-27 17:50:32 +00:00
|
|
|
gpu_stats = GPUStatsMonitor()
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
trainer = Trainer(default_root_dir=tmpdir, callbacks=[gpu_stats], max_epochs=1, gpus=1, logger=False)
|
2020-08-27 17:50:32 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
with pytest.raises(MisconfigurationException, match="Trainer that has no logger."):
|
2020-08-27 17:50:32 +00:00
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
|
2021-03-02 08:03:32 +00:00
|
|
|
@RunIf(min_gpus=1)
|
2020-08-27 17:50:32 +00:00
|
|
|
def test_gpu_stats_monitor_no_gpu_warning(tmpdir):
|
|
|
|
"""
|
|
|
|
Test GPUStatsMonitor raises a warning when not training on GPU device.
|
|
|
|
"""
|
2021-02-16 19:59:57 +00:00
|
|
|
model = BoringModel()
|
2020-08-27 17:50:32 +00:00
|
|
|
gpu_stats = GPUStatsMonitor()
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
trainer = Trainer(default_root_dir=tmpdir, callbacks=[gpu_stats], max_steps=1, gpus=None)
|
2020-08-27 17:50:32 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
with pytest.raises(MisconfigurationException, match="not running on GPU"):
|
2020-08-27 17:50:32 +00:00
|
|
|
trainer.fit(model)
|
2020-09-25 05:30:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_gpu_stats_monitor_parse_gpu_stats():
|
2021-07-26 11:37:35 +00:00
|
|
|
logs = GPUStatsMonitor._parse_gpu_stats([1, 2], [[3, 4, 5], [6, 7]], [("gpu", "a"), ("memory", "b")])
|
2021-07-19 11:42:43 +00:00
|
|
|
expected = {
|
2021-07-26 11:37:35 +00:00
|
|
|
"device_id: 1/gpu (a)": 3,
|
|
|
|
"device_id: 1/memory (b)": 4,
|
|
|
|
"device_id: 2/gpu (a)": 6,
|
|
|
|
"device_id: 2/memory (b)": 7,
|
2021-07-19 11:42:43 +00:00
|
|
|
}
|
2020-09-25 05:30:30 +00:00
|
|
|
assert logs == expected
|
2021-07-19 11:42:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mock.patch.dict(os.environ, {})
|
2021-07-26 11:37:35 +00:00
|
|
|
@mock.patch("torch.cuda.is_available", return_value=True)
|
|
|
|
@mock.patch("torch.cuda.device_count", return_value=2)
|
2021-07-19 11:42:43 +00:00
|
|
|
def test_gpu_stats_monitor_get_gpu_ids_cuda_visible_devices_unset(device_count_mock, is_available_mock):
|
|
|
|
gpu_ids = GPUStatsMonitor._get_gpu_ids([1, 0])
|
2021-07-26 11:37:35 +00:00
|
|
|
expected = ["1", "0"]
|
2021-07-19 11:42:43 +00:00
|
|
|
assert gpu_ids == expected
|
|
|
|
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
@mock.patch.dict(os.environ, {"CUDA_VISIBLE_DEVICES": "3,2,4"})
|
|
|
|
@mock.patch("torch.cuda.is_available", return_value=True)
|
|
|
|
@mock.patch("torch.cuda.device_count", return_value=3)
|
2021-07-19 11:42:43 +00:00
|
|
|
def test_gpu_stats_monitor_get_gpu_ids_cuda_visible_devices_integers(device_count_mock, is_available_mock):
|
|
|
|
gpu_ids = GPUStatsMonitor._get_gpu_ids([1, 2])
|
2021-07-26 11:37:35 +00:00
|
|
|
expected = ["2", "4"]
|
2021-07-19 11:42:43 +00:00
|
|
|
assert gpu_ids == expected
|
|
|
|
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
@mock.patch.dict(os.environ, {"CUDA_VISIBLE_DEVICES": "GPU-01a23b4c,GPU-56d78e9f,GPU-02a46c8e"})
|
|
|
|
@mock.patch("torch.cuda.is_available", return_value=True)
|
|
|
|
@mock.patch("torch.cuda.device_count", return_value=3)
|
2021-07-19 11:42:43 +00:00
|
|
|
def test_gpu_stats_monitor_get_gpu_ids_cuda_visible_devices_uuids(device_count_mock, is_available_mock):
|
|
|
|
gpu_ids = GPUStatsMonitor._get_gpu_ids([1, 2])
|
2021-07-26 11:37:35 +00:00
|
|
|
expected = ["GPU-56d78e9f", "GPU-02a46c8e"]
|
2021-07-19 11:42:43 +00:00
|
|
|
assert gpu_ids == expected
|