Skip to content

Commit 0c142b1

Browse files
make separate page
1 parent db3e69b commit 0c142b1

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

compile_tensorflow_cpp.md

+164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# Compile TensorFlow C++ from source code
2+
3+
Building TensorFlow C++ API is very tricky and can be a pain as there is not much information you can find about it even on TensorFlow's official documentation. Following you will find a step-by-step instruction showing how to build TensorFlow C++ v2 on Linux. It works well for my Ubuntu 20.04 running on AMD Ryzen processors.
4+
5+
## Dependencies
6+
7+
- Conda environment
8+
- Python 3.9.0
9+
- TensorFlow 2.7
10+
- Bazel 3.7.2
11+
- Protobuf 3.9.2 (must be compatible with the version of TensorFlow-built protobuf or protoc)
12+
13+
## Environment setup & install Python
14+
```
15+
conda create -n tfcc
16+
conda activate tfcc
17+
conda install python
18+
conda update --all -y
19+
```
20+
21+
## Install bazel
22+
```
23+
sudo apt install bazel-3.7.2
24+
```
25+
26+
## Install TensorFlow CC
27+
```
28+
git clone https://github.com/tensorflow/tensorflow
29+
cd tensorflow
30+
git checkout r2.7
31+
```
32+
33+
---
34+
35+
## 1. Compile TF shared library (with optimization)
36+
```
37+
export CC=gcc
38+
export CXX=g++
39+
bazel build --jobs=4 --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" -c opt \
40+
//tensorflow:libtensorflow.so \
41+
//tensorflow:libtensorflow_cc.so \
42+
//tensorflow:libtensorflow_framework.so \
43+
//tensorflow:install_headers \
44+
//tensorflow/tools/pip_package:build_pip_package
45+
```
46+
47+
Note:
48+
49+
1. Building TF uses a lot of memory, I prefer a small number of CPUs (`--jobs`)
50+
2. The whole process can take several hours
51+
3. Add `-D_GLIBCXX_USE_CXX11_ABI=0` if you use GCC 5 or higher
52+
4. Flags for optimization: `--copt="-O3"`
53+
5. Flasg for both AMD and Intel chips: `--copt=-mfma --copt=-msse4.1 --copt=-msse4.2 --copt=-mfpmath=both`
54+
6. Flags for Intel: `--copt=-mavx --copt=-mavx2`
55+
7. Rebuild with `--config=monolithic` if you want compile all TF C++ code into a single shared object
56+
57+
**optional**
58+
```
59+
bazel test --jobs=4 --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" -c opt \
60+
//tensorflow/tools/lib_package:libtensorflow_test
61+
```
62+
63+
## 2. Install protobuf
64+
65+
1. Check the version of protobuf that TF is built with
66+
```
67+
bazel-bin/external/com_google_protobuf/protoc --version
68+
libprotoc 3.9.2
69+
```
70+
2. Download protobuf source code from its GitHub release https://github.com/protocolbuffers/protobuf/tags
71+
3. Compile and link
72+
```
73+
./configure --prefix=/home/rangsiman/protobuf-3.9.2/
74+
make
75+
make check
76+
make install
77+
```
78+
79+
## 3. Copy required files into a single path for C++ linkage
80+
```
81+
sudo mkdir /usr/local/tensorflow
82+
sudo cp -r bazel-bin/tensorflow/include/ /usr/local/tensorflow/
83+
sudo cp -r /home/rangsiman/protobuf-3.9.2/include/google/ /usr/local/tensorflow/include/
84+
sudo mkdir /usr/local/tensorflow/lib
85+
sudo cp -r bazel-bin/tensorflow/*.so* /usr/local/tensorflow/lib
86+
sudo cp -r /home/rangsiman/protobuf-3.9.2/lib/*.so* /usr/local/tensorflow/lib
87+
```
88+
89+
## 4. Compiling the op library and example code
90+
91+
**Example-1**: Zero out
92+
93+
Create `zero_out.cpp`
94+
```
95+
#include "tensorflow/core/framework/op.h"
96+
#include "tensorflow/core/framework/shape_inference.h"
97+
98+
using namespace tensorflow;
99+
100+
REGISTER_OP("ZeroOut")
101+
.Input("to_zero: int32")
102+
.Output("zeroed: int32")
103+
.SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) {
104+
c->set_output(0, c->input(0));
105+
return Status::OK();
106+
});
107+
```
108+
109+
Run the following
110+
```
111+
g++ -Wall -fPIC -D_GLIBCXX_USE_CXX11_ABI=0 \
112+
-shared zero_out.cpp -o zero_out.so \
113+
-I/usr/local/tensorflow/include/ -L/usr/local/tensorflow/lib \
114+
-ltensorflow_cc -ltensorflow_framework
115+
```
116+
117+
**Example-2**: Call TF session
118+
119+
Create `session.cpp`
120+
```
121+
#include <tensorflow/core/platform/env.h>
122+
#include <tensorflow/core/public/session.h>
123+
124+
#include <iostream>
125+
126+
using namespace std;
127+
using namespace tensorflow;
128+
129+
int main()
130+
{
131+
Session* session;
132+
Status status = NewSession(SessionOptions(), &session);
133+
if (!status.ok()) {
134+
cout << status.ToString() << "\n";
135+
return 1;
136+
}
137+
cout << "Session successfully created.\n";
138+
}
139+
```
140+
141+
Run the following
142+
```
143+
g++ -Wall -fPIC -D_GLIBCXX_USE_CXX11_ABI=0 \
144+
session.cpp -o session \
145+
-I/usr/local/tensorflow/include/ -L/usr/local/tensorflow/lib \
146+
-ltensorflow_cc -ltensorflow_framework
147+
```
148+
149+
To run the executable, you also need to add `/usr/local/tensorflow/lib/` into `LD_LIBRARY_PATH` env var.
150+
151+
---
152+
153+
## Optional: Compile TF via pip (wheel) builder
154+
```
155+
## create a wheel package
156+
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
157+
158+
## install TF using a created wheel
159+
pip install /tmp/tensorflow_pkg/tensorflow-*.whl
160+
```
161+
162+
## References
163+
1. https://www.tensorflow.org/guide/create_op#compile_the_op_using_your_system_compiler_tensorflow_binary_installation
164+
2. https://www.tensorflow.org/install/source#bazel_build_options

0 commit comments

Comments
 (0)