在一台计算机上运行多个torchrun任务
1. 问题
当我在一个终端中运行 CUDA_VISIBLE_DEVICES=6,7 torchrun --nproc_per_node=2 xxx.py ,然后打开了第二个终端运行 CUDA_VISIBLE_DEVICES=3,4 torchrun --nproc_per_node=2 xxx.py 时,产生了报错:
1 | |
原因在于,torchrun 默认使用 29500 端口。第二个任务必须指定不同的端口,否则会报 Address already in use 错误。
2. 避免端口冲突
因此,要在同一台机器上同时运行两个或多个 torchrun 任务,必须解决端口冲突 。解决方法也很简单:
- 手动指定端口:使用
—master_port=29501或--rdzv_endpoint=localhost:端口号。 - 自动分配端口:将端口设置为
0,系统会自动找空闲端口。
此外,注意设置好CUDA_VISIBLE_DEVICES 。
在一台计算机上运行多个torchrun任务
https://misaka0502.github.io/2026/01/14/torchrun-multitask/