Remove `AcceleratorConnector.root_gpu` and deprecate `Trainer.root_gpu` (#12262)

This commit is contained in:
DuYicong515 2022-03-19 16:53:50 -07:00 committed by GitHub
parent 6ba66789ae
commit 523200971d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 38 deletions

View File

@ -527,6 +527,9 @@ 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))
### Removed
- Removed deprecated parameter `method` in `pytorch_lightning.utilities.model_helpers.is_overridden` ([#10507](https://github.com/PyTorchLightning/pytorch-lightning/pull/10507))
@ -714,6 +717,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Removed public attribute `sync_batchnorm` from strategies ([#11754](https://github.com/PyTorchLightning/pytorch-lightning/pull/11754))
- Removed `AcceleratorConnector.root_gpu` property ([#12262](https://github.com/PyTorchLightning/pytorch-lightning/pull/12262))
### 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))

View File

@ -788,14 +788,6 @@ class AcceleratorConnector:
def num_processes(self) -> int:
return self.devices if self.devices is not None else 1
@property
def root_gpu(self) -> Optional[int]:
return (
self.strategy.root_device.index
if not isinstance(self.accelerator, (IPUAccelerator, TPUAccelerator))
else None
)
@property
def devices(self) -> int:
if isinstance(self.strategy, SingleDeviceStrategy):

View File

@ -2055,7 +2055,11 @@ class Trainer(
@property
def root_gpu(self) -> Optional[int]:
return self._accelerator_connector.root_gpu
rank_zero_deprecation(
"`Trainer.root_gpu` is deprecated in v1.6 and will be removed in v1.8. "
"Please use `Trainer.strategy.root_device.index` instead."
)
return self.strategy.root_device.index if isinstance(self.accelerator, GPUAccelerator) else None
@property
def tpu_cores(self) -> int:

View File

@ -887,3 +887,41 @@ def test_trainer_config_device_ids():
" Please use `Trainer.num_devices` or `Trainer.device_ids` to get device information instead."
):
trainer.devices == 2
@pytest.mark.parametrize(
["gpus", "expected_root_gpu", "strategy"],
[
pytest.param(None, None, "ddp", id="None is None"),
pytest.param(0, None, "ddp", id="O gpus, expect gpu root device to be None."),
pytest.param(1, 0, "ddp", id="1 gpu, expect gpu root device to be 0."),
pytest.param(-1, 0, "ddp", id="-1 - use all gpus, expect gpu root device to be 0."),
pytest.param("-1", 0, "ddp", id="'-1' - use all gpus, expect gpu root device to be 0."),
pytest.param(3, 0, "ddp", id="3 gpus, expect gpu root device to be 0.(backend:ddp)"),
],
)
def test_root_gpu_property(monkeypatch, gpus, expected_root_gpu, strategy):
monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
monkeypatch.setattr(torch.cuda, "device_count", lambda: 16)
with pytest.deprecated_call(
match="`Trainer.root_gpu` is deprecated in v1.6 and will be removed in v1.8. "
"Please use `Trainer.strategy.root_device.index` instead."
):
assert Trainer(gpus=gpus, strategy=strategy).root_gpu == expected_root_gpu
@pytest.mark.parametrize(
["gpus", "expected_root_gpu", "strategy"],
[
pytest.param(None, None, None, id="None is None"),
pytest.param(None, None, "ddp", id="None is None"),
pytest.param(0, None, "ddp", id="None is None"),
],
)
def test_root_gpu_property_0_passing(monkeypatch, gpus, expected_root_gpu, strategy):
monkeypatch.setattr(torch.cuda, "device_count", lambda: 0)
with pytest.deprecated_call(
match="`Trainer.root_gpu` is deprecated in v1.6 and will be removed in v1.8. "
"Please use `Trainer.strategy.root_device.index` instead."
):
assert Trainer(gpus=gpus, strategy=strategy).root_gpu == expected_root_gpu

View File

@ -101,8 +101,8 @@ def run_test_from_config(trainer_options, on_gpu, check_size=True):
if on_gpu:
trainer = Trainer(gpus=1, strategy="horovod", max_epochs=1)
# Test the root_gpu property
assert trainer.root_gpu == hvd.local_rank()
# test root gpu index
assert trainer.strategy.root_device.index == hvd.local_rank()
if __name__ == "__main__":

View File

@ -118,33 +118,6 @@ def test_trainer_num_gpu_0(mocked_device_count_0, gpus, expected_num_gpus, strat
assert Trainer(gpus=gpus, strategy=strategy).num_gpus == expected_num_gpus
@pytest.mark.parametrize(
["gpus", "expected_root_gpu", "strategy"],
[
pytest.param(None, None, "ddp", id="None is None"),
pytest.param(0, None, "ddp", id="O gpus, expect gpu root device to be None."),
pytest.param(1, 0, "ddp", id="1 gpu, expect gpu root device to be 0."),
pytest.param(-1, 0, "ddp", id="-1 - use all gpus, expect gpu root device to be 0."),
pytest.param("-1", 0, "ddp", id="'-1' - use all gpus, expect gpu root device to be 0."),
pytest.param(3, 0, "ddp", id="3 gpus, expect gpu root device to be 0.(backend:ddp)"),
],
)
def test_root_gpu_property(mocked_device_count, gpus, expected_root_gpu, strategy):
assert Trainer(gpus=gpus, strategy=strategy).root_gpu == expected_root_gpu
@pytest.mark.parametrize(
["gpus", "expected_root_gpu", "strategy"],
[
pytest.param(None, None, None, id="None is None"),
pytest.param(None, None, "ddp", id="None is None"),
pytest.param(0, None, "ddp", id="None is None"),
],
)
def test_root_gpu_property_0_passing(mocked_device_count_0, gpus, expected_root_gpu, strategy):
assert Trainer(gpus=gpus, strategy=strategy).root_gpu == expected_root_gpu
# Asking for a gpu when non are available will result in a MisconfigurationException
@pytest.mark.parametrize(
["gpus", "expected_root_gpu", "strategy"],