|
20 | 20 | import pytest |
21 | 21 | import torch |
22 | 22 |
|
| 23 | +import pytorch_lightning |
23 | 24 | from pytorch_lightning import Callback, LightningDataModule, Trainer |
24 | 25 | from pytorch_lightning.callbacks.gpu_stats_monitor import GPUStatsMonitor |
25 | 26 | from pytorch_lightning.callbacks.lr_monitor import LearningRateMonitor |
|
35 | 36 | TorchElasticEnvironment, |
36 | 37 | ) |
37 | 38 | from pytorch_lightning.strategies import SingleDeviceStrategy |
| 39 | +from pytorch_lightning.utilities.exceptions import MisconfigurationException |
38 | 40 | from tests.deprecated_api import _soft_unimport_module |
39 | 41 | from tests.helpers import BoringModel |
40 | 42 | from tests.helpers.datamodules import MNISTDataModule |
@@ -393,8 +395,8 @@ def test_v1_7_0_deprecate_gpu_stats_monitor(tmpdir): |
393 | 395 | _ = GPUStatsMonitor() |
394 | 396 |
|
395 | 397 |
|
396 | | -@RunIf(tpu=True) |
397 | | -def test_v1_7_0_deprecate_xla_stats_monitor(tmpdir): |
| 398 | +def test_v1_7_0_deprecate_xla_stats_monitor(monkeypatch): |
| 399 | + monkeypatch.setattr(pytorch_lightning.callbacks.xla_stats_monitor, "_TPU_AVAILABLE", True) |
398 | 400 | with pytest.deprecated_call(match="The `XLAStatsMonitor` callback was deprecated in v1.5"): |
399 | 401 | _ = XLAStatsMonitor() |
400 | 402 |
|
@@ -516,3 +518,17 @@ def post_dispatch(self, trainer): |
516 | 518 |
|
517 | 519 | with pytest.deprecated_call(match=escape("`CustomPlugin.post_dispatch()` has been deprecated in v1.6")): |
518 | 520 | CustomPlugin(torch.device("cpu")) |
| 521 | + |
| 522 | + |
| 523 | +def test_xla_stats_monitor_tpu_not_used(monkeypatch): |
| 524 | + monkeypatch.setattr(pytorch_lightning.callbacks.xla_stats_monitor, "_TPU_AVAILABLE", True) |
| 525 | + with pytest.deprecated_call(match="The `XLAStatsMonitor` callback was deprecated in v1.5"): |
| 526 | + xla_stats = XLAStatsMonitor() |
| 527 | + |
| 528 | + trainer = Trainer(accelerator="cpu", callbacks=[xla_stats]) |
| 529 | + model = BoringModel() |
| 530 | + with pytest.raises( |
| 531 | + MisconfigurationException, |
| 532 | + match="You are using XLAStatsMonitor but are not running on TPU. The accelerator is set to CPUAccelerator.", |
| 533 | + ): |
| 534 | + trainer.fit(model) |
0 commit comments