|
| 1 | +import os |
| 2 | +import torch |
| 3 | +import torch.distributed as dist |
| 4 | +from mpi4py import MPI |
| 5 | + |
| 6 | +class distributed(): |
| 7 | + def get_size(self): |
| 8 | + if dist.is_available() and dist.is_initialized(): |
| 9 | + size = dist.get_world_size() |
| 10 | + else: |
| 11 | + size = 1 |
| 12 | + return size |
| 13 | + |
| 14 | + def get_rank(self): |
| 15 | + if dist.is_available() and dist.is_initialized(): |
| 16 | + rank = dist.get_rank() |
| 17 | + else: |
| 18 | + rank = 0 |
| 19 | + return rank |
| 20 | + |
| 21 | + def get_local_rank(self): |
| 22 | + if not (dist.is_available() and dist.is_initialized()): |
| 23 | + return 0 |
| 24 | + # Number of GPUs per node |
| 25 | + if torch.cuda.is_available(): |
| 26 | + local_rank = dist.get_rank() % torch.cuda.device_count() |
| 27 | + else: |
| 28 | + # raise NotImplementedError() |
| 29 | + # running on cpu device should not call this function |
| 30 | + local_rank = -1 |
| 31 | + return local_rank |
| 32 | + |
| 33 | + def __init__(self, method): |
| 34 | + # MASTER_PORT - required; has to be a free port on machine with rank 0 |
| 35 | + # MASTER_ADDR - required (except for rank 0); address of rank 0 node |
| 36 | + # WORLD_SIZE - required; can be set either here, or in a call to init function |
| 37 | + # RANK - required; can be set either here, or in a call to init function |
| 38 | + self.mpi_comm = MPI.COMM_WORLD |
| 39 | + |
| 40 | + if method == "nccl-slurm": |
| 41 | + # MASTER_ADDR can be set in the slurm batch script using command |
| 42 | + # scontrol show hostnames $SLURM_JOB_NODELIST |
| 43 | + if "MASTER_ADDR" not in os.environ: |
| 44 | + # Try SLURM_LAUNCH_NODE_IPADDR but it is the IP address of the node |
| 45 | + # from which the task launch was initiated (where the srun command |
| 46 | + # ran from). It may not be the node of rank 0. |
| 47 | + if "SLURM_LAUNCH_NODE_IPADDR" in os.environ: |
| 48 | + os.environ["MASTER_ADDR"] = os.environ["SLURM_LAUNCH_NODE_IPADDR"] |
| 49 | + else: |
| 50 | + raise Exception("Error: nccl-slurm - SLURM_LAUNCH_NODE_IPADDR is not set") |
| 51 | + |
| 52 | + # Use the default pytorch port |
| 53 | + if "MASTER_PORT" not in os.environ: |
| 54 | + if "SLURM_SRUN_COMM_PORT" in os.environ: |
| 55 | + os.environ["MASTER_PORT"] = os.environ["SLURM_SRUN_COMM_PORT"] |
| 56 | + else: |
| 57 | + os.environ["MASTER_PORT"] = "29500" |
| 58 | + |
| 59 | + # obtain WORLD_SIZE |
| 60 | + if "WORLD_SIZE" not in os.environ: |
| 61 | + if "SLURM_NTASKS" in os.environ: |
| 62 | + world_size = os.environ["SLURM_NTASKS"] |
| 63 | + else: |
| 64 | + if "SLURM_JOB_NUM_NODES" in os.environ: |
| 65 | + num_nodes = os.environ["SLURM_JOB_NUM_NODES"] |
| 66 | + else: |
| 67 | + raise Exception("Error: nccl-slurm - SLURM_JOB_NUM_NODES is not set") |
| 68 | + if "SLURM_NTASKS_PER_NODE" in os.environ: |
| 69 | + ntasks_per_node = os.environ["SLURM_NTASKS_PER_NODE"] |
| 70 | + elif "SLURM_TASKS_PER_NODE" in os.environ: |
| 71 | + ntasks_per_node = os.environ["SLURM_TASKS_PER_NODE"] |
| 72 | + else: |
| 73 | + raise Exception("Error: nccl-slurm - SLURM_(N)TASKS_PER_NODE is not set") |
| 74 | + world_size = ntasks_per_node * num_nodes |
| 75 | + os.environ["WORLD_SIZE"] = str(world_size) |
| 76 | + |
| 77 | + # obtain RANK |
| 78 | + if "RANK" not in os.environ: |
| 79 | + if "SLURM_PROCID" in os.environ: |
| 80 | + os.environ["RANK"] = os.environ["SLURM_PROCID"] |
| 81 | + else: |
| 82 | + raise Exception("Error: nccl-slurm - SLURM_PROCID is not set") |
| 83 | + |
| 84 | + # Initialize DDP module |
| 85 | + dist.init_process_group(backend = "nccl", init_method='env://') |
| 86 | + |
| 87 | + elif method == "nccl-openmpi": |
| 88 | + if "MASTER_ADDR" not in os.environ: |
| 89 | + if "PMIX_SERVER_URI2" in os.environ: |
| 90 | + os.environ["MASTER_ADDR"] = os.environ("PMIX_SERVER_URI2").split("//")[1] |
| 91 | + else: |
| 92 | + raise Exception("Error: nccl-openmpi - PMIX_SERVER_URI2 is not set") |
| 93 | + |
| 94 | + # Use the default pytorch port |
| 95 | + if "MASTER_PORT" not in os.environ: |
| 96 | + os.environ["MASTER_PORT"] = "29500" |
| 97 | + |
| 98 | + if "WORLD_SIZE" not in os.environ: |
| 99 | + if "OMPI_COMM_WORLD_SIZE" not in os.environ: |
| 100 | + raise Exception("Error: nccl-openmpi - OMPI_COMM_WORLD_SIZE is not set") |
| 101 | + os.environ["WORLD_SIZE"] = os.environ["OMPI_COMM_WORLD_SIZE"] |
| 102 | + |
| 103 | + if "RANK" not in os.environ: |
| 104 | + if "OMPI_COMM_WORLD_RANK" not in os.environ: |
| 105 | + raise Exception("Error: nccl-openmpi - OMPI_COMM_WORLD_RANK is not set") |
| 106 | + os.environ["RANK"] = os.environ["OMPI_COMM_WORLD_RANK"] |
| 107 | + |
| 108 | + # Initialize DDP module |
| 109 | + dist.init_process_group(backend = "nccl", init_method='env://') |
| 110 | + |
| 111 | + elif method == "nccl-mpich": |
| 112 | + if "MASTER_ADDR" not in os.environ: |
| 113 | + os.environ['MASTER_ADDR'] = "localhost" |
| 114 | + |
| 115 | + # Use the default pytorch port |
| 116 | + if "MASTER_PORT" not in os.environ: |
| 117 | + os.environ["MASTER_PORT"] = "29500" |
| 118 | + |
| 119 | + if "WORLD_SIZE" not in os.environ: |
| 120 | + if "PMI_SIZE" in os.environ: |
| 121 | + world_size = os.environ["PMI_SIZE"] |
| 122 | + elif MPI.Is_initialized(): |
| 123 | + world_size = MPI.COMM_WORLD.Get_size() |
| 124 | + else: |
| 125 | + world_size = 1 |
| 126 | + os.environ["WORLD_SIZE"] = str(world_size) |
| 127 | + |
| 128 | + if "RANK" not in os.environ: |
| 129 | + if "PMI_RANK" in os.environ: |
| 130 | + rank = os.environ["PMI_RANK"] |
| 131 | + elif MPI.Is_initialized(): |
| 132 | + rank = MPI.COMM_WORLD.Get_rank() |
| 133 | + else: |
| 134 | + rank = 0 |
| 135 | + os.environ["RANK"] = str(rank) |
| 136 | + |
| 137 | + # Initialize DDP module |
| 138 | + dist.init_process_group(backend = "nccl", init_method='env://') |
| 139 | + |
| 140 | + elif method == "gloo": |
| 141 | + if "MASTER_ADDR" not in os.environ: |
| 142 | + # check if OpenMPI is used |
| 143 | + if "PMIX_SERVER_URI2" in os.environ: |
| 144 | + addr = os.environ["PMIX_SERVER_URI2"] |
| 145 | + addr = addr.split("//")[1].split(":")[0] |
| 146 | + os.environ["MASTER_ADDR"] = addr |
| 147 | + else: |
| 148 | + os.environ['MASTER_ADDR'] = "localhost" |
| 149 | + |
| 150 | + # Use the default pytorch port |
| 151 | + if "MASTER_PORT" not in os.environ: |
| 152 | + os.environ["MASTER_PORT"] = "29500" |
| 153 | + |
| 154 | + # obtain WORLD_SIZE |
| 155 | + if "WORLD_SIZE" not in os.environ: |
| 156 | + # check if OpenMPI is used |
| 157 | + if "OMPI_COMM_WORLD_SIZE" in os.environ: |
| 158 | + world_size = os.environ["OMPI_COMM_WORLD_SIZE"] |
| 159 | + elif "PMI_SIZE" in os.environ: |
| 160 | + world_size = os.environ["PMI_SIZE"] |
| 161 | + elif MPI.Is_initialized(): |
| 162 | + world_size = MPI.COMM_WORLD.Get_size() |
| 163 | + else: |
| 164 | + world_size = 1 |
| 165 | + os.environ["WORLD_SIZE"] = str(world_size) |
| 166 | + |
| 167 | + # obtain RANK |
| 168 | + if "RANK" not in os.environ: |
| 169 | + # check if OpenMPI is used |
| 170 | + if "OMPI_COMM_WORLD_RANK" in os.environ: |
| 171 | + rank = os.environ["OMPI_COMM_WORLD_RANK"] |
| 172 | + elif "PMI_RANK" in os.environ: |
| 173 | + rank = os.environ["PMI_RANK"] |
| 174 | + elif MPI.Is_initialized(): |
| 175 | + rank = MPI.COMM_WORLD.Get_rank() |
| 176 | + else: |
| 177 | + rank = 0 |
| 178 | + os.environ["RANK"] = str(rank) |
| 179 | + |
| 180 | + # Initialize DDP module |
| 181 | + dist.init_process_group(backend = "gloo", init_method='env://') |
| 182 | + |
| 183 | + else: |
| 184 | + raise NotImplementedError() |
| 185 | + |
| 186 | + def finalize(self): |
| 187 | + dist.destroy_process_group() |
| 188 | + |
| 189 | +#----< init_parallel() >------------------------------------------------------- |
| 190 | +def init_parallel(): |
| 191 | + # check if cuda device is available |
| 192 | + ngpu_per_node = torch.cuda.device_count() |
| 193 | + if not torch.cuda.is_available(): |
| 194 | + backend = "gloo" |
| 195 | + else: |
| 196 | + backend = "nccl-mpich" |
| 197 | + |
| 198 | + # initialize parallel/distributed environment |
| 199 | + comm = distributed(backend) |
| 200 | + rank = comm.get_rank() |
| 201 | + world_size = comm.get_size() |
| 202 | + local_rank = comm.get_local_rank() |
| 203 | + |
| 204 | + # select training device: cpu or cuda |
| 205 | + if not torch.cuda.is_available(): |
| 206 | + device = torch.device("cpu") |
| 207 | + else: |
| 208 | + device = torch.device("cuda:"+str(local_rank)) |
| 209 | + |
| 210 | + return comm, device |
| 211 | + |
| 212 | + |
0 commit comments