目的
现在深度学习框架大多是为了多卡并行训练而写的框架,对于新手来说,Debug代码是学习模型结构,了解基本输入输出流的最佳方式。但是多卡程序一般是
python -m torch.distributed.launch
来启动程序,这无法通过常规的Debug方法进行调试。为此需要修改配置文件。
方法
确定环境变量
- 确定你所使用的conda环境,如环境为my_pytorch
- 找到你所使用环境对应的解释器,一般情况下位于
/root/anaconda3/envs/my_pytorch/lib/python3.7/site-package/torch/distributed/launch.py
,注意其中的python
版本号需要根据你的环境更换。当你使用conda create -p ...
创建环境时需要特别注意路径
修改VScode Debug配置文件
点击左侧Debug按钮,并再次点击齿轮打开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": "/root/anaconda3/envs/my_pytorch/lib/python3.7/site-package/torch/distributed/launch.py",
"console": "integratedTerminal",
"justMyCode": true,
"args": [
"--nproc_per_node=1",
"--master_port=12098",
"train1.py",
"--config", "config/*.yaml",
"--other_params", "your_params",
],
"env":{
"CUDA_VISIBLE_DEVICES": "7",
},
}
]
}
- “program” 为第一步查到的解释路径
- “args” 为你需要更改的参数
-
- “–nproc_per_node=1”, 一般来说,debug选择单卡会方便
-
- “–master_port=12098”, 随便设置一个端口号用于通信,不要与之前使用的端口号相同
-
- “train.py”, 训练文件,或者接收参数的文件
-
- “–config”, “config/*.yaml”,
train.py
文件需要的参数
- “–config”, “config/*.yaml”,
-
- “–other_params”, “your_params”,
train.py
文件需要的参数
- “–other_params”, “your_params”,
- ”envs" 你需要选择在哪块GPU进行debug
修改数据加载参数
根据大佬 的说明,VScode调试模型并不能成功的开启新进程,所以我们在数据加载时需要设置num_worksers=0
来避免调试程序卡住。