Tensorflow Debug with CDT

原创文章,转载请注明: 转载自慢慢的回味

本文链接地址: Tensorflow Debug with CDT

Get the source from github
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
Install the prerequirement
sudo apt install python3-dev python3-pip
pip install -U --user pip six 'numpy<1.19.0' wheel setuptools mock 'future>=0.17.1' 'gast==0.3.3' typing_extensions
pip install -U --user keras_applications --no-deps
pip install -U --user keras_preprocessing --no-deps
Configure the tensorflow
./configure
Install the build tool: Bazel
sudo apt install curl gnupg
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg
sudo mv bazel.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
sudo apt update && sudo apt install bazel
编译和Debug设置

1 Build Tensorflow的debug版本,即可以用GDB Debug的版本:
Build的时候加上copt “-g”:
通过gdb conv_ops_3d.pic.o验证,只要不出现No symbols则说明debug信息有。

 
bazel build --config=opt --copt -g //tensorflow/tools/lib_package:libtensorflow --verbose_failures
#You can also use --per_file_copt to allow only some library with debug info.
bazel build --config=opt  --per_file_copt=.*tensorflow.*@-g //tensorflow/tools/lib_package:libtensorflow --verbose_failures
 
[root@localhost conv_ops]# gdb conv_ops_3d.pic.o
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.el7
Copyright (C) 2013 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-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/.cache/bazel/_bazel_root/e88dc1dcc3c90dfdeee7304faf39c313/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow/core/kernels/_objs/conv_ops/conv_ops_3d.pic.o...done.
(gdb) 
 
#Other libraries may need for debuging.
bazel build --config=opt  --copt -g //tensorflow/cc:cc_ops --verbose_failures
bazel build --config=opt  --copt -g //tensorflow/c:c_test_util --verbose_failures
bazel build --config=opt  --copt -g //tensorflow/c:c_api --verbose_failures
bazel build --config=opt  --copt -g //tensorflow/cc:cc_ops --verbose_failures
bazel build --config=opt  --copt -g //tensorflow/cc:const_op --verbose_failures
bazel build --config=opt  --copt -g //tensorflow/cc:array_ops --verbose_failures
bazel build --config=opt  --copt -g //tensorflow/cc:ops --verbose_failures
bazel build --config=opt  --copt -g //tensorflow/cc:match_ops --verbose_failures
bazel build --config=opt  --copt -g //tensorflow/cc:math_ops --verbose_failures
bazel build --config=opt  --copt -g //tensorflow/cc:scope --verbose_failures
bazel build --config=opt  --copt -g //tensorflow/core:test --verbose_failures
bazel build --config=opt  --copt -g //tensorflow/core:testlib --verbose_failures
bazel build --config=opt  --copt -g //tensorflow/core/kernels:ops_testutil --verbose_failures


-glevel
Request debugging information and also use level to specify how much information. The default level is 2.
Level 0 produces no debug information at all. Thus, -g0 negates -g.
Level 1 produces minimal information, enough for making backtraces in parts of the program that you don’t plan to debug. This includes descriptions of functions and external variables, but no information about local variables and no line numbers.
Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.
2 创建TensorflowTest C++工程:
创建一个测试类TensorflowTest.cpp:

//============================================================================
// Name        : TensorflowTest.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
 
#include <iostream>
#include <tensorflow/c/c_api.h>
#include <tensorflow/c/c_test_util.h>
 
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <memory>
#include <vector>
#include <string.h>
 
using namespace std;
 
int main() {
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	cout << "Hello from TensorFlow C library version" << TF_Version() << endl;
 
	TF_Status* s = TF_NewStatus();
	TF_Graph* graph = TF_NewGraph();
 
	// Construct the graph: A + 2 + B
	TF_Operation* a = Placeholder(graph, s, "A");
	cout << TF_Message(s);
 
	TF_Operation* b = Placeholder(graph, s, "B");
	cout << TF_Message(s);
 
	TF_Operation* two = ScalarConst(2, graph, s);
	cout << TF_Message(s);
 
	TF_Operation* plus2 = Add(a, two, graph, s, "plus2");
	cout << TF_Message(s);
 
	TF_Operation* plusB = Add(plus2, b, graph, s, "plusB");
	cout << TF_Message(s);
 
	// Setup a session and a partial run handle.  The partial run will allow
	// computation of A + 2 + B in two phases (calls to TF_SessionPRun):
	// 1. Feed A and get (A+2)
	// 2. Feed B and get (A+2)+B
	TF_SessionOptions* opts = TF_NewSessionOptions();
	TF_Session* sess = TF_NewSession(graph, opts, s);
	TF_DeleteSessionOptions(opts);
 
	TF_Output feeds[] = { TF_Output { a, 0 }, TF_Output { b, 0 } };
	TF_Output fetches[] = { TF_Output { plus2, 0 }, TF_Output { plusB, 0 } };
 
	const char* handle = nullptr;
	TF_SessionPRunSetup(sess, feeds, TF_ARRAYSIZE(feeds), fetches,
			TF_ARRAYSIZE(fetches), NULL, 0, &handle, s);
	cout << TF_Message(s);
 
	// Feed A and fetch A + 2.
	TF_Output feeds1[] = { TF_Output { a, 0 } };
	TF_Output fetches1[] = { TF_Output { plus2, 0 } };
	TF_Tensor* feedValues1[] = { Int32Tensor(1) };
	TF_Tensor* fetchValues1[1];
	TF_SessionPRun(sess, handle, feeds1, feedValues1, 1, fetches1, fetchValues1,
			1, NULL, 0, s);
	cout << TF_Message(s);
	cout << *(static_cast<int*>(TF_TensorData(fetchValues1[0]))) << endl;
 
	// Clean up.
	TF_DeletePRunHandle(handle);
	TF_DeleteSession(sess, s);
	cout << TF_Message(s);
	TF_DeleteGraph(graph);
	TF_DeleteStatus(s);
	return 0;
}

参考如下进行C++的设置:

附工程文件:TensorflowTest.zip
附常用断点:

NonBlockingThreadPool.h [line: 108]	
NonBlockingThreadPool.h [line: 261]	
NonBlockingThreadPool.h [line: 323]	
NonBlockingThreadPool.h [line: 324]	
NonBlockingThreadPool.h [line: 328]	
TensorflowTest2.cpp [line: 50]	
TensorflowTest2.cpp [line: 57]	
TensorflowTest2.cpp [line: 66]	
aggregate_ops.cc [line: 28]	
aggregate_ops.cc [line: 49]	
aggregate_ops.cc [line: 50]	
aggregate_ops.cc [line: 52]	
aggregate_ops.cc [line: 58]	
aggregate_ops.cc [line: 167]	
array_ops.cc [line: 1883]	
array_ops.cc [line: 1883]	
array_ops.cc [line: 1888]	
c_api.cc [line: 2668]	
conv_ops.cc [line: 74]	
conv_ops.cc [line: 128]	
conv_ops.cc [line: 135]	
conv_ops.cc [line: 453]	
conv_ops.cc [line: 457]	
conv_ops_test.cc [line: 24]	
conv_ops_test.cc [line: 189]	
conv_ops_test.cc [line: 208]	
direct_session.cc [line: 915]	
direct_session.cc [line: 923]	
direct_session.cc [line: 972]	
direct_session.cc [line: 1266]	
direct_session.cc [line: 1267]	
direct_session.cc [line: 1269]	
direct_session.cc [line: 1301]	
direct_session.cc [line: 1308]	
direct_session.cc [line: 1464]	
direct_session.cc [line: 1548]	
direct_session.cc [line: 1581]	
direct_session.cc [line: 1582]	
executor.cc [line: 1608]	
executor.cc [line: 1609]	
executor.cc [line: 1693]	
executor.cc [line: 2097]	
executor.cc [line: 2231]	
graph_execution_state.cc [line: 599]	
graph_execution_state.cc [line: 689]	
graph_optimizer.cc [line: 43]	
math_ops.cc [line: 29]	
math_ops.cc [line: 35]	
math_ops.cc [line: 35]	
math_ops.cc [line: 36]	
math_ops.cc [line: 104]	
math_ops.cc [line: 104]	
math_ops.cc [line: 104]	
math_ops.cc [line: 105]	
math_ops.cc [line: 106]	
math_ops.cc [line: 107]	
math_ops.cc [line: 108]	
math_ops.cc [line: 1675]	
model_pruner.cc [line: 430]	
op.cc [line: 57]	
op_kernel.cc [line: 1250]	
op_kernel.cc [line: 1250]	
op_kernel.cc [line: 1253]	
op_kernel.cc [line: 1254]	
op_kernel.cc [line: 1254]	
op_kernel.cc [line: 1258]	
op_kernel.cc [line: 1258]	
op_kernel.cc [line: 1307]	
threadpool.cc [line: 53]	

本作品采用知识共享署名 4.0 国际许可协议进行许可。

发表回复