从源代码构建 TensorFlow pip 软件包并将其安装在 Ubuntu Linux 和 macOS 上。尽管这些说明可能适用于其他系统,但仅针对 Ubuntu 和 macOS 进行了测试并在这两种平台上受支持。

注意:我们已针对 Linux 和 macOS 系统提供了经过充分测试的预构建 TensorFlow 软件包。Linux 和 macOS 设置

安装以下构建工具以配置开发环境。

安装 Python 和 TensorFlow 软件包依赖项

Ubuntusudo apt install python3-dev python3-pipmacOS

需要 Xcode 9.2 或更高版本。

使用 Homebrew 软件包管理器安装:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"export PATH="/usr/local/opt/python/libexec/bin:$PATH"# if you are on macOS 10.12 (Sierra) use export PATH="/usr/local/bin:/usr/local/sbin:$PATH"brew install python

安装 TensorFlow pip 软件包依赖项(如果使用虚拟环境,请省略 --user 参数):

pip install -U --user pip numpy wheelpip install -U --user keras_preprocessing --no-deps注意:必须使用 pip 19.0 以上的版本才能安装 TensorFlow 2 .whl 软件包。请参见 setup.py 文件中的 REQUIRED_PACKAGES 部分,了解其他必需的依赖项。安装 Bazel

您需要安装 Bazel,才能构建 TensorFlow。您可以使用 Bazelisk 轻松安装 Bazel,并且 Bazelisk 可以自动为 TensorFlow 下载合适的 Bazel 版本。为便于使用,请在 PATH 中将 Bazelisk 添加为 bazel 可执行文件。

如果没有 Bazelisk,您可以手动安装 Bazel。请务必安装受支持的 Bazel 版本,可以是 tensorflow/configure.py 中指定的介于 _TF_MIN_BAZEL_VERSION 和 _TF_MAX_BAZEL_VERSION 之间的任意版本。

安装 GPU 支持项(可选,仅限 Linux)

没有针对 macOS 的 GPU 支持版本。

如需安装在 GPU 上运行 TensorFlow 所需的驱动程序和其他软件,请参阅 GPU 支持指南。

注意:您可以轻松设置 TensorFlow 的某个支持 GPU 的 Docker 映像。下载 TensorFlow 源代码

使用 Git 克隆 TensorFlow 代码库:

git clone https://github.com/tensorflow/tensorflow.gitcd tensorflow

该代码库默认为 master 开发分支。您也可以检出要构建的版本分支:

git checkout branch_name # r2.2, r2.3, etc.配置 build

通过运行 TensorFlow 源代码树根目录下的 ./configure 配置系统 build。此脚本会提示您指定 TensorFlow 依赖项的位置,并要求指定其他构建配置选项(例如,编译器标记)。

./configure

如果使用的是虚拟环境,python configure.py 会优先处理环境内的路径,而 ./configure 会优先处理环境外的路径。在这两种情况下,您都可以更改默认设置。

示例会话

以下代码展示了 ./configure 脚本的示例运行会话(您的会话可能会有所不同):

查看示例配置会话./configureYou have bazel 3.0.0 installed.Please specify the location of python. [Default is /usr/bin/python3]:Found possible Python library paths: /usr/lib/python3/dist-packages /usr/local/lib/python3.6/dist-packagesPlease input the desired Python library path to use. Default is [/usr/lib/python3/dist-packages]Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]:No OpenCL SYCL support will be enabled for TensorFlow.Do you wish to build TensorFlow with ROCm support? [y/N]:No ROCm support will be enabled for TensorFlow.Do you wish to build TensorFlow with CUDA support? [y/N]: YCUDA support will be enabled for TensorFlow.Do you wish to build TensorFlow with TensorRT support? [y/N]:No TensorRT support will be enabled for TensorFlow.Found CUDA 10.1 in: /usr/local/cuda-10.1/targets/x86_64-linux/lib /usr/local/cuda-10.1/targets/x86_64-linux/includeFound cuDNN 7 in: /usr/lib/x86_64-linux-gnu /usr/includePlease 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 Each capability can be specified as "x.y" or "compute_xy" to include both virtual and binary GPU code, or as "sm_xy" to only include the binary code.Please note that each additional compute capability significantly increases your build time and binary size, and that TensorFlow only supports compute capabilities >= 3.5 [Default is: 3.5,7.0]: 6.1Do you want to use clang as CUDA compiler? [y/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]:Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]: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 .bazelrc for more details. --config=mkl # Build with MKL support. --config=monolithic # Config for mostly static monolithic build. --config=ngraph # Build with Intel nGraph support. --config=numa # Build with NUMA support. --config=dynamic_kernels # (Experimental) Build kernels into separate shared objects. --config=v2 # Build TensorFlow 2.x instead of 1.x.Preconfigured Bazel build configs to DISABLE default on features: --config=noaws # Disable AWS S3 filesystem support. --config=nogcp # Disable GCP support. --config=nohdfs # Disable HDFS support. --config=nonccl # Disable NVIDIA NCCL support.Configuration finished配置选项GPU 支持

为了支持 GPU,请在配置过程中设置 cuda=Y 并指定 CUDA 和 cuDNN 的版本。如果您的系统安装了多个版本的 CUDA 或 cuDNN,请明确设置版本,而不是依赖于默认版本。./configure 会创建指向系统 CUDA 库的符号


比丘资源网 » 从源代码构建  

发表回复

提供最优质的资源集合

立即查看 了解详情