(
raw_argv,
world_size,
num_machines,
machine_rank,
num_gpus_per_machine,
dist_url,
args,
)
| 91 | |
| 92 | |
| 93 | def launch_by_subprocess( |
| 94 | raw_argv, |
| 95 | world_size, |
| 96 | num_machines, |
| 97 | machine_rank, |
| 98 | num_gpus_per_machine, |
| 99 | dist_url, |
| 100 | args, |
| 101 | ): |
| 102 | assert ( |
| 103 | world_size > 1 |
| 104 | ), "subprocess mode doesn't support single GPU, use spawn mode instead" |
| 105 | |
| 106 | if dist_url is None: |
| 107 | # ------------------------hack for multi-machine training -------------------- # |
| 108 | if num_machines > 1: |
| 109 | master_ip = subprocess.check_output(["hostname", "--fqdn"]).decode("utf-8") |
| 110 | master_ip = str(master_ip).strip() |
| 111 | dist_url = "tcp://{}".format(master_ip) |
| 112 | ip_add_file = "./" + args[1].experiment_name + "_ip_add.txt" |
| 113 | if machine_rank == 0: |
| 114 | port = _find_free_port() |
| 115 | with open(ip_add_file, "w") as ip_add: |
| 116 | ip_add.write(dist_url+'\n') |
| 117 | ip_add.write(str(port)) |
| 118 | else: |
| 119 | while not os.path.exists(ip_add_file): |
| 120 | time.sleep(0.5) |
| 121 | |
| 122 | with open(ip_add_file, "r") as ip_add: |
| 123 | dist_url = ip_add.readline().strip() |
| 124 | port = ip_add.readline() |
| 125 | else: |
| 126 | dist_url = "tcp://127.0.0.1" |
| 127 | port = _find_free_port() |
| 128 | |
| 129 | # set PyTorch distributed related environmental variables |
| 130 | current_env = os.environ.copy() |
| 131 | current_env["MASTER_ADDR"] = dist_url |
| 132 | current_env["MASTER_PORT"] = str(port) |
| 133 | current_env["WORLD_SIZE"] = str(world_size) |
| 134 | assert num_gpus_per_machine <= torch.cuda.device_count() |
| 135 | |
| 136 | if "OMP_NUM_THREADS" not in os.environ and num_gpus_per_machine > 1: |
| 137 | current_env["OMP_NUM_THREADS"] = str(1) |
| 138 | logger.info( |
| 139 | "\n*****************************************\n" |
| 140 | "Setting OMP_NUM_THREADS environment variable for each process " |
| 141 | "to be {} in default, to avoid your system being overloaded, " |
| 142 | "please further tune the variable for optimal performance in " |
| 143 | "your application as needed. \n" |
| 144 | "*****************************************".format( |
| 145 | current_env["OMP_NUM_THREADS"] |
| 146 | ) |
| 147 | ) |
| 148 | |
| 149 | processes = [] |
| 150 | for local_rank in range(0, num_gpus_per_machine): |
no test coverage detected