-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
Strategy._move_optimizer_states
as utility functions (#11758)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: thomas chaton <thomas@grid.ai>
- Loading branch information
1 parent
d613719
commit cf64f34
Showing
9 changed files
with
78 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,6 +288,7 @@ Utilities API | |
finite_checks | ||
memory | ||
model_summary | ||
optimizer | ||
parsing | ||
rank_zero | ||
seed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 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 typing import Iterable | ||
|
||
import torch | ||
from torch.optim import Optimizer | ||
|
||
from pytorch_lightning.utilities.apply_func import apply_to_collection, move_data_to_device | ||
from pytorch_lightning.utilities.types import _DEVICE | ||
|
||
|
||
def optimizers_to_device(optimizers: Iterable[Optimizer], device: _DEVICE) -> None: | ||
"""Moves optimizer states for a sequence of optimizers to the device.""" | ||
for opt in optimizers: | ||
optimizer_to_device(opt, device) | ||
|
||
|
||
def optimizer_to_device(optimizer: Optimizer, device: _DEVICE) -> None: | ||
"""Moves the state of a single optimizer to the device.""" | ||
for p, v in optimizer.state.items(): | ||
optimizer.state[p] = apply_to_collection(v, torch.Tensor, move_data_to_device, device) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import collections | ||
|
||
import torch | ||
|
||
from pytorch_lightning.utilities.optimizer import optimizer_to_device | ||
|
||
|
||
def test_optimizer_to_device(): | ||
class TestOptimizer(torch.optim.SGD): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.state["dummy"] = torch.tensor(0) | ||
|
||
layer = torch.nn.Linear(32, 2) | ||
opt = TestOptimizer(layer.parameters(), lr=0.1) | ||
optimizer_to_device(opt, "cpu") | ||
if torch.cuda.is_available(): | ||
optimizer_to_device(opt, "cuda") | ||
assert_opt_parameters_on_device(opt, "cuda") | ||
|
||
|
||
def assert_opt_parameters_on_device(opt, device: str): | ||
for param in opt.state.values(): | ||
# Not sure there are any global tensors in the state dict | ||
if isinstance(param, torch.Tensor): | ||
assert param.data.device.type == device | ||
elif isinstance(param, collections.Mapping): | ||
for subparam in param.values(): | ||
if isinstance(subparam, torch.Tensor): | ||
assert param.data.device.type == device |