TensorFlow編譯安裝(Linux)

一、TensorFlow依賴項:
1.bazel-0.15.2(bazel-0.15.2-installer-linux-x86_64.sh)
下載:https://github.com/bazelbuild/bazel/releases?after=0.19.1
安裝:

$ sudo ./bazel-0.15.2-installer-linux-x86_64.sh

2.protobuf-3.6.1(protobuf-all-3.6.1.tar.gz):
安裝:

二、TensorFlow編譯(版本:1.12)
我下載的是1.12版本:https://github.com/tosonw/tensorflow/archive/v1.12.0.tar.gz
解壓後命令行進入目錄:

$ mkdir build
$ cd build
$ ../configure 
Extracting Bazel installation...
WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown".
You have bazel 0.15.2 installed.
Please specify the location of python. [Default is /home/toson/anaconda3/bin/python]: 


Found possible Python library paths:
  /home/toson/anaconda3/lib/python3.6/site-packages
  /opt/intel/openvino_2019.1.144/python/python3.6
  /opt/intel/openvino_2019.1.144/deployment_tools/model_optimizer
  /home/toson/compile_libs/caffes/caffe_origin/python
Please input the desired Python library path to use.  Default is [/home/toson/anaconda3/lib/python3.6/site-packages]

Do you wish to build TensorFlow with Apache Ignite support? [Y/n]: n
No Apache Ignite support will be enabled for TensorFlow.

Do you wish to build TensorFlow with XLA JIT support? [Y/n]: n
No XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n
No OpenCL SYCL support will be enabled for TensorFlow.

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]: y
CUDA support will be enabled for TensorFlow.

Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 10.1


Please specify the location where CUDA 10.1 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 


Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 


Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 


Do you wish to build TensorFlow with TensorRT support? [y/N]: n
No TensorRT support will be enabled for TensorFlow.

Please specify the NCCL version you want to use. If NCCL 2.2 is not installed, then you can use version 1.3 that can be fetched automatically but it may have worse performance with multiple GPUs. [Default is 2.2]: 1.3


Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 6.1]: 


Do you want to use clang as CUDA compiler? [y/N]: n
nvcc will be used as CUDA compiler.

Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: 


Do you wish to build TensorFlow with MPI support? [y/N]: n
No MPI support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: 


Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: 
Not configuring the WORKSPACE for Android builds.

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See tools/bazel.rc for more details.
	--config=mkl         	# Build with MKL support.
	--config=monolithic  	# Config for mostly static monolithic build.
	--config=gdr         	# Build with GDR support.
	--config=verbs       	# Build with libverbs support.
	--config=ngraph      	# Build with Intel nGraph support.
Configuration finished

編譯:

$ bazel build --config=opt --config=cuda --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" //tensorflow:libtensorflow_cc.so
#等待有點久,最後提示以下內容就算成功了:
INFO: Elapsed time: 1365.375s, Critical Path: 185.64s
INFO: 4956 processes: 4956 local.
INFO: Build completed successfully, 4985 total actions

如果有問題:

#找不到庫文件
Cuda Configuration Error: Cannot find cuda library libcublas.so.10.1
#我查詢後發現確實找不到,但是隻是名字不對應
$ locate libcublas.so.10.1
/usr/lib/x86_64-linux-gnu/libcublas.so.10.1.0.105
/usr/local/cuda-10.1/targets/x86_64-linux/lib/libcublas.so.10.1.0.105
#我就手動拷貝了一下
sudo cp /usr/local/cuda-10.1/targets/x86_64-linux/lib/libcublas.so.10.1.0.105 /usr/local/cuda-10.1/targets/x86_64-linux/lib/libcublas.so.10.1
#再次編譯,仍有問題提示:找不到庫文件。通過上述方式進行拷貝可解決。
Cuda Configuration Error: Cannot find cuda library libcusolver.so.10.1
Cuda Configuration Error: Cannot find cuda library libcurand.so.10.1
Cuda Configuration Error: Cannot find cuda library libcufft.so.10.1

注:RTX2060 的 Compute Capability 爲 7.5(但是tensorflow_1.12不支持該架構:compute_75,tensorflow_2.0也許支持,但需要bazel_1.22,未繼續研究)


編譯成功後,在 /bazel-bin/tensorflow 目錄下會出現 libtensorflow_cc.so 文件

C版本: bazel build :libtensorflow.so
C++版本: bazel build :libtensorflow_cc.so

需要的頭文件,要在源碼裏拷貝出來使用:
bazel-genfiles/...eigen/...include/...tf/...

發佈了26 篇原創文章 · 獲贊 13 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章