1. 程式人生 > >如何編譯執行tensorflow的demo

如何編譯執行tensorflow的demo

1.安裝編譯工具bazel,具體可以參照官方教程。
https://docs.bazel.build/versions/master/install-ubuntu.html
2. 配置tensorflow的編譯環境
執行tensorflow目錄下的configure檔案,根據自己的環境進行配置。比如下面這樣:

**root@fly-virtual-machine:/home/share/tensorflow# ./configure** 
Please specify the location of python. [Default is /root/anaconda3/bin//python]: /root/anaconda3/bin/python
**Do
you wish to use jemalloc as the malloc implementation? (Linux only) [Y/n] y** jemalloc enabled on Linux **Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] n** No Google Cloud Platform support will be enabled for TensorFlow **Do you wish to build TensorFlow with Hadoop File System support? [y/N] n** No
Hadoop File System support will be enabled for TensorFlow **Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N] n** No XLA JIT support will be enabled for TensorFlow Found possible Python library paths: **/root/anaconda3/lib/python3.5/site-packages** Please input the desired Python library path to
use. Default is [/root/anaconda3/lib/python3.5/site-packages] Using python library path: /root/anaconda3/lib/python3.5/site-packages Do you wish to build TensorFlow with O**重點內容**penCL support? [y/N] n No OpenCL support will be enable**重點內容**d for TensorFlow Do you wish to build TensorFlow with CUDA support? [y/N] n No CUDA support will be enabled for TensorFlow Configuration finished INFO: Starting clean (this may take a while). Consider using --expunge_async if the clean takes more than several minutes. ............... Building: no action running Building: no action running Building: no action running Building: no action running Building: no action running Building: no action running Building: no action running Building: no action running
  1. 編譯tensorflow動態庫
    按照bazel的語法規則,找到相應的BUILD檔案,路徑為tensorflow/BUILD。可以看到
cc_binary(
    name = "libtensorflow_cc.so",
    linkshared = 1,
    deps = [
        "//tensorflow/c:c_api",
        "//tensorflow/cc:cc_ops",
        "//tensorflow/core:tensorflow",
    ],

當前tensorflow/BUILD目錄下執行bazel build :libtensorflow.so,執行效果如下:
這裡寫圖片描述

編譯時間大概2000秒左右,看電腦效能,本次編譯採用雙核心四執行緒,4GB記憶體。
執行完成後,會在WORKSPACE檔案所在目錄生成以下幾個目錄:bazel-bin ,bazel-genfiles,bazel-out, bazel-tensorflow-master和 bazel-testlogs。進入bazel-out/local-py3-fastbuild/bin/tensorflow目錄可以看到libtensorflow_cc.so動態庫。
4. 編譯demo,以tensorflow\cc為例,編譯c++的demo
同樣在該目錄下找到BUILD檔案,找到要編譯demo的bazel定義

tf_cc_test(
    name = "client_client_session_test",
    srcs = ["client/client_session_test.cc"],
    deps = [
        ":cc_ops",
        ":client_session",
        "//tensorflow/core:all_kernels",
        "//tensorflow/core:core_cpu_internal",
        "//tensorflow/core:framework",
        "//tensorflow/core:lib",
        "//tensorflow/core:tensorflow",
        "//tensorflow/core:test",
        "//tensorflow/core:test_main",
        "//tensorflow/core:testlib",
    ],

在BUILD目錄位置執行bazel build :client_client_session_test,最終的結果生成路徑如下:tensorflow-master/bazel-out/local-py3-fastbuild/bin/tensorflow/cc,該路徑下面有可執行程式client_client_session_test,執行成功後的結果如下

root@fly-virtual-machine:/home/fly/tensorPro/tensorflow-master/tensorflow-master/bazel-out/local-py3-fastbuild/bin/tensorflow/cc# ./client_client_session_test
add by zgh, Running main() from test_main.cc
[==========] Running 4 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from ClientSessionTest
[ RUN      ] ClientSessionTest.Basic
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
[       OK ] ClientSessionTest.Basic (184 ms)
[ RUN      ] ClientSessionTest.Feed
[       OK ] ClientSessionTest.Feed (35 ms)
[ RUN      ] ClientSessionTest.Extend
[       OK ] ClientSessionTest.Extend (15 ms)
[ RUN      ] ClientSessionTest.MultiThreaded
[       OK ] ClientSessionTest.MultiThreaded (39 ms)
[----------] 4 tests from ClientSessionTest (273 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (274 ms total)
[  PASSED  ] 4 tests.

補充bazel的清除命令為:bazel clean