ubuntu 20.04LTS 编译tensorflow C++

ubuntu 20.04LTS 编译tensorflow C++

NOTE! 因为ubuntu在虚拟机里,我是用的笔记本的显卡比较落后,安装NVIDIA驱动失败,故未使用 GPU功能,仅使用了CPU模式;

编译得到libtensorflow_cc.so和libtensorflow_framework.so 是C++ 所需要的

tensorflow 和Bazel 以及python 对应的版本

Linux

CPU
VersionPython versionCompilerBuild tools
tensorflow-2.4.03.6-3.8GCC 7.3.1Bazel 3.1.0
tensorflow-2.3.03.5-3.8GCC 7.3.1Bazel 3.1.0
tensorflow-2.2.03.5-3.8GCC 7.3.1Bazel 2.0.0
tensorflow-2.1.02.7, 3.5-3.7GCC 7.3.1Bazel 0.27.1
tensorflow-2.0.02.7, 3.3-3.7GCC 7.3.1Bazel 0.26.1
tensorflow-1.15.02.7, 3.3-3.7GCC 7.3.1Bazel 0.26.1
tensorflow-1.14.02.7, 3.3-3.7GCC 4.8Bazel 0.24.1
tensorflow-1.13.12.7, 3.3-3.7GCC 4.8Bazel 0.19.2
tensorflow-1.12.02.7, 3.3-3.6GCC 4.8Bazel 0.15.0
tensorflow-1.11.02.7, 3.3-3.6GCC 4.8Bazel 0.15.0
tensorflow-1.10.02.7, 3.3-3.6GCC 4.8Bazel 0.15.0
tensorflow-1.9.02.7, 3.3-3.6GCC 4.8Bazel 0.11.0
tensorflow-1.8.02.7, 3.3-3.6GCC 4.8Bazel 0.10.0
tensorflow-1.7.02.7, 3.3-3.6GCC 4.8Bazel 0.10.0
tensorflow-1.6.02.7, 3.3-3.6GCC 4.8Bazel 0.9.0
tensorflow-1.5.02.7, 3.3-3.6GCC 4.8Bazel 0.8.0
tensorflow-1.4.02.7, 3.3-3.6GCC 4.8Bazel 0.5.4
tensorflow-1.3.02.7, 3.3-3.6GCC 4.8Bazel 0.4.5
tensorflow-1.2.02.7, 3.3-3.6GCC 4.8Bazel 0.4.5
tensorflow-1.1.02.7, 3.3-3.6GCC 4.8Bazel 0.4.2
tensorflow-1.0.02.7, 3.3-3.6GCC 4.8Bazel 0.4.2
GPU
VersionPython versionCompilerBuild toolscuDNNCUDA
tensorflow-2.4.03.6-3.8GCC 7.3.1Bazel 3.1.08.011.0
tensorflow-2.3.03.5-3.8GCC 7.3.1Bazel 3.1.07.610.1
tensorflow-2.2.03.5-3.8GCC 7.3.1Bazel 2.0.07.610.1
tensorflow-2.1.02.7, 3.5-3.7GCC 7.3.1Bazel 0.27.17.610.1
tensorflow-2.0.02.7, 3.3-3.7GCC 7.3.1Bazel 0.26.17.410.0
tensorflow_gpu-1.15.02.7, 3.3-3.7GCC 7.3.1Bazel 0.26.17.410.0
tensorflow_gpu-1.14.02.7, 3.3-3.7GCC 4.8Bazel 0.24.17.410.0
tensorflow_gpu-1.13.12.7, 3.3-3.7GCC 4.8Bazel 0.19.27.410.0
tensorflow_gpu-1.12.02.7, 3.3-3.6GCC 4.8Bazel 0.15.079
tensorflow_gpu-1.11.02.7, 3.3-3.6GCC 4.8Bazel 0.15.079
tensorflow_gpu-1.10.02.7, 3.3-3.6GCC 4.8Bazel 0.15.079
tensorflow_gpu-1.9.02.7, 3.3-3.6GCC 4.8Bazel 0.11.079
tensorflow_gpu-1.8.02.7, 3.3-3.6GCC 4.8Bazel 0.10.079
tensorflow_gpu-1.7.02.7, 3.3-3.6GCC 4.8Bazel 0.9.079
tensorflow_gpu-1.6.02.7, 3.3-3.6GCC 4.8Bazel 0.9.079
tensorflow_gpu-1.5.02.7, 3.3-3.6GCC 4.8Bazel 0.8.079
tensorflow_gpu-1.4.02.7, 3.3-3.6GCC 4.8Bazel 0.5.468
tensorflow_gpu-1.3.02.7, 3.3-3.6GCC 4.8Bazel 0.4.568
tensorflow_gpu-1.2.02.7, 3.3-3.6GCC 4.8Bazel 0.4.55.18
tensorflow_gpu-1.1.02.7, 3.3-3.6GCC 4.8Bazel 0.4.25.18
tensorflow_gpu-1.0.02.7, 3.3-3.6GCC 4.8Bazel 0.4.25.18

具体步骤

一 首先安装python3 环境, 如果你没有安装的话

sudo apt install python3-dev python3-pip

安装TensorFlow 2 .whl包需要一个pip版本>19.0

二 安装 Bazel

这里我才用的是安装程序文件来进行的.

安装程序可以从Bazel’s下载 https://github.com/bazelbuild/bazel/releases 选择上面你要编译的对应的bazel版本

安装程序包含Bazel二进制文件,并将其提取到**$HOME/bin**文件夹中。为了让Bazel工作,必须手动安装一些附加库。

如果没有以下环境. 需要进行安装

sudo apt install g++ unzip zip

如果想用Bazel 构建JAVA代码, 使用JDK

# Ubuntu 16.04 (LTS) uses OpenJDK 8 by default:
sudo apt-get install openjdk-8-jdk

# Ubuntu 18.04 (LTS) uses OpenJDK 11 by default:
sudo apt-get install openjdk-11-jdk

下载之后, 运行

chmod +x bazel-<version>-installer-linux-x86_64.sh
./bazel-<version>-installer-linux-x86_64.sh --user

The --user flag installs Bazel to the $HOME/bin directory on your system and sets the .bazelrc path to $HOME/.bazelrc. Use the --help command to see additional installation options.

当使用–user 来安装时, Bazel 会被安装到 $HOME/bin 路径下, 添加环境语句到环境**/.bashrc**或者**/.zshrc**变量里

可以使用VIM

vim ~/.bashrc

添加以下语句

export PATH="$PATH:$HOME/bin"

然后实质生效

source ~/.bashrc

三下载tensorflow源码

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

选择要安装的版本

git checkout branch_name  # r2.2, r2.3, r2.4 etc.

构建环境

./configure
configure 会话展示

这里要说明的是如果有CUDA,那么Do you wish to build Tensorflow with CUDA?一定要选Y,然后其它的我基本都是N

./configure
You 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-packages
Please 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]: Y
CUDA 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/include
Found cuDNN 7 in:
    /usr/lib/x86_64-linux-gnu
    /usr/include


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 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.1


Do 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

编译

配置好之后就进行编译:若采用CUDA 则如下

bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so

​ 这里如果不用cuda的话(前面配置的时候在CUDA那一项那里输入N),就输入:

bazel build --config=opt  //tensorflow:libtensorflow_cc.so

可能会遇到的问题:

  1. /usr/bin/env: ‘python‘: No such file or directory

    如果以及安装了python3的版本,可能是因为显示的是python3.X 但用的的名称为python

    所以可以建立一个软连接

    whereis python3
    # 如果安装的话,默认在/usr/bin/目录下
    sudo ln -s /usr/bin/python3 /usr/bin/python
    

接下来就是漫长的等待,

因为我没有用CUDA( NVIDIA的驱动没有安装上,所以用不了GPU加速) 耗时3+小时img

最终会在 当前目录下生成一个 bazel-bin 目录下 tensorflow 里 找到这两个文件

image-20210415213223587

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值