Skip to content

Commit 0aa220b

Browse files
rohitgr7carmocca
andauthored
Remove deprecated distributed_backend from Trainer (#10017)
* rm distributed_backend from Trainer * unused * chlog * internal distributed_backend * Docstring Co-authored-by: Carlos Mocholi <[email protected]>
1 parent bb2dc68 commit 0aa220b

File tree

9 files changed

+24
-87
lines changed

9 files changed

+24
-87
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
496496
- Removed `should_rank_save_checkpoint` property from Trainer ([#9433](https://github.com/PyTorchLightning/pytorch-lightning/pull/9433))
497497

498498

499+
- Remove deprecated `distributed_backend` from `Trainer` ([#10017](https://github.com/PyTorchLightning/pytorch-lightning/pull/10017))
500+
501+
499502
### Fixed
500503

501504

docs/source/common/trainer.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ accelerator
216216

217217
|
218218
219-
The accelerator backend to use (previously known as distributed_backend).
219+
The accelerator backend to use:
220220

221221
- (``'dp'``) is DataParallel (split batch among GPUs of same machine)
222222
- (``'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.
553553
# default used by the Trainer
554554
trainer = Trainer(default_root_dir=os.getcwd())
555555

556-
distributed_backend
557-
^^^^^^^^^^^^^^^^^^^
558-
Deprecated: This has been renamed ``accelerator``.
559-
560556
enable_checkpointing
561557
^^^^^^^^^^^^^^^^^^^^
562558

pytorch_lightning/trainer/connectors/accelerator_connector.py

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ def __init__(
9292
devices,
9393
tpu_cores,
9494
ipus,
95-
distributed_backend,
9695
accelerator,
9796
strategy: Optional[Union[str, TrainingTypePlugin]],
9897
gpus,
@@ -113,7 +112,8 @@ def __init__(
113112
self._accelerator_type = None
114113

115114
self.strategy = strategy.lower() if isinstance(strategy, str) else strategy
116-
self.distributed_backend = distributed_backend or accelerator
115+
# TODO: Rename this to something else once all the distributed flags are moved to strategy
116+
self.distributed_backend = accelerator
117117

118118
self._init_deterministic(deterministic)
119119

@@ -152,7 +152,7 @@ def __init__(
152152

153153
self.plugins = plugins
154154

155-
self._handle_accelerator_and_distributed_backend(distributed_backend, accelerator)
155+
self._handle_accelerator_and_strategy()
156156

157157
self._validate_accelerator_and_devices()
158158

@@ -176,10 +176,6 @@ def __init__(
176176
self._training_type_plugin_resolved = False
177177
self.accelerator = self.select_accelerator()
178178

179-
# override dist backend when using tpus
180-
if self.use_tpu:
181-
self.distributed_backend = "tpu"
182-
183179
# init flags for SLURM+DDP to work
184180
self.world_size = 1
185181
self.interactive_ddp_procs = []
@@ -285,31 +281,16 @@ def _set_devices_if_none(self) -> None:
285281
elif self._accelerator_type == DeviceType.CPU:
286282
self.devices = self.num_processes
287283

288-
def _handle_accelerator_and_distributed_backend(
289-
self, distributed_backend: Optional[str], accelerator: Optional[Union[str, Accelerator]]
290-
) -> None:
291-
if distributed_backend is not None:
292-
rank_zero_deprecation(
293-
f"`Trainer(distributed_backend={distributed_backend!r})` "
294-
"has been deprecated and will be removed in v1.5."
295-
f" Use `Trainer(strategy={distributed_backend!r})` instead."
296-
)
297-
if self.strategy is not None:
298-
raise MisconfigurationException(
299-
f"You have passed `Trainer(strategy={self.strategy!r})` but have"
300-
f" also passed `Trainer(distributed_backend={distributed_backend!r})`."
301-
f" HINT: Use just `Trainer(strategy={self.strategy!r})` instead."
302-
)
303-
304-
if accelerator is not None and accelerator in list(DistributedType):
284+
def _handle_accelerator_and_strategy(self) -> None:
285+
if self.distributed_backend is not None and self.distributed_backend in list(DistributedType):
305286
rank_zero_deprecation(
306-
f"Passing `Trainer(accelerator={accelerator!r})` has been deprecated"
307-
f" in v1.5 and will be removed in v1.7. Use `Trainer(strategy={accelerator!r})` instead."
287+
f"Passing `Trainer(accelerator={self.distributed_backend!r})` has been deprecated"
288+
f" in v1.5 and will be removed in v1.7. Use `Trainer(strategy={self.distributed_backend!r})` instead."
308289
)
309290
if self.strategy is not None:
310291
raise MisconfigurationException(
311292
f"You have passed `Trainer(strategy={self.strategy!r})` but have"
312-
f" also passed `Trainer(accelerator={accelerator!r})`."
293+
f" also passed `Trainer(accelerator={self.distributed_backend!r})`."
313294
f" HINT: Use just `Trainer(strategy={self.strategy!r})` instead."
314295
)
315296

@@ -783,15 +764,15 @@ def select_cluster_environment(self) -> ClusterEnvironment:
783764
env = LightningEnvironment()
784765
return env
785766

786-
def set_distributed_mode(self, distributed_backend: Optional[str] = None):
767+
def set_distributed_mode(self, strategy: Optional[str] = None):
787768

788-
if distributed_backend is None and self.is_training_type_in_plugins:
769+
if strategy is None and self.is_training_type_in_plugins:
789770
return
790771

791-
if distributed_backend is not None and distributed_backend in TrainingTypePluginsRegistry:
792-
self.distributed_backend = TrainingTypePluginsRegistry[distributed_backend]["distributed_backend"]
793-
elif distributed_backend is not None:
794-
self.distributed_backend = distributed_backend
772+
if strategy is not None and strategy in TrainingTypePluginsRegistry:
773+
self.distributed_backend = TrainingTypePluginsRegistry[strategy]["distributed_backend"]
774+
elif strategy is not None:
775+
self.distributed_backend = strategy
795776

796777
if isinstance(self.distributed_backend, Accelerator):
797778
return

pytorch_lightning/trainer/trainer.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ def __init__(
176176
plugins: Optional[Union[PLUGIN_INPUT, List[PLUGIN_INPUT]]] = None,
177177
amp_backend: str = "native",
178178
amp_level: Optional[str] = None,
179-
distributed_backend: Optional[str] = None,
180179
move_metrics_to_cpu: bool = False,
181180
multiple_trainloader_mode: str = "max_size_cycle",
182181
stochastic_weight_avg: bool = False,
@@ -187,7 +186,7 @@ def __init__(
187186
188187
Args:
189188
190-
accelerator: Previously known as distributed_backend (dp, ddp, ddp2, etc...).
189+
accelerator: (dp, ddp, ddp2, etc...).
191190
Can also take in an accelerator object for custom hardware.
192191
193192
accumulate_grad_batches: Accumulates grads every k batches or as set up in the dict.
@@ -241,8 +240,6 @@ def __init__(
241240
devices: Will be mapped to either `gpus`, `tpu_cores`, `num_processes` or `ipus`,
242241
based on the accelerator type.
243242
244-
distributed_backend: Deprecated. Please use ``accelerator``.
245-
246243
fast_dev_run: Runs n if set to ``n`` (int) else 1 if set to ``True`` batch(es)
247244
of train, val and test to find any bugs (ie: a sort of unit test).
248245
@@ -430,7 +427,6 @@ def __init__(
430427
devices,
431428
tpu_cores,
432429
ipus,
433-
distributed_backend,
434430
accelerator,
435431
strategy,
436432
gpus,
@@ -1513,11 +1509,6 @@ def _on_exception(self):
15131509
def accelerator(self) -> Accelerator:
15141510
return self.accelerator_connector.accelerator
15151511

1516-
@property
1517-
def distributed_backend(self) -> Optional[str]:
1518-
# for backward compatibility
1519-
return self.accelerator_connector.distributed_backend
1520-
15211512
@property
15221513
def training_type_plugin(self) -> TrainingTypePlugin:
15231514
return self.accelerator.training_type_plugin

tests/accelerators/test_accelerator_connector.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,6 @@ def test_accelerator_ddp_for_cpu(tmpdir):
634634
assert isinstance(trainer.training_type_plugin, DDPPlugin)
635635

636636

637-
def test_exception_when_strategy_used_with_distributed_backend():
638-
with pytest.raises(MisconfigurationException, match="but have also passed"):
639-
Trainer(distributed_backend="ddp_cpu", strategy="ddp_spawn")
640-
641-
642637
def test_exception_when_strategy_used_with_accelerator():
643638
with pytest.raises(MisconfigurationException, match="but have also passed"):
644639
Trainer(accelerator="ddp", strategy="ddp_spawn")

tests/deprecated_api/test_remove_1-5.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/models/test_gpu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
@RunIf(min_gpus=2)
4141
def test_multi_gpu_none_backend(tmpdir):
42-
"""Make sure when using multiple GPUs the user can't use `distributed_backend = None`."""
42+
"""Make sure when using multiple GPUs the user can't use `accelerator = None`."""
4343
tutils.set_random_master_port()
4444
trainer_options = dict(
4545
default_root_dir=tmpdir,

tests/models/test_tpu.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@
3939

4040
SERIAL_EXEC = xmp.MpSerialExecutor()
4141

42-
_LARGER_DATASET = RandomDataset(32, 2000)
43-
44-
45-
# 8 cores needs a big dataset
46-
def _serial_train_loader():
47-
return DataLoader(_LARGER_DATASET, batch_size=32)
48-
4942

5043
class SerialLoaderBoringModel(BoringModel):
5144
def train_dataloader(self):
@@ -277,9 +270,9 @@ def test_exception_when_no_tpu_found(tmpdir):
277270

278271
@pytest.mark.parametrize("tpu_cores", [1, 8, [1]])
279272
@RunIf(tpu=True)
280-
def test_distributed_backend_set_when_using_tpu(tmpdir, tpu_cores):
281-
"""Test if distributed_backend is set to `tpu` when tpu_cores is not None."""
282-
assert Trainer(tpu_cores=tpu_cores).distributed_backend == "tpu"
273+
def test_accelerator_set_when_using_tpu(tmpdir, tpu_cores):
274+
"""Test if the accelerator is set to `tpu` when tpu_cores is not None."""
275+
assert isinstance(Trainer(tpu_cores=tpu_cores).accelerator, TPUAccelerator)
283276

284277

285278
@RunIf(tpu=True)

tests/utilities/test_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def _raise():
134134
# interface.
135135
min_steps=None,
136136
max_steps=None,
137-
distributed_backend=None,
137+
accelerator=None,
138138
weights_save_path=None,
139139
resume_from_checkpoint=None,
140140
profiler=None,

0 commit comments

Comments
 (0)