diff --git a/CHANGELOG.md b/CHANGELOG.md index 888d22a520f75..638afacc0261f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -496,6 +496,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). - Removed `should_rank_save_checkpoint` property from Trainer ([#9433](https://github.com/PyTorchLightning/pytorch-lightning/pull/9433)) +- Remove deprecated `distributed_backend` from `Trainer` ([#10017](https://github.com/PyTorchLightning/pytorch-lightning/pull/10017)) + + ### Fixed diff --git a/docs/source/common/trainer.rst b/docs/source/common/trainer.rst index b5fed43ed2b0c..f8d815432a41c 100644 --- a/docs/source/common/trainer.rst +++ b/docs/source/common/trainer.rst @@ -216,7 +216,7 @@ accelerator | -The accelerator backend to use (previously known as distributed_backend). +The accelerator backend to use: - (``'dp'``) is DataParallel (split batch among GPUs of same machine) - (``'ddp'``) is DistributedDataParallel (each gpu on each node trains, and syncs grads) @@ -553,10 +553,6 @@ will need to be set up to use remote filepaths. # default used by the Trainer trainer = Trainer(default_root_dir=os.getcwd()) -distributed_backend -^^^^^^^^^^^^^^^^^^^ -Deprecated: This has been renamed ``accelerator``. - enable_checkpointing ^^^^^^^^^^^^^^^^^^^^ diff --git a/pytorch_lightning/trainer/connectors/accelerator_connector.py b/pytorch_lightning/trainer/connectors/accelerator_connector.py index 53f95ae4c8a14..878b3bfb0d0d4 100644 --- a/pytorch_lightning/trainer/connectors/accelerator_connector.py +++ b/pytorch_lightning/trainer/connectors/accelerator_connector.py @@ -92,7 +92,6 @@ def __init__( devices, tpu_cores, ipus, - distributed_backend, accelerator, strategy: Optional[Union[str, TrainingTypePlugin]], gpus, @@ -113,7 +112,8 @@ def __init__( self._accelerator_type = None self.strategy = strategy.lower() if isinstance(strategy, str) else strategy - self.distributed_backend = distributed_backend or accelerator + # TODO: Rename this to something else once all the distributed flags are moved to strategy + self.distributed_backend = accelerator self._init_deterministic(deterministic) @@ -152,7 +152,7 @@ def __init__( self.plugins = plugins - self._handle_accelerator_and_distributed_backend(distributed_backend, accelerator) + self._handle_accelerator_and_strategy() self._validate_accelerator_and_devices() @@ -176,10 +176,6 @@ def __init__( self._training_type_plugin_resolved = False self.accelerator = self.select_accelerator() - # override dist backend when using tpus - if self.use_tpu: - self.distributed_backend = "tpu" - # init flags for SLURM+DDP to work self.world_size = 1 self.interactive_ddp_procs = [] @@ -285,31 +281,16 @@ def _set_devices_if_none(self) -> None: elif self._accelerator_type == DeviceType.CPU: self.devices = self.num_processes - def _handle_accelerator_and_distributed_backend( - self, distributed_backend: Optional[str], accelerator: Optional[Union[str, Accelerator]] - ) -> None: - if distributed_backend is not None: - rank_zero_deprecation( - f"`Trainer(distributed_backend={distributed_backend!r})` " - "has been deprecated and will be removed in v1.5." - f" Use `Trainer(strategy={distributed_backend!r})` instead." - ) - if self.strategy is not None: - raise MisconfigurationException( - f"You have passed `Trainer(strategy={self.strategy!r})` but have" - f" also passed `Trainer(distributed_backend={distributed_backend!r})`." - f" HINT: Use just `Trainer(strategy={self.strategy!r})` instead." - ) - - if accelerator is not None and accelerator in list(DistributedType): + def _handle_accelerator_and_strategy(self) -> None: + if self.distributed_backend is not None and self.distributed_backend in list(DistributedType): rank_zero_deprecation( - f"Passing `Trainer(accelerator={accelerator!r})` has been deprecated" - f" in v1.5 and will be removed in v1.7. Use `Trainer(strategy={accelerator!r})` instead." + f"Passing `Trainer(accelerator={self.distributed_backend!r})` has been deprecated" + f" in v1.5 and will be removed in v1.7. Use `Trainer(strategy={self.distributed_backend!r})` instead." ) if self.strategy is not None: raise MisconfigurationException( f"You have passed `Trainer(strategy={self.strategy!r})` but have" - f" also passed `Trainer(accelerator={accelerator!r})`." + f" also passed `Trainer(accelerator={self.distributed_backend!r})`." f" HINT: Use just `Trainer(strategy={self.strategy!r})` instead." ) @@ -783,15 +764,15 @@ def select_cluster_environment(self) -> ClusterEnvironment: env = LightningEnvironment() return env - def set_distributed_mode(self, distributed_backend: Optional[str] = None): + def set_distributed_mode(self, strategy: Optional[str] = None): - if distributed_backend is None and self.is_training_type_in_plugins: + if strategy is None and self.is_training_type_in_plugins: return - if distributed_backend is not None and distributed_backend in TrainingTypePluginsRegistry: - self.distributed_backend = TrainingTypePluginsRegistry[distributed_backend]["distributed_backend"] - elif distributed_backend is not None: - self.distributed_backend = distributed_backend + if strategy is not None and strategy in TrainingTypePluginsRegistry: + self.distributed_backend = TrainingTypePluginsRegistry[strategy]["distributed_backend"] + elif strategy is not None: + self.distributed_backend = strategy if isinstance(self.distributed_backend, Accelerator): return diff --git a/pytorch_lightning/trainer/trainer.py b/pytorch_lightning/trainer/trainer.py index 07b2e62a436af..307043d0628ed 100644 --- a/pytorch_lightning/trainer/trainer.py +++ b/pytorch_lightning/trainer/trainer.py @@ -176,7 +176,6 @@ def __init__( plugins: Optional[Union[PLUGIN_INPUT, List[PLUGIN_INPUT]]] = None, amp_backend: str = "native", amp_level: Optional[str] = None, - distributed_backend: Optional[str] = None, move_metrics_to_cpu: bool = False, multiple_trainloader_mode: str = "max_size_cycle", stochastic_weight_avg: bool = False, @@ -187,7 +186,7 @@ def __init__( Args: - accelerator: Previously known as distributed_backend (dp, ddp, ddp2, etc...). + accelerator: (dp, ddp, ddp2, etc...). Can also take in an accelerator object for custom hardware. accumulate_grad_batches: Accumulates grads every k batches or as set up in the dict. @@ -241,8 +240,6 @@ def __init__( devices: Will be mapped to either `gpus`, `tpu_cores`, `num_processes` or `ipus`, based on the accelerator type. - distributed_backend: Deprecated. Please use ``accelerator``. - fast_dev_run: Runs n if set to ``n`` (int) else 1 if set to ``True`` batch(es) of train, val and test to find any bugs (ie: a sort of unit test). @@ -430,7 +427,6 @@ def __init__( devices, tpu_cores, ipus, - distributed_backend, accelerator, strategy, gpus, @@ -1513,11 +1509,6 @@ def _on_exception(self): def accelerator(self) -> Accelerator: return self.accelerator_connector.accelerator - @property - def distributed_backend(self) -> Optional[str]: - # for backward compatibility - return self.accelerator_connector.distributed_backend - @property def training_type_plugin(self) -> TrainingTypePlugin: return self.accelerator.training_type_plugin diff --git a/tests/accelerators/test_accelerator_connector.py b/tests/accelerators/test_accelerator_connector.py index d39eb82cc95cb..6f168b9275241 100644 --- a/tests/accelerators/test_accelerator_connector.py +++ b/tests/accelerators/test_accelerator_connector.py @@ -634,11 +634,6 @@ def test_accelerator_ddp_for_cpu(tmpdir): assert isinstance(trainer.training_type_plugin, DDPPlugin) -def test_exception_when_strategy_used_with_distributed_backend(): - with pytest.raises(MisconfigurationException, match="but have also passed"): - Trainer(distributed_backend="ddp_cpu", strategy="ddp_spawn") - - def test_exception_when_strategy_used_with_accelerator(): with pytest.raises(MisconfigurationException, match="but have also passed"): Trainer(accelerator="ddp", strategy="ddp_spawn") diff --git a/tests/deprecated_api/test_remove_1-5.py b/tests/deprecated_api/test_remove_1-5.py deleted file mode 100644 index a9d17601153ae..0000000000000 --- a/tests/deprecated_api/test_remove_1-5.py +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright The PyTorch Lightning team. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Test deprecated functionality which will be removed in v1.5.0.""" -import pytest - -from pytorch_lightning import Trainer - - -def test_v1_5_0_distributed_backend_trainer_flag(): - with pytest.deprecated_call(match="has been deprecated and will be removed in v1.5."): - Trainer(distributed_backend="ddp_cpu") diff --git a/tests/models/test_gpu.py b/tests/models/test_gpu.py index 2e30652995db2..9317804b1cca3 100644 --- a/tests/models/test_gpu.py +++ b/tests/models/test_gpu.py @@ -39,7 +39,7 @@ @RunIf(min_gpus=2) def test_multi_gpu_none_backend(tmpdir): - """Make sure when using multiple GPUs the user can't use `distributed_backend = None`.""" + """Make sure when using multiple GPUs the user can't use `accelerator = None`.""" tutils.set_random_master_port() trainer_options = dict( default_root_dir=tmpdir, diff --git a/tests/models/test_tpu.py b/tests/models/test_tpu.py index 13003676b176b..7806f257a3036 100644 --- a/tests/models/test_tpu.py +++ b/tests/models/test_tpu.py @@ -39,13 +39,6 @@ SERIAL_EXEC = xmp.MpSerialExecutor() -_LARGER_DATASET = RandomDataset(32, 2000) - - -# 8 cores needs a big dataset -def _serial_train_loader(): - return DataLoader(_LARGER_DATASET, batch_size=32) - class SerialLoaderBoringModel(BoringModel): def train_dataloader(self): @@ -277,9 +270,9 @@ def test_exception_when_no_tpu_found(tmpdir): @pytest.mark.parametrize("tpu_cores", [1, 8, [1]]) @RunIf(tpu=True) -def test_distributed_backend_set_when_using_tpu(tmpdir, tpu_cores): - """Test if distributed_backend is set to `tpu` when tpu_cores is not None.""" - assert Trainer(tpu_cores=tpu_cores).distributed_backend == "tpu" +def test_accelerator_set_when_using_tpu(tmpdir, tpu_cores): + """Test if the accelerator is set to `tpu` when tpu_cores is not None.""" + assert isinstance(Trainer(tpu_cores=tpu_cores).accelerator, TPUAccelerator) @RunIf(tpu=True) diff --git a/tests/utilities/test_cli.py b/tests/utilities/test_cli.py index b7c443fb24d4a..eb96a1ad8344e 100644 --- a/tests/utilities/test_cli.py +++ b/tests/utilities/test_cli.py @@ -134,7 +134,7 @@ def _raise(): # interface. min_steps=None, max_steps=None, - distributed_backend=None, + accelerator=None, weights_save_path=None, resume_from_checkpoint=None, profiler=None,