MCPcopy Create free account
hub / github.com/OpenSparseLLMs/Linear-MoE / _initialize_distributed

Function _initialize_distributed

linear_moe/initialize.py:87–146  ·  view source on GitHub ↗

Initialize torch.distributed and core model parallel.

()

Source from the content-addressed store, hash-verified

85 return None
86
87def _initialize_distributed():
88 """Initialize torch.distributed and core model parallel."""
89 args = get_args()
90
91 device_count = torch.cuda.device_count()
92 if torch.distributed.is_initialized():
93
94 if args.rank == 0:
95 print(
96 "torch distributed is already initialized, "
97 "skipping initialization ...",
98 flush=True,
99 )
100 args.rank = torch.distributed.get_rank()
101 args.world_size = torch.distributed.get_world_size()
102
103 else:
104
105 if args.rank == 0:
106 print("> initializing torch distributed ...", flush=True)
107 # Manually set the device ids.
108 if device_count > 0:
109 device = args.rank % device_count
110 if args.local_rank is not None:
111 assert (
112 args.local_rank == device
113 ), "expected local-rank to be the same as rank % device-count."
114 else:
115 args.local_rank = device
116 torch.cuda.set_device(device)
117 # Call the init process
118 torch.distributed.init_process_group(
119 backend=args.distributed_backend,
120 world_size=args.world_size,
121 rank=args.rank,
122 timeout=timedelta(minutes=args.distributed_timeout_minutes),
123 )
124
125 # Set the tensor model-parallel, pipeline model-parallel, and
126 # data-parallel communicators.
127 if device_count > 0:
128 if mpu.model_parallel_is_initialized():
129 print("model parallel is already initialized")
130 else:
131 mpu.initialize_model_parallel(
132 args.tensor_model_parallel_size,
133 args.pipeline_model_parallel_size,
134 args.virtual_pipeline_model_parallel_size,
135 args.pipeline_model_parallel_split_rank,
136 )
137
138 if args.rank == 0:
139 print(
140 f"> initialized tensor model parallel with size "
141 f"{mpu.get_tensor_model_parallel_world_size()}"
142 )
143 print(
144 f"> initialized pipeline model parallel with size "

Callers 1

finish_mpu_initFunction · 0.85

Calls 1

get_argsFunction · 0.90

Tested by

no test coverage detected