Skip to content

Commit

Permalink
Remove AcceleratorConnector.num_gpus and deprecate `Trainer.num_gpu…
Browse files Browse the repository at this point in the history
…s` (#12384)
  • Loading branch information
DuYicong515 authored Mar 21, 2022
1 parent caed77f commit 31c68d1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 36 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Deprecated `Trainer.devices` in favor of `Trainer.num_devices` and `Trainer.device_ids` ([#12151](https://github.com/PyTorchLightning/pytorch-lightning/pull/12151))


- Deprecated `Trainer.root_gpu` in favor of `Trainer.strategy.root_device.index` when GPU is used. ([#12262](https://github.com/PyTorchLightning/pytorch-lightning/pull/12262))
- Deprecated `Trainer.root_gpu` in favor of `Trainer.strategy.root_device.index` when GPU is used ([#12262](https://github.com/PyTorchLightning/pytorch-lightning/pull/12262))


- Deprecated `Trainer.num_gpus` in favor of `Trainer.num_devices` when GPU is used ([#12384](https://github.com/PyTorchLightning/pytorch-lightning/pull/12384))


### Removed
Expand Down Expand Up @@ -720,6 +723,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Removed `AcceleratorConnector.root_gpu` property ([#12262](https://github.com/PyTorchLightning/pytorch-lightning/pull/12262))


- Removed `AcceleratorConnector.num_gpus` property ([#12384](https://github.com/PyTorchLightning/pytorch-lightning/pull/12384))


### Fixed

- Fixed an issue where `ModelCheckpoint` could delete older checkpoints when `dirpath` has changed during resumed training ([#12045](https://github.com/PyTorchLightning/pytorch-lightning/pull/12045))
Expand Down
6 changes: 0 additions & 6 deletions pytorch_lightning/trainer/connectors/accelerator_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -815,12 +815,6 @@ def num_ipus(self) -> int:
return self.devices
return 0

@property
def num_gpus(self) -> int:
if isinstance(self.accelerator, GPUAccelerator):
return self.devices
return 0

@property
def gpus(self) -> Optional[Union[List[int], str, int]]:
return self._gpus
Expand Down
6 changes: 5 additions & 1 deletion pytorch_lightning/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2071,7 +2071,11 @@ def ipus(self) -> int:

@property
def num_gpus(self) -> int:
return self._accelerator_connector.num_gpus
rank_zero_deprecation(
"`Trainer.num_gpus` was deprecated in v1.6 and will be removed in v1.8."
" Please use `Trainer.num_devices` instead."
)
return self.num_devices if isinstance(self.accelerator, GPUAccelerator) else 0

@property
def devices(self) -> int:
Expand Down
37 changes: 37 additions & 0 deletions tests/deprecated_api/test_remove_1-8.py
Original file line number Diff line number Diff line change
Expand Up @@ -925,3 +925,40 @@ def test_root_gpu_property_0_passing(monkeypatch, gpus, expected_root_gpu, strat
"Please use `Trainer.strategy.root_device.index` instead."
):
assert Trainer(gpus=gpus, strategy=strategy).root_gpu == expected_root_gpu


@pytest.mark.parametrize(
["gpus", "expected_num_gpus", "strategy"],
[
pytest.param(None, 0, None, id="None - expect 0 gpu to use."),
pytest.param(0, 0, None, id="Oth gpu, expect 1 gpu to use."),
pytest.param(1, 1, None, id="1st gpu, expect 1 gpu to use."),
pytest.param(-1, 16, "ddp", id="-1 - use all gpus"),
pytest.param("-1", 16, "ddp", id="'-1' - use all gpus"),
pytest.param(3, 3, "ddp", id="3rd gpu - 1 gpu to use (backend:ddp)"),
],
)
def test_trainer_gpu_parse(monkeypatch, gpus, expected_num_gpus, strategy):
monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
monkeypatch.setattr(torch.cuda, "device_count", lambda: 16)
with pytest.deprecated_call(
match="`Trainer.num_gpus` was deprecated in v1.6 and will be removed in v1.8."
" Please use `Trainer.num_devices` instead."
):
assert Trainer(gpus=gpus, strategy=strategy).num_gpus == expected_num_gpus


@pytest.mark.parametrize(
["gpus", "expected_num_gpus", "strategy"],
[
pytest.param(None, 0, None, id="None - expect 0 gpu to use."),
pytest.param(None, 0, "ddp", id="None - expect 0 gpu to use."),
],
)
def test_trainer_num_gpu_0(monkeypatch, gpus, expected_num_gpus, strategy):
monkeypatch.setattr(torch.cuda, "device_count", lambda: 0)
with pytest.deprecated_call(
match="`Trainer.num_gpus` was deprecated in v1.6 and will be removed in v1.8."
" Please use `Trainer.num_devices` instead."
):
assert Trainer(gpus=gpus, strategy=strategy).num_gpus == expected_num_gpus
26 changes: 0 additions & 26 deletions tests/models/test_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,32 +92,6 @@ def device_count():
monkeypatch.setattr(torch.cuda, "device_count", device_count)


@pytest.mark.parametrize(
["gpus", "expected_num_gpus", "strategy"],
[
pytest.param(None, 0, None, id="None - expect 0 gpu to use."),
pytest.param(0, 0, None, id="Oth gpu, expect 1 gpu to use."),
pytest.param(1, 1, None, id="1st gpu, expect 1 gpu to use."),
pytest.param(-1, PRETEND_N_OF_GPUS, "ddp", id="-1 - use all gpus"),
pytest.param("-1", PRETEND_N_OF_GPUS, "ddp", id="'-1' - use all gpus"),
pytest.param(3, 3, "ddp", id="3rd gpu - 1 gpu to use (backend:ddp)"),
],
)
def test_trainer_gpu_parse(mocked_device_count, gpus, expected_num_gpus, strategy):
assert Trainer(gpus=gpus, strategy=strategy).num_gpus == expected_num_gpus


@pytest.mark.parametrize(
["gpus", "expected_num_gpus", "strategy"],
[
pytest.param(None, 0, None, id="None - expect 0 gpu to use."),
pytest.param(None, 0, "ddp", id="None - expect 0 gpu to use."),
],
)
def test_trainer_num_gpu_0(mocked_device_count_0, gpus, expected_num_gpus, strategy):
assert Trainer(gpus=gpus, strategy=strategy).num_gpus == expected_num_gpus


# Asking for a gpu when non are available will result in a MisconfigurationException
@pytest.mark.parametrize(
["gpus", "expected_root_gpu", "strategy"],
Expand Down
6 changes: 4 additions & 2 deletions tests/trainer/test_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,8 @@ def test_trainer_config_accelerator(
assert isinstance(trainer.strategy, strategy_cls)
assert strategy_cls.strategy_name == strategy_name
assert isinstance(trainer.accelerator, accelerator_cls)
assert trainer.num_gpus == num_gpus
trainer_num_gpus = trainer.num_devices if isinstance(trainer.accelerator, GPUAccelerator) else 0
assert trainer_num_gpus == num_gpus


def test_trainer_subclassing():
Expand Down Expand Up @@ -2097,7 +2098,8 @@ def test_trainer_config_strategy(monkeypatch, trainer_kwargs, strategy_cls, stra
assert isinstance(trainer.strategy, strategy_cls)
assert strategy_cls.strategy_name == strategy_name
assert isinstance(trainer.accelerator, accelerator_cls)
assert trainer.num_gpus == num_gpus
trainer_num_gpus = trainer.num_devices if isinstance(trainer.accelerator, GPUAccelerator) else 0
assert trainer_num_gpus == num_gpus
assert trainer.num_nodes == trainer_kwargs.get("num_nodes", 1)


Expand Down

0 comments on commit 31c68d1

Please sign in to comment.