安装
- 安装包
pip install debugpy -U
- 安装vscode关于python的相关插件
写配置
一般情况下,大家都是使用deepspeed、torchrun运行代码。参数都特别多,然后都是使用sh xxxx.sh
启动脚本。
例如
deepspeed --num_nodes=1 --num_gpus=2 \
testcode003.py \
--deepspeed "./config/default_offlload_zero2.json" \
--model_name_or_path my_model \
--output_dir hhh
在python代码里面(最前面加上这句话)
import debugpy
try:
# 5678 is the default attach port in the VS Code debug configurations. Unless a host and port are specified, host defaults to 127.0.0.1
debugpy.listen(("localhost", 9501))
print("Waiting for debugger attach")
debugpy.wait_for_client()
except Exception as e:
pass
在vscode的launch.json的configuration里面,加上这个配置
{
"name": "sh_file_debug",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 9501
},
"justMyCode": false// 表示不只是我自己写的代码,还有库的可以追入
},
🚨 上面的端口号都写一样。别搞错了。
启动
- 就正常启动,直接
sh xxx.sh
- 在你需要debug的python文件,打上debug断点。
- 你看打印出来的东西,是不是出现
Waiting for debugger attach
. - 再在vscode的debug页面,选择
sh_file_debug
进行debug。 - 就基本上完成了。确实是很方便。
- debug结束之后,别忘记把代码里面的 添加的代码,注销掉