MCPcopy Create free account
hub / github.com/alinlab/SelfPatch / init_distributed_mode

Function init_distributed_mode

utils.py:447–479  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

445
446
447def init_distributed_mode(args):
448 # launched with torch.distributed.launch
449 if 'RANK' in os.environ and 'WORLD_SIZE' in os.environ:
450 args.rank = int(os.environ["RANK"])
451 args.world_size = int(os.environ['WORLD_SIZE'])
452 args.gpu = int(os.environ['LOCAL_RANK'])
453 # launched with submitit on a slurm cluster
454 elif 'SLURM_PROCID' in os.environ:
455 args.rank = int(os.environ['SLURM_PROCID'])
456 args.gpu = args.rank % torch.cuda.device_count()
457 # launched naively with `python main_dino.py`
458 # we manually add MASTER_ADDR and MASTER_PORT to env variables
459 elif torch.cuda.is_available():
460 print('Will run the code on one GPU.')
461 args.rank, args.gpu, args.world_size = 0, 0, 1
462 os.environ['MASTER_ADDR'] = '127.0.0.1'
463 os.environ['MASTER_PORT'] = '29500'
464 else:
465 print('Does not support training without GPU.')
466 sys.exit(1)
467
468 dist.init_process_group(
469 backend="nccl",
470 init_method=args.dist_url,
471 world_size=args.world_size,
472 rank=args.rank,
473 )
474
475 torch.cuda.set_device(args.gpu)
476 print('| distributed init (rank {}): {}'.format(
477 args.rank, args.dist_url), flush=True)
478 dist.barrier()
479 setup_for_distributed(args.rank == 0)
480
481
482def accuracy(output, target, topk=(1,)):

Callers

nothing calls this directly

Calls 2

printFunction · 0.85
setup_for_distributedFunction · 0.85

Tested by

no test coverage detected