Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Accelerator.parallel_device_ids and deprecate Trainer.data_parallel_device_ids #12072

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- The strategies that support `sync_batchnorm` now only apply it when fitting ([#11919](https://github.com/PyTorchLightning/pytorch-lightning/pull/11919))


- Changed `Trainer.data_parallel_device_ids` to return a list of GPU device indexes ([#12072](https://github.com/PyTorchLightning/pytorch-lightning/pull/12072))

DuYicong515 marked this conversation as resolved.
Show resolved Hide resolved

### Deprecated

Expand Down Expand Up @@ -533,6 +535,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Deprecated `Trainer.num_gpus` in favor of `Trainer.num_devices` when GPU is used ([#12384](https://github.com/PyTorchLightning/pytorch-lightning/pull/12384))


- Deprecated `Trainer.data_parallel_device_ids` in favor of `Trainer.device_ids` ([#12072](https://github.com/PyTorchLightning/pytorch-lightning/pull/12072))


### Removed

- Removed deprecated parameter `method` in `pytorch_lightning.utilities.model_helpers.is_overridden` ([#10507](https://github.com/PyTorchLightning/pytorch-lightning/pull/10507))
Expand Down Expand Up @@ -726,6 +731,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Removed `AcceleratorConnector.num_gpus` property ([#12384](https://github.com/PyTorchLightning/pytorch-lightning/pull/12384))


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


### 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
3 changes: 1 addition & 2 deletions pytorch_lightning/callbacks/gpu_stats_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ def setup(self, trainer: "pl.Trainer", pl_module: "pl.LightningModule", stage: O
)

# The logical device IDs for selected devices
# ignoring mypy check because `trainer.data_parallel_device_ids` is None when using CPU
self._device_ids = sorted(set(trainer.data_parallel_device_ids)) # type: ignore
self._device_ids = sorted(set(trainer.device_ids))

# The unmasked real GPU IDs
self._gpu_ids = self._get_gpu_ids(self._device_ids)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,10 +819,6 @@ def num_ipus(self) -> int:
def gpus(self) -> Optional[Union[List[int], str, int]]:
return self._gpus

@property
def parallel_device_ids(self) -> List[int]:
return [i for i in range(len(self.parallel_devices))] if isinstance(self.accelerator, GPUAccelerator) else []

@property
def is_distributed(self) -> bool:
# Used for custom plugins.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def _log_gpus_metrics(self) -> None:
self.trainer.lightning_module.log(key, mem, prog_bar=False, logger=True)
else:
gpu_id = int(key.split("/")[0].split(":")[1])
if gpu_id in self.trainer._accelerator_connector.parallel_device_ids:
if gpu_id in self.trainer.device_ids:
self.trainer.lightning_module.log(
key, mem, prog_bar=False, logger=True, on_step=True, on_epoch=False
)
Expand Down
6 changes: 4 additions & 2 deletions pytorch_lightning/trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2087,9 +2087,11 @@ def devices(self) -> int:

@property
def data_parallel_device_ids(self) -> Optional[List[int]]:
return (
self._accelerator_connector.parallel_device_ids if self._accelerator_connector.parallel_device_ids else None
rank_zero_deprecation(
"`Trainer.data_parallel_device_ids` was deprecated in v1.6 and will be removed in v1.8."
" Please use `Trainer.device_ids` instead."
)
return self.device_ids if isinstance(self.accelerator, GPUAccelerator) else None

@property
def lightning_module(self) -> "pl.LightningModule":
Expand Down
27 changes: 27 additions & 0 deletions tests/deprecated_api/test_remove_1-8.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,3 +962,30 @@ def test_trainer_num_gpu_0(monkeypatch, gpus, expected_num_gpus, strategy):
" Please use `Trainer.num_devices` instead."
):
assert Trainer(gpus=gpus, strategy=strategy).num_gpus == expected_num_gpus


@pytest.mark.parametrize(
["trainer_kwargs", "expected_data_parallel_device_ids"],
[
({}, None),
({"devices": 1}, None),
({"devices": "1"}, None),
({"accelerator": "gpu", "devices": 1}, [0]),
({"accelerator": "gpu", "devices": 2}, [0, 1]),
({"accelerator": "gpu", "devices": [1]}, [1]),
({"accelerator": "gpu", "devices": "0"}, None),
({"accelerator": "gpu", "devices": "0,"}, [0]),
],
)
def test_trainer_data_parallel_device_ids(monkeypatch, trainer_kwargs, expected_data_parallel_device_ids):
"""Test multi type argument with bool."""
if trainer_kwargs.get("accelerator") == "gpu":
monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
monkeypatch.setattr(torch.cuda, "device_count", lambda: 2)

trainer = Trainer(**trainer_kwargs)
with pytest.deprecated_call(
match="`Trainer.data_parallel_device_ids` was deprecated in v1.6 and will be removed in v1.8."
" Please use `Trainer.device_ids` instead."
):
assert trainer.data_parallel_device_ids == expected_data_parallel_device_ids
6 changes: 4 additions & 2 deletions tests/models/test_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,16 @@ def test_parse_gpu_returns_none_when_no_devices_are_available(mocked_device_coun
)
@mock.patch("torch.cuda.device_count", return_value=1)
@mock.patch("torch.cuda.is_available", return_value=True)
@pytest.mark.parametrize("gpus", [[0, 1, 2], 2, "0"])
@pytest.mark.parametrize("gpus", [[0, 1, 2], 2, "0", [0, 2]])
def test_torchelastic_gpu_parsing(mocked_device_count, mocked_is_available, gpus):
"""Ensure when using torchelastic and nproc_per_node is set to the default of 1 per GPU device That we omit
sanitizing the gpus as only one of the GPUs is visible."""
trainer = Trainer(gpus=gpus)
assert isinstance(trainer._accelerator_connector.cluster_environment, TorchElasticEnvironment)
assert trainer.data_parallel_device_ids == device_parser.parse_gpu_ids(gpus)
assert trainer.gpus == gpus
# when use gpu
if device_parser.parse_gpu_ids(gpus) is not None:
assert trainer.device_ids == device_parser.parse_gpu_ids(gpus)


@RunIf(min_gpus=1)
Expand Down
9 changes: 4 additions & 5 deletions tests/trainer/test_trainer_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ def test_argparse_args_parsing_fast_dev_run(cli_args, expected):


@pytest.mark.parametrize(
["cli_args", "expected_parsed", "expected_device_ids"],
[("", None, None), ("--accelerator gpu --devices 1", "1", [0]), ("--accelerator gpu --devices 0,", "0,", [0])],
["cli_args", "expected_parsed"],
[("", None), ("--accelerator gpu --devices 1", "1"), ("--accelerator gpu --devices 0,", "0,")],
)
def test_argparse_args_parsing_devices(cli_args, expected_parsed, expected_device_ids, monkeypatch):
def test_argparse_args_parsing_devices(cli_args, expected_parsed, monkeypatch):
"""Test multi type argument with bool."""

monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
Expand All @@ -177,8 +177,7 @@ def test_argparse_args_parsing_devices(cli_args, expected_parsed, expected_devic
args = Trainer.parse_argparser(parser)

assert args.devices == expected_parsed
trainer = Trainer.from_argparse_args(args)
assert trainer.data_parallel_device_ids == expected_device_ids
assert Trainer.from_argparse_args(args)
DuYicong515 marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize(
Expand Down
6 changes: 4 additions & 2 deletions tests/utilities/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ def test_parse_args_parsing_complex_types(cli_args, expected, instantiate):
assert Trainer.from_argparse_args(args)


@pytest.mark.parametrize(["cli_args", "expected_gpu"], [("--gpus 1", [0]), ("--gpus 0,", [0]), ("--gpus 0,1", [0, 1])])
@pytest.mark.parametrize(
["cli_args", "expected_gpu"], [("--gpus 1", [0]), ("--gpus 0,", [0]), ("--gpus 1,", [1]), ("--gpus 0,1", [0, 1])]
)
def test_parse_args_parsing_gpus(monkeypatch, cli_args, expected_gpu):
"""Test parsing of gpus and instantiation of Trainer."""
monkeypatch.setattr("torch.cuda.device_count", lambda: 2)
Expand All @@ -192,7 +194,7 @@ def test_parse_args_parsing_gpus(monkeypatch, cli_args, expected_gpu):
args = parser.parse_args()

trainer = Trainer.from_argparse_args(args)
assert trainer.data_parallel_device_ids == expected_gpu
assert trainer.device_ids == expected_gpu


@pytest.mark.skipif(
Expand Down