2021-04-10 06:55:07 +00:00
|
|
|
.. _plugins:
|
|
|
|
|
2020-10-22 09:15:51 +00:00
|
|
|
#######
|
|
|
|
Plugins
|
|
|
|
#######
|
|
|
|
|
2021-09-06 12:49:09 +00:00
|
|
|
.. include:: ../links.rst
|
|
|
|
|
2021-04-14 20:53:21 +00:00
|
|
|
Plugins allow custom integrations to the internals of the Trainer such as a custom precision or
|
|
|
|
distributed implementation.
|
2020-10-22 09:15:51 +00:00
|
|
|
|
2021-04-14 20:53:21 +00:00
|
|
|
Under the hood, the Lightning Trainer is using plugins in the training routine, added automatically
|
|
|
|
depending on the provided Trainer arguments. For example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# accelerator: GPUAccelerator
|
2021-12-22 20:23:30 +00:00
|
|
|
# training strategy: DDPStrategy
|
2021-04-14 20:53:21 +00:00
|
|
|
# precision: NativeMixedPrecisionPlugin
|
|
|
|
trainer = Trainer(gpus=4, precision=16)
|
|
|
|
|
|
|
|
|
|
|
|
We expose Accelerators and Plugins mainly for expert users that want to extend Lightning for:
|
|
|
|
|
|
|
|
- New hardware (like TPU plugin)
|
|
|
|
- Distributed backends (e.g. a backend not yet supported by
|
|
|
|
`PyTorch <https://pytorch.org/docs/stable/distributed.html#backends>`_ itself)
|
|
|
|
- Clusters (e.g. customized access to the cluster's environment interface)
|
|
|
|
|
|
|
|
There are two types of Plugins in Lightning with different responsibilities:
|
|
|
|
|
2021-12-20 12:50:11 +00:00
|
|
|
Strategy
|
|
|
|
--------
|
2021-04-14 20:53:21 +00:00
|
|
|
|
|
|
|
- Launching and teardown of training processes (if applicable)
|
|
|
|
- Setup communication between processes (NCCL, GLOO, MPI, ...)
|
|
|
|
- Provide a unified communication interface for reduction, broadcast, etc.
|
|
|
|
- Provide access to the wrapped LightningModule
|
|
|
|
|
|
|
|
|
2021-04-21 23:38:16 +00:00
|
|
|
Futhermore, for multi-node training Lightning provides cluster environment plugins that allow the advanced user
|
2021-11-29 20:38:23 +00:00
|
|
|
to configure Lightning to integrate with a :ref:`custom-cluster`.
|
2021-04-21 23:38:16 +00:00
|
|
|
|
|
|
|
|
2021-04-14 20:53:21 +00:00
|
|
|
.. image:: ../_static/images/accelerator/overview.svg
|
|
|
|
|
|
|
|
|
|
|
|
**********************
|
|
|
|
Create a custom plugin
|
|
|
|
**********************
|
|
|
|
|
|
|
|
Expert users may choose to extend an existing plugin by overriding its methods ...
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2021-12-23 07:26:28 +00:00
|
|
|
from pytorch_lightning.strategies import DDPStrategy
|
2021-04-14 20:53:21 +00:00
|
|
|
|
|
|
|
|
2021-12-21 08:55:51 +00:00
|
|
|
class CustomDDPStrategy(DDPStrategy):
|
2021-04-14 20:53:21 +00:00
|
|
|
def configure_ddp(self):
|
|
|
|
self._model = MyCustomDistributedDataParallel(
|
|
|
|
self.model,
|
|
|
|
device_ids=...,
|
|
|
|
)
|
|
|
|
|
2021-12-22 20:23:30 +00:00
|
|
|
or by subclassing the base classes :class:`~pytorch_lightning.strategies.Strategy` or
|
2021-04-14 20:53:21 +00:00
|
|
|
:class:`~pytorch_lightning.plugins.precision.PrecisionPlugin` to create new ones. These custom plugins
|
|
|
|
can then be passed into the Trainer directly or via a (custom) accelerator:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# custom plugins
|
2021-12-21 08:55:51 +00:00
|
|
|
trainer = Trainer(strategy=CustomDDPStrategy(), plugins=[CustomPrecisionPlugin()])
|
2021-04-14 20:53:21 +00:00
|
|
|
|
|
|
|
# fully custom accelerator and plugins
|
2021-12-16 04:41:34 +00:00
|
|
|
accelerator = MyAccelerator()
|
|
|
|
precision_plugin = MyPrecisionPlugin()
|
2021-12-21 08:55:51 +00:00
|
|
|
training_type_plugin = CustomDDPStrategy(accelerator=accelerator, precision_plugin=precision_plugin)
|
2021-12-16 04:41:34 +00:00
|
|
|
trainer = Trainer(strategy=training_type_plugin)
|
2021-04-14 20:53:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
The full list of built-in plugins is listed below.
|
|
|
|
|
|
|
|
|
|
|
|
.. warning:: The Plugin API is in beta and subject to change.
|
|
|
|
For help setting up custom plugins/accelerators, please reach out to us at **support@pytorchlightning.ai**
|
|
|
|
|
|
|
|
|
|
|
|
----------
|
|
|
|
|
|
|
|
|
2021-12-22 20:23:30 +00:00
|
|
|
Training Strategies
|
|
|
|
-------------------
|
2021-04-14 20:53:21 +00:00
|
|
|
|
2021-12-22 20:23:30 +00:00
|
|
|
.. currentmodule:: pytorch_lightning.strategies
|
2021-04-14 20:53:21 +00:00
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
:nosignatures:
|
|
|
|
:template: classtemplate.rst
|
|
|
|
|
2021-12-20 12:50:11 +00:00
|
|
|
Strategy
|
2021-12-21 23:56:14 +00:00
|
|
|
SingleDeviceStrategy
|
2021-12-22 01:09:17 +00:00
|
|
|
ParallelStrategy
|
2021-12-21 22:00:24 +00:00
|
|
|
DataParallelStrategy
|
2021-12-21 08:55:51 +00:00
|
|
|
DDPStrategy
|
2021-12-21 19:21:00 +00:00
|
|
|
DDP2Strategy
|
2021-12-21 17:18:25 +00:00
|
|
|
DDPShardedStrategy
|
2021-12-22 00:27:36 +00:00
|
|
|
DDPSpawnShardedStrategy
|
2021-12-21 23:06:14 +00:00
|
|
|
DDPSpawnStrategy
|
2022-02-04 17:02:09 +00:00
|
|
|
BaguaStrategy
|
2021-12-21 15:18:01 +00:00
|
|
|
DeepSpeedStrategy
|
2021-12-21 13:31:41 +00:00
|
|
|
HorovodStrategy
|
2021-12-21 20:09:30 +00:00
|
|
|
SingleTPUStrategy
|
2021-12-21 16:36:16 +00:00
|
|
|
TPUSpawnStrategy
|
2021-04-14 20:53:21 +00:00
|
|
|
|
|
|
|
|
2021-12-21 23:06:14 +00:00
|
|
|
|
2021-04-14 20:53:21 +00:00
|
|
|
Precision Plugins
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
.. currentmodule:: pytorch_lightning.plugins.precision
|
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
:nosignatures:
|
|
|
|
:template: classtemplate.rst
|
|
|
|
|
|
|
|
PrecisionPlugin
|
2021-10-19 17:48:57 +00:00
|
|
|
MixedPrecisionPlugin
|
2021-04-14 20:53:21 +00:00
|
|
|
NativeMixedPrecisionPlugin
|
|
|
|
ShardedNativeMixedPrecisionPlugin
|
|
|
|
ApexMixedPrecisionPlugin
|
|
|
|
DeepSpeedPrecisionPlugin
|
2021-10-19 17:48:57 +00:00
|
|
|
TPUPrecisionPlugin
|
2021-10-19 21:09:37 +00:00
|
|
|
TPUBf16PrecisionPlugin
|
2021-04-14 20:53:21 +00:00
|
|
|
DoublePrecisionPlugin
|
2021-10-19 17:48:57 +00:00
|
|
|
FullyShardedNativeMixedPrecisionPlugin
|
|
|
|
IPUPrecisionPlugin
|
2021-04-14 20:53:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
Cluster Environments
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
.. currentmodule:: pytorch_lightning.plugins.environments
|
|
|
|
|
|
|
|
.. autosummary::
|
|
|
|
:nosignatures:
|
|
|
|
:template: classtemplate.rst
|
|
|
|
|
|
|
|
ClusterEnvironment
|
|
|
|
LightningEnvironment
|
2021-07-09 14:14:26 +00:00
|
|
|
LSFEnvironment
|
2021-04-14 20:53:21 +00:00
|
|
|
TorchElasticEnvironment
|
2021-05-17 08:05:24 +00:00
|
|
|
KubeflowEnvironment
|
2021-04-14 20:53:21 +00:00
|
|
|
SLURMEnvironment
|