MCPcopy Create free account
hub / github.com/csuhan/OneLLM / init_distributed_mode

Function init_distributed_mode

util/misc.py:230–272  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

228 torch.save(*args, **kwargs)
229
230def init_distributed_mode(args):
231 if args.dist_on_itp:
232 args.rank = int(os.environ['OMPI_COMM_WORLD_RANK'])
233 args.world_size = int(os.environ['OMPI_COMM_WORLD_SIZE'])
234 args.gpu = int(os.environ['OMPI_COMM_WORLD_LOCAL_RANK'])
235 args.dist_url = "tcp://%s:%s" % (os.environ['MASTER_ADDR'], os.environ['MASTER_PORT'])
236 os.environ['LOCAL_RANK'] = str(args.gpu)
237 os.environ['RANK'] = str(args.rank)
238 os.environ['WORLD_SIZE'] = str(args.world_size)
239 # ["RANK", "WORLD_SIZE", "MASTER_ADDR", "MASTER_PORT", "LOCAL_RANK"]
240 elif 'RANK' in os.environ and 'WORLD_SIZE' in os.environ:
241 args.rank = int(os.environ["RANK"])
242 args.world_size = int(os.environ['WORLD_SIZE'])
243 args.gpu = int(os.environ['LOCAL_RANK'])
244 elif 'SLURM_PROCID' in os.environ:
245 os.environ['MASTER_PORT'] = '8994'
246 while 'MASTER_ADDR' not in os.environ or len(os.environ['MASTER_ADDR'].strip()) == 0:
247 os.environ['MASTER_ADDR'] = subprocess.check_output('sinfo -Nh -n %s | head -n 1 | awk \'{print $1}\'' % os.environ['SLURM_NODELIST'], shell=True, ).decode().strip()
248 time.sleep(1)
249 print(os.environ['MASTER_ADDR'])
250 args.world_size = int(os.environ['SLURM_NPROCS'])
251 args.rank = int(os.environ['SLURM_PROCID'])
252 args.gpu = args.rank % torch.cuda.device_count()
253 args.local_rank = args.gpu
254 os.environ['LOCAL_RANK'] = str(args.gpu)
255 os.environ['WORLD_SIZE'] = str(args.world_size)
256 os.environ['RANK'] = str(args.rank)
257 else:
258 print('Not using distributed mode')
259 setup_for_distributed(is_master=True) # hack
260 args.distributed = False
261 return
262
263 args.distributed = True
264
265 torch.cuda.set_device(args.gpu)
266 args.dist_backend = 'nccl'
267 print('| distributed init (rank {}): {}, gpu {}'.format(
268 args.rank, args.dist_url, args.gpu), flush=True)
269 torch.distributed.init_process_group(backend=args.dist_backend, init_method=args.dist_url,
270 world_size=args.world_size, rank=args.rank)
271 torch.distributed.barrier()
272 setup_for_distributed(args.rank == 0)
273
274
275def init_distributed_mode1(args):

Callers

nothing calls this directly

Calls 3

printFunction · 0.85
setup_for_distributedFunction · 0.85
decodeMethod · 0.80

Tested by

no test coverage detected