本文以 Ubuntu 14.04 為示範環境。
sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java8-installer
apt-get install pkg-config zip g++ zlib1g-dev unzip
下載安裝包後賦予執行權限並安裝:
chmod +x bazel-version-installer-os.sh ./bazel-version-installer-os.sh --user
sudo apt-get install python-numpy swig python-dev python-wheel
執行 ./configure,依提示指定 Python 路徑、是否啟用 GPU、GCC 主機編譯器、CUDA SDK 版本與路徑、cuDNN 版本與路徑,以及要編譯的 CUDA compute capability(例如 3.5)。範例輸出如下:
Please specify the location of python. [Default is /usr/bin/python]: Do you wish to build TensorFlow with GPU support? [y/N] y Please specify the Cuda SDK version you want to use. [Leave empty to use system default]: 7.5 Please specify the location where CUDA 7.5 toolkit is installed. [default is: /usr/local/cuda]: /usr/local/cuda Please specify the Cudnn version you want to use. [Leave empty to use system default]: 4.0.4 Please specify the location where the cuDNN 4.0.4 library is installed. [default is: /usr/local/cuda]: /usr/local/cudnn-r4-rc/ Please specify a list of comma-separated Cuda compute capabilities you want to build with. [Default is: "3.5,5.2"]: 3.5 Configuration finished
加上 --config=cuda 參數即可啟用 GPU 支援:
bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu
此範例會在 GPU 上反覆計算 2x2 矩陣的主要特徵值,輸出結尾的 lambda 與向量會逐步收斂。
雖然可在同一原始碼樹中同時建置 CUDA 與非 CUDA 版本,但切換這兩種設定時,建議先執行 bazel clean。此外,必須先執行 configure 再執行 bazel build,否則編譯會失敗並顯示明確錯誤訊息。