55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
# 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 unittest.mock import Mock
|
|
|
|
import pytest
|
|
import torch
|
|
|
|
from lightning_fabric.strategies import SingleDeviceStrategy
|
|
|
|
|
|
def test_single_device_default_device():
|
|
assert SingleDeviceStrategy().root_device == torch.device("cpu")
|
|
|
|
|
|
@pytest.mark.parametrize("device", ["cpu", torch.device("cpu"), "cuda:1", torch.device("cuda")])
|
|
def test_single_device_root_device(device):
|
|
assert SingleDeviceStrategy(device).root_device == torch.device(device)
|
|
|
|
|
|
@pytest.mark.parametrize("device", [torch.device("cpu"), torch.device("cuda", 3)])
|
|
def test_single_device_ranks(device):
|
|
strategy = SingleDeviceStrategy(device)
|
|
assert strategy.world_size == 1
|
|
assert strategy.local_rank == 0
|
|
assert strategy.global_rank == 0
|
|
assert strategy.is_global_zero
|
|
|
|
|
|
def test_single_device_collectives():
|
|
"""Test that collectives in the single-device strategy act as the identity."""
|
|
strategy = SingleDeviceStrategy()
|
|
tensor = Mock()
|
|
assert strategy.all_gather(tensor) == tensor
|
|
assert strategy.reduce(tensor) == tensor
|
|
assert strategy.broadcast(tensor) == tensor
|
|
|
|
|
|
def test_single_device_module_to_device():
|
|
strategy = SingleDeviceStrategy()
|
|
strategy._root_device = Mock()
|
|
module = Mock(spec=torch.nn.Module)
|
|
strategy.module_to_device(module)
|
|
module.to.assert_called_with(strategy.root_device)
|