從源碼編譯TensorFlow

新的cudnnV6出了很久了,但是1.2的還不支持
就想自己編譯一個
按照官方的教程試一下,記錄在此
本人環境 GTX1080; i7 7700k; gcc 5.4; cuda8.0; cudnn6.0。 使用的是TF1.2
首先裝Bazel,Google就是喜歡定標準啊哈哈

echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install bazel

隨後如果想更新,那麼

sudo apt-get upgrade bazel

然後切換到源碼目錄,我覺得git好慢就直接下載壓縮包解壓了。。
執行 ./configure 可得如下
目前具體的參數設置我還沒研究,姑且這麼搞吧
現在是GTX1080,所以計算能力6.1 cudnn我已經替換成6.0版本了,貌似它都可以檢測到,直接默認即可

You have bazel 0.5.2 installed.
Please specify the location of python. [Default is /home/hzy/anaconda3/bin/python]: 
Found possible Python library paths:
  /home/hzy/anaconda3/lib/python3.6/site-packages
Please input the desired Python library path to use.  Default is [/home/hzy/anaconda3/lib/python3.6/site-packages]

Using python library path: /home/hzy/anaconda3/lib/python3.6/site-packages
Do you wish to build TensorFlow with MKL support? [y/N] n
No MKL 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]: 
Do you wish to use jemalloc as the malloc implementation? [Y/n] y
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] n
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N] y
Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N] n
No XLA JIT support will be enabled for TensorFlow
Do you wish to build TensorFlow with VERBS support? [y/N] y
VERBS support will be enabled for TensorFlow
Do you wish to build TensorFlow with OpenCL support? [y/N] n
No OpenCL 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
Do you want to use clang as CUDA compiler? [y/N] n
nvcc will be used as CUDA compiler
Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]: 
Please specify the location where CUDA  toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: 
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 
Please specify the location where cuDNN  library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: 
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 wish to build TensorFlow with MPI support? [y/N] n
MPI support will not be enabled for TensorFlow
Configuration finished

這個介紹就提示你一下finished,接下來重頭戲。
build時候官網提示,如果是gcc5或者更高版本的話,請加上後面--cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0"這句。

bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package  --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0"

編譯過程還是很漫長的。。
我這個顯示INFO: Elapsed time: 1320.414s, Critical Path: 130.33s
然後

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

OK 這就生成了whl文件了,我們切換路徑到/tmp/tensorflow_pkg, ls一下看到
tensorflow-1.2.0-cp36-cp36m-linux_x86_64.whl
pip install 一下
這裏寫圖片描述
一顆賽艇,安裝成功! import一下試試,發現慘遭翻車,下面是車禍現場

ImportError: /home/hzy/anaconda3/bin/../lib/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by /home/hzy/anaconda3/lib/python3.6/site-packages/tensorflow/python/_pywrap_tensorflow_internal.so)

Google一下,感謝曾冠奇的解決方案~

I solved this problem by copying the libstdc++.so.6 file which contains version CXXABI_1.3.8.

Try run the following search command first:

$ strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep CXXABI_1.3.8

If it returns CXXABI_1.3.8. Then you can do the copying.

$ cp /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /home/jj/anaconda2/bin/../lib/libstdc++.so.6

吼 大功告成,待隨後測試
這裏寫圖片描述

補充:
cool 我試了下編譯1.1的,configure選項和1.2有些不同,似乎無法支持cudnn6.0 。編譯了下,它的計算能力默認支持的是3.5和5.2,針對1080我加入了6.1。其他有一些支持hadoop和谷歌雲計算的選項。耗時Elapsed time: 1145.992s, Critical Path: 127.80s
1.0的耗時Elapsed time: 1211.101s, Critical Path: 243.46s
所以在1.0 1.1時代,直接用預編譯好的版本基本就可以,推薦清華源的。1.2的話根據個人喜好可以自行編譯~~這些編譯好的我整理後發佈在我的github上,同時做一份國內的百度雲分享,歡迎使用提issue~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章