Skip to content

Commit 27113a4

Browse files
Improved
1 parent 28c96b1 commit 27113a4

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

compile_tensorflow_cpp.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
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.
44

5-
In this page I will walk you through the steps to install TensorFlow C++ API version 2.7.
5+
In this page I will walk you through the steps to install **TensorFlow C++ API version 2.7**.
66

77
## Dependencies
88

99
- Conda
10-
- Python 3.9.9
11-
- GCC 5 or newer
12-
- Bazel 3.7.2
13-
- Protobuf 3.9.2 (must be compatible with the version of TensorFlow-built protobuf or protoc)
10+
- Python
11+
- GCC 5 or higher
12+
- Bazel*
13+
- Protobuf*
1414

15-
Check a list of supported Python version, compiler, and bazel at https://www.tensorflow.org/install/source#tested_build_configurations.
15+
*a supported version depends on the version of TensorFlow. For TensorFlow v2.7, Python 3.9.9, GCC 10.3.0, Bazel 3.7.2, and Protobuf 3.9.2 work for me.
16+
17+
A list of supported Python, compiler, and Bazel can be found [here](https://www.tensorflow.org/install/source#tested_build_configurations).
1618

1719
---
1820

@@ -28,6 +30,8 @@ conda update --all -y
2830

2931
### 2. Install bazel
3032

33+
Check a supported version at `tensorflow/.bazelversion`.
34+
3135
Add bazel repository:
3236
```bash
3337
sudo apt install apt-transport-https curl gnupg
@@ -39,13 +43,11 @@ echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" |
3943
Install:
4044
```bash
4145
sudo apt install bazel
42-
# or a specific version
46+
# or a specific version, e.g.,
4347
sudo apt install bazel-3.7.2
4448
```
4549

46-
You can also run the installer:
47-
48-
Download from Bazel GitHub release page and run the installer in your home:
50+
You can also run the installer. Download from Bazel GitHub release page and run the installer in your home:
4951
```bash
5052
https://github.com/bazelbuild/bazel/releases/download/3.7.2/bazel-3.7.2-installer-linux-x86_64.sh
5153
chmod +x bazel-3.7.2-installer-linux-x86_64.sh
@@ -88,13 +90,13 @@ bazel query ...
8890

8991
Note:
9092

91-
1. Building TF uses a lot of memory, I prefer a small number of CPUs (`--jobs`)
93+
1. Building TensorFlow uses a lot of memory, I prefer a small number of CPUs (`--jobs`)
9294
2. The whole process can take several hours
9395
3. Add `-D_GLIBCXX_USE_CXX11_ABI=0` if you use GCC 5 or higher
9496
4. Flags for optimization: `--copt="-O3"`
9597
5. Flasg for both AMD and Intel chips: `--copt=-mfma --copt=-msse4.1 --copt=-msse4.2 --copt=-mfpmath=both`
9698
6. Flags for Intel: `--copt=-mavx --copt=-mavx2`
97-
7. Rebuild with `--config=monolithic` if you want compile all TF C++ code into a single shared object
99+
7. Rebuild with `--config=monolithic` if you want compile all TensorFlow C++ code into a single shared object
98100

99101
**optional**
100102
```bash
@@ -104,7 +106,7 @@ bazel test --jobs=4 --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" -c opt \
104106

105107
### 2. Install protobuf
106108

107-
1. Check the version of protobuf that TF is built with
109+
1. Check the version of protobuf that is supported by TensorFlow
108110
```bash
109111
bazel-bin/external/com_google_protobuf/protoc --version
110112
libprotoc 3.9.2
@@ -157,7 +159,7 @@ g++ -Wall -fPIC -D_GLIBCXX_USE_CXX11_ABI=0 \
157159
-ltensorflow_cc -ltensorflow_framework
158160
```
159161

160-
**Example-2**: Call TF session
162+
**Example-2**: Call TensorFlow session
161163

162164
Create `session.cpp`
163165
```cpp

0 commit comments

Comments
 (0)