Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate state when retrieving sharded state dict in Lite #10746

Merged
merged 7 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed the `{validation,test}_step` outputs getting moved to CPU with `Trainer(move_metrics_to_cpu=True)` ([#10631](https://github.com/PyTorchLightning/pytorch-lightning/pull/10631))


- Fixed a consolidation error in Lite when attempting to save the state dict of a sharded optimizer ([#10746](https://github.com/PyTorchLightning/pytorch-lightning/pull/10746))



## [1.5.2] - 2021-11-16

Expand Down
7 changes: 5 additions & 2 deletions pytorch_lightning/lite/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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 Any, Callable, Generator, Iterator, Optional, Union
from typing import Any, Callable, Dict, Generator, Iterator, Optional, Union

import torch
from torch import nn as nn
Expand Down Expand Up @@ -42,7 +42,7 @@ def __init__(self, optimizer: Optimizer, accelerator: Accelerator) -> None:
"""
# `__del__` is skipped in case the optimizer has implemented custom destructor logic which we would
# not want to call on destruction of the `_LiteOptimizer
self.__dict__ = {k: v for k, v in optimizer.__dict__.items() if k not in ("step", "__del__")}
self.__dict__ = {k: v for k, v in optimizer.__dict__.items() if k not in ("state_dict", "step", "__del__")}
self.__class__ = type("Lite" + optimizer.__class__.__name__, (self.__class__, optimizer.__class__), {})
self._optimizer = optimizer
self._accelerator = accelerator
Expand All @@ -51,6 +51,9 @@ def __init__(self, optimizer: Optimizer, accelerator: Accelerator) -> None:
def optimizer(self) -> Optimizer:
return self._optimizer

def state_dict(self) -> Dict[str, Tensor]:
return self._accelerator.optimizer_state(self.optimizer)

def step(self, closure: Optional[Callable] = None) -> None:
closure = closure or _do_nothing_closure
self._accelerator.optimizer_step(
Expand Down
9 changes: 9 additions & 0 deletions tests/lite/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ def test_lite_optimizer_wraps():
assert isinstance(lite_optimizer, optimizer_cls)


def test_lite_optimizer_state_dict():
"""Test that the LiteOptimizer calls into the accelerator/strategy to collect the state."""
optimizer = Mock()
accelerator = Mock()
lite_optimizer = _LiteOptimizer(optimizer=optimizer, accelerator=accelerator)
lite_optimizer.state_dict()
accelerator.optimizer_state.assert_called_with(optimizer)


def test_lite_optimizer_steps():
"""Test that the LiteOptimizer forwards the step() and zero_grad() calls to the wrapped optimizer."""
optimizer = Mock()
Expand Down