Pytorch's C++ part debug

Talk about the debug function of VSCode for C++ code

tasks.json is not required every time

If we just want to use VSCode's debug window to debug the executable file we have generated, then we do n't need it at alltasks.json . This file is a help file for compilation. tasks.jsonAfter setting the parameters, execute the set gcc command Wait to compile. If we already have compiled, we don't need this thing.

We only need launch.jsonfiles, the following is an example I configured:

{  
    "version": "0.2.0",  
    "configurations": [  
        {  
            "name": "debug",  
            "type": "cppdbg",  
            "request": "launch",  
            "program": "path/to/bin",  
            "args": [  
                "--model-repository=/test_model_repository_debug/centernet-trt-ensemble",  
                "--http-port=8007",  
                "--cuda-memory-pool-byte-size=0:134217728"  
            ],  
            "stopAtEntry": false,  
            "cwd": "${workspaceFolder}",  
            "environment": [{"name":"LD_LIBRARY_PATH", "value":"${LD_LIBRARY_PATH}:/usr/local/cuda/lib64:/home/re2/obj/so"},  
                            {"name":"CUDA_VISIBLE_DEVICES","value": "4"}],  
            "externalConsole": false,  
            "MIMode": "gdb",  
            "setupCommands": [  
                {  
                    "description": "Enable pretty-printing for gdb",  
                    "text": "-enable-pretty-printing",  
                    "ignoreFailures": true  
                }  
            ],  
            "miDebuggerPath": "/usr/bin/gdb",  
            "logging": { "engineLogging": true }  
        }  
    ]  
}  

Let's analyze some of our commonly used places:

  • remove preLaunchTask": "g++", because we don't needtasks.json

  • "request": "launch"Generally launch, if you need to capture the process for attach, set it to attach, you can see how to "deep" DEBUG for Pytorch

  • "program": "path/to/bin"The address of the compiled executable file

  • "args": [ ],Command line parameters, how to write the specific look at the top

  • "environment": [{"name":"CUDA_VISIBLE_DEVICES","value": "4"}],Environment variable, if our executable file needs to set an environment variable, modify this, modify the format and see the example above

Others that are not commonly used will not be introduced. If you want to know more, you can refer to the official document https://code.visualstudio.com/docs/editor/debugging.

 debugging_hero

Debug all the way from python to C++

In addition to debugging C++, there are also some libraries that are a combination of C++ and python (such as Pytorch, TVM), which is equivalent to a layer of python wrapped around the C++ core. Encountered this kind of software library, or we need to build a similar software library, how to debug in this situation?

Our debugging of Pytorch is generally performed on the python side, which is sufficient for the general task of building models. But if we need to make some modifications to Pytorch or study how the machine or deep learning system is built, we must involve the source code level of C++ if we want to explore in depth. For example, for torch.rand(3, 4)this function, in Python, we cannot enter its internal implementation through python-side debug, nor can we find its definition, and naturally we cannot explore its specific implementation details. Therefore, in order to better explore and debug Pytorch, It is necessary to debug the C++ part of Pytorch.

Preparation

First of all, we need Pycharm+VSCODE (linux side), and of course python environment and gdb (this is generally available), then create a virtual environment and compile the source code of Pytorch. Since we want to debug the source code of Pytorch, first we need to compile the source code of Pytorch. When compiling, you need to modify the DEBUG environment variable, and compile the Debug version of pytorch. The command is DEBUG=1 python setup.py install, for more detailed compilation steps, see the following article, and I won’t go into details here:

  • A Concise Guide to Pytorch Source Code Compilation

After compiling Pytorch, we use VSCODE to open the Pytorch directory, open the entire project file, and then click the debug icon on the left to start debugging for the first time, VSCODE will prompt us to add a configuration file, which is in the launch.jsondirectory .vscode.

Then we modify launch.jsonthe file, mainly to modify the program column to the path of the python interpreter, and do not change the others:

"name": "(gdb) Attach",  
"type": "cppdbg",  
"request": "attach",  
"program": "/home/prototype/anaconda3/envs/pytorch/bin/python", -- 修改这一栏为你执行pytorch的python路径  
"processId": "${command:pickProcess}",  
"MIMode": "gdb",  
"setupCommands": [  
    {  
        "description": "Enable pretty-printing for gdb",  
        "text": "-enable-pretty-printing",  
        "ignoreFailures": true  
    }  
]  
Start Debugging

The principle of our debugging is:

  • First run the python code to get the currently running id process number

  • Secondly, capture the process number through gdb and then debug the C++ code

Because there is a capture process, in order to prevent the program from running through the breakpoint we set in C++ before we capture, we need to first add a sleep statement to the .py file we want to run and let the program fly in the air After a while, I usually decide the time myself time.sleep(50). In addition, set a breakpoint in the C++ code you want to break in advance, and click on the line of code you want to break in VScode to set it. Then run the pytorch code in debug mode (click the debug button in pycharm), and you can see that the process at this time is 28536 in the console .

Check the process number

Click debug in VSCODE, which we have set up before:  click the small green dot

At this time, enter our previous process number to attach. Note that the system may require root privileges at this time, just enter y to confirm. Enter the process number

After waiting for a while (the time of time.sleep), we can see that the program stopped at the breakpoint we set. At this moment, we have entered the C++ backend from the python frontend of pytorch: we have locked the C++ source code 

The debug information is as follows: whaosoft  aiot  http://143ai.com  

GNU gdb (Ubuntu 8.2-0ubuntu1~16.04.1) 8.2  
Copyright (C) 2018 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-linux-gnu".  
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".  
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.  
=cmd-param-changed,param="pagination",value="off"  
[New LWP 15524]  
[New LWP 15525]  
[New LWP 15528]  
[Thread debugging using libthread_db enabled]  
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".  
0x00007fe9efc605d3 in select () at ../sysdeps/unix/syscall-template.S:84  
Loaded '/lib/x86_64-linux-gnu/libpthread.so.0'. Symbols loaded.  
Loaded '/lib/x86_64-linux-gnu/libc.so.6'. Symbols loaded.  
Loaded '/lib/x86_64-linux-gnu/libdl.so.2'. Symbols loaded.  
Loaded '/lib/x86_64-linux-gnu/libutil.so.1'. Symbols loaded.  
Loaded '/lib/x86_64-linux-gnu/librt.so.1'. Symbols loaded.  
Loaded '/lib/x86_64-linux-gnu/libm.so.6'. Symbols loaded.  
Loaded '/lib64/ld-linux-x86-64.so.2'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_heapq.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_opcode.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/zlib.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/../../libz.so.1'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_bz2.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_lzma.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/../../liblzma.so.5'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/grp.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_posixsubprocess.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/select.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/math.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_hashlib.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/../../libcrypto.so.1.1'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_blake2.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_sha3.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_bisect.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_random.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_struct.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/binascii.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_socket.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_datetime.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_ssl.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/../../libssl.so.1.1'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/../../libffi.so.6'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/_mklinit.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/../../../libmkl_rt.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/../../../libiomp5.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/bin/../lib/libgcc_s.so.1'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/core/_multiarray_umath.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_pickle.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/core/_multiarray_tests.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/linalg/lapack_lite.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/linalg/_umath_linalg.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_decimal.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/fft/fftpack_lite.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/mkl_fft/_pydfti.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/random/mtrand.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/../../../libmkl_core.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/../../../libmkl_intel_thread.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/../../../libmkl_intel_lp64.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/../../../libmkl_avx512.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/numpy/../../../libmkl_vml_avx512.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_json.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/pyexpat.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/fcntl.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/termios.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/resource.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/array.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_multiprocessing.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_asyncio.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_sqlite3.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/../../libsqlite3.so.0'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/unicodedata.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/lib-dynload/_lsprof.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/_C.cpython-36m-x86_64-linux-gnu.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/lib/libshm.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/lib/libtorch_python.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/libstdc++.so.6'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/lib/libtorch.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/lib/libc10.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/libmkl_gnu_thread.so'. Symbols loaded.  
Loaded '/home/prototype/anaconda3/envs/pytorch/lib/libgomp.so.1'. Symbols loaded.  
Loaded '/usr/lib/libmpi_cxx.so.1'. Symbols loaded.  
Loaded '/usr/lib/libmpi.so.12'. Symbols loaded.  
Loaded '/usr/lib/x86_64-linux-gnu/libnuma.so.1'. Symbols loaded.  
Loaded '/usr/lib/libibverbs.so.1'. Symbols loaded.  
Loaded '/usr/lib/libopen-rte.so.12'. Symbols loaded.  
Loaded '/usr/lib/libopen-pal.so.13'. Symbols loaded.  
Loaded '/usr/lib/x86_64-linux-gnu/libhwloc.so.5'. Symbols loaded.  
Loaded '/usr/lib/x86_64-linux-gnu/libltdl.so.7'. Symbols loaded.  
[Switching to thread 4 (Thread 0x7fe9de0e0700 (LWP 15528))](running)  
=thread-selected,id="4"  
  
Thread 1 "python" hit Breakpoint 4, torch::autograd::THPVariable_rand (self_=0x0, args=0x7fe9df2cff08, kwargs=0x0) at ../torch/csrc/autograd/generated/python_torch_functions.cpp:8134  
8134   }, /*traceable=*/true);  
Execute debugger commands using "-exec <command>", for example "-exec info registers" will list registers in use (when GDB is the debugger)  

You can see that we hit Breakpoint 4 successfully . At this point, you can happily debug Pytorch with VSCODE. The debugging method at this time is no different from the previous debugging of C++ alone.

Guess you like

Origin blog.csdn.net/qq_29788741/article/details/131745717