且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在SWIG界面后调试Tensorflow的C ++代码

更新时间:2023-11-27 12:00:46

TensorFlow的C ++代码在与调用它的Python代码相同的过程中执行(或者如果您使用分布式版本,则以与之相同的过程Python程序创建了一个 tf.GrpcServer )。



Python和C ++之间最简单的界面是pure- C API在 tensor_c_api.h 。要拦截其中一个调用,您可以将 gdb 附加到运行TensorFlow的Python解释器的进程ID中,并在其中一个函数上创建一个断点。 >

例如,使用交互式Python会话,在第一个终端输入:

  $ python 
>>> import tensorflow
>>> import os
>>> os.getpid()
14680

然后,在另一个终端,开始 gdb

  $ gdb -p 14680 
[...]
(gdb)break TF_NewSession
断点1在0x7f15f450a4d0
(gdb)continue
继续。

回到Python解释器,创建一个新的会话:

 >>> sess = tf.Session()

解释器将暂停,您的调试器将打印如下:

  TF_NewSession()中的断点1,0x00007f15f450a4d0来自[...] / tensorflow / python / _pywrap_tensorflow.so $ b $来自[...] / tensorflow / python / _pywrap_tensorflow.so 
#1 0x00007f15f3ac5cdb的_wrap_TF_NewSession()中的b(gdb)backtrace
#0 0x00007f15f450a4d0在TF_NewSession() python / _pywrap_tensorflow.so
#2 0xE000000000049968d在PyEval_EvalFrameEx()
#3 0x000000004a090c在PyEval_EvalCodeEx()
#4 0x00000000499a52在PyEval_EvalFrameEx()
[...]

现在,您可以使用 gdb 的全部功能进行调试TensorFlow。


I'm not sure how to debug (presumably with GDB) the Python code behind a SWIG interface.

I can use ipdb to watch the execution of Tensorflow's Python code all the way to the SWIG wrapper (e.g. tf_session.TF_Run in session.py), but I would like to debug the C++ code behind the SWIG interface.

Presumably I build Tensorflow with bazel build --config debug, but how do I attach gdb to the resulting code when called from the Python interface?

TensorFlow's C++ code executes in the same process as the Python code that calls it (or, if you are using the distributed version, in the same process as one of the Python programs that created a tf.GrpcServer).

The simplest interface between Python and C++ is the pure-C API in tensor_c_api.h. To intercept one of these calls, you can attach gdb to the process ID of the Python interpreter that is running TensorFlow, and create a breakpoint on one of these functions.

For example, using an interactive Python session, in the first terminal enter:

$ python
>>> import tensorflow
>>> import os
>>> os.getpid()
14680

Then, in another terminal, start gdb:

$ gdb -p 14680
[...]
(gdb) break TF_NewSession
Breakpoint 1 at 0x7f15f450a4d0
(gdb) continue
Continuing.

Back in the Python interpreter, create a new session:

>>> sess = tf.Session()

The interpreter will pause, and your debugger will print something like the following:

Breakpoint 1, 0x00007f15f450a4d0 in TF_NewSession () from [...]/tensorflow/python/_pywrap_tensorflow.so
(gdb) backtrace
#0  0x00007f15f450a4d0 in TF_NewSession () from [...]/tensorflow/python/_pywrap_tensorflow.so
#1  0x00007f15f3ac5cdb in _wrap_TF_NewSession () from [...]/tensorflow/python/_pywrap_tensorflow.so
#2  0x000000000049968d in PyEval_EvalFrameEx ()
#3  0x00000000004a090c in PyEval_EvalCodeEx ()
#4  0x0000000000499a52 in PyEval_EvalFrameEx ()
[...]

You can now use the full power of gdb to debug TensorFlow.