TensorFlow源码编译-基于Ubuntu 15.04

Ubuntu/Linux直接安装:

# 仅使用 CPU 的版本
$ pip install https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl
# 开启 GPU 支持的版本 (安装该版本的前提是已经安装了 CUDA sdk)
$ pip install https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.5.0-cp27-none-linux_x86_64.whl


源码编译:

克隆 TensorFlow 仓库

$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow

--recurse-submodules 参数是必须得, 用于获取 TesorFlow 依赖的 protobuf 库.html

Linux 安装

安装 Bazel

首先依照 教程 安装 Bazel 的依赖. 而后使用下列命令下载和编译 Bazel 的源码:python

$ git clone https://github.com/bazelbuild/bazel.git
$ cd bazel
$ git checkout tags/0.1.0$ ./compile.sh

上面命令中拉取的代码标签为 0.1.0, 兼容 Tensorflow 目前版本. bazel 的HEAD 版本 (即最新版本) 在这里可能不稳定.linux

将执行路径 output/bazel 添加到 $PATH 环境变量中.git

安装其余依赖

$ sudo apt-get install python-numpy swig python-dev

可选: 安装 CUDA (在 Linux 上开启 GPU 支持)

为了编译并运行可以使用 GPU 的 TensorFlow, 须要先安装 NVIDIA 提供的 Cuda Toolkit 7.0 和 CUDNN 6.5 V2.github

TensorFlow 的 GPU 特性只支持 NVidia Compute Capability >= 3.5 的显卡. 被支持的显卡 包括但不限于:api

  • NVidia Titan工具

  • NVidia Titan Xui

  • NVidia K20google

  • NVidia K40spa

下载并安装 Cuda Toolkit 7.0

下载地址

将工具安装到诸如 /usr/local/cuda 之类的路径.

下载并安装 CUDNN Toolkit 6.5

下载地址

解压并拷贝 CUDNN 文件到 Cuda Toolkit 7.0 安装路径下. 假设 Cuda Toolkit 7.0 安装 在 /usr/local/cuda, 执行如下命令:

tar xvzf cudnn-6.5-linux-x64-v2.tgz
sudo cp cudnn-6.5-linux-x64-v2/cudnn.h /usr/local/cuda/include
sudo cp cudnn-6.5-linux-x64-v2/libcudnn* /usr/local/cuda/lib64

配置 TensorFlow 的 Cuba 选项

从源码树的根路径执行:

$ ./configure
Do you wish to bulid TensorFlow with GPU support? [y/n] y
GPU support will be enabled for TensorFlow

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

Please specify the location where CUDNN 6.5 V2 library is installed. Refer to
README.md for more details. [default is: /usr/local/cuda]: /usr/local/cuda

Setting up Cuda include
Setting up Cuda lib64
Setting up Cuda bin
Setting up Cuda nvvm
Configuration finished

这些配置将创建到系统 Cuda 库的符号连接. 每当 Cuda 库的路径发生变动时, 必须从新执行上述 步骤, 不然没法调用 bazel 编译命令.

编译目标程序, 开启 GPU 支持

从源码树的根路径执行:

$ bazel build -c opt --config=cuda //tensorflow/cc:tutorials_example_trainer

$ bazel-bin/tensorflow/cc/tutorials_example_trainer --use_gpu# 大量的输出信息. 这个例子用 GPU 迭代计算一个 2x2 矩阵的主特征值 (major eigenvalue).# 最后几行输出和下面的信息相似.000009/000005 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]000006/000001 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]000009/000009 lambda = 2.000000 x = [0.894427 -0.447214] y = [1.788854 -0.894427]

注意, GPU 支持需经过编译选项 "--config=cuda" 开启.

已知问题
  • 尽管能够在同一个源码树下编译开启 Cuda 支持和禁用 Cuda 支持的版本, 咱们仍是推荐在 在切换这两种不一样的编译配置时, 使用 "bazel clean" 清理环境.

  • 在执行 bazel 编译前必须先运行 configure, 不然编译会失败并提示错误信息. 将来, 咱们可能考虑将 configure 步骤包含在编译过程当中, 以简化整个过程, 前提是 bazel 可以提供新的特性支持这样.

相关文章
相关标签/搜索