-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathaccelerator.py
493 lines (389 loc) · 20 KB
/
accelerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# 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.
import contextlib
from typing import Any, Callable, Dict, Generator, Iterable, List, Optional, Union
import torch
from torch import Tensor
from torch.cuda.amp import GradScaler
from torch.nn import Module
from torch.optim import Optimizer
from torch.utils.data import DataLoader
import pytorch_lightning as pl
from pytorch_lightning.plugins.precision import ApexMixedPrecisionPlugin, NativeMixedPrecisionPlugin, PrecisionPlugin
from pytorch_lightning.plugins.training_type import DataParallelPlugin, TrainingTypePlugin
from pytorch_lightning.trainer.states import TrainerFn
from pytorch_lightning.utilities import rank_zero_deprecation
from pytorch_lightning.utilities.apply_func import apply_to_collection, move_data_to_device
from pytorch_lightning.utilities.enums import AMPType, GradClipAlgorithmType, LightningEnum
from pytorch_lightning.utilities.types import _PATH, STEP_OUTPUT
class Accelerator:
"""The Accelerator Base Class. An Accelerator is meant to deal with one type of Hardware.
Currently there are accelerators for:
- CPU
- GPU
- TPU
- IPU
Each Accelerator gets two plugins upon initialization:
One to handle differences from the training routine and one to handle different precisions.
"""
def __init__(self, precision_plugin: PrecisionPlugin, training_type_plugin: TrainingTypePlugin) -> None:
"""
Args:
precision_plugin: the plugin to handle precision-specific parts
training_type_plugin: the plugin to handle different training routines
"""
self.precision_plugin = precision_plugin
self.training_type_plugin = training_type_plugin
self.optimizers: List = []
self.lr_schedulers: List = []
self.optimizer_frequencies: List = []
def connect(self, model: "pl.LightningModule") -> None:
"""Transfers ownership of the model to this plugin."""
self.training_type_plugin.connect(model)
def setup_environment(self) -> None:
"""Setup any processes or distributed connections.
This is called before the LightningModule/DataModule setup hook which allows the user to access the accelerator
environment before setup is complete.
"""
self.training_type_plugin.setup_environment()
def setup(self, trainer: "pl.Trainer") -> None:
"""Setup plugins for the trainer fit and creates optimizers.
Args:
trainer: the trainer instance
"""
self.setup_training_type_plugin()
if not self.training_type_plugin.setup_optimizers_in_pre_dispatch:
self.setup_optimizers(trainer)
self.setup_precision_plugin()
def start_training(self, trainer: "pl.Trainer") -> None:
self.training_type_plugin.start_training(trainer)
def start_evaluating(self, trainer: "pl.Trainer") -> None:
self.training_type_plugin.start_evaluating(trainer)
def start_predicting(self, trainer: "pl.Trainer") -> None:
self.training_type_plugin.start_predicting(trainer)
def pre_dispatch(self, trainer: "pl.Trainer") -> None:
"""Hook to do something before the training/evaluation/prediction starts."""
self._move_optimizer_state()
self.training_type_plugin.pre_dispatch()
if self.training_type_plugin.setup_optimizers_in_pre_dispatch:
self.setup_optimizers(trainer)
self.precision_plugin.pre_dispatch()
def _move_optimizer_state(self, device: Optional[torch.device] = None) -> None:
"""Moves the state of the optimizers to the GPU if needed."""
device = device or self.root_device
for opt in self.optimizers:
for p, v in opt.state.items():
opt.state[p] = apply_to_collection(v, torch.Tensor, move_data_to_device, device)
def dispatch(self, trainer: "pl.Trainer") -> None:
"""Hook to do something before the training/evaluation/prediction starts."""
self.training_type_plugin.dispatch(trainer)
self.precision_plugin.dispatch(trainer)
def post_dispatch(self, trainer: "pl.Trainer") -> None:
"""Hook to do something after the training/evaluation/prediction starts."""
self.training_type_plugin.post_dispatch(trainer)
self.precision_plugin.post_dispatch()
@property
def model(self) -> Module:
"""Returns the model.
This can also be a wrapped LightningModule. For retrieving the pure LightningModule use
:attr:`Accelerator.lightning_module`
"""
return self.training_type_plugin.model
@model.setter
def model(self, new_model: Module) -> None:
self.training_type_plugin.model = new_model
@property
def lightning_module(self) -> "pl.LightningModule":
"""Returns the pure LightningModule.
To get the potentially wrapped model use :attr:`Accelerator.model`
"""
return self.training_type_plugin.lightning_module
@property
def root_device(self) -> torch.device:
"""Returns the root device."""
return self.training_type_plugin.root_device
def teardown(self) -> None:
"""This method is called to teardown the training process.
It is the right place to release memory and free other resources.
"""
self.training_type_plugin.teardown()
def batch_to_device(self, batch: Any, device: Optional[torch.device] = None, dataloader_idx: int = 0) -> Any:
"""Moves the batch to the correct device. The returned batch is of the same type as the input batch, just
having all tensors on the correct device.
Args:
batch: The batch of samples to move to the correct device
device: The target device
dataloader_idx: The index of the dataloader to which the batch belongs.
"""
model = self.lightning_module
device = device or self.root_device
if model is not None and not isinstance(self.training_type_plugin, DataParallelPlugin):
# no need to transfer batch to device in DP mode
return model._apply_batch_transfer_handler(batch, device=device, dataloader_idx=dataloader_idx)
return move_data_to_device(batch, device)
def training_step(self, step_kwargs: Dict[str, Union[Any, int]]) -> STEP_OUTPUT:
"""The actual training step.
See :meth:`~pytorch_lightning.core.lightning.LightningModule.training_step` for more details
"""
with self.precision_plugin.train_step_context():
return self.training_type_plugin.training_step(*step_kwargs.values())
def post_training_step(self) -> None:
self.training_type_plugin.post_training_step()
def validation_step(self, step_kwargs: Dict[str, Union[Any, int]]) -> Optional[STEP_OUTPUT]:
"""The actual validation step.
See :meth:`~pytorch_lightning.core.lightning.LightningModule.validation_step` for more details
"""
with self.precision_plugin.val_step_context():
return self.training_type_plugin.validation_step(*step_kwargs.values())
def test_step(self, step_kwargs: Dict[str, Union[Any, int]]) -> Optional[STEP_OUTPUT]:
"""The actual test step.
See :meth:`~pytorch_lightning.core.lightning.LightningModule.test_step` for more details
"""
with self.precision_plugin.test_step_context():
return self.training_type_plugin.test_step(*step_kwargs.values())
def predict_step(self, step_kwargs: Dict[str, Union[Any, int]]) -> STEP_OUTPUT:
"""The actual predict step.
See :meth:`~pytorch_lightning.core.lightning.LightningModule.predict_step` for more details
"""
with self.precision_plugin.predict_step_context():
return self.training_type_plugin.predict_step(*step_kwargs.values())
def training_step_end(self, output: STEP_OUTPUT) -> STEP_OUTPUT:
"""A hook to do something at the end of the training step.
Args:
output: the output of the training step
"""
return self.training_type_plugin.training_step_end(output)
def test_step_end(self, output: Optional[STEP_OUTPUT]) -> Optional[STEP_OUTPUT]:
"""A hook to do something at the end of the test step.
Args:
output: the output of the test step
"""
return self.training_type_plugin.test_step_end(output)
def validation_step_end(self, output: Optional[STEP_OUTPUT]) -> Optional[STEP_OUTPUT]:
"""A hook to do something at the end of the validation step.
Args:
output: the output of the validation step
"""
return self.training_type_plugin.validation_step_end(output)
def backward(self, closure_loss: Tensor, *args: Any, **kwargs: Any) -> Tensor:
"""Forwards backward-calls to the precision plugin.
Args:
closure_loss: a tensor holding the loss value to backpropagate
"""
self.training_type_plugin.pre_backward(closure_loss)
closure_loss = self.precision_plugin.pre_backward(self.lightning_module, closure_loss)
self.precision_plugin.backward(self.lightning_module, closure_loss, *args, **kwargs)
closure_loss = self.precision_plugin.post_backward(self.lightning_module, closure_loss)
self.training_type_plugin.post_backward(closure_loss)
return closure_loss
def optimizer_step(self, optimizer: Optimizer, opt_idx: int, lambda_closure: Callable, **kwargs: Any) -> None:
"""performs the actual optimizer step.
Args:
optimizer: the optimizer performing the step
opt_idx: index of the current optimizer
lambda_closure: closure calculating the loss value
"""
make_optimizer_step = self.precision_plugin.pre_optimizer_step(
self.lightning_module, optimizer, opt_idx, lambda_closure, **kwargs
)
if make_optimizer_step:
self.run_optimizer_step(optimizer, opt_idx, lambda_closure, **kwargs)
def run_optimizer_step(
self, optimizer: Optimizer, optimizer_idx: int, lambda_closure: Callable, **kwargs: Any
) -> None:
self.training_type_plugin.optimizer_step(optimizer, lambda_closure=lambda_closure, **kwargs)
def optimizer_zero_grad(self, current_epoch: int, batch_idx: int, optimizer: Optimizer, opt_idx: int) -> None:
"""Zeros all model parameter's gradients."""
model_ref = self.lightning_module
model_ref.optimizer_zero_grad(current_epoch, batch_idx, optimizer, opt_idx)
def clip_gradients(
self,
optimizer: Optimizer,
clip_val: Union[int, float],
gradient_clip_algorithm: GradClipAlgorithmType = GradClipAlgorithmType.NORM,
) -> None:
"""clips all the optimizer parameters to the given value."""
self.precision_plugin.clip_gradients(
optimizer, clip_val, gradient_clip_algorithm=gradient_clip_algorithm, model=self.model
)
def setup_optimizers(self, trainer: "pl.Trainer") -> None:
"""Creates optimizers and schedulers.
Args:
trainer: the Trainer, these optimizers should be connected to
"""
if trainer.state.fn not in (TrainerFn.FITTING, TrainerFn.TUNING):
return
optimizers, lr_schedulers, optimizer_frequencies = self.training_type_plugin.init_optimizers(
trainer=trainer, model=self.lightning_module
)
self.optimizers = optimizers
self.lr_schedulers = lr_schedulers
self.optimizer_frequencies = optimizer_frequencies
def setup_training_type_plugin(self) -> None:
"""Attaches the training type plugin to the accelerator."""
self.training_type_plugin.setup()
def setup_precision_plugin(self) -> None:
"""Attaches the precision plugin to the accelerator."""
model, optimizers, schedulers = self.precision_plugin.connect(self.model, self.optimizers, self.lr_schedulers)
self.model = model
self.optimizers = optimizers
self.lr_schedulers = schedulers
@property
def amp_backend(self) -> Optional[LightningEnum]:
if isinstance(self.precision_plugin, ApexMixedPrecisionPlugin):
return AMPType.APEX
if isinstance(self.precision_plugin, NativeMixedPrecisionPlugin):
return AMPType.NATIVE
return None
@property
def precision(self) -> Union[str, int]:
return self.precision_plugin.precision
@property
def scaler(self) -> Optional["GradScaler"]:
return getattr(self.precision_plugin, "scaler", None)
def optimizer_state(self, optimizer: Optimizer) -> Dict[str, Tensor]:
"""Returns state of an optimizer.
Allows for syncing/collating optimizer state from processes in custom plugins.
"""
return getattr(self.training_type_plugin, "optimizer_state", lambda x: x.state_dict())(optimizer)
def lightning_module_state_dict(self) -> Dict[str, Union[Any, Tensor]]:
"""Returns state of model.
Allows for syncing/collating model state from processes in custom plugins.
"""
return self.training_type_plugin.lightning_module_state_dict()
def barrier(self, name: Optional[str] = None) -> None:
"""
.. deprecated:: v1.5
This method is deprecated in v1.5 and will be removed in v1.6.
Please call ``training_type_plugin.barrier`` directly.
"""
rank_zero_deprecation(
"`Accelerator.barrier` is deprecated in v1.5 and will be removed in v1.6. "
"Barrier logic is implemented directly in the `TrainingTypePlugin` implementations."
)
self.training_type_plugin.barrier(name=name)
def broadcast(self, obj: object, src: int = 0) -> object:
"""Broadcasts an object to all processes, such that the src object is broadcast to all other ranks if
needed.
.. deprecated:: v1.5
This method is deprecated in v1.5 and will be removed in v1.6.
Please call ``training_type_plugin.broadcast`` directly.
Args:
obj: Object to broadcast to all process, usually a tensor or collection of tensors.
src: The source rank of which the object will be broadcast from
"""
rank_zero_deprecation(
"`Accelerator.broadcast` is deprecated in v1.5 and will be removed in v1.6. "
"Broadcast logic is implemented directly in the `TrainingTypePlugin` implementations."
)
return self.training_type_plugin.broadcast(obj, src)
def all_gather(self, tensor: Tensor, group: Optional[Any] = None, sync_grads: bool = False) -> Tensor:
"""Function to gather a tensor from several distributed processes.
.. deprecated:: v1.5
This method is deprecated in v1.5 and will be removed in v1.6.
Please call ``training_type_plugin.all_gather`` directly.
Args:
tensor: tensor of shape (batch, ...)
group: the process group to gather results from. Defaults to all processes (world)
sync_grads: flag that allows users to synchronize gradients for all_gather op
Return:
A tensor of shape (world_size, batch, ...)
"""
rank_zero_deprecation(
"`Accelerator.all_gather` is deprecated in v1.5 and will be removed in v1.6. "
"All-gather logic is implemented directly in the `TrainingTypePlugin` implementations."
)
return self.training_type_plugin.all_gather(tensor, group=group, sync_grads=sync_grads)
def process_dataloader(self, dataloader: Union[Iterable, DataLoader]) -> Union[Iterable, DataLoader]:
"""Wraps the dataloader if necessary.
Args:
dataloader: iterable. Ideally of type: :class:`torch.utils.data.DataLoader`
"""
return self.training_type_plugin.process_dataloader(dataloader)
@property
def results(self) -> Any:
"""The results of the last run will be cached within the training type plugin.
In distributed training, we make sure to transfer the results to the appropriate master process.
"""
return self.training_type_plugin.results
@contextlib.contextmanager
def model_sharded_context(self) -> Generator[None, None, None]:
"""Provide hook to create modules in a distributed aware context. This is useful for when we'd like to.
shard the model instantly - useful for extremely large models. Can save memory and
initialization time.
Returns:
Model parallel context.
"""
with self.training_type_plugin.model_sharded_context():
yield
def save_checkpoint(self, checkpoint: Dict[str, Any], filepath: _PATH) -> None:
"""Save model/training states as a checkpoint file through state-dump and file-write.
Args:
checkpoint: dict containing model and trainer state
filepath: write-target file's path
"""
self.training_type_plugin.save_checkpoint(checkpoint, filepath)
@property
def setup_optimizers_in_pre_dispatch(self) -> bool:
"""Override to delay setting optimizers and schedulers till after dispatch. This is useful when the
`TrainingTypePlugin` requires operating on the wrapped accelerator model. However this may break certain
precision plugins such as APEX which require optimizers to be set.
Returns:
If True, delay setup optimizers until `pre_dispatch`, else call within `setup`.
"""
return self.training_type_plugin.setup_optimizers_in_pre_dispatch
@property
def restore_checkpoint_after_pre_dispatch(self) -> bool:
"""Override to delay restoring from checkpoint till after pre-dispatch. This is useful when the plugin
requires all the setup hooks to run before loading checkpoint.
Returns:
If true, restore checkpoint after pre_dispatch.
"""
return self.training_type_plugin.restore_checkpoint_after_pre_dispatch
def get_device_stats(self, device: Union[str, torch.device]) -> Dict[str, Any]:
"""Gets stats for a given device.
Args:
device: device for which to get stats
Returns:
Dictionary of device stats
"""
raise NotImplementedError
def on_train_start(self) -> None:
"""Called when train begins."""
return self.training_type_plugin.on_train_start()
def on_validation_start(self) -> None:
"""Called when validation begins."""
return self.training_type_plugin.on_validation_start()
def on_test_start(self) -> None:
"""Called when test begins."""
return self.training_type_plugin.on_test_start()
def on_predict_start(self) -> None:
"""Called when predict begins."""
return self.training_type_plugin.on_predict_start()
def on_validation_end(self) -> None:
"""Called when validation ends."""
return self.training_type_plugin.on_validation_end()
def on_test_end(self) -> None:
"""Called when test end."""
return self.training_type_plugin.on_test_end()
def on_predict_end(self) -> None:
"""Called when predict ends."""
return self.training_type_plugin.on_predict_end()
def on_train_end(self) -> None:
"""Called when train ends."""
return self.training_type_plugin.on_train_end()
# TODO: Update this in v1.7 (deprecation: #9816)
def on_train_batch_start(self, batch: Any, batch_idx: int, dataloader_idx: int = 0) -> None:
"""Called in the training loop before anything happens for that batch."""
return self.training_type_plugin.on_train_batch_start(batch, batch_idx)