2021-07-30 10:31:08 +00:00
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any, Dict, Union
|
2022-09-12 16:00:14 +00:00
|
|
|
from unittest.mock import Mock
|
2021-02-22 00:02:31 +00:00
|
|
|
|
ruff: replace isort with ruff +TPU (#17684)
* ruff: replace isort with ruff
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixing & imports
* lines in warning test
* docs
* fix enum import
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixing
* import
* fix lines
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* type ClusterEnvironment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-09-26 15:54:55 +00:00
|
|
|
import lightning.pytorch as pl
|
2021-02-22 00:02:31 +00:00
|
|
|
import pytest
|
|
|
|
import torch
|
2023-02-01 20:34:38 +00:00
|
|
|
from lightning.fabric.plugins import TorchCheckpointIO
|
2023-02-02 10:06:45 +00:00
|
|
|
from lightning.pytorch import Trainer
|
|
|
|
from lightning.pytorch.accelerators import CPUAccelerator
|
|
|
|
from lightning.pytorch.demos.boring_classes import BoringModel
|
2023-10-30 20:53:13 +00:00
|
|
|
from lightning.pytorch.plugins.precision.precision import Precision
|
2023-02-02 10:06:45 +00:00
|
|
|
from lightning.pytorch.strategies import SingleDeviceStrategy
|
ruff: replace isort with ruff +TPU (#17684)
* ruff: replace isort with ruff
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixing & imports
* lines in warning test
* docs
* fix enum import
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fixing
* import
* fix lines
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* type ClusterEnvironment
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-09-26 15:54:55 +00:00
|
|
|
|
2022-09-12 16:00:14 +00:00
|
|
|
from tests_pytorch.helpers.runif import RunIf
|
2021-02-22 00:02:31 +00:00
|
|
|
|
|
|
|
|
2021-12-20 17:16:52 +00:00
|
|
|
def test_restore_checkpoint_after_pre_setup_default():
|
|
|
|
"""Assert default for restore_checkpoint_after_setup is False."""
|
2021-12-21 23:56:14 +00:00
|
|
|
plugin = SingleDeviceStrategy(
|
2023-10-30 20:53:13 +00:00
|
|
|
accelerator=CPUAccelerator(), device=torch.device("cpu"), precision_plugin=Precision()
|
2021-12-16 04:41:34 +00:00
|
|
|
)
|
2021-12-20 17:16:52 +00:00
|
|
|
assert not plugin.restore_checkpoint_after_setup
|
2021-07-30 10:31:08 +00:00
|
|
|
|
|
|
|
|
2022-02-09 23:11:27 +00:00
|
|
|
def test_availability():
|
|
|
|
assert CPUAccelerator.is_available()
|
|
|
|
|
|
|
|
|
2022-09-12 16:00:14 +00:00
|
|
|
@RunIf(psutil=True)
|
|
|
|
def test_get_device_stats(tmpdir):
|
|
|
|
gpu_stats = CPUAccelerator().get_device_stats(Mock())
|
|
|
|
fields = ["cpu_vm_percent", "cpu_percent", "cpu_swap_percent"]
|
|
|
|
|
|
|
|
for f in fields:
|
2023-04-24 21:57:08 +00:00
|
|
|
assert any(f in h for h in gpu_stats)
|
2022-09-12 16:00:14 +00:00
|
|
|
|
|
|
|
|
2021-12-20 17:16:52 +00:00
|
|
|
@pytest.mark.parametrize("restore_after_pre_setup", [True, False])
|
|
|
|
def test_restore_checkpoint_after_pre_setup(tmpdir, restore_after_pre_setup):
|
2023-08-09 14:44:20 +00:00
|
|
|
"""Test to ensure that if restore_checkpoint_after_setup is True, then we only load the state after pre- dispatch
|
|
|
|
is called."""
|
2021-07-30 10:31:08 +00:00
|
|
|
|
2021-12-21 23:56:14 +00:00
|
|
|
class TestPlugin(SingleDeviceStrategy):
|
2021-12-20 16:41:22 +00:00
|
|
|
setup_called = False
|
2021-07-30 10:31:08 +00:00
|
|
|
|
2021-12-20 16:41:22 +00:00
|
|
|
def setup(self, trainer: "pl.Trainer") -> None:
|
|
|
|
super().setup(trainer)
|
|
|
|
self.setup_called = True
|
2021-07-30 10:31:08 +00:00
|
|
|
|
|
|
|
@property
|
2021-12-20 17:16:52 +00:00
|
|
|
def restore_checkpoint_after_setup(self) -> bool:
|
|
|
|
return restore_after_pre_setup
|
2021-07-30 10:31:08 +00:00
|
|
|
|
2021-08-23 20:12:18 +00:00
|
|
|
def load_checkpoint(self, checkpoint_path: Union[str, Path]) -> Dict[str, Any]:
|
2021-12-20 17:16:52 +00:00
|
|
|
assert self.setup_called == restore_after_pre_setup
|
2021-08-23 20:12:18 +00:00
|
|
|
return super().load_checkpoint(checkpoint_path)
|
2021-07-30 10:31:08 +00:00
|
|
|
|
|
|
|
model = BoringModel()
|
|
|
|
trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True)
|
|
|
|
trainer.fit(model)
|
|
|
|
|
|
|
|
checkpoint_path = os.path.join(tmpdir, "model.pt")
|
|
|
|
trainer.save_checkpoint(checkpoint_path)
|
|
|
|
|
2021-12-16 04:41:34 +00:00
|
|
|
plugin = TestPlugin(
|
|
|
|
accelerator=CPUAccelerator(),
|
2023-10-30 20:53:13 +00:00
|
|
|
precision_plugin=Precision(),
|
2021-12-16 04:41:34 +00:00
|
|
|
device=torch.device("cpu"),
|
|
|
|
checkpoint_io=TorchCheckpointIO(),
|
|
|
|
)
|
2021-12-20 17:16:52 +00:00
|
|
|
assert plugin.restore_checkpoint_after_setup == restore_after_pre_setup
|
2021-07-30 10:31:08 +00:00
|
|
|
|
2021-12-16 04:41:34 +00:00
|
|
|
trainer = Trainer(default_root_dir=tmpdir, strategy=plugin, fast_dev_run=True)
|
2021-10-25 19:05:31 +00:00
|
|
|
trainer.fit(model, ckpt_path=checkpoint_path)
|
2021-07-30 10:31:08 +00:00
|
|
|
for func in (trainer.test, trainer.validate, trainer.predict):
|
2021-12-20 16:41:22 +00:00
|
|
|
plugin.setup_called = False
|
2021-07-30 10:31:08 +00:00
|
|
|
func(model, ckpt_path=checkpoint_path)
|