1.首先安装bazel
如果linux系统上没有bazel,可参考https://blog.csdn.net/qq_41204464/article/details/95333396进行安装。建议采用“用二进制安装程序安装”
2.下载tensorflow源文件
从https://github.com/tensorflow/tensorflow/tags中选择需要编译的tensorflow版本
3.配置tensroflow库
进入tensorflow源码根目录,运行 ./configure 进行配置,根据需求进行配置,以下是我的配置。从下面看出,所有配置我都选择no,因为我都不需要,连cuda我也不要,因为我要编译的tensorflow是要用在cpu上面的。
You have bazel 0.15.0 installed.
Please specify the location of python. [Default is /media/huaxin/tcl2/nlp/anaconda3/bin/python]:
Found possible Python library paths:
/media/huaxin/tcl2/nlp/anaconda3/lib/python3.6/site-packages
Please input the desired Python library path to use. Default is [/media/huaxin/tcl2/nlp/anaconda3/lib/python3.6/site-packages]
Do you wish to build TensorFlow with Apache Ignite support? [Y/n]: n
No Apache Ignite support will be enabled for TensorFlow.
Do you wish to build TensorFlow with XLA JIT support? [Y/n]: n
No XLA JIT support will be enabled for TensorFlow.
Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n
No OpenCL SYCL support will be enabled for TensorFlow.
Do you wish to build TensorFlow with ROCm support? [y/N]: n
No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: n
No CUDA support will be enabled for TensorFlow.
Do you wish to download a fresh release of clang? (Experimental) [y/N]: n
Clang will not be downloaded.
Do you wish to build TensorFlow with MPI support? [y/N]: n
No MPI 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]:
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: 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 tools/bazel.rc for more details.
--config=mkl # Build with MKL support.
--config=monolithic # Config for mostly static monolithic build.
--config=gdr # Build with GDR support.
--config=verbs # Build with libverbs support.
--config=ngraph # Build with Intel nGraph support.
Configuration finished
4.编译tensroflow库
由于我需要将mkl库加到tensorflow中,所以使用一下命令进行编译
bazel build --config=mkl -c opt --copt=-march=native //tensorflow/tools/pip_package:build_pip_package
5.生成whl文件
通过一下命令,生成whl文件,[path_to_save_wheel]为生成的whl文件的保存路径
bazel-bin/tensorflow/tools/pip_package/build_pip_package ~/path_to_save_wheel
6.使用pip命令进行安装即可
7.出现的坑
在第4步中,出现下述报错,可参考https://www.freesion.com/article/5917370408/和https://blog.csdn.net/weixin_39634443/article/details/111109185进行解决