使用pytorch進行分布式訓練,需要指定 local_rank,主機 local_rank = 0
1 """ 2 pytorch 分布式訓練初始化 3 1) backend (str): 指定通信所用后端,可以是'ncll'、'gloo' 或者是一個torch.ditributed.Backend類 4 2) init_method (str): 這個URL指定了如何初始化互相通信的進程 5 3) world_size (int): 執行訓練的所有的進程數 等於 cuda.device 數量 6 4) rank (int): 進程的編號,即優先級 7 """ 8 torch.distributed.init_process_group(backend="nccl", init_method="env://", world_size=torch.cuda.device_count(), 9 rank=args.local_rank)
若是報錯:
raise _env_error("MASTER_ADDR")
ValueError: Error initializing torch.distributed using env:// rendezvous: environment variable MASTER_ADDR expected, but not set
可使用如下兩條指令解決上述問題:
1 export MASTER_ADDR=localhost 2 3 export MASTER_PORT=5678