Initialize torch.distributed and mpu.
()
| 122 | |
| 123 | |
| 124 | def _initialize_distributed(): |
| 125 | """Initialize torch.distributed and mpu.""" |
| 126 | args = get_args() |
| 127 | |
| 128 | device_count = torch.cuda.device_count() |
| 129 | if torch.distributed.is_initialized(): |
| 130 | |
| 131 | if args.rank == 0: |
| 132 | print('torch distributed is already initialized, ' |
| 133 | 'skipping initialization ...', flush=True) |
| 134 | args.rank = torch.distributed.get_rank() |
| 135 | args.world_size = torch.distributed.get_world_size() |
| 136 | |
| 137 | else: |
| 138 | |
| 139 | if args.rank == 0: |
| 140 | print('> initializing torch distributed ...', flush=True) |
| 141 | # Manually set the device ids. |
| 142 | if device_count > 0: |
| 143 | device = args.rank % device_count |
| 144 | if args.local_rank is not None: |
| 145 | assert args.local_rank == device, \ |
| 146 | 'expected local-rank to be the same as rank % device-count.' |
| 147 | else: |
| 148 | args.local_rank = device |
| 149 | torch.cuda.set_device(device) |
| 150 | # Call the init process |
| 151 | init_method = 'tcp://' |
| 152 | master_ip = os.getenv('MASTER_ADDR', 'localhost') |
| 153 | master_port = os.getenv('MASTER_PORT', '6000') |
| 154 | init_method += master_ip + ':' + master_port |
| 155 | torch.distributed.init_process_group( |
| 156 | backend=args.distributed_backend, |
| 157 | world_size=args.world_size, rank=args.rank, |
| 158 | init_method=init_method) |
| 159 | |
| 160 | # Set the model-parallel / data-parallel communicators. |
| 161 | if device_count > 0: |
| 162 | if mpu.model_parallel_is_initialized(): |
| 163 | print('model parallel is already initialized') |
| 164 | else: |
| 165 | mpu.initialize_model_parallel(args.model_parallel_size) |
| 166 | |
| 167 | # Optional DeepSpeed Activation Checkpointing Features |
| 168 | # |
| 169 | if args.deepspeed and args.deepspeed_activation_checkpointing: |
| 170 | setup_deepspeed_random_and_activation_checkpointing(args) |
| 171 | |
| 172 | def _init_autoresume(): |
| 173 | """Set autoresume start time.""" |
no test coverage detected