在SWIG界面后调试Tensorflow的C ++代码 [英] debugging Tensorflow's C++ code behind the SWIG interface

查看:1456
本文介绍了在SWIG界面后调试Tensorflow的C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何调试(大概是GDB)SWIG界面后的Python代码。



我可以使用 ipdb 将Tensorflow的Python代码一直执行到SWIG包装器(例如 tf_session.TF_Run session.py ),但是我想调试SWIG界面后的C ++代码。



大概我用 bazel build构建Tensorflow --config debug ,但是从Python界面调用时,如何将 gdb 附加到生成的代码?

解决方案

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.

这篇关于在SWIG界面后调试Tensorflow的C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆