CUDAVISIBLEDEVICES=0,1,2,3 python -m torch.distributed.launch imagetrainh5.py…
- User
CUDA_VISIBLE_DEVICES=0,1,2,3 python -m torch.distributed.launch image_train_h5.py /home/amax/anaconda3/lib/python3.11/site-packages/torch/distributed/launch.py:183: FutureWarning: The module torch.distributed.launch is deprecated and will be removed in future. Use torchrun. Note that --use-env is set by default in torchrun. If your script expects
--local-rankargument to be set, please change it to read fromos.environ['LOCAL_RANK']instead. See https://pytorch.org/docs/stable/distributed.html#launch-utility for further instructionswarnings.warn( usage: image_train_h5.py [-h] [--workers WORKERS] [--train_test_h5_path TRAIN_TEST_H5_PATH] [--val_test_h5_path VAL_TEST_H5_PATH] [--model_outpath MODEL_OUTPATH] [--lr LR] [--model MODEL] [--seed SEED] [--batch_size BATCH_SIZE] [--epochs EPOCHS] [--decay DECAY] [--milestone MILESTONE] [--local_rank LOCAL_RANK] [--nproc_per_node NPROC_PER_NODE] image_train_h5.py: error: unrecognized arguments: --local-rank=0 [2024-03-27 21:38:22,700] torch.distributed.elastic.multiprocessing.api: [ERROR] failed (exitcode: 2) local_rank: 0 (pid: 443088) of binary: /home/amax/anaconda3/bin/python Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "/home/amax/anaconda3/lib/python3.11/site-packages/torch/distributed/launch.py", line 198, in main() File "/home/amax/anaconda3/lib/python3.11/site-packages/torch/distributed/launch.py", line 194, in main launch(args) File "/home/amax/anaconda3/lib/python3.11/site-packages/torch/distributed/launch.py", line 179, in launch run(args) File "/home/amax/anaconda3/lib/python3.11/site-packages/torch/distributed/run.py", line 803, in run elastic_launch( File "/home/amax/anaconda3/lib/python3.11/site-packages/torch/distributed/launcher/api.py", line 135, in call return launch_agent(self._config, self._entrypoint, list(args)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/amax/anaconda3/lib/python3.11/site-packages/torch/distributed/launcher/api.py", line 268, in launch_agent raise ChildFailedError( torch.distributed.elastic.multiprocessing.errors.ChildFailedError:
image_train_h5.py FAILED
Failures: <NO_OTHER_FAILURES>
Root Cause (first observed failure): [0]: time : 2024-03-27_21:38:22 host : amax rank : 0 (local_rank: 0) exitcode : 2 (pid: 443088) error_file: <N/A> traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html
这是什么错误,应该怎么解决
- Assistant
此错误提示来自于尝试在使用PyTorch分布式训练时遇到的几个问题。这里有一个解决方案的步骤:
-
弃用警告:
torch.distributed.launch已被弃用,并将在未来版本中删除。PyTorch官方建议改用torchrun。因此,您应该避免使用python -m torch.distributed.launch来启动分布式训练,而应使用torchrun命令。 -
--local_rank的使用: 旧的torch.distributed.launch工具需要使用--local_rank参数来手动传递每个进程的本地排名。但在torchrun中,默认情况下使用环境变量LOCAL_RANK来替代。因此,您应该确保您的代码内部是从os.environ['LOCAL_RANK']读取本地排名的,而不是期望从命令行参数获取。
结合您的错误提示,您可以按照以下步骤尝试解决这个问题:
调整您的代码
-
确保代码中使用
os.environ['LOCAL_RANK']获取本地排名,例如:pythonimport os local_rank = int(os.environ['LOCAL_RANK'])
使用
torchrun代替torch.distributed.launch-
根据您之前的命令,使用
torchrun的等效形式可能如下:
-
- User
- Assistant
这个错误信息指出的问题是关于无效的设备序号(Invalid device ordinal),这通常意味着你的代码试图访问一个不存在的CUDA设备。这种情况通常发生于当你尝试在一个有限数量的GPU上运行分布式程序,但程序却试图访问超出这个范围的设备时。
例如,如果你的系统中只有2个GPU(设备序号为0和1),但是你试图设置设备为2或更高的设备序号,你就会遇到这个错误。
要解决这个问题,你需要确保几个方面:
-
确认GPU数量:首先,验证你机器上可用的GPU数量。你可以通过运行
nvidia-smi命令来查看。确保你没有试图使用超过这个数量的GPU。 -
适当设置CUDA_VISIBLE_DEVICES:如果你使用的是
CUDA_VISIBLE_DEVICES环境变量来限制可用的GPU,确保你没有设置超过实际存在的GPU序号。例如,如果你有2个GPU,你应当只设置CUDA_VISIBLE_DEVICES=0,1。 -
检查代码:如果你在代码中显式地设置了设备(例如,
torch.cuda.set_device(local_rank)),确保local_rank没有超出可用设备的范围。你可能需要根据CUDA_VISIBLE_DEVICES环境变量或其他逻辑来动态地确定local_rank的值,以确保它不会超出实际可用的GPU数量。 -
torchrun的使用:当使用
torchrun时,通过--nproc_per_node指定每个节点的进程数应该匹配你希望使用的GPU数量。例如,如果你有2个GPU并希望每个GPU上运行一个进程,你应当设置--nproc_per_node=2。
基于给出的错误和情况,你可以尝试检查并修改代码,例如:
bashCUDA_VISIBLE_DEVICES=0,1 torchrun --nproc_per_node=2 image_train_h5.py [其他参数] -