vscode调试torch.distributed.launch

在PyTorch中,如果我们要运行一个分布式的程序会用到以下命令

python -m torch.distributed.launch --nproc_per_node 8 train.py

但是如果我们想调试的时候如果使用命令行调试就会很麻烦,这里我们需要用到vscode的launch.json调试方法,首先我们打开vscode,看一下文件目录下有没有.vscode目录,如果有看一下里面有没有launch.json文件,如果没有我们需要新建一下,如下所示,依次点击

在这里插入图片描述
然后选择相应的python调试文件即可,此时会生成一个launch.json文件,内容如下所示

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

然后我们将其进行修改,注意这里采用单卡调试,也就是说我们的调试要达到一下命令的效果

python -m torch.distributed.launch --nproc_per_node 1 train.py

下面是修改后的文件,其中program是我们要运行的文件,就是launch.py,然后-m参数忽略即可,args重视我们后续的参数,有–nproc_per_node 1以及train.py

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "/home/wangyh/anaconda3/envs/torch/lib/python3.6/site-packages/torch/distributed/launch.py",
            "console": "integratedTerminal",
            "args": [
                "--nproc_per_node=1",
                "train.py",
            ],
            "env": {"CUDA_VISIBLE_DEVICES":"0"},
        }
    ]
}

注意:调试的时候点击下面图片的绿色三角才能进行调试!!!!!!如果点击插件run中的debug in terminal是不能进行调试的!!!好了到这里就可以开心的调试啦

在这里插入图片描述

Update

在Pytorch1.9及以上版本,如果依然采用上述方法启动DDP会出现如下警告,这是因为新版本舍弃了launch.py的启动方法,上述中的launch.py变为run.py即可

/home/wangyh/anaconda3/envs/python3.10/lib/python3.10/site-packages/torch/distributed/launch.py:178:FutureWarning: The module torch.distributed.launch is deprecated
and will be removed in future. Use torchrun.
  • 27
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: torch.distributed.launch是PyTorch的一个工具,可以用来启动分布式训练任务。具体使用方法如下: 首先,在你的代码中使用torch.distributed模块来定义分布式训练的参数,如下所示: ``` import torch.distributed as dist dist.init_process_group(backend="nccl", init_method="env://") ``` 这个代码片段定义了使用NCCL作为分布式后端,以及使用环境变量作为初始化方法。 接下来,在命令行中使用torch.distributed.launch来启动分布式训练任务,如下所示: ``` python -m torch.distributed.launch --nproc_per_node=NUM_GPUS YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3 and so on) ``` 其中,NUM_GPUS是每个节点上使用的GPU数量,YOUR_TRAINING_SCRIPT.py是你的训练脚本,(--arg1 --arg2 --arg3 and so on)是传递给训练脚本的参数。 torch.distributed.launch会自动为每个节点启动一个进程,并传递适当的环境变量和命令行参数。在训练过程中,你可以使用torch.distributed模块来进行分布式的操作,如在每个节点之间同步参数、收集梯度等。 希望这个回答对你有所帮助! ### 回答2: torch.distributed.launch是PyTorch中用于多节点分布式训练的一个工具。它能够帮助我们简化在多个节点上启动分布式训练的过程,使得代码编写更加简单方便。 使用torch.distributed.launch,首先需要确保环境中已经安装了PyTorch库。然后,在命令行中执行以下命令: python -m torch.distributed.launch --nproc_per_node=<num_gpus> <your_script.py> (--arg1 --arg2 ...) 其中,"<num_gpus>"是每个节点上的GPU数量,"<your_script.py>"是要运行的脚本路径。"--arg1 --arg2 ..."是你的脚本所需的各种参数,与普通的命令行参数传递方式相同。 执行上述命令后,torch.distributed.launch将会自动在每个节点上启动训练进程,并负责进程间的通信和同步。每个进程将会自动获得一个本地的rank编号,从0开始递增,并且可以通过torch.distributed.get_rank()函数获得。 在你的训练脚本中,可以通过torch.distributed.get_world_size()获得总的节点数量,通过torch.distributed.get_rank()获得当前节点的rank编号。你可以根据这些信息来区分不同的节点,进行相应的分布式操作。 除了以上基本用法外,torch.distributed.launch还提供了其他的一些选项,如--use_env、--master_addr、--master_port等,可以根据需要进行使用。可以通过在命令行中执行python -m torch.distributed.launch --help来查看更多详细的帮助信息。 总之,使用torch.distributed.launch可以方便地实现多节点分布式训练,简化了代码编写和启动的过程,提高了训练效率和灵活性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值