2021-10-28 16:21:23 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
2022-02-14 03:15:02 +00:00
|
|
|
import torch.distributed as dist
|
|
|
|
from colossalai.global_variables import tensor_parallel_env as env
|
2021-10-28 16:21:23 +00:00
|
|
|
from colossalai.registry import DIST_GROUP_INITIALIZER
|
2022-02-14 03:15:02 +00:00
|
|
|
|
2021-10-28 16:21:23 +00:00
|
|
|
from ..parallel_mode import ParallelMode
|
2022-02-14 03:15:02 +00:00
|
|
|
from .process_group_initializer import ProcessGroupInitializer
|
2021-10-28 16:21:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@DIST_GROUP_INITIALIZER.register_module
|
|
|
|
class Initializer_1D(ProcessGroupInitializer):
|
2022-03-25 05:02:39 +00:00
|
|
|
"""A ProcessGroupInitializer for 1d tensor parallelism.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
rank (int): The rank of current process.
|
|
|
|
world_size (int): Size of whole communication world.
|
|
|
|
config (Config): Running configuration.
|
|
|
|
data_parallel_size (int): Size of data parallel.
|
|
|
|
pipeline_parallel_size (int): Size of pipeline parallel.
|
|
|
|
tensor_parallel_size (int): Size of tensor parallel.
|
|
|
|
"""
|
2021-10-28 16:21:23 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.num_group = self.world_size // self.tensor_parallel_size
|
|
|
|
|
|
|
|
def init_dist_group(self):
|
2022-01-21 02:44:30 +00:00
|
|
|
"""Initialize 1D tensor parallel groups, and assign local_ranks and groups to each gpu.
|
2022-03-25 05:02:39 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
Tuple (local_rank, group_world_size, process_group, ranks_in_group, mode):
|
|
|
|
1D tensor parallelism's information in a tuple.
|
2022-01-21 02:44:30 +00:00
|
|
|
"""
|
2021-10-28 16:21:23 +00:00
|
|
|
local_rank = None
|
|
|
|
ranks_in_group = None
|
|
|
|
process_group = None
|
2022-04-01 02:15:52 +00:00
|
|
|
cpu_group = None
|
2021-10-28 16:21:23 +00:00
|
|
|
group_world_size = None
|
|
|
|
mode = ParallelMode.PARALLEL_1D
|
2022-02-14 03:15:02 +00:00
|
|
|
env.parallel_input_1d = False
|
2021-10-28 16:21:23 +00:00
|
|
|
|
|
|
|
for i in range(self.num_group):
|
|
|
|
ranks = [i * self.tensor_parallel_size + j for j in range(self.tensor_parallel_size)]
|
|
|
|
group = dist.new_group(ranks)
|
2022-04-01 02:15:52 +00:00
|
|
|
group_cpu = dist.new_group(ranks, backend='gloo') if dist.get_backend() != 'gloo' else group
|
2021-10-28 16:21:23 +00:00
|
|
|
|
|
|
|
if self.rank in ranks:
|
|
|
|
local_rank = ranks.index(self.rank)
|
|
|
|
group_world_size = len(ranks)
|
|
|
|
process_group = group
|
2022-04-01 02:15:52 +00:00
|
|
|
cpu_group = group_cpu
|
2021-10-28 16:21:23 +00:00
|
|
|
ranks_in_group = ranks
|
|
|
|
|
2022-04-01 02:15:52 +00:00
|
|
|
return local_rank, group_world_size, process_group, cpu_group, ranks_in_group, mode
|