##In actual use, we found that when running a python file with a shell script, it may not be possible to debug using pdb and will exit. At this time, you can only run the py file directly for debugging.
3. Use the break command to set a breakpoint at the specified location in the specified file
3.1 Set a breakpoint at the specified location in this file
For example, in the following example, if you want to enter the forward() method of the model to view the data processing process during forward propagation, you can only set a break in the first line of forward() (line 26). Point, pdb.set_trace()
But sometimes the model is very complex, and using this method will cause the program to report an error and exit directly (I don’t know the reason), then we can consider using the break command here Insert a breakpoint in one line so that the program will stop when it reaches forward().
import torchimport torch.nn as nnimport pdbclass EncoderLayer(nn.Module): def __init__(self): super().__init__()
self.conv1 = nn.Conv2d(4, 10, (3, 3))
self.conv2 = nn.Conv2d(10, 4, (3, 3))
self.relu = nn.ReLU() def forward(self, x):
x=self.relu(self.conv1(x)) return self.relu(self.conv2(x))class Encoder(nn.Module): def __init__(self,num_layers): super().__init__() # encoders 由 num_layers个 EncoderLayer子层组成,每个子层结构相同,但参数不一定相同。
self.ModelList = nn.ModuleList([EncoderLayer() for _ in range(num_layers)]) def forward(self, x): # ModuleList是一个list,只能通过list的操作方式(如用for循环、下标索引等)进行forward计算。
for layer in self.ModelList:
x = layer(x) return xif __name__=="__main__":
pdb.set_trace()
input = torch.rand(5, 4, 30, 30)
model = Encoder(num_layers=4)
output = model(input)
Specific method:
(1) First set pdb.set_trace() on any previous line to stop the program.
(2) Just enter break 26. As shown in the picture:
In this way, the breakpoint is set successfully, and the program will stop when it reaches forward().
The 26 here is the number of lines. It should be noted that the breakpoint position cannot be a comment . For example, if we set a breakpoint on line 25 (comment line), it will fail:
To summarize, the command to set a breakpoint in the same file is:
break line
3.2 In others Set a breakpoint at a specified location in the file
If the breakpoint you want to set is not in the initial run file, how can you use the break command to set a breakpoint in other files? Let’s look at this example:
Divide the 3.1 code into three py files and put them under the same path:
![Summary of usage of python debugging tool pdb (Python Debugger)](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4b5d476ba5b14b0ba541d78930b9704a~tplv-k3u1fbpfcp-zoom-1.image)
Take a look at the contents of each file:
run.py:
The initial pdb.set_trace() setting is in run.py.
import torchfrom encoder import Encoderimport pdbif __name__=="__main__":
pdb.set_trace() input = torch.rand(5, 4, 30, 30)
model = Encoder(num_layers=4)
output = model(input)
encoder.py:
from encoder_layer import EncoderLayerimport torch.nn as nnclass Encoder(nn.Module): def __init__(self,num_layers): super().__init__() # encoders 由 num_layers个 EncoderLayer子层组成,每个子层结构相同,但参数不一定相同。
self.ModelList = nn.ModuleList([EncoderLayer() for _ in range(num_layers)]) def forward(self, x): # ModuleList是一个list,只能通过list的操作方式(如用for循环、下标索引等)进行forward计算。
for layer in self.ModelList:
x = layer(x) return x
encoder_layer.py:
import torch.nn as nnclass EncoderLayer(nn.Module): def __init__(self): super().__init__()
self.conv1 = nn.Conv2d(4, 10, (3, 3))
self.conv2 = nn.Conv2d(10, 4, (3, 3))
self.relu = nn.ReLU() def forward(self, x):
x=self.relu(self.conv1(x)) return self.relu(self.conv2(x))
Now we run run.py and then Set a breakpoint on line 12 of encoder.py, that is,
for layer in self.ModelList:
The command is:
break encoder.py:12
That is, break filename: lineWe can see that the program can enter forward() from output = model(input):This is very convenient to debug.
If the initial breakpoint and the target breakpoint are not in the file in the same directory, you can also set the breakpoint through the file name under the relative path, such as:
(Pdb) break ../transformer/asr_model.py:91Breakpoint 1 at /local/wenet/examples/aishell/s0/wenet/transformer/asr_model.py:91(Pdb)
4. When using pdb Problems discovered
4.1 When using soft links, the file path displayed by pdb is inconsistent with the actual path
As shown in the figure, pdb It consists of three lines. The first line is the file path, the second line is the currently executed code line, and the third line is the input command line.
When there is a soft link, the path displayed by pdb is the path pointed by the soft link, but the actual code path is the path that copies the content of the soft link. These two paths are different, so you must pay attention.
4.2 pdb sometimes cannot add breakpoints in the forward() method of the model
pdbSometimes pdb cannot be used. set_trace() adds a breakpoint to the forward() method of the model. The error message is:
Compiled functions can't take variable number of arguments or use keyword-only arguments with defaul
Probably means "The compiled function cannot accept a variable number of parameters, nor can it use keyword-only parameters in default."
I don't understand what it means, and this problem has not been solved.
5. Post-debugging after the program crashes: pdb.pm()
As mentioned above, breakpoints are inserted when the program starts running, and pdb is used for debugging, that is, Predebug. In fact, pdb can also be used for post-mortem debugging, that is, after the program has a bug and crashes, use the python debugger to view it.
For example, test.py is obviously buggy:
# test.pydef add(n): return n+1add("hello")
Run directly:
python test.py
The program runs Collapse:
F:\PycharmProjects\pytorch_practice>python test.py
Traceback (most recent call last):
File "test.py", line 4, in <module>
add("hello")
File "test.py", line 2, in add
return n+1
TypeError: can only concatenate str (not "int") to str</module>
In this way we cannot use pdb for debugging. So when the program crashes, how should we debug it?
We can use the following command for simple debugging:
python -i test.py
-i option allows you to open a Interactive shell, as follows:
F:\PycharmProjects\pytorch_practice>python -i test.py
Traceback (most recent call last):
File "test.py", line 4, in <module>
add("hello")
File "test.py", line 2, in add
return n+1
TypeError: can only concatenate str (not "int") to str
>>></module>
现在我们发现程序结束后出现了 >>> 符号,这就是python调试器。
输入命令:
import pdb
pdb.pm()
其中 pdb.pm() 用于程序发生异常导致奔溃后的事后调试,可以跟踪异常程序最后的堆在信息。
执行命令后得到:
TypeError: can only concatenate str (not "int") to str
>>> import pdb
>>> pdb.pm()
> f:\pycharmprojects\pytorch_practice\test.py(2)add()
-> return n+1
(Pdb)
可以发现,pdb.pm() 已经追踪到了导致程序奔溃的语句:return n+1
此时可以打印 n 的值进行检查:
(Pdb) p n'hello'(Pdb) q>>> quit()
F:\PycharmProjects\pytorch_practice>
q 表示退出pdb调试,quit() 表示退出 python 调试器。
【相关推荐:Python3视频教程 】