Build TensorFlow from Source on Windows 10 Deep Learning Framework for Python and C++ on CPU and ...

Build TensorFlow from Source on Windows 10

Deep Learning Framework for Python and C++ on CPU and GPU

[图片上传失败...(image-66ceb9-1655260073828)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">[TensorFlow] Build from Source on Windows (Python & C++) (CPU & GPU)</figcaption>

Introduction

This is the first tutorial on a series of building deep learning frameworks from source that aims to offer a step by step guide for anyone struggling on the compilation of deep learning framework from source.

Why do you need to build a deep learning framework from source?

Majority of deep learning frameworks are open source (i.e. their codes are published on GitHub). Each release of deep learning frameworks contains compiled binaries that vary according to the Operating System (OS), Programming Language, and targeted Hardware Acceleration (such as CPU or GPU), as shown in the figure below.

[图片上传失败...(image-342695-1655260073828)]

[图片上传失败...(image-962334-1655260073828)]

[图片上传失败...(image-3765a2-1655260073828)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co nb dq nc nd" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); position: relative; margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px; width: 670.725px; left: calc(-7% - 8px); transform: translateX(-50%);">TensorFlow Binaries, PyTorch Binaries, ONNX Runtime Binaries</figcaption>

So, you are recommended to build from source if you are looking to use a different version of programming language, a different version of hardware accelerator, the latest source codes of the framework that contain some bug fixes but they are still not included in any release yet,or you want to** edit certain functions of the framework to perform a specific or custom task**.

For instance, if you want to use PyTorch with GPU CUDA 11, as shown in the figure above, the PyTorch binaries only support up to CUDA 10.2. In addition, looking at the TensorFlow CPU binaries, it is built to be generic for the majority of CPU, some binaries compiled without any CPU extensions, and starting with TensorFlow 1.6, binaries use AVX instructions as default CPU extensions, but what about the rest of CPU extensions such as AVX2, SSE4.1, SSE4.2, which by using them the execution performance can be increased.

In this tutorial, the procedure to build TensorFlow (CPU & GPU) from source on Windows 10 for Python and C++ will be discussed in details.

This tutorial covers:

  1. Git Installation
  2. MSYS2 Installation with its Command-line Tools
  3. Bazel Installation
  4. Install Visual Studio 2019 Build Tools
  5. Install Python 3.8 64-bits
  6. Install NVIDIA CUDA 10.2 and cuDNN 7.6 (for GPU Version)
  7. Clone TensorFlow Source Code
  8. Configure Build Parameters (CPU & GPU)
  9. Build TensorFlow Wheel File for Python 3.8 (CPU & GPU)
  10. Install and Test TensorFlow Python Installation with Sample Application (CPU & GPU)
  11. Build and Test Shared DLL Library for C++ API (CPU & GPU)

Step 1: Git Installation

Download Git for Windows from here and install it. Please make sure that the git.exe path is included in the environment variable. By default, it should be added. I used Git v2.28.0 64-bit. My environment variable path is C:\Program Files\Git\cmd.

To test the Git installation, open CMD, and type the following command:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">git --version</pre>

The expected output from the CMD will be: git version 2.28.0

Step 2: MSYS2 Installation with its Command-line Tools

Download MSYS2 from here and install it. The link includes a step by step guide on the installation process. Once you finish it, you are required to add MSYS2 installation path to the environment path manually by following this link.

[图片上传失败...(image-3c6a1e-1655260073829)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Add MSYS2 to be the environment variable</figcaption>

To test the MSYS2 installation, open CMD, and type the following command:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">mintty --version</pre>

The expected output from the CMD will be:

[图片上传失败...(image-c2f37e-1655260073829)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Verification of MSYS2 Installation</figcaption>

Step 3: Bazel Installation

The version of TensorFlow should be matched to a certain version of Bazel. Refer to this link to figure out the Bazel version that matches the targeted TensorFlow version. In this tutorial, Bazel 2.0.0 is selected for compiling TensorFlow 2.2.0. The full Bazel installation guide can be found here. If you need Bazel 2.0.0 executable file, you can download from here. The general steps include:

[图片上传失败...(image-f4a3cc-1655260073829)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Adding Bazel to Environment PATH</figcaption>

  1. Rename bazel-2.0.0-windows-x86_64.exe to bazel.exe.
  2. Move bazel.exe to any directory.
  3. Add this directory to be the environment variable.

To test the Bazel installation, open CMD, and type the following command:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">bazel --version</pre>

The expected output from the CMD will be: bazel 2.0.0

A SMALL REMINDER: Start a new CMD every time after you editted the environment variables.

Step 4: Install Visual Studio 2019 along with its Build Tools

Download from here and here to install Visual Studio 2019 Community & Visual Studio Build Tools respectively. It is recommended not to change the default installation path.

[图片上传失败...(image-f9581c-1655260073828)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Visual Studio 2019 installer</figcaption>

Please make sure that both options of “MSVC v142-VS2019 C++ build tools” and “Windows 10 SDK” are selected.

Step 5: Install Python 3.8 64-bits

Download Python 3.8 from here and install it. Please have a check on the option of “Add Python to PATH”, so the installer will include the Python directory into the environment variable directly.

[图片上传失败...(image-51744b-1655260073829)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">The user interface of Python installer</figcaption>

Once you finish the installation, you can test it. Open CMD and type the following command:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">python</pre>

The expected output from the CMD will be:

[图片上传失败...(image-b39630-1655260073828)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Verification of Python Installation</figcaption>

Step 6: Install NVIDIA CUDA 10.2 and cuDNN 7.6 (for GPU Version)

The installation process of CUDA is quite straightforward. Install CUDA v10.2 from here. Next, install cuDNN by downloading the installer from here. Choose v7.6.5 for CUDA v10.2, unzip it and move the files:

  1. [unzipped dir]\bin\ → C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\bin
  2. [unzipped dir]\include\ → C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\include
  3. [unzipped dir]\lib\ → C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\lib

To verify the CUDA installation, open CMD, and type the following command:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">nvcc --version</pre>

The expected output from the CMD will be:

[图片上传失败...(image-8c00bb-1655260073829)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Verification of CUDA Installation</figcaption>

Step 7: Clone TensorFlow Source Code

Clone TensorFlow source code from GitHub, open CMD, and type:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">git clone https://github.com/tensorflow/tensorflow
cd tensorflow
git checkout v2.2.0</pre>

The latest stable version of TensorFlow is verified. In my case, it is in the version of 2.2.0.

Configure Visual C++ Build Tools for Bazel:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">set BAZEL_VC=C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC</pre>

Please re-execute the following command for every new CMD or set it as one of the environment variables.

Install the required Python packages by creating a requirements.txt file in the TensorFlow directory and copying the texts below to the requirements.txtfile. For more information about the required package, please refer to this link.

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;"><mark class="yl ym ro" style="box-sizing: inherit; cursor: pointer; background-color: rgb(232, 243, 232); color: currentcolor;">absl-py >= 0.7.0
astunparse == 1.6.3
flatbuffers >= 1.12
gast == 0.3.3
google_pasta >= 0.1.8
h5py >= 2.10.0, < 2.11.0
keras_preprocessing >= 1.1.1, < 1.2
numpy >= 1.16.0, < 1.19.0
opt_einsum >= 2.3.2
protobuf >= 3.9.2
tensorboard >= 2.2.0, < 2.3.0
tensorflow_estimator >= 2.2.0, < 2.3.0
termcolor >= 1.1.0
wrapt >= 1.11.1
wheel >= 0.26
six >= 1.12.0
scipy == 1.4.1</mark></pre>

Install packages using pip by executing the following command in CMD:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">pip install -r requirements.txt</pre>

Step 8: Configure Build Parameters (CPU & GPU)

In this step, the system parameters which will be used for building the binary are configured. Execute the following command in CMD (make sure you are in the TensorFlow directory):

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">python ./configure.py</pre>

An interactive configuration terminal will be started. Answer all given questions on the terminal. The following is an example of the configuration for CPU Build.

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">You have bazel 2.0.0 installed.
Please specify the location of python. [Default is C:\Users\Viper\AppData\Local\Programs\Python\Python38\python.exe]:Found possible Python library paths:
C:\Users\Viper\AppData\Local\Programs\Python\Python38\lib\site-packages
Please input the desired Python library path to use. Default is [C:\Users\Viper\AppData\Local\Programs\Python\Python38\lib\site-packages]Do you wish to build TensorFlow with ROCm support? [y/N]: N
No ROCm support will be enabled for TensorFlow.Do you wish to build TensorFlow with CUDA support? [y/N]: N
No CUDA support will be enabled for TensorFlow.Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]:Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]:
Eigen strong inline overridden.Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]:
Not configuring the WORKSPACE for Android builds.</pre>

Please execute ***bazel clean*** when switching from CPU to GPU or GPU to CPU.

To configure for GPU version, you need to execute "python ./configure.py” again and answer the following question with “y”.

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">Do you wish to build TensorFlow with CUDA support? [y/N]: y</pre>

You should get the output messages as above. Include the CUDA and cuDNN versions as stated below.

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">Do you wish to build TensorFlow with CUDA support? [y/N]: y
CUDA support will be enabled for TensorFlow.Found CUDA 10.2 in:
C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/lib/x64
C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/include
Found cuDNN 7 in:
C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/lib/x64
C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.2/include</pre>

Step 9: Build TensorFlow Wheel File for Python 3.8 (CPU & GPU)

Bazel Build Command will be used to create an executable named build_pip_package which is the program that builds the pip package.

CPU Version

Execute the following command in CMD (make sure you are in the TensorFlow directory):

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package</pre>

Then, .whl package in the directory named D:\ibrahim\frameworks\builds\python\cpu will be generated.

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">bazel-bin\tensorflow\tools\pip_package\build_pip_package D:\ibrahim\frameworks\builds\python\cpu</pre>

Please execute ***bazel clean*** when switching from CPU to GPU or GPU to CPU.

GPU Version

Execute the following command in CMD (make sure you are in the TensorFlow directory):

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package</pre>

Then, .whl package in the directory named D:\ibrahim\frameworks\builds\python\gpu will be generated.

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">bazel-bin\tensorflow\tools\pip_package\build_pip_package D:\ibrahim\frameworks\builds\python\gpu</pre>

Step 10: Install and Test TensorFlow Python Installation with Sample Application (CPU & GPU)

CPU Version

Install TensorFlow CPU wheel by executing the following command in CMD:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">pip install D:\ibrahim\frameworks\builds\python\cpu[filename].whl</pre>

Next, the installation is tested by starting a Python program from CMD. Import TensorFlow and check its version as following:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">python
import tensorflow as tf
tf.version</pre>

The expected output from the CMD will be:

[图片上传失败...(image-683258-1655260073827)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Verification of TensorFlow CPU Installation</figcaption>

GPU Version

Uninstall the CPU version if you wish to test the CPU version.

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">pip uninstall tensorflow</pre>

Next, install the GPU version by executing the following command:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">pip install D:\ibrahim\frameworks\builds\python\gpu[filename].whl</pre>

Then, the installation is tested by starting a Python program from CMD. Import TensorFlow and check its version as following:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">python
import tensorflow as tf
tf.version</pre>

The expected output from the CMD will be:

[图片上传失败...(image-ae0e87-1655260073827)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Verification of TensorFlow GPU Installation</figcaption>

To check the visible GPU devices:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">import tensorflow as tf
if tf.test.gpu_device_name():
print(f'Default GPU Device:{tf.test.gpu_device_name()}')
else:
print("Please install GPU version of TF")</pre>

The expected output from the CMD will be:

[图片上传失败...(image-fa703d-1655260073827)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">The expected output for checking visible GPU devices(Python)</figcaption>

Step 11: Build and Test Shared DLL Library for C++ API (CPU & GPU)

In this step, the Bazel Build Command will be used to create a shared DLL library of TensorFlow which can be used in any C++ or C# application.

Some Bug Fixing Before Compiling :

Before we start, we should edit the TensorFlow source code, because you will end up with linking errors (unresolved external symbols) as shown below if you follow the typical framework building steps.

[图片上传失败...(image-b6297d-1655260073827)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Session and SessionOptions symbols are not exported</figcaption>

So what are these errors?

Dynamic-link library(DLL) can only expose a maximum of 60,000 symbols which is one of DLL limitation. As TensorFlow has more than 60,000 symbols, so we have to manually choose which symbols to be exposed if they are not exposed from TensorFlow by default.

According to the experience, in order to deploy a TensorFlow model, the TensorFlow Session and SessionOptions Classes are required to be exported. The steps include:

  1. Edit tensorflow\core\public\session.h
  • Firstly, include TensorFlow macro header.

[图片上传失败...(image-8e15b0-1655260073827)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Include TF Macro at session.h</figcaption>

  • Secondly, add TF_EXPORT in front of any function definition or class definition that caused the missing symbol error.

[图片上传失败...(image-a58039-1655260073827)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Export Session Constructor</figcaption>

[图片上传失败...(image-2cee2e-1655260073827)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Export override Constructor NewSession</figcaption>

[图片上传失败...(image-736788-1655260073827)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Export override Constructor NewSession</figcaption>

2. Edit TensorFlow/core/public/session_options.h

Same as session.h, include the macro header and add TF_EXPORT in front of any function definition or class definition that caused the missing symbol error.

[图片上传失败...(image-26e23e-1655260073827)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Export SessionOptions Struct</figcaption>

[图片上传失败...(image-1b5907-1655260073827)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">Export SessionOptions Constructor</figcaption>

If you are facing any missing symbol during your development, you can follow similar steps to export function or class and rebuild again.

Now, we are ready to start the compilation.

Please execute ***bazel clean*** when switching from CPU to GPU or GPU to CPU.

CPU Version

Execute the following command in CMD after re-execute **Step 8 **(make sure you are in the TensorFlow directory):

  1. Build Dynamic-link library (.dll)

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">bazel build --config=opt tensorflow:tensorflow.dll</pre>

2. Build Static import libraries (.lib)

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">bazel build --config=opt tensorflow:tensorflow.lib</pre>

3. Build header files

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">bazel build --config=opt tensorflow:install_headers</pre>

Link your C++ Project to .dll, .lib, and headers on TensorFlow directory, or copy tensorflow.dll and tenorflow.lib from bazel-bin\tensorflow and copy TensorFlow requiring headers from bazel-bin/tensorflow/include

I prefer to copy each version separately so I can keep the original repository clean for future compilation.

Please execute ***bazel clean*** when switching from CPU to GPU or GPU to CPU.

GPU Version

Execute the following command in CMD after re-execute **Step 8 **(make sure you are in the TensorFlow directory):

  1. build Dynamic-link library (.dll)

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">bazel build --config=opt --config=cuda tensorflow:tensorflow.dll</pre>

2. build Static import libraries (.lib)

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">bazel build --config=opt --config=cuda tensorflow:tensorflow.lib</pre>

3. build header files

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">bazel build --config=opt --config=cuda tensorflow:install_headers</pre>

Link your C++ Project to .dll, .lib, and headers on TensorFlow directory, or copy tensorflow.dll and tenorflow.lib from bazel-bin\tensorflow and copy TensorFlow requiring headers from bazel-bin/tensorflow/include

C++ Application Test

Create a new console application on visual studio and link it to TensorFlow,

The following is the sample code:

<pre class="km kn ko kp gv ou bt ov" style="box-sizing: inherit; margin: 56px 0px 0px; background: rgb(242, 242, 242); padding: 20px; overflow-x: auto;">#include <iostream>

include “tensorflow/core/framework/graph.pb.h”

include “tensorflow/core/public/session.h”

include “tensorflow/core/public/version.h”

int main() {
std::cout << tf_git_version() << std::endl;
std::unique_ptr<tensorflow::Session> session(tensorflow::NewSession({}));
}</pre>

The expected output from the CMD will be:

[图片上传失败...(image-a8216-1655260073827)]

<figcaption class="kx bm gl gj gk ky kz bn b bo bp co" data-selectable-paragraph="" style="box-sizing: inherit; font-weight: 400; text-align: center; font-family: sohne, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px; color: rgb(117, 117, 117); margin-left: auto; margin-right: auto; max-width: 728px; margin-top: 10px;">The expected output for initializing new TensorFlow session (C++)</figcaption>

Finally, I have finished building TensorFlow (CPU & GPU) from source on Windows 10 for Python and C++.

How about you?

Thanks for your reading and stay tuned for the future articles.

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,189评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,577评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,857评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,703评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,705评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,620评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,995评论 3 396
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,656评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,898评论 1 298
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,639评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,720评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,395评论 4 319
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,982评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,953评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,195评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,907评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,472评论 2 342

推荐阅读更多精彩内容