2022-10-19 04:53:06 +00:00
|
|
|
|
import operator
|
2022-08-12 06:02:32 +00:00
|
|
|
|
from copy import deepcopy
|
|
|
|
|
from functools import reduce
|
2022-10-19 04:53:06 +00:00
|
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
|
|
|
|
|
from colossalai.device.device_mesh import DeviceMesh
|
2022-08-12 06:02:32 +00:00
|
|
|
|
|
2022-11-08 09:03:50 +00:00
|
|
|
|
from .utils import merge_same_dim_mesh_list
|
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
|
__all__ = ["_DimSpec", "ShardingException", "ShardingSpec"]
|
2022-10-12 03:16:18 +00:00
|
|
|
|
|
2022-08-12 06:02:32 +00:00
|
|
|
|
ALLGATHER_COST = 20
|
|
|
|
|
SHARD_COST = 5
|
|
|
|
|
STEP_PENALTY = 6
|
2023-09-19 06:20:26 +00:00
|
|
|
|
NAN = "nan"
|
2022-08-08 03:15:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _DimSpec:
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2023-04-26 03:38:43 +00:00
|
|
|
|
Sharding spec for single dimension of the sharded tensor describe the sharding dimension of
|
2022-08-08 03:15:57 +00:00
|
|
|
|
logical device mesh and give a method to compute the difference between them.
|
|
|
|
|
This class is used internally in ShardingSpec.
|
|
|
|
|
|
|
|
|
|
Argument:
|
2022-10-27 02:42:54 +00:00
|
|
|
|
shard_list(List[int]): if shard_list is None, the dim spec will be 'R' type.
|
2022-08-08 03:15:57 +00:00
|
|
|
|
Otherwise, the element in shard_list means the data will be sharded in that dimension.
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-08 03:15:57 +00:00
|
|
|
|
|
|
|
|
|
def __init__(self, shard_list):
|
2022-08-10 03:29:17 +00:00
|
|
|
|
self.is_replica = len(shard_list) == 0
|
2022-08-08 03:15:57 +00:00
|
|
|
|
self.shard_list = shard_list
|
2022-08-12 06:02:32 +00:00
|
|
|
|
self.build_difference_2d_dict()
|
2022-08-08 03:15:57 +00:00
|
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2022-08-12 03:33:09 +00:00
|
|
|
|
return str(self) == str(other)
|
2022-08-08 03:15:57 +00:00
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
if self.is_replica:
|
2023-09-19 06:20:26 +00:00
|
|
|
|
return "R"
|
|
|
|
|
target = "S"
|
2022-08-08 03:15:57 +00:00
|
|
|
|
for dim in self.shard_list:
|
|
|
|
|
target += str(dim)
|
|
|
|
|
return target
|
|
|
|
|
|
2022-08-12 06:02:32 +00:00
|
|
|
|
def _convert_str_to_shard_list(self, str_spec):
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2023-06-06 06:07:36 +00:00
|
|
|
|
Convert str_spec into shard_list.
|
2022-08-12 06:02:32 +00:00
|
|
|
|
|
|
|
|
|
Argument:
|
|
|
|
|
str_spec(str): dim spec in str type.
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-12 06:02:32 +00:00
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
|
if str_spec == "R":
|
2022-08-12 06:02:32 +00:00
|
|
|
|
return []
|
2023-09-19 06:20:26 +00:00
|
|
|
|
if str_spec == "S0":
|
2022-08-12 06:02:32 +00:00
|
|
|
|
return [0]
|
2023-09-19 06:20:26 +00:00
|
|
|
|
if str_spec == "S1":
|
2022-08-12 06:02:32 +00:00
|
|
|
|
return [1]
|
2023-09-19 06:20:26 +00:00
|
|
|
|
if str_spec == "S01":
|
2022-08-12 06:02:32 +00:00
|
|
|
|
return [0, 1]
|
|
|
|
|
|
|
|
|
|
def build_difference_2d_dict(self):
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2023-06-06 06:07:36 +00:00
|
|
|
|
Build a difference mapping for 2D device mesh case. It will be used to
|
2022-08-12 06:02:32 +00:00
|
|
|
|
compute the difference between DimSpec pairs.
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-12 06:02:32 +00:00
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
|
source_spec_list = ["R", "S0", "S1", "S01"]
|
|
|
|
|
target_spec_list = ["R", "S0", "S1", "S01"]
|
2022-08-12 06:02:32 +00:00
|
|
|
|
difference_dict = {}
|
|
|
|
|
for source_spec in source_spec_list:
|
|
|
|
|
for target_spec in target_spec_list:
|
|
|
|
|
spec_pair = (deepcopy(source_spec), deepcopy(target_spec))
|
|
|
|
|
source_shard_list = self._convert_str_to_shard_list(source_spec)
|
|
|
|
|
target_shard_list = self._convert_str_to_shard_list(target_spec)
|
|
|
|
|
|
|
|
|
|
# source same as target
|
|
|
|
|
if source_shard_list == target_shard_list:
|
|
|
|
|
difference = 0
|
|
|
|
|
|
|
|
|
|
# all_gather(source) -> target
|
2023-09-19 06:20:26 +00:00
|
|
|
|
elif (
|
|
|
|
|
len(source_shard_list) == len(target_shard_list) + 1 and source_shard_list[:-1] == target_shard_list
|
|
|
|
|
):
|
2022-08-12 06:02:32 +00:00
|
|
|
|
difference = ALLGATHER_COST
|
|
|
|
|
|
|
|
|
|
# shard(source) -> target
|
2023-09-19 06:20:26 +00:00
|
|
|
|
elif (
|
|
|
|
|
len(source_shard_list) == len(target_shard_list) - 1
|
|
|
|
|
and source_shard_list == target_shard_list[:-1]
|
|
|
|
|
and target_shard_list[-1] not in source_shard_list
|
|
|
|
|
):
|
2022-08-12 06:02:32 +00:00
|
|
|
|
difference = SHARD_COST
|
|
|
|
|
|
|
|
|
|
# S1 -> S0 or S0 -> S1
|
|
|
|
|
elif len(source_shard_list) == len(target_shard_list):
|
|
|
|
|
# source -> R -> target
|
|
|
|
|
difference = ALLGATHER_COST + STEP_PENALTY + SHARD_COST
|
|
|
|
|
|
|
|
|
|
# R -> S01
|
|
|
|
|
elif len(source_shard_list) == len(target_shard_list) - 2:
|
|
|
|
|
difference = SHARD_COST + STEP_PENALTY + SHARD_COST
|
|
|
|
|
|
|
|
|
|
# S01 -> R
|
|
|
|
|
elif len(source_shard_list) == len(target_shard_list) + 2:
|
|
|
|
|
difference = ALLGATHER_COST + STEP_PENALTY + ALLGATHER_COST
|
|
|
|
|
|
|
|
|
|
# S1 -> S01
|
|
|
|
|
elif len(source_shard_list) == len(target_shard_list) - 1:
|
|
|
|
|
difference = ALLGATHER_COST + STEP_PENALTY + SHARD_COST + STEP_PENALTY + SHARD_COST
|
|
|
|
|
|
|
|
|
|
# S01 -> S1
|
|
|
|
|
elif len(source_shard_list) == len(target_shard_list) + 1:
|
|
|
|
|
difference = ALLGATHER_COST + STEP_PENALTY + ALLGATHER_COST + STEP_PENALTY + SHARD_COST
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
difference = NAN
|
|
|
|
|
difference_dict[spec_pair] = difference
|
|
|
|
|
|
|
|
|
|
self.difference_dict = difference_dict
|
|
|
|
|
|
2022-08-08 03:15:57 +00:00
|
|
|
|
def difference(self, other):
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-12 06:02:32 +00:00
|
|
|
|
The difference between two _DimSpec.
|
|
|
|
|
|
|
|
|
|
Argument:
|
|
|
|
|
other(_DimSpec): the dim spec to compare with.
|
|
|
|
|
|
|
|
|
|
Return:
|
|
|
|
|
difference(int): the difference between two _DimSpec.
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
dim_spec = _DimSpec([0])
|
|
|
|
|
other_dim_spec = _DimSpec([0, 1])
|
|
|
|
|
print(dim_spec.difference(other_dim_spec))
|
|
|
|
|
|
|
|
|
|
Output:
|
|
|
|
|
5
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-12 06:02:32 +00:00
|
|
|
|
difference = self.difference_dict[(str(self), str(other))]
|
|
|
|
|
return difference
|
2022-08-08 03:15:57 +00:00
|
|
|
|
|
|
|
|
|
|
2022-10-19 04:53:06 +00:00
|
|
|
|
class ShardingSpecException(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ShardingOutOfIndexError(ShardingSpecException):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DuplicatedShardingDimensionError(ShardingSpecException):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ShardingNotDivisibleError(ShardingSpecException):
|
2022-10-12 03:16:18 +00:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2022-08-08 03:15:57 +00:00
|
|
|
|
class ShardingSpec:
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-08 03:15:57 +00:00
|
|
|
|
Sharding spec for a tensor, it contains info of the logical device mesh this tensor belong
|
2022-10-27 02:42:54 +00:00
|
|
|
|
to, the entire shape of the tensor before sharded, and the sharding sequence looks like
|
2022-08-08 03:15:57 +00:00
|
|
|
|
[R, R, S0, S1].
|
2022-10-27 02:42:54 +00:00
|
|
|
|
|
2022-08-08 03:15:57 +00:00
|
|
|
|
Argument:
|
|
|
|
|
device_mesh(DeviceMesh): A logical view of a physical mesh.
|
|
|
|
|
entire_shape(torch.Size): The entire shape of tensor before sharded.
|
2022-08-12 06:02:32 +00:00
|
|
|
|
dim_partition_dict(Dict[int, List[int]], optional): The key is the dimension of tensor to be sharded,
|
2023-06-06 06:07:36 +00:00
|
|
|
|
and the value of the key describe which logical axis will be sharded in that dimension.
|
2022-08-12 06:02:32 +00:00
|
|
|
|
sharding_sequence(List[_DimSpec], optional): A straight view of ShardingSpec looks like [R, R, S0, S1].
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-08 03:15:57 +00:00
|
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
|
def __init__(
|
|
|
|
|
self, device_mesh: DeviceMesh, entire_shape: torch.Size, dim_partition_dict=None, sharding_sequence=None
|
|
|
|
|
):
|
2022-08-08 03:15:57 +00:00
|
|
|
|
self.device_mesh = device_mesh
|
2022-11-01 07:14:53 +00:00
|
|
|
|
|
|
|
|
|
if isinstance(entire_shape, (list, tuple)):
|
|
|
|
|
entire_shape = torch.Size(entire_shape)
|
2022-08-08 03:15:57 +00:00
|
|
|
|
self.entire_shape = entire_shape
|
|
|
|
|
self.dim_partition_dict = dim_partition_dict
|
2022-08-10 03:29:17 +00:00
|
|
|
|
self.sharding_sequence = sharding_sequence
|
|
|
|
|
if self.sharding_sequence is None:
|
2023-09-19 06:20:26 +00:00
|
|
|
|
assert (
|
|
|
|
|
self.dim_partition_dict is not None
|
|
|
|
|
), f"dim_partition_dict should not be None, if sharding_sequence is NoneType object."
|
|
|
|
|
self.dim_partition_dict = merge_same_dim_mesh_list(
|
|
|
|
|
dim_size=len(entire_shape), dim_partition_dict=self.dim_partition_dict
|
|
|
|
|
)
|
2022-08-10 03:29:17 +00:00
|
|
|
|
self.convert_dict_to_shard_sequence()
|
|
|
|
|
elif self.dim_partition_dict is None:
|
2023-09-19 06:20:26 +00:00
|
|
|
|
assert (
|
|
|
|
|
self.sharding_sequence is not None
|
|
|
|
|
), f"sharding_sequence should not be None, if dim_partition_dict is NoneType object."
|
2022-08-10 03:29:17 +00:00
|
|
|
|
self.convert_shard_sequence_to_dict()
|
2022-08-08 03:15:57 +00:00
|
|
|
|
self._sanity_check()
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
res_list = ["DistSpec:"]
|
|
|
|
|
res_list.append(f"\n\tshard_sequence: " + ",".join(str(dimspec) for dimspec in self.sharding_sequence))
|
2023-06-26 07:50:07 +00:00
|
|
|
|
res_list.append(f"\n\tdevice_mesh_shape: {self.device_mesh.shape}")
|
2023-09-19 06:20:26 +00:00
|
|
|
|
return " ".join(res_list)
|
2022-08-08 03:15:57 +00:00
|
|
|
|
|
|
|
|
|
def _sanity_check(self):
|
2022-10-19 04:53:06 +00:00
|
|
|
|
# make sure all axes in logical device mesh only be used once
|
|
|
|
|
dim_check_list = list(range(self.device_mesh.logical_mesh_id.dim()))
|
2022-08-08 03:15:57 +00:00
|
|
|
|
for dim, shard_list in self.dim_partition_dict.items():
|
|
|
|
|
for element in shard_list:
|
|
|
|
|
if element in dim_check_list:
|
|
|
|
|
dim_check_list.remove(element)
|
|
|
|
|
else:
|
2022-10-19 04:53:06 +00:00
|
|
|
|
raise DuplicatedShardingDimensionError(
|
2023-09-19 06:20:26 +00:00
|
|
|
|
f"find an invalid sharding axis {element} in dim_partition_dict in tensor dimension {dim}."
|
|
|
|
|
)
|
2022-08-08 03:15:57 +00:00
|
|
|
|
|
2022-10-19 04:53:06 +00:00
|
|
|
|
# make sure that the dimension is not out of index
|
|
|
|
|
for dim in self.dim_partition_dict.keys():
|
|
|
|
|
if dim >= len(self.entire_shape):
|
|
|
|
|
raise ShardingOutOfIndexError(
|
|
|
|
|
f"The dim_partition_dict specifies to shard dimension {dim} but the entire_shape only has {len(self.entire_shape)} dimensions"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# make sure that the sharding for a dimension is divisible by the number of devices
|
|
|
|
|
for dim, shard_list in self.dim_partition_dict.items():
|
|
|
|
|
tensor_dim_size = self.entire_shape[dim]
|
|
|
|
|
num_devices = 1
|
|
|
|
|
|
|
|
|
|
for element in shard_list:
|
2023-06-26 07:50:07 +00:00
|
|
|
|
num_devices *= self.device_mesh.shape[element]
|
2022-10-19 04:53:06 +00:00
|
|
|
|
|
|
|
|
|
if tensor_dim_size % num_devices != 0:
|
|
|
|
|
raise ShardingNotDivisibleError(
|
2023-09-19 06:20:26 +00:00
|
|
|
|
f"The size of dimension at index {dim} is {tensor_dim_size}, it cannot be sharded over {num_devices} devices."
|
2022-10-19 04:53:06 +00:00
|
|
|
|
)
|
|
|
|
|
|
2022-08-08 03:15:57 +00:00
|
|
|
|
def convert_dict_to_shard_sequence(self):
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-12 06:02:32 +00:00
|
|
|
|
Convert dim_partition_dict into list of _DimSpec, and assign it to sharding_sequence.
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-10 03:29:17 +00:00
|
|
|
|
sharding_sequence = [_DimSpec([])] * len(self.entire_shape)
|
2022-08-08 03:15:57 +00:00
|
|
|
|
for dim, shard_list in self.dim_partition_dict.items():
|
|
|
|
|
sharding_sequence[dim] = _DimSpec(shard_list)
|
2022-08-10 03:29:17 +00:00
|
|
|
|
self.sharding_sequence = sharding_sequence
|
|
|
|
|
|
|
|
|
|
def convert_shard_sequence_to_dict(self):
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-12 06:02:32 +00:00
|
|
|
|
Convert sharding_sequence into dim_partition_dict.
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-10 03:29:17 +00:00
|
|
|
|
new_dim_partition_dict = {}
|
|
|
|
|
for index, dim_spec in enumerate(self.sharding_sequence):
|
|
|
|
|
if not dim_spec.is_replica:
|
|
|
|
|
if index not in new_dim_partition_dict:
|
|
|
|
|
new_dim_partition_dict[index] = []
|
2022-08-19 06:57:23 +00:00
|
|
|
|
new_dim_partition_dict[index].extend(dim_spec.shard_list)
|
2022-08-10 03:29:17 +00:00
|
|
|
|
self.dim_partition_dict = new_dim_partition_dict
|
2022-08-08 03:15:57 +00:00
|
|
|
|
|
|
|
|
|
def sharding_sequence_difference(self, other):
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-12 06:02:32 +00:00
|
|
|
|
This function is a naive version of difference computation. It just simply accumulates difference every dimension between the
|
|
|
|
|
pair of sharding sequence.
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
dim_partition_dict = {0: [0, 1]}
|
|
|
|
|
# DistSpec:
|
|
|
|
|
# shard_sequence: S01,R,R
|
|
|
|
|
# device_mesh_shape: (4, 4)
|
|
|
|
|
sharding_spec = ShardingSpec(device_mesh, entire_shape, dim_partition_dict)
|
|
|
|
|
dim_partition_dict_to_compare = {0: [0], 1: [1]}
|
|
|
|
|
# DistSpec:
|
|
|
|
|
# shard_sequence: S0,S1,R
|
|
|
|
|
# device_mesh_shape: (4, 4)
|
|
|
|
|
sharding_spec_to_compare = ShardingSpec(device_mesh, entire_shape, dim_partition_dict_to_compare)
|
|
|
|
|
print(sharding_spec.sharding_sequence_difference(sharding_spec_to_compare))
|
2022-10-27 02:42:54 +00:00
|
|
|
|
|
2022-08-12 06:02:32 +00:00
|
|
|
|
Output:
|
|
|
|
|
25
|
2022-10-27 02:42:54 +00:00
|
|
|
|
|
2022-08-12 06:02:32 +00:00
|
|
|
|
Argument:
|
|
|
|
|
other(ShardingSpec): The ShardingSpec to compared with.
|
|
|
|
|
|
|
|
|
|
Return:
|
|
|
|
|
difference(int): Difference between two ShardingSpec.
|
2023-09-19 06:20:26 +00:00
|
|
|
|
"""
|
2022-08-12 06:02:32 +00:00
|
|
|
|
assert len(self.sharding_sequence) == len(
|
2023-09-19 06:20:26 +00:00
|
|
|
|
other.sharding_sequence
|
|
|
|
|
), f"Cannot compare difference for two sharding specs with different length."
|
2022-08-12 06:02:32 +00:00
|
|
|
|
difference = 0
|
|
|
|
|
for orig_dim_spec, other_dim_spec in zip(self.sharding_sequence, other.sharding_sequence):
|
|
|
|
|
difference += orig_dim_spec.difference(other_dim_spec)
|
|
|
|
|
return difference
|
|
|
|
|
|
|
|
|
|
def get_sharded_shape_per_device(self):
|
|
|
|
|
sharded_shape = list(self.entire_shape)
|
|
|
|
|
for dim, shard_list in self.dim_partition_dict.items():
|
2023-06-26 07:50:07 +00:00
|
|
|
|
mesh_list = [self.device_mesh.shape[mesh_dim] for mesh_dim in shard_list]
|
2022-08-12 06:02:32 +00:00
|
|
|
|
shard_partitions = reduce(operator.mul, mesh_list, 1)
|
2023-09-19 06:20:26 +00:00
|
|
|
|
assert (
|
|
|
|
|
sharded_shape[dim] % shard_partitions == 0
|
|
|
|
|
), f"Cannot shard dimension {dim} into {shard_partitions} partitions."
|
2022-08-12 06:02:32 +00:00
|
|
|
|
sharded_shape[dim] //= shard_partitions
|
|
|
|
|
return torch.Size(sharded_shape)
|