(args)
| 17 | |
| 18 | import colorsys |
| 19 | def init_distributed_mode(args): |
| 20 | if 'WORLD_SIZE' in os.environ and os.environ['WORLD_SIZE'] != '': # 'RANK' in os.environ and |
| 21 | args.rank = int(os.environ["RANK"]) |
| 22 | args.world_size = int(os.environ['WORLD_SIZE']) |
| 23 | args.gpu = args.local_rank = int(os.environ['LOCAL_RANK']) |
| 24 | |
| 25 | # launch by torch.distributed.launch |
| 26 | # Single node |
| 27 | # python -m torch.distributed.launch --nproc_per_node=8 main.py --world-size 1 --rank 0 ... |
| 28 | # Multi nodes |
| 29 | # python -m torch.distributed.launch --nproc_per_node=8 main.py --world-size 2 --rank 0 --dist-url 'tcp://IP_OF_NODE0:FREEPORT' ... |
| 30 | # python -m torch.distributed.launch --nproc_per_node=8 main.py --world-size 2 --rank 1 --dist-url 'tcp://IP_OF_NODE0:FREEPORT' ... |
| 31 | # args.rank = int(os.environ.get('OMPI_COMM_WORLD_RANK')) |
| 32 | # local_world_size = int(os.environ['GPU_PER_NODE_COUNT']) |
| 33 | # args.world_size = args.world_size * local_world_size |
| 34 | # args.gpu = args.local_rank = int(os.environ['LOCAL_RANK']) |
| 35 | # args.rank = args.rank * local_world_size + args.local_rank |
| 36 | print('world size: {}, rank: {}, local rank: {}'.format(args.world_size, args.rank, args.local_rank)) |
| 37 | print(json.dumps(dict(os.environ), indent=2)) |
| 38 | elif 'SLURM_PROCID' in os.environ: |
| 39 | args.rank = int(os.environ['SLURM_PROCID']) |
| 40 | args.gpu = args.local_rank = int(os.environ['SLURM_LOCALID']) |
| 41 | args.world_size = int(os.environ['SLURM_NPROCS']) |
| 42 | |
| 43 | if os.environ.get('HAND_DEFINE_DIST_URL', 0) == '1': |
| 44 | pass |
| 45 | else: |
| 46 | import util.hostlist as uh |
| 47 | nodenames = uh.parse_nodelist(os.environ['SLURM_JOB_NODELIST']) |
| 48 | gpu_ids = [int(node[3:]) for node in nodenames] |
| 49 | fixid = int(os.environ.get('FIX_DISTRIBUTED_PORT_NUMBER', 0)) |
| 50 | # fixid += random.randint(0, 300) |
| 51 | port = str(3137 + int(min(gpu_ids)) + fixid) |
| 52 | args.dist_url = "tcp://{ip}:{port}".format(ip=uh.nodename_to_ip(nodenames[0]), port=port) |
| 53 | |
| 54 | print('world size: {}, world rank: {}, local rank: {}, device_count: {}'.format(args.world_size, args.rank, args.local_rank, torch.cuda.device_count())) |
| 55 | |
| 56 | |
| 57 | else: |
| 58 | print('Not using distributed mode') |
| 59 | args.distributed = False |
| 60 | args.world_size = 1 |
| 61 | args.rank = 0 |
| 62 | args.local_rank = 0 |
| 63 | return |
| 64 | |
| 65 | print("world_size:{} rank:{} local_rank:{}".format(args.world_size, args.rank, args.local_rank)) |
| 66 | args.distributed = True |
| 67 | torch.cuda.set_device(args.local_rank) |
| 68 | args.dist_backend = 'nccl' |
| 69 | print('| distributed init (rank {}): {}'.format(args.rank, args.dist_url), flush=True) |
| 70 | |
| 71 | torch.distributed.init_process_group( |
| 72 | backend=args.dist_backend, |
| 73 | world_size=args.world_size, |
| 74 | rank=args.rank, |
| 75 | init_method=args.dist_url, |
| 76 | ) |
nothing calls this directly
no outgoing calls
no test coverage detected