pytorch多卡训练时,常遇到的问题:RuntimeError: Address already in use
File "/data/wanglaoba/anaconda3/envs/BGE/lib/python3.8/site-packages/torch/distributed/elastic/rendezvous/static_tcp_rendezvous.py", line 55, in next_rendezvous
self._store = TCPStore( # type: ignore[call-arg]
RuntimeError: Address already in use
原因:程序默认端口号被占用。
出现该问题可能的场景:
1. 程序非正常停止,实际进程并未停止;
2. 该设备上已有自己的或其他用户运行的多卡程序,大多是自己的。
解决方法:修改程序运行端口号,示例如下:
python -m torch.distributed.run --master_port [随机五位数] --nproc_per_node 4 train.py
注意:--master_port 只能放到.run之后,train.py之前,否则不会改变默认端口号。