2021-03-29 20:50:51 +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-09-12 16:00:14 +00:00
|
|
|
from typing import Any, Dict
|
2021-10-29 20:31:32 +00:00
|
|
|
|
2022-09-12 16:00:14 +00:00
|
|
|
import torch
|
|
|
|
|
2022-02-28 16:06:23 +00:00
|
|
|
from pytorch_lightning import Trainer
|
2022-09-18 22:48:45 +00:00
|
|
|
from pytorch_lightning.accelerators import Accelerator
|
2022-02-28 16:06:23 +00:00
|
|
|
from pytorch_lightning.strategies import DDPStrategy
|
2021-10-29 20:31:32 +00:00
|
|
|
|
|
|
|
|
2022-02-28 16:06:23 +00:00
|
|
|
def test_pluggable_accelerator():
|
|
|
|
class TestAccelerator(Accelerator):
|
2022-09-12 16:00:14 +00:00
|
|
|
def setup_device(self, device: torch.device) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_device_stats(self, device: torch.device) -> Dict[str, Any]:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def teardown(self) -> None:
|
|
|
|
pass
|
|
|
|
|
2022-02-28 16:06:23 +00:00
|
|
|
@staticmethod
|
|
|
|
def parse_devices(devices):
|
|
|
|
return devices
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_parallel_devices(devices):
|
|
|
|
return ["foo"] * devices
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def auto_device_count():
|
|
|
|
return 3
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_available():
|
|
|
|
return True
|
|
|
|
|
2022-03-02 10:07:49 +00:00
|
|
|
@staticmethod
|
|
|
|
def name():
|
|
|
|
return "custom_acc_name"
|
|
|
|
|
2022-02-28 16:06:23 +00:00
|
|
|
trainer = Trainer(accelerator=TestAccelerator(), devices=2, strategy="ddp")
|
|
|
|
assert isinstance(trainer.accelerator, TestAccelerator)
|
|
|
|
assert isinstance(trainer.strategy, DDPStrategy)
|
2022-03-25 01:45:40 +00:00
|
|
|
assert trainer.strategy.parallel_devices == ["foo"] * 2
|
2022-02-28 16:06:23 +00:00
|
|
|
|
|
|
|
trainer = Trainer(strategy=DDPStrategy(TestAccelerator()), devices="auto")
|
|
|
|
assert isinstance(trainer.accelerator, TestAccelerator)
|
|
|
|
assert isinstance(trainer.strategy, DDPStrategy)
|
2022-03-25 01:45:40 +00:00
|
|
|
assert trainer.strategy.parallel_devices == ["foo"] * 3
|