I managed to use pdb to explore python source code of pytorch, but I want to explore lower level code written in C/C++.
for example, to explore F.conv2d, with pdb I can locate
50 -> f = ConvNd(_pair(stride), _pair(padding), _pair(dilation), False,
51 _pair(0), groups, torch.backends.cudnn.benchmark, torch.backends.cudnn.enabled)
then I can locate ConvNd
at ConvNd = torch._C._functions.ConvNd
in L19, then pdb can’t help me go further to explore C/C++ code underneath pytorch, so with help from @albanD, I can see ConvNd at a cpp file L248 , but pdb can no longer help me explore functions and values of C/C++ codes.
I have managed to install gdb and am able to debug C code with it. However, I can’t make it work with pytorch code snippet. Below is the code snippet and my attempts of gdb to explore pytorch code from python level to C/C++ level.
import sys
import torch # numpy
from torch.autograd import Variable # tensor with gradient
import torch.nn as nn # all layers classes
import torch.nn.functional as F # other functions for building model
class Net(nn.Module): # nn.Module里面到底有什么?
def __init__(self):
super(Net, self).__init__() # Module init 到底做了什么?
self.conv1 = nn.Conv2d(1, 6, 5)
self.conv2 = nn.Conv2d(6, 16, 5)
# an affine operation: y = Wx + b
self.fc1 = nn.Linear(16 * 5 * 5, 120) # nn.Linear 在构建什么?
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2)) # 看看F.relu的代码逻辑
x = F.max_pool2d(F.relu(self.conv2(x)), 2) # 看看F.max_pool2d代码逻辑
x = x.view(-1, self.num_flat_features(x)) # 在Net.forward里面调用Net.num_flat_features
x = F.relu(self.fc1(x))# 如何使用Net.forward 和 Net.num_flat_features
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def num_flat_features(self, x): #
size = x.size()[1:]
num_features = 1
for s in size:
num_features *= s
return num_features
net = Net()
single_sample = torch.randn(32,32).unsqueeze(0).unsqueeze(0)
inputs = Variable(torch.randn(1, 1, 32, 32))
output = net(inputs)
conv1_weight_1 = list(net.parameters())[0][0]
target = Variable(torch.arange(1, 11))
criterion = nn.MSELoss()
loss = criterion(output, target)
net.zero_grad() # zeroes the gradient buffers of all parameters
net.conv1.weight.grad
import torch.optim as optim
optimizer = optim.SGD(net.parameters(), lr=0.01)
optimizer.zero_grad() # optimizer.zero_grad() == net.zero_grad()
loss.backward() # 参数的导数,从无到有
optimizer.step() # 只是更新参数,不输出任何值
net.conv1.bias # 展示某层的参数
net.conv1.weight.grad # 展示某层的导数
net.conv1.zero_grad() # 将某层的导数归0
my attempt 1:
Focus on one: /Users/Natsume/Documents/shendusuipian/pytorch/raw_pytorch/60min_intro
->gdb python3 03neural_networks_tutorial.py
GNU gdb (GDB) 8.0.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin16.7.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from python3...(no debugging symbols found)...done.
Illegal process-id: 03neural_networks_tutorial.py.
"/Users/Natsume/Documents/shendusuipian/pytorch/raw_pytorch/60min_intro/03neural_networks_tutorial.py" is not a core dump: File format not recognized
my attempt 2:
Focus on one: /Users/Natsume/Documents/shendusuipian/pytorch/raw_pytorch/60min_intro
->gdb --args python3 03neural_networks_tutorial.py
GNU gdb (GDB) 8.0.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin16.7.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from python3...(no debugging symbols found)...done.
(gdb) start
Function "main" not defined.
Starting program: /usr/local/bin/python3 03neural_networks_tutorial.py
[New Thread 0x1403 of process 2031]
warning: unhandled dyld version (15)
[New Thread 0x1503 of process 2031]
Thread 3 received signal SIGTRAP, Trace/breakpoint trap.
[Switching to Thread 0x1503 of process 2031]
0x0000000100005000 in ?? ()
(gdb) next
Cannot find bounds of current function
(gdb)
Is it possible to use pdb or gdb to explore pytorch code from python level all the way down to C/C++ level?
If so, how could I do it with the code snippet provided above?
if not, how would you explore pytorch code from python level all the way down to C/C++ level? Could you give me a guided map? ( I have started to learn C/C++ already)
Thank you very much!