tensorflow原始碼編譯whl安裝檔案
技術標籤:tensorflow
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
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進行解決