Standalone Lite: Strategy base classes and registry (#14662)
* add accelerator implementations to lite
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix imports
* rename registry argument
* fix test
* fix tests
* remove duplicated test
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix tests
* deprecation
* deprecations
* flake8
* fixes
* add mps to runif
* fix tests
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Apply suggestions from code review
Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* remove more
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* local import
* undo device stats :(
* fix import
* stupid typehints
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* more refactors :(
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* rename init_device to setup_device
* remove unused import
* make uppercase to differentiate from class
* trick test after moving import locally
* add base classes and registry
* reg
* registry
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* tests
* update to other branches
* resolve todo(lite)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* add very basic unit tests
* fix name assignment
* Update src/lightning_lite/strategies/parallel.py
Co-authored-by: Kaushik B <45285388+kaushikb11@users.noreply.github.com>
* remove deprecated property
* remove pre- and post backward for now
* protecting the registry utility function
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* remove unused import
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
Co-authored-by: Kaushik B <45285388+kaushikb11@users.noreply.github.com>
2022-09-14 13:15:21 +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.
|
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import torch
|
|
|
|
|
2023-01-04 15:57:18 +00:00
|
|
|
from lightning_fabric.strategies import SingleDeviceStrategy
|
Standalone Lite: Strategy base classes and registry (#14662)
* add accelerator implementations to lite
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix imports
* rename registry argument
* fix test
* fix tests
* remove duplicated test
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix tests
* deprecation
* deprecations
* flake8
* fixes
* add mps to runif
* fix tests
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Apply suggestions from code review
Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* remove more
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* local import
* undo device stats :(
* fix import
* stupid typehints
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* more refactors :(
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* rename init_device to setup_device
* remove unused import
* make uppercase to differentiate from class
* trick test after moving import locally
* add base classes and registry
* reg
* registry
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* tests
* update to other branches
* resolve todo(lite)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* add very basic unit tests
* fix name assignment
* Update src/lightning_lite/strategies/parallel.py
Co-authored-by: Kaushik B <45285388+kaushikb11@users.noreply.github.com>
* remove deprecated property
* remove pre- and post backward for now
* protecting the registry utility function
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* remove unused import
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
Co-authored-by: Kaushik B <45285388+kaushikb11@users.noreply.github.com>
2022-09-14 13:15:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2023-01-16 13:17:45 +00:00
|
|
|
assert strategy.all_reduce(tensor) == tensor
|
Standalone Lite: Strategy base classes and registry (#14662)
* add accelerator implementations to lite
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix imports
* rename registry argument
* fix test
* fix tests
* remove duplicated test
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix tests
* deprecation
* deprecations
* flake8
* fixes
* add mps to runif
* fix tests
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* Apply suggestions from code review
Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* remove more
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* local import
* undo device stats :(
* fix import
* stupid typehints
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* more refactors :(
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* fix
* rename init_device to setup_device
* remove unused import
* make uppercase to differentiate from class
* trick test after moving import locally
* add base classes and registry
* reg
* registry
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* tests
* update to other branches
* resolve todo(lite)
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* add very basic unit tests
* fix name assignment
* Update src/lightning_lite/strategies/parallel.py
Co-authored-by: Kaushik B <45285388+kaushikb11@users.noreply.github.com>
* remove deprecated property
* remove pre- and post backward for now
* protecting the registry utility function
* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
* remove unused import
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
Co-authored-by: Kaushik B <45285388+kaushikb11@users.noreply.github.com>
2022-09-14 13:15:21 +00:00
|
|
|
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)
|