2022-05-13 07:13:52 +00:00
|
|
|
from enum import Enum
|
2022-08-16 01:21:05 +00:00
|
|
|
from typing import List
|
2022-05-13 07:13:52 +00:00
|
|
|
|
2022-08-16 02:38:41 +00:00
|
|
|
__all__ = ['ReplicaSpec', 'ShardSpec']
|
2022-05-13 07:13:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DistPlacementPattern(Enum):
|
|
|
|
REPLICATE = 'r'
|
|
|
|
SHARD = 's'
|
|
|
|
|
|
|
|
|
|
|
|
class _DistSpec:
|
2022-08-16 02:38:41 +00:00
|
|
|
"""_DistSpec
|
2023-02-03 03:44:10 +00:00
|
|
|
|
2022-08-16 02:38:41 +00:00
|
|
|
A class indicates Distributed Specification.
|
|
|
|
The DistSpec is only works for the tensor parallel process groups.
|
|
|
|
Because the dist spec of data parallel process group can be automatically deduced.
|
|
|
|
This is an internal data structrue.
|
|
|
|
The API for users should be `ShardSpec` and `ReplicaSpec`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
dist_placement_pattern (DistPlacementPattern): the pattern describing how tensors are distributed among processes.
|
|
|
|
The dist_placement_pattern is picked from a limited set, now including two patterns: replicate and shard.
|
|
|
|
process_group (Optional[ProcessGroup], optional): the process group contains processes. Defaults to None.
|
|
|
|
"""
|
2022-05-13 07:13:52 +00:00
|
|
|
|
2022-07-06 08:15:16 +00:00
|
|
|
def __init__(self, dist_placement_pattern: DistPlacementPattern, **meta_info):
|
2022-06-22 08:47:20 +00:00
|
|
|
|
2022-05-13 07:13:52 +00:00
|
|
|
self.placement = dist_placement_pattern
|
|
|
|
for k, v in meta_info.items():
|
|
|
|
setattr(self, k, v)
|
|
|
|
|
|
|
|
def __eq__(self, other: "_DistSpec") -> bool:
|
|
|
|
if dir(self) != dir(other):
|
|
|
|
return False
|
|
|
|
for attr in dir(self):
|
|
|
|
if not attr.startswith('__') and getattr(self, attr) != getattr(other, attr):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2022-05-26 10:15:42 +00:00
|
|
|
def __repr__(self) -> str:
|
2023-02-03 03:44:10 +00:00
|
|
|
attr_list = []
|
2022-05-26 10:15:42 +00:00
|
|
|
for attr in dir(self):
|
|
|
|
if not attr.startswith('__'):
|
2023-02-03 03:44:10 +00:00
|
|
|
attr_list.append(f'{attr}={str(getattr(self, attr))}')
|
|
|
|
attr_str = ", ".join(attr_list)
|
|
|
|
return "DistSpec(" + attr_str + ")"
|
2022-05-13 07:13:52 +00:00
|
|
|
|
2022-06-22 08:47:20 +00:00
|
|
|
|
2022-08-16 02:38:41 +00:00
|
|
|
def ReplicaSpec() -> _DistSpec:
|
|
|
|
"""ReplicaSpec
|
|
|
|
|
|
|
|
A distributed specification represents the tensor is replicated among the tensor parallel process group.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
_DistSpec: an replicated dist spec instance.
|
|
|
|
"""
|
2022-07-06 08:15:16 +00:00
|
|
|
return _DistSpec(DistPlacementPattern.REPLICATE)
|
2022-05-13 07:13:52 +00:00
|
|
|
|
|
|
|
|
2022-08-16 02:38:41 +00:00
|
|
|
def ShardSpec(dims: List[int], num_partitions: List[int]) -> _DistSpec:
|
|
|
|
"""ShardSpec
|
|
|
|
|
|
|
|
A distributed specification represents the tensor is sharded among the tensor parallel process group.
|
|
|
|
|
|
|
|
Note:
|
|
|
|
Currently, only shard on one dimension is valid. In another word, dims should be of size 1.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
dims (List[int]): a list of dimensions
|
|
|
|
num_partitions (List[int]): a list of partition number of each dimensions.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
_DistSpec: an shard dist spec instance.
|
|
|
|
"""
|
2022-05-13 07:13:52 +00:00
|
|
|
assert isinstance(dims, list) and isinstance(num_partitions, list)
|
|
|
|
assert len(dims) == len(num_partitions)
|
2022-07-06 08:15:16 +00:00
|
|
|
return _DistSpec(DistPlacementPattern.SHARD, dims=tuple(dims), num_partitions=tuple(num_partitions))
|