vscode 如何debug python torchrun deepspeed[自用,防忘记]

⚠️ 写在前面(一定要看)

  1. debug程序的方式有很多种。每一种方式都各有缺点:有的方式虽然优雅,但是局限性很大;有的方式麻烦,但是局限性小。
    • 常规方式:
      • 优点:然后可以观察所有线程。一劳永逸。
      • 缺点:就是写参数很麻烦,但是你可以让chatgpt等大模型帮你写。
    • 最最最优雅的方式:
      • 优点:就是需要在代码里面,加入几行代码。方便快捷。
      • 缺点:有时候断点不生效,只能在一个线程里面启动。
  2. 建议先使用【常规形式】、如果【常规形式】不够用,再使用【最最最优雅的方式】

B站手把手视频

  1. 常规方式:https://www.bilibili.com/video/BV1Hh4y1i7Li
  2. 最最最优雅的方式:https://www.bilibili.com/video/BV1wt421V718

最优雅的方式

安装

  1. 安装包 pip install debugpy -U
  2. 安装vscode关于python的相关插件

写配置

一般情况下,大家都是使用deepspeed、torchrun运行代码。参数都特别多,然后都是使用sh xxxx.sh启动脚本。

在python代码里面(最前面加上这句话)

1
2
3
4
5
6
7
8
9
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里面,加上这个配置

1
2
3
4
5
6
7
8
9
10
{
"name": "sh_file_debug",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 9501
}
},

🚨 上面的端口号都写一样。别搞错了。

启动

  1. 就正常启动,直接sh xxx.sh
  2. 在你需要debug的python文件,打上debug断点。
  3. 你看打印出来的东西,是不是出现Waiting for debugger attach.一般来说,都很快,就出现了。
  4. 再在vscode的debug页面,选择sh_file_debug进行debug。
  5. 就基本上完成了。确实是很方便。
  6. debug结束之后,别忘记把代码里面的 添加的代码,注销掉

参考仓库

参考仓库