vscode调试pytorch分布式训练

launch.json文件如下"cwd"= "${fileDirname}"表示代码调试的根目录是当前你调试的文件,也就是pretrain.py所在的目录。其他路径参数都是相对这个目录的

如果改成"cwd"= "${workspaceFolder}" 表示代码调试的根目录是打开的工作目录,也就是你vscode打开的工程文件夹的目录

"program"参数是是我们要运行的文件,默认是"program": "${file}",也就是你要调试的文件本身,

比如不调试分布式的话,就是pretrain.py,分布式的话要改成"program": "/root/miniconda3/lib/python3.10/site-packages/torch/distributed/run.py"表示我们其实是要运行这个文件,然后加入args参数,把pretrain.py作为一个参数

注意:pytorch1.9之后torch.distributed.launch 即将被废弃,取而代之的是torch.distributed.run

https://www.cnblogs.com/tencent-cloud-native/p/15186403.html

而torchrun脚本就是torch.distributed.run的内容,所以在终端运行的话

bash 复制代码
python -m torch.distributed.run --nproc_per_node=8 pretrain.py --config ./configs/Pretrain.yaml --output_dir output/Pretrain

bash 复制代码
torchrun --nproc_per_node=8 pretrain.py --config ./configs/Pretrain.yaml --output_dir output/Pretrain

二者是等价的,终端直接输入torchrun

bash 复制代码
root@autodl-container-115911b7ae-3dcde09d:~# torchrun
usage: torchrun [-h] [--nnodes NNODES] [--nproc_per_node NPROC_PER_NODE]
                [--rdzv_backend RDZV_BACKEND] [--rdzv_endpoint RDZV_ENDPOINT]
                [--rdzv_id RDZV_ID] [--rdzv_conf RDZV_CONF] [--standalone]
                [--max_restarts MAX_RESTARTS] [--monitor_interval MONITOR_INTERVAL]
                [--start_method {spawn,fork,forkserver}] [--role ROLE] [-m] [--no_python]
                [--run_path] [--log_dir LOG_DIR] [-r REDIRECTS] [-t TEE]
                [--node_rank NODE_RANK] [--master_addr MASTER_ADDR]
                [--master_port MASTER_PORT]
                training_script ...
torchrun: error: the following arguments are required: training_script, training_script_args
root@autodl-container-115911b7ae-3dcde09d:~# which torchrun
/root/miniconda3/bin/torchrun

但vscode调试配置如下,如果直接用"program": "/root/miniconda3/bin/torchrun"也是可以的

"name": "Python: Current File", 可以改成 "name": "Python: torchrun" 不过这个不影响,只是调试窗口名字

python 复制代码
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "/root/miniconda3/lib/python3.10/site-packages/torch/distributed/run.py",
            "console": "integratedTerminal",
            "justMyCode": false,
            "cwd": "${fileDirname}",
            "args": [
                "--nproc_per_node=1",
                "pretrain.py",
                "--config=./configs/pretrain.yaml",
                "--output_dir=output/Pretrain"
            ],
            // "env": {"CUDA_VISIBLE_DEVICES":"0"},

        }
    ]
}

最后:建议在每个工程都创建一个.vscode文件夹,在里面建一个lanuch.json文件,因为每个工程运行脚本肯定都不一样,不要在/root目录搞一个.vscode文件夹,不然你每次都是打开/root文件夹都要修改lanuch.json的内容,每次VSCODE都只打开一个工程,不要直接打开root目录

相关推荐
西南胶带の池上桜16 小时前
1.Pytorch模型应用(线性与非线性预测)
人工智能·pytorch·python
HarrySunCn19 小时前
如何使用VSCode开发Arduino项目
ide·vscode·单片机·编辑器
面壁的熊猫1 天前
cursor+cline+MCP(论文agent)
vscode·agent·cursor·mcp
Aspect of twilight1 天前
vscode python debug方式
ide·vscode·python·debug
啃火龙果的兔子1 天前
vscode中可以使用的免费的AI编程工具有哪些
ide·vscode·ai编程
littlezls1 天前
在VSCode中运行Python脚本文件时如何传参
vscode·python
宋明炜1 天前
VSCode + MSYS2 配置 C 语言开发环境(详细步骤)
c语言·ide·vscode
yscript1 天前
GPU分配BUG: Duplicate GPU detected : rank 1 and rank 0 both on CUDA device d5000
linux·运维·服务器·vscode·bug
裤裤兔1 天前
已经安装了PyTorch,Jupyter Notebook仍然报错“No module named torch“
人工智能·pytorch·jupyter
瀚岳-诸葛弩1 天前
对比tensorflow,从0开始学pytorch(三)--自定义层
人工智能·pytorch·tensorflow