Skip to content

Commit b238ef0

Browse files
carmoccalantiga
authored andcommitted
The psutil package is now required for CPU monitoring (#17010)
(cherry picked from commit 8d586c6)
1 parent 10774ca commit b238ef0

File tree

7 files changed

+17
-21
lines changed

7 files changed

+17
-21
lines changed

src/pytorch_lightning/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
2020
- Fixed `num_nodes` not being set for `DDPFullyShardedNativeStrategy` ([#17160](https://github.com/Lightning-AI/lightning/pull/17160))
2121

2222

23+
- The `psutil` package is now required for CPU monitoring ([#17010](https://github.com/Lightning-AI/lightning/pull/17010))
24+
25+
2326
## [1.9.4] - 2023-03-01
2427

2528
### Added

src/pytorch_lightning/accelerators/cpu.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
from typing import Any, Dict, List, Union
1515

1616
import torch
17+
from lightning_utilities.core.imports import RequirementCache
1718

1819
from lightning_fabric.accelerators.cpu import _parse_cpu_cores
1920
from lightning_fabric.utilities.types import _DEVICE
2021
from pytorch_lightning.accelerators.accelerator import Accelerator
2122
from pytorch_lightning.utilities.exceptions import MisconfigurationException
22-
from pytorch_lightning.utilities.imports import _PSUTIL_AVAILABLE
2323

2424

2525
class CPUAccelerator(Accelerator):
@@ -76,13 +76,13 @@ def register_accelerators(cls, accelerator_registry: Dict) -> None:
7676
_CPU_VM_PERCENT = "cpu_vm_percent"
7777
_CPU_PERCENT = "cpu_percent"
7878
_CPU_SWAP_PERCENT = "cpu_swap_percent"
79+
_PSUTIL_AVAILABLE = RequirementCache("psutil")
7980

8081

8182
def get_cpu_stats() -> Dict[str, float]:
8283
if not _PSUTIL_AVAILABLE:
8384
raise ModuleNotFoundError(
84-
"Fetching CPU device stats requires `psutil` to be installed."
85-
" Install it by running `pip install -U psutil`."
85+
f"Fetching CPU device stats requires `psutil` to be installed. {str(_PSUTIL_AVAILABLE)}"
8686
)
8787
import psutil
8888

src/pytorch_lightning/accelerators/mps.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
from lightning_fabric.utilities.device_parser import _parse_gpu_ids
2020
from lightning_fabric.utilities.types import _DEVICE
2121
from pytorch_lightning.accelerators.accelerator import Accelerator
22+
from pytorch_lightning.accelerators.cpu import _PSUTIL_AVAILABLE
2223
from pytorch_lightning.utilities.exceptions import MisconfigurationException
23-
from pytorch_lightning.utilities.imports import _PSUTIL_AVAILABLE
2424

2525

2626
class MPSAccelerator(Accelerator):
@@ -84,8 +84,7 @@ def register_accelerators(cls, accelerator_registry: Dict) -> None:
8484
def get_device_stats() -> Dict[str, float]:
8585
if not _PSUTIL_AVAILABLE:
8686
raise ModuleNotFoundError(
87-
"Fetching M1 device stats requires `psutil` to be installed."
88-
" Install it by running `pip install -U psutil`."
87+
f"Fetching MPS device stats requires `psutil` to be installed. {str(_PSUTIL_AVAILABLE)}"
8988
)
9089
import psutil
9190

src/pytorch_lightning/callbacks/device_stats_monitor.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
from typing import Any, Dict, Optional
2222

2323
import pytorch_lightning as pl
24+
from pytorch_lightning.accelerators.cpu import _PSUTIL_AVAILABLE
2425
from pytorch_lightning.callbacks.callback import Callback
2526
from pytorch_lightning.utilities.exceptions import MisconfigurationException
26-
from pytorch_lightning.utilities.imports import _PSUTIL_AVAILABLE
27-
from pytorch_lightning.utilities.rank_zero import rank_zero_warn
2827
from pytorch_lightning.utilities.types import STEP_OUTPUT
2928

3029

@@ -36,14 +35,14 @@ class DeviceStatsMonitor(Callback):
3635
3736
Args:
3837
cpu_stats: if ``None``, it will log CPU stats only if the accelerator is CPU.
39-
It will raise a warning if ``psutil`` is not installed till v1.9.0.
40-
If ``True``, it will log CPU stats regardless of the accelerator, and it will
41-
raise an exception if ``psutil`` is not installed.
38+
If ``True``, it will log CPU stats regardless of the accelerator.
4239
If ``False``, it will not log CPU stats regardless of the accelerator.
4340
4441
Raises:
4542
MisconfigurationException:
4643
If ``Trainer`` has no logger.
44+
ModuleNotFoundError:
45+
If ``psutil`` is not installed and CPU stats are monitored.
4746
4847
Example:
4948
>>> from pytorch_lightning import Trainer
@@ -70,13 +69,9 @@ def setup(
7069
# warn in setup to warn once
7170
device = trainer.strategy.root_device
7271
if self._cpu_stats is None and device.type == "cpu" and not _PSUTIL_AVAILABLE:
73-
# TODO: raise an exception from v1.9
74-
rank_zero_warn(
75-
"`DeviceStatsMonitor` will not log CPU stats as `psutil` is not installed."
76-
" To install `psutil`, run `pip install psutil`."
77-
" It will raise an exception if `psutil` is not installed post v1.9.0."
72+
raise ModuleNotFoundError(
73+
f"`DeviceStatsMonitor` cannot log CPU stats as `psutil` is not installed. {str(_PSUTIL_AVAILABLE)} "
7874
)
79-
self._cpu_stats = False
8075

8176
def _get_and_log_device_stats(self, trainer: "pl.Trainer", key: str) -> None:
8277
if not trainer._logger_connector.should_update_logs:

src/pytorch_lightning/utilities/imports.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
_HIVEMIND_AVAILABLE = package_available("hivemind")
3232
_KINETO_AVAILABLE = torch.profiler.kineto_available()
3333
_OMEGACONF_AVAILABLE = package_available("omegaconf")
34-
_PSUTIL_AVAILABLE = package_available("psutil")
34+
_POPTORCH_AVAILABLE = package_available("poptorch")
3535
_RICH_AVAILABLE = package_available("rich") and compare_version("rich", operator.ge, "10.2.2")
3636
_TORCH_QUANTIZE_AVAILABLE = bool([eg for eg in torch.backends.quantized.supported_engines if eg != "none"])
3737
_TORCHVISION_AVAILABLE = RequirementCache("torchvision")

tests/tests_pytorch/callbacks/test_device_stats_monitor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ def test_device_stats_monitor_warning_when_psutil_not_available(monkeypatch, tmp
165165
monitor = DeviceStatsMonitor()
166166
trainer = Trainer(logger=CSVLogger(tmp_path))
167167
assert trainer.strategy.root_device == torch.device("cpu")
168-
# TODO: raise an exception from v1.9
169-
with pytest.warns(UserWarning, match="psutil` is not installed"):
168+
with pytest.raises(ModuleNotFoundError, match="psutil` is not installed"):
170169
monitor.setup(trainer, Mock(), "fit")
171170

172171

tests/tests_pytorch/helpers/runif.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from packaging.version import Version
2323

2424
from lightning_fabric.accelerators.cuda import num_cuda_devices
25+
from pytorch_lightning.accelerators.cpu import _PSUTIL_AVAILABLE
2526
from pytorch_lightning.accelerators.hpu import _HPU_AVAILABLE
2627
from pytorch_lightning.accelerators.ipu import _IPU_AVAILABLE
2728
from pytorch_lightning.accelerators.mps import MPSAccelerator
@@ -37,7 +38,6 @@
3738
_HIVEMIND_AVAILABLE,
3839
_OMEGACONF_AVAILABLE,
3940
_ONNX_AVAILABLE,
40-
_PSUTIL_AVAILABLE,
4141
_TORCH_GREATER_EQUAL_2_0,
4242
_TORCH_QUANTIZE_AVAILABLE,
4343
)

0 commit comments

Comments
 (0)