forked from Lightning-AI/pytorch-lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccelerator.py
81 lines (63 loc) · 2.29 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
# 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.
from abc import ABC, abstractmethod
from typing import Any, Dict, Union
import torch
import pytorch_lightning as pl
class Accelerator(ABC):
"""The Accelerator Base Class. An Accelerator is meant to deal with one type of Hardware.
Currently there are accelerators for:
- CPU
- GPU
- TPU
- IPU
- HPU
"""
def setup_environment(self, root_device: torch.device) -> 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.
"""
def setup(self, trainer: "pl.Trainer") -> None:
"""Setup plugins for the trainer fit and creates optimizers.
Args:
trainer: the trainer instance
"""
def get_device_stats(self, device: Union[str, torch.device]) -> Dict[str, Any]:
"""Get stats for a given device.
Args:
device: device for which to get stats
Returns:
Dictionary of device stats
"""
raise NotImplementedError
@staticmethod
@abstractmethod
def parse_devices(devices: Any) -> Any:
"""Accelerator device parsing logic."""
@staticmethod
@abstractmethod
def get_parallel_devices(devices: Any) -> Any:
"""Gets parallel devices for the Accelerator."""
@staticmethod
@abstractmethod
def auto_device_count() -> int:
"""Get the device count when set to auto."""
@staticmethod
@abstractmethod
def is_available() -> bool:
"""Detect if the hardware is available."""
@classmethod
def register_accelerators(cls, accelerator_registry: Dict) -> None:
pass