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.
|
2022-01-23 19:47:43 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2021-12-16 04:41:34 +00:00
|
|
|
from typing import Any, Dict, Union
|
2021-01-30 19:55:28 +00:00
|
|
|
|
|
|
|
import torch
|
|
|
|
|
2021-04-10 06:55:07 +00:00
|
|
|
import pytorch_lightning as pl
|
2021-01-30 19:55:28 +00:00
|
|
|
|
|
|
|
|
2022-01-23 19:47:43 +00:00
|
|
|
class Accelerator(ABC):
|
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
|
|
|
"""
|
|
|
|
|
2021-12-16 04:41:34 +00:00
|
|
|
def setup_environment(self, root_device: torch.device) -> 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
|
|
|
"""
|
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-09-27 04:09:16 +00:00
|
|
|
def get_device_stats(self, device: Union[str, torch.device]) -> Dict[str, Any]:
|
2022-01-23 19:47:43 +00:00
|
|
|
"""Get stats for a given device.
|
2021-09-27 04:09:16 +00:00
|
|
|
|
|
|
|
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:
|
2022-01-23 19:47:43 +00:00
|
|
|
"""Get the device count when set to auto."""
|