2023-05-22 07:02:17 +00:00
|
|
|
from dataclasses import dataclass
|
2023-06-19 05:53:17 +00:00
|
|
|
|
2023-06-30 01:58:08 +00:00
|
|
|
import torch.distributed as dist
|
|
|
|
from torch.distributed import ProcessGroup
|
|
|
|
|
2023-06-19 05:53:17 +00:00
|
|
|
from colossalai.cluster.dist_coordinator import DistCoordinator
|
2023-05-22 07:02:17 +00:00
|
|
|
|
2023-05-24 08:01:26 +00:00
|
|
|
__all__ = ['ShardConfig']
|
|
|
|
|
2023-05-22 07:02:17 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ShardConfig:
|
2023-06-09 06:36:54 +00:00
|
|
|
r"""
|
|
|
|
The config for sharding the huggingface model
|
|
|
|
|
|
|
|
Args:
|
2023-06-30 01:58:08 +00:00
|
|
|
tensor_parallel_process_group (int): The process group for tensor parallelism, defaults to None, which is the global process group.
|
2023-06-30 01:32:37 +00:00
|
|
|
enable_fused_normalization (bool): Whether to use fused layernorm, default is False
|
2023-05-22 07:02:17 +00:00
|
|
|
"""
|
2023-06-30 01:58:08 +00:00
|
|
|
tensor_parallel_process_group: int = None
|
2023-06-30 01:32:37 +00:00
|
|
|
enable_fused_normalization: bool = False
|
2023-06-30 02:56:29 +00:00
|
|
|
enable_all_optimization: bool = False
|
2023-06-19 05:53:17 +00:00
|
|
|
|
2023-06-19 02:47:16 +00:00
|
|
|
# TODO: add support for tensor parallel
|
|
|
|
# pipeline_parallel_size: int
|
|
|
|
# data_parallel_size: int
|
2023-06-19 05:53:17 +00:00
|
|
|
# tensor_parallel_mode: Literal['1d', '2d', '2.5d', '3d']
|
|
|
|
# inference_only: bool = True
|
|
|
|
# gather_output: bool = True
|
|
|
|
|
2023-06-30 02:56:29 +00:00
|
|
|
@property
|
|
|
|
def tensor_parallel_size(self):
|
|
|
|
return self._tensor_parallel_size
|
|
|
|
|
2023-06-19 05:53:17 +00:00
|
|
|
def __post_init__(self):
|
2023-06-30 01:58:08 +00:00
|
|
|
# get the parallel size
|
2023-06-30 02:56:29 +00:00
|
|
|
self._tensor_parallel_size = dist.get_world_size(self.tensor_parallel_process_group)
|
|
|
|
|
|
|
|
# turn on all optimization if all_optimization is set to True
|
|
|
|
if self.enable_all_optimization:
|
|
|
|
self._turn_on_all_optimization()
|
|
|
|
|
|
|
|
def _turn_on_all_optimization(self):
|
|
|
|
"""
|
|
|
|
Turn on all optimization.
|
|
|
|
"""
|
|
|
|
# you can add all the optimization flag here
|
|
|
|
self.fused_layernorm = True
|