mirror of https://github.com/hpcaitech/ColossalAI
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.5 KiB
42 lines
1.5 KiB
3 years ago
|
#!/usr/bin/env python
|
||
|
# -*- encoding: utf-8 -*-
|
||
|
|
||
|
import torch.distributed as dist
|
||
|
|
||
|
from colossalai.registry import DIST_GROUP_INITIALIZER
|
||
|
from .process_group_initializer import ProcessGroupInitializer
|
||
|
from ..parallel_mode import ParallelMode
|
||
|
|
||
|
|
||
|
@DIST_GROUP_INITIALIZER.register_module
|
||
|
class Initializer_Tensor(ProcessGroupInitializer):
|
||
|
'''A ProcessGroupInitializer for tensor parallelism.
|
||
|
'''
|
||
|
def __init__(self, *args, **kwargs):
|
||
|
super().__init__(*args, **kwargs)
|
||
|
self.num_tensor_parallel_group = self.world_size // self.tensor_parallel_size
|
||
|
|
||
|
def init_dist_group(self):
|
||
|
'''Initialize tensor parallel groups, and assign local_ranks and groups to each gpu.
|
||
|
|
||
|
:return: tensor parallelism's information
|
||
|
:rtype: tuple(local_rank, group_world_size, process_group, ranks_in_group, mode)
|
||
|
'''
|
||
|
local_rank = None
|
||
|
ranks_in_group = None
|
||
|
process_group = None
|
||
|
group_world_size = None
|
||
|
mode = ParallelMode.TENSOR
|
||
|
|
||
|
for i in range(self.num_tensor_parallel_group):
|
||
|
ranks = [i * self.tensor_parallel_size + j for j in range(self.tensor_parallel_size)]
|
||
|
group = dist.new_group(ranks)
|
||
|
|
||
|
if self.rank in ranks:
|
||
|
local_rank = ranks.index(self.rank)
|
||
|
group_world_size = len(ranks)
|
||
|
process_group = group
|
||
|
ranks_in_group = ranks
|
||
|
|
||
|
return local_rank, group_world_size, process_group, ranks_in_group, mode
|