-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reviewed By: JKSenthil Differential Revision: D54447983 fbshipit-source-id: b458639aab4bdf2825865304eda6a06d70600393
- Loading branch information
1 parent
e0184bf
commit 6bd9dc6
Showing
3 changed files
with
169 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import os | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
import torch | ||
import torch.distributed as dist | ||
from torch.distributed.fsdp import FullyShardedDataParallel as FSDP, StateDictType | ||
from torchsnapshot import Snapshot | ||
from torchsnapshot.test_utils import check_state_dict_eq, run_with_pet | ||
|
||
|
||
def _create_fsdp_model( | ||
seed: int, | ||
device: torch.device, | ||
) -> torch.nn.Module: | ||
torch.manual_seed(seed) | ||
model = torch.nn.Linear(32, 32) | ||
|
||
fsdp_model = FSDP( | ||
module=model, | ||
device_id=device, | ||
) | ||
FSDP.set_state_dict_type(fsdp_model, StateDictType.SHARDED_STATE_DICT) | ||
return fsdp_model | ||
|
||
|
||
@pytest.mark.skipif( | ||
bool(not torch.cuda.is_available()), reason="The test requires GPUs to run." | ||
) | ||
@pytest.mark.skipif( | ||
bool(torch.cuda.device_count() < 2), reason="At least two GPUs are required." | ||
) | ||
@run_with_pet(nproc=2) | ||
def test_model_and_optim_fsdp(tmp_path: Path) -> None: | ||
dist.init_process_group(backend="nccl") | ||
local_rank = int(os.environ["LOCAL_RANK"]) | ||
device = torch.device(f"cuda:{local_rank}") | ||
torch.cuda.set_device(device) | ||
|
||
fsdp_model = _create_fsdp_model(17, device) | ||
|
||
snapshot = Snapshot.take( | ||
path=str(tmp_path), | ||
app_state={"fsdp_model": fsdp_model}, | ||
) | ||
state_dict_from_method = snapshot.get_state_dict_for_key("fsdp_model") | ||
FSDP.set_state_dict_type(fsdp_model, StateDictType.FULL_STATE_DICT) | ||
|
||
full_state_dict = fsdp_model.state_dict() | ||
for k, v in full_state_dict.items(): | ||
full_state_dict[k] = v.cpu() | ||
|
||
assert check_state_dict_eq(full_state_dict, state_dict_from_method) |
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,69 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import tempfile | ||
import unittest | ||
from typing import cast, Dict | ||
|
||
import torch | ||
import torchsnapshot | ||
from torchsnapshot import Stateful | ||
|
||
|
||
class MyModule(torch.nn.Module): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
self.foo = torch.nn.Parameter(torch.randn(20, 20)) | ||
|
||
|
||
class MyStateful(Stateful): | ||
def __init__(self) -> None: | ||
self.foo = 1 | ||
self.bar = "bar" | ||
|
||
def state_dict(self) -> Dict[str, object]: | ||
return {"foo": self.foo, "bar": self.bar} | ||
|
||
def load_state_dict(self, state_dict: Dict[str, object]) -> None: | ||
self.foo = cast(int, state_dict["foo"]) | ||
self.bar = cast(str, state_dict["bar"]) | ||
|
||
|
||
class StateDictTest(unittest.TestCase): | ||
def test_get_state_dict(self) -> None: | ||
my_module = MyModule() | ||
with tempfile.TemporaryDirectory() as path: | ||
torchsnapshot.Snapshot.take( | ||
path=path, | ||
app_state={"my_module": my_module}, | ||
) | ||
snapshot = torchsnapshot.Snapshot(path) | ||
state_dict = snapshot.get_state_dict_for_key("my_module") | ||
self.assertTrue(torch.allclose(state_dict["foo"], my_module.foo)) | ||
|
||
def test_get_state_dict_with_invalid_key(self) -> None: | ||
my_module = MyModule() | ||
with tempfile.TemporaryDirectory() as path: | ||
torchsnapshot.Snapshot.take( | ||
path=path, | ||
app_state={"my_module": my_module}, | ||
) | ||
snapshot = torchsnapshot.Snapshot(path) | ||
with self.assertRaisesRegex( | ||
AssertionError, "is absent in both manifest and flattened" | ||
): | ||
snapshot.get_state_dict_for_key("invalid_key") | ||
|
||
def test_generic_stateful(self) -> None: | ||
my_stateful = MyStateful() | ||
my_stateful.foo = 2 | ||
my_stateful.bar = "baz" | ||
with tempfile.TemporaryDirectory() as path: | ||
snapshot = torchsnapshot.Snapshot(path) | ||
snapshot.take(path, app_state={"my_stateful": my_stateful}) | ||
state_dict = snapshot.get_state_dict_for_key("my_stateful") | ||
self.assertDictEqual(state_dict, my_stateful.state_dict()) |
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