当前位置:   article > 正文

vscode调试pytorch的DistributedDataParallel代码_vscode 分布式调试pytorch rank

vscode 分布式调试pytorch rank

一、查找launch.py

使用代码。

find / -name launch.py | grep distributed
  • 1

得到的结果如下
在这里插入图片描述
这里我们得到了两个结果,看目标文件的路径名,第二个launch.py应该在软件的解压缩包里,因此这里使用第一个,记下这个绝对路径。

二、修改launch.json

修改默认的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: train.py",
            "type": "python",
            "python": "/root/miniconda3/envs/onnx/bin/python",
            "request": "launch",
            //program参数一定要放launch.py文件,
            "program": "/root/miniconda3/envs/onnx/lib/python3.8/site-packages/torch/distributed/launch.py",
            "args": [
                //命令行参数
                "--nproc_per_node=1", 
                //程序代码
                "train.py"
                //程序入参
            ],
            //环境变量
            "env": {
                "CUDA_VISIBLE_DEVICES":"0"
            },
            "console": "integratedTerminal",
            "justMyCode": true
    
    
        }
    ]

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

program就是我们上一步复制的绝对路径。
建议只用一个GPU来调试,所以nproc_per_node的值设置为1,CUDA_VISIBLE_DEVICES只用一个设备。

三、特别提醒

上面的写法是默认没有任何参数传递给train.py的,如果要给它传递参数,则一定要写到main.py之后,例如

3.1 错误的写法

  	 "args": [
                "--nproc_per_node=1", 
                "--use_env",
                "--cfg = cfgfile",
                "main.py"
            ],
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

上面的写法会报错,因为–nproc_per_node=1,–use_env以及–cfg=cfgfile三个变量实际上都是传递给前面"program"所指示的launch.py文件的,而该文件并没有cfg参数,因此会报错。

3.2 正确的写法

  	 "args": [
                "--nproc_per_node=1", 
                "--use_env",
                "main.py",
                "--cfg = cfgfile"
            ],
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

这样写就表示–cfg=cfgfile参数是传递给main.py文件的,这样就没有问题啦

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/67356
推荐阅读
相关标签
  

闽ICP备14008679号