vscode里几种程序调试配置

标题调试python嵌入的c++代码,例如

python 复制代码
import torch
from torch.utils.cpp_extension import load

test_load = load(
    name='test_load', 
    sources=['test.cpp'],
    extra_cflags=['-O0', '-g'],
    #extra_cflags=['-O1'],
    verbose=True,
)
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
result = test_load.add(a, b)
print(result)  # Should print tensor([5, 7, 9])
cpp 复制代码
#include <torch/extension.h>

// 定义一个简单的加法函数
at::Tensor add(at::Tensor a, at::Tensor b) {
    int size = a.size(0);
    int dim = a.dim();
    printf("size: %d, dim: %d\n", size, dim);
    return a + b;
}

PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
    m.def("add", &add, "Add two tensors");
}

launch.json配置

python 复制代码
{
    "version": "0.2.0",
    "configurations": [																																																																																																																																																																																																																																																																																																																																																																																																																		
        {
            "name": "(gdb) Launch test",
            "type": "cppdbg",
            "request": "launch",
            "program": "/root/miniconda3/bin/python",
            "args": ["test.py"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

或者这种简单配置也行

python 复制代码
{
    // 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 test",
            "type": "cppdbg",
            "request": "launch",
            "program": "/root/miniconda3/bin/python",
            "console": "integratedTerminal",
            "args": ["test.py"],
            "cwd": "${workspaceFolder}",
            "MIMode": "gdb",
            "env": {
                "PATH": "/root/miniconda3/bin/:$PATH",
                "LD_LIBRARY_PATH": "$PWD:extensions/aec/lib/:$LD_LIBRARY_PATH",
                "CUDA_VISIBLE_DEVICES": "0,1,2,3,4,5,6,7"
            }
        }
    ]
}

标题调试torchrun启动代码

bash启动脚本是这样

powershell 复制代码
#!/bin/bash
. /root/miniconda3/etc/profile.d/conda.sh
conda activate /root/miniconda3
export LD_LIBRARY_PATH=$PWD:extensions/lib/:/myso:$LD_LIBRARY_PATH
export PATH=$PWD:extensions/lib/:$PATH
CUDA_VISIBLE_DEVICES=0,1,2,3 torchrun --nproc_per_node=4 --master_port=25642 debug.py

调试配置需要先配置tasks.json激活环境,然后配置launch.json

launch.json

python 复制代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: noise_gen_debug",
            "type": "python",
            "request": "launch",
            "program": "/root/miniconda3/bin/torchrun",
            "args": [
                "--nproc_per_node=1",
                "--master_port=25642",
                "debug.py"
            ],
            "console": "integratedTerminal",
            "env": {
                "LD_LIBRARY_PATH": "${workspaceFolder}:${workspaceFolder}/extensions/lib/:/root/myso:${env:LD_LIBRARY_PATH}",
                "CUDA_VISIBLE_DEVICES": "0"
            }
        }
    ]
}

tasks.json 里先激活虚拟环境

python 复制代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "setup-conda",
            "type": "shell",
            "command": "source /root/miniconda3/etc/profile.d/conda.sh && conda activate /root/miniconda3"
        }
    ]
}

标题调试python启动ddp代码

powershell 复制代码
#!/bin/bash
export PATH=/root/miniconda3/bin/:$PATH

export LD_LIBRARY_PATH=$PWD/extensions/lib/:$LD_LIBRARY_PATH
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
python xspeech/bin/ddp_train_debug.py --train_conf=conf/test.yaml --data_conf=conf/test.cfg --gpus=1
python 复制代码
{
    // 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: Debug ddp_train_test.py",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/xspeech/bin/ddp_train_test.py",
            "console": "integratedTerminal",
            "python": "/root/miniconda3/bin/python",
            "args": ["--train_conf=conf/test.yaml", "--data_conf=conf/test.cfg", "--gpus=1"],
            "justMyCode": false,
            "env": {
                "PATH": "/root/miniconda3/bin/:$PATH",
                "LD_LIBRARY_PATH": "$PWD:extensions/lib/:$LD_LIBRARY_PATH",
                "CUDA_VISIBLE_DEVICES": "0,1,2,3,4,5,6,7"
            }
        }
    ]
}

标题调试已经编译好的c++程序

yaml 复制代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/test-debug",
            "args": [ "model.pkg", "test.conf", "data/test.lst", "10", "2", "1"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

settings.json

yaml 复制代码
{
    "files.associations": {
        "thread": "cpp"
    }
}
相关推荐
酷虎软件1 小时前
分享链接+视频音频文案提取 API 接口文档
ide·macos·xcode
HhzZzzzz_9 小时前
萨科微Slkor2026年7月10日每日芯闻。
人工智能·智能手机·编辑器
p似笑非笑11 小时前
在Mac上完美配置VSCode的C/C++开发环境(GCC/G++详细教程)
c语言·vscode·macos
daad77716 小时前
记录vscode连接matlab启动环境搭建
java·vscode·matlab
我命由我123451 天前
Android Studio - Android Studio 自定义预览尺寸
android·java·ide·java-ee·android studio·android-studio·android runtime
小林ixn1 天前
Node.js 文件系统与路径处理:从 API 到工程化实战
node.js·编辑器·vim
Luoxi_81 天前
Anaconda超详细的安装教程+VScode的使用
ide·vscode·编辑器
球king1 天前
CC GUI 插件:在 IDEA 中使用 CodeX
java·ide·intellij-idea
大龄牛码2 天前
从模型基座到工程范式:GPT-5.6与Grok 4.5在主流AI IDE中的架构适配与选型分析
ide·人工智能·gpt
无糖冰可乐212 天前
安装wsl2,并链接Windows上的vscode运行项目
pytorch·vscode·python·pip