标题调试python嵌入的c++代码,例如
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])
#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配置
{
"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
}
]
}
]
}
或者这种简单配置也行
{
// 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启动脚本是这样
#!/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
{
"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 里先激活虚拟环境
{
"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代码
#!/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
{
// 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++程序
{
"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
{
"files.associations": {
"thread": "cpp"
}
}