-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsharding_groups_helper.py
56 lines (42 loc) · 1.99 KB
/
sharding_groups_helper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
from torch.distributed._shard.sharded_tensor import ShardedTensor
from torch.distributed._tensor import DTensor, Replicate, sharding_prop
from torch.distributed._tensor.device_mesh import init_device_mesh
import os
def create_device_mesh(
replica_groups_size=None,
sharding_group_size=None,
device=None,
):
"""wrapper function for creating a device mesh - partially for educational purposes, you can simply
use *init_device_mesh* api directly if desired.
Provides some automation:
1 - If no sharding group size, will default to gpus per node (same as HSDP default)
2 - If no replica group size, will auto-create replica group size based on sharding group size and available world GPUs.
Usage:
1 - generate device mesh
If your model fits on 4 GPUS, and you have 3 nodes of 8 GPUs, then:
Sharding_Group_Size = 4
Replica_Groups_Size = (24 total gpus, 4 per sharding group) = 6 Replica Groups
2 - pass into FSDP init via:
sharded_model = FSDP(model, device_mesh = device_mesh ... )
Note that this requires FSDP.ShardingStrategy to be in [HYBRID_SHARD, _HYBRID_SHARD_ZERO2]
"""
_rank = int(os.environ["RANK"])
_local_rank = int(os.environ["LOCAL_RANK"])
_world_size = int(os.environ["WORLD_SIZE"])
_local_world_size = int(os.environ["LOCAL_WORLD_SIZE"])
if device is None:
device = f"cuda" # :{_local_rank}"
if sharding_group_size is None:
sharding_group_size = _local_world_size
if replica_groups_size is None:
assert (
_world_size % sharding_group_size == 0
), f"unable to evenly shard {_world_size=} by {sharding_group_size}"
replica_groups_size = _world_size // sharding_group_size
device_mesh = None
device_mesh = init_device_mesh(device, (replica_groups_size, sharding_group_size))
assert device_mesh is not None, "unable to create valid device mesh"
return device_mesh