PyTorch C++源码调试

4 篇文章 0 订阅

PyTorch C++源码调试


环境:

  • PyTorch源码:截止到(2021-6-25)源码,编译后torch.__version__=1.10.0a0+gitd03ff1a
  • CPU编译:(因为需要debug 模块的C++运行逻辑,这里只编译了CPU支持)
  • 系统:5.10.18-1-MANJARO。需要注意的是manjaro滚动更新,当你目前滚动升级了,你的gcc版本将为11,cuda的编译将不支持。不过你要是编译CPU版本应该无所谓,我没有试过,保持的gcc还是10.2。

1.编译PyTorch源码

源码目录下执行:

export DEBUG=1
export USE_DISTRIBUTED=0 
export USE_MKLDNN=1 
export USE_CUDA=0 
export BUILD_TEST=0 
export USE_FBGEMM=1 
export USE_NNPACK=0 
export USE_QNNPACK=0
python setup.py develop

2.Debug 流程

1.使用Python编写 PyTorch 代码
2.python运行你写好的py代码,需要使用debug模式(让程序在进程在系统中保留,你可以用pdb.set_trace)实现或者用time.sleep()让程序休眠,以获取其进程号。不过我更加喜欢的是直接-m,即:python -m pdb test.py
3.gdb连接这个运行的python程序进程。

3. 实际演示(VSCode调试)

需要debug的代码段:

import torch
import numpy as np
from torch import nn
from os.path import *
import pdb
# 设置debug模式
pdb.set_trace()

input_num = 16
hidden_num = 32
layer_nums = 2
torch.manual_seed(1)
lstm_path = 'lstm.pth'
if not exists(lstm_path):
    lstm = torch.nn.LSTM(input_size=input_num,
                         hidden_size=hidden_num,
                         num_layers=layer_nums,
                         bias=True,
                         batch_first=True)
    torch.save(lstm_path, lstm)

lstm = torch.load(lstm_path)

config = {
    'batch_first': lstm.batch_first,
    'is_bias': lstm.bias,
    'bidirectional': lstm.bidirectional,
    'dropout': lstm.dropout,
    'num_layers': lstm.num_layers,
    'hidden_size': lstm.hidden_size,
    'input_size': lstm.input_size
}

weights = load_weights(lstm)
weights_np = {key: tensor_to_numpy(weights[key]) for key in weights}

batch = 1
current = torch.randn(layer_nums, batch, hidden_num)
current_np = tensor_to_numpy(current)
hidden = torch.randn(layer_nums, batch, hidden_num)
hidden_np = tensor_to_numpy(hidden)
input_data = torch.rand(batch, hidden_num, input_num)
input_data_np = tensor_to_numpy(input_data)
# debug 代码
torch_output, (torch_hidden, torch_cell) = lstm(input_data, (hidden, current))

运行此代码后在pdb.set_trace()部分暂停。进程暂停,然后使用vscode连接此进程:
设置vscode连接进程:

  1. 配置vscode debug模式(唯一需要修改program为你的python路径,如果你有多个python路径,你需要填写包含有你要debug torch的那个):
{
    // 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": "(gdb) 附加",
        "type": "cppdbg",
        "request": "attach",
        "program": "/home/bleedingfight/miniconda3/bin/python",
        "processId": "${command:pickProcess}",
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "为 gdb 启用整齐打印",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    },
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}
  1. f5填写你需要调试的python代码的进程号,你可以直接输入你的程序名称,vscode将自动给你对应找到其进程号,或者你可以ps aux|grep python找到对应的进程号。这时候你可能会看到vscode已经连接进程但是无法step into。其按钮都是灰色的,这是因为pdb停止的位置还没有到调用C++代码的位置,你需要切换到python代码,next直到运行此代码然后到C++代码(C++代码部分需要提前打好断点)。你可以打开c++源文件:/home/bleedingfight/pytorch/aten/src/ATen/native/RNN.cpp,在1507行打上断点:
    在这里插入图片描述
    然后你就可以在vscode里面愉快的调试了。

注:

网络上有很多方式,说一说我的测试(假设python代码为file.py):

  1. vscode同时debug:通过上面的launch配置,可以调试python和进程。你需要在vscode下Debug python代码,然后停在某一位置之后调试进程,然后继续切换到python调试。这个方法本质上和我上面说的方法是一致的。不过需要注意的是不能直接通过文件查找进程,这是因为debug的时候会有一个两个file.py的文件,如下:
  • /home/liushuai/miniconda3/bin/python /home/liushuai/.vscode/extensions/ms-python.python-2021.6.944021595/pythonFiles/lib/python/debugpy/launcher 33429 -- xxx/file.py
  • pythonFiles/lib/python/debugpy --connect 127.0.0.1:40327 --configure-qt none --adapter-access-token 484c98d174b24a263ebabe7865655ea904daa87ca9c859c4de6c4ce35a5121f7 xxx/file.py
    第一个进程是vscode launch进程,就是你用来配置debug通过f5启动python调试的进程。debug的进程是下一个,如果你直接输入file.py 展示框太短不容易看到究竟调试的是哪一个。你应该调试的是第二个。你可以使用 ps aux|grep python|grep file.py查看到类似上面的信息。通过输入进程号确定你需要debug的程序。
  1. 还有用pycharm debug 你的python代码,然后同样使用vscode在CPP上打断点的方式,事实上你要是理解这个流程,你就知道debug无论是pdb还是借用ide的debug或者直接使用vscode的debug本质上都是一样的,没有必要启动一个pycharm去调试。除非你就是喜欢用pycharm和其提供的工具。
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值