Model Debugging
If you encounter issues during training, use the following methods for debugging to identify the location and cause of the problematic code.
Print-based Locating
Use print to determine the code location where the model issue occurs. Since the PyTorch framework is an asynchronous execution framework, direct printing may result in inaccurate locations, requiring the addition of stream synchronization logic to assist with print-based debugging.
print(torch.npu.synchronize(),"debug message")
print(inputs.shape, inputs.dtype) # The parameters to print can be changed based on the actual situation
Note
This method is suitable for problem location scenarios and can be removed during formal training.
pdb Breakpoint Debugging
Add a function at the location where a breakpoint needs to be set.
-
Method 1: Import the pdb module in the code and add the
set_tracefunction at the location where a breakpoint needs to be set to trigger the debugger.import pdb pdb.set_trace()The script will pause when it reaches
pdb.set_trace(). You can proceed by entering commands in the command line. For example, enteringnexecutes the next line of code, and enteringp {variable_name}prints the current value of a variable. For more parameter descriptions, see Common pdb Arguments.The following figure shows the effect of setting pdb in the training loop and attempting to print the value of the variable
loss.
-
Method 2: Add the
breakpointfunction at the location where you want to set a breakpoint. The script will pause when it reaches this point.breakpoint()The following image shows the effect of setting a breakpoint within the training loop.
GDB Command-line Debugging
The main functions of the GDB debugging tool include setting breakpoints in a program, monitoring variables, single-step execution, changing variable values at runtime, tracing paths, and switching threads. This method is primarily used for core dump scenarios. A core dump file is generated in the execution directory. Use GDB to debug this file and print the stack trace. The detailed steps are as follows:
-
Install GDB by referring to the official GDB documents.
-
Set up core dump file generation via
ulimit.ulimit -c # Check the current setting. 0 indicates that no core dump file will be generated, and the ulimit setting needs to be changed. ulimit -c unlimited # Setting to unlimited removes the size limit for core dump files. If the process crashes, a core dump file will be generated. -
Set the storage location and name of the core dump file.
# Temporarily modify the name of the generated core dump file. The variable placeholders can be configured as needed. sysctl -w kernel.core_pattern=core-%e.%p.%h.%t # Set the core dump generation directory. The variable placeholders and the generation directory can be configured as needed. echo "/path/to/core/dump/core.%e.%p" >/proc/sys/kernel/core_patternThe variables that can be used in the
core.patterntemplate are shown in Table 1.Table 1 Variables available in the
core.patterntemplateName Description %% A single % character %p Process ID of the dumped processes %u Real user ID of the dumped processes %g Real group ID of the dumped processes %s Signal that caused the core dump %t Time of the core dump (seconds since January 1, 1970) %h Host name %e Program file name -
Generate the core dump file.
Run the model script. If the model reports an error or the process crashes, a core dump file will be generated in the configured directory.
-
Debug the core dump file.
Execute the following command to enter GDB mode and debug the core dump file.
gdb python3 core*.* # Modify the core dump file name according to the actual situationAfter executing the command, the GDB tool will print the code where the exception occurred, its function, file name, and line number to the screen, making it easier to locate the problem. A sample output is shown in the following figure.
Common debugging commands in GDB mode are shown in Table 2.
Table 2 Common debugging commands in GDB mode
Command Description l List code break num Set a breakpoint at a line, where num is the sample line number info break View breakpoint information r Run the program n Execute a single command c Continue execution p paramname Print a variable bt View the function stack finish Exit the function q Quit GDB For more debugging commands, refer to the official documents.
Hook-based Locating
This method is primarily used to locate errors in a specific module within the model, and is generally suitable for locating errors in forward and backward modules.
Add the following code to the script to define the hook.
def hook_func(name, module):
def hook_function(module, inputs, outputs):
print(name)
return hook_function
Add the following hook code before the code executes model computation to print the module names during the model's forward and backward execution. After locating the specific issue, print all shapes, dtypes, and formats, and then use host logs to assist in troubleshooting.
for name, module in model.named_modules():
if module is not None:
module.register_forward_hook(hook_func('[forward]:' + name, module))
module.register_full_backward_hook(hook_func('[backward]:' + name, module))
The following figure shows the log output after adding the hook function. You can see that the name of each module in the model is printed. Users can also customize the content to be printed by modifying the hook_func function.
