-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ManagedDeviceMesh to integrate DeviceMesh with TorchFT
Summary: ManagedDeviceMesh allow users to manipulate DeviceMesh with TorchFT ManagedProcessGroup. ghstack-source-id: 888be370d2f8e81fbe0a9a29a9a99a4e6404cab8 Pull Request resolved: #56
- Loading branch information
Showing
3 changed files
with
335 additions
and
13 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,70 @@ | ||
# 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 concurrent.futures import ThreadPoolExecutor | ||
from typing import Any, Dict, Tuple | ||
from unittest import skipUnless, TestCase | ||
from unittest.mock import Mock | ||
|
||
import torch | ||
import torch.distributed as dist | ||
from torch import nn | ||
from torch._C._distributed_c10d import ( | ||
_resolve_process_group, | ||
AllgatherOptions, | ||
AllreduceOptions, | ||
BroadcastOptions, | ||
ReduceOp, | ||
) | ||
from torch.distributed import ( | ||
_functional_collectives, | ||
get_world_size, | ||
ReduceOp, | ||
TCPStore, | ||
Work, | ||
) | ||
from torch.distributed._composable.fsdp import fully_shard | ||
from torch.distributed.device_mesh import init_device_mesh | ||
from torch.testing._internal.common_distributed import MultiProcessTestCase | ||
|
||
from torchft.manager import Manager | ||
from torchft.process_group import ft_init_device_mesh, ManagedProcessGroup | ||
|
||
|
||
class FSDPTest(MultiProcessTestCase): | ||
@property | ||
def world_size(self): | ||
return 4 | ||
|
||
def setUp(self): | ||
super().setUp() | ||
os.environ["TORCH_NCCL_DESYNC_DEBUG"] = "0" | ||
self._spawn_processes() | ||
|
||
def test_fsdp(self) -> None: | ||
group_size = self.world_size // 2 | ||
group = self.rank // group_size | ||
group_rank = self.rank % group_size | ||
|
||
os.environ["MASTER_ADDR"] = "127.0.0.1" | ||
os.environ["MASTER_PORT"] = str(12346 + group) | ||
os.environ["RANK"] = str(group_rank) | ||
os.environ["WORLD_SIZE"] = str(group_size) | ||
|
||
manager = Mock(spec=Manager) | ||
device_mesh = ft_init_device_mesh( | ||
device_type="cuda", | ||
mesh_shape=(2, 2), | ||
mesh_dim_names=("dp_replicate", "dp_shard"), | ||
replicate_dim=0, | ||
manager=manager, | ||
) | ||
manager.num_participants.return_value = 1 | ||
model = nn.Linear(128, 128).cuda() | ||
batch = torch.randn(4, 128).cuda() | ||
shard_model = fully_shard(model, mesh=device_mesh) | ||
shard_model(batch).mean().backward() |
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
Oops, something went wrong.