2021-01-30 19:55:28 +00:00
|
|
|
# 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.
|
2021-10-29 20:31:32 +00:00
|
|
|
from abc import abstractmethod
|
2021-12-02 03:34:51 +00:00
|
|
|
from typing import Any, Dict, Optional, Union
|
2021-01-30 19:55:28 +00:00
|
|
|
|
|
|
|
import torch
|
2021-04-15 16:48:16 +00:00
|
|
|
from torch.nn import Module
|
2021-01-30 19:55:28 +00:00
|
|
|
|
2021-04-10 06:55:07 +00:00
|
|
|
import pytorch_lightning as pl
|
2021-11-30 08:31:23 +00:00
|
|
|
from pytorch_lightning.plugins.precision import PrecisionPlugin
|
2021-11-29 20:11:21 +00:00
|
|
|
from pytorch_lightning.plugins.training_type import TrainingTypePlugin
|
2021-01-30 19:55:28 +00:00
|
|
|
|
|
|
|
|
2021-04-15 16:48:16 +00:00
|
|
|
class Accelerator:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""The Accelerator Base Class. An Accelerator is meant to deal with one type of Hardware.
|
2021-01-30 19:55:28 +00:00
|
|
|
|
|
|
|
Currently there are accelerators for:
|
2021-04-10 06:55:07 +00:00
|
|
|
|
2021-01-30 19:55:28 +00:00
|
|
|
- CPU
|
|
|
|
- GPU
|
|
|
|
- TPU
|
2021-09-27 04:09:16 +00:00
|
|
|
- IPU
|
2021-01-30 19:55:28 +00:00
|
|
|
|
|
|
|
Each Accelerator gets two plugins upon initialization:
|
|
|
|
One to handle differences from the training routine and one to handle different precisions.
|
|
|
|
"""
|
|
|
|
|
2021-11-19 00:39:01 +00:00
|
|
|
def __init__(self, precision_plugin: Optional[PrecisionPlugin], training_type_plugin: TrainingTypePlugin) -> None:
|
2021-01-30 19:55:28 +00:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
precision_plugin: the plugin to handle precision-specific parts
|
2021-11-19 00:39:01 +00:00
|
|
|
|
|
|
|
.. deprecated::
|
|
|
|
The ``precision_plugin`` parameter has been deprecated and will be removed soon.
|
|
|
|
Pass the precision plugin as a parameter to the ``TrainingTypePlugin`` instead.
|
|
|
|
|
2021-01-30 19:55:28 +00:00
|
|
|
training_type_plugin: the plugin to handle different training routines
|
|
|
|
"""
|
2021-11-19 00:39:01 +00:00
|
|
|
|
2021-01-30 19:55:28 +00:00
|
|
|
self.training_type_plugin = training_type_plugin
|
|
|
|
|
2021-11-19 00:39:01 +00:00
|
|
|
if precision_plugin is not None:
|
|
|
|
self.training_type_plugin._precision_plugin = precision_plugin
|
|
|
|
|
2021-03-18 21:33:39 +00:00
|
|
|
def setup_environment(self) -> None:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""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.
|
2021-03-18 21:33:39 +00:00
|
|
|
"""
|
|
|
|
self.training_type_plugin.setup_environment()
|
2021-01-30 19:55:28 +00:00
|
|
|
|
2021-08-04 15:43:34 +00:00
|
|
|
def setup(self, trainer: "pl.Trainer") -> None:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Setup plugins for the trainer fit and creates optimizers.
|
2021-04-10 06:55:07 +00:00
|
|
|
|
2021-01-30 19:55:28 +00:00
|
|
|
Args:
|
2021-03-18 21:33:39 +00:00
|
|
|
trainer: the trainer instance
|
2021-01-30 19:55:28 +00:00
|
|
|
"""
|
2021-11-30 08:31:23 +00:00
|
|
|
self.training_type_plugin.setup(trainer)
|
2021-02-17 20:41:18 +00:00
|
|
|
|
2021-01-30 19:55:28 +00:00
|
|
|
@property
|
2021-04-15 16:48:16 +00:00
|
|
|
def model(self) -> Module:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Returns the model.
|
|
|
|
|
|
|
|
This can also be a wrapped LightningModule. For retrieving the pure LightningModule use
|
|
|
|
:attr:`Accelerator.lightning_module`
|
2021-01-30 19:55:28 +00:00
|
|
|
"""
|
|
|
|
return self.training_type_plugin.model
|
|
|
|
|
|
|
|
@model.setter
|
2021-04-15 16:48:16 +00:00
|
|
|
def model(self, new_model: Module) -> None:
|
2021-01-30 19:55:28 +00:00
|
|
|
self.training_type_plugin.model = new_model
|
|
|
|
|
|
|
|
@property
|
2021-07-26 11:37:35 +00:00
|
|
|
def lightning_module(self) -> "pl.LightningModule":
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Returns the pure LightningModule.
|
|
|
|
|
2021-01-30 19:55:28 +00:00
|
|
|
To get the potentially wrapped model use :attr:`Accelerator.model`
|
|
|
|
"""
|
|
|
|
return self.training_type_plugin.lightning_module
|
|
|
|
|
|
|
|
@property
|
|
|
|
def root_device(self) -> torch.device:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Returns the root device."""
|
2021-01-30 19:55:28 +00:00
|
|
|
return self.training_type_plugin.root_device
|
|
|
|
|
2021-02-25 06:42:23 +00:00
|
|
|
def teardown(self) -> None:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""This method is called to teardown the training process.
|
|
|
|
|
2021-05-22 20:19:24 +00:00
|
|
|
It is the right place to release memory and free other resources.
|
2021-01-30 19:55:28 +00:00
|
|
|
"""
|
2021-05-22 20:19:24 +00:00
|
|
|
self.training_type_plugin.teardown()
|
2021-01-30 19:55:28 +00:00
|
|
|
|
2021-09-27 04:09:16 +00:00
|
|
|
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
|
|
|
|
|
2021-10-29 20:31:32 +00:00
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def auto_device_count() -> int:
|
|
|
|
"""Get the devices when set to auto."""
|