This repository was archived by the owner on May 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmcore_parallel_utils.py
More file actions
86 lines (71 loc) · 2.78 KB
/
Copy pathmcore_parallel_utils.py
File metadata and controls
86 lines (71 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=C0115,C0116,C0301
"""
Megatron Model Parallel Initialization
"""
import os
import megatron.core.parallel_state as ps
import torch
# pylint: disable=C0116
class Utils:
world_size = torch.cuda.device_count()
# rank = int(os.environ["LOCAL_RANK"])
rank = 0
@staticmethod
def initialize_distributed(tensor_model_parallel_size=1, pipeline_model_parallel_size=1, context_parallel_size=1):
ps.destroy_model_parallel()
# Torch setup for distributed training
rank = int(os.environ["LOCAL_RANK"])
world_size = 1 # torch.cuda.device_count()
torch.cuda.set_device(rank)
torch.distributed.init_process_group(world_size=world_size, rank=rank)
# Megatron core distributed training initialization
ps.initialize_model_parallel(
tensor_model_parallel_size, pipeline_model_parallel_size, context_parallel_size=context_parallel_size
)
@staticmethod
def set_world_size(world_size=None, rank=None):
Utils.world_size = torch.cuda.device_count() if world_size is None else world_size
if torch.distributed.is_initialized() and Utils.world_size != torch.distributed.get_world_size():
torch.distributed.destroy_process_group()
if rank is None:
# Utils.rank = int(os.environ["LOCAL_RANK"])
Utils.rank = 0
if Utils.rank >= Utils.world_size:
Utils.rank = -1
else:
Utils.rank = rank
@staticmethod
def destroy_model_parallel():
ps.destroy_model_parallel()
torch.distributed.barrier()
@staticmethod
def initialize_model_parallel(
tensor_model_parallel_size=1,
pipeline_model_parallel_size=1,
virtual_pipeline_model_parallel_size=None,
pipeline_model_parallel_split_rank=None,
**kwargs,
):
ps.destroy_model_parallel()
Utils.initialize_distributed()
ps.initialize_model_parallel(
tensor_model_parallel_size,
pipeline_model_parallel_size,
virtual_pipeline_model_parallel_size,
pipeline_model_parallel_split_rank,
**kwargs,
)
# pylint: disable=C0116