很久没编源码了,每次都会遇到编译问题…

本次编译主要是为了运行XLA,并且只编CPU版本,所以configure的时候除了XLA,其他全选N。

bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

第1个是少了个io_bazel_rules_docker

https://github.com/tensorflow/tensorflow/issues/28824

参考上面的链接,在源码根目录下WORKSPACE内添加如下内容:

diff --git a/WORKSPACE b/WORKSPACE
index 868421d..615ce91 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -28,6 +28,14 @@ load(

 bazel_toolchains_repositories()

+http_archive(
+    name = "io_bazel_rules_docker",
+    sha256 = "aed1c249d4ec8f703edddf35cbe9dfaca0b5f5ea6e4cd9e83e99f3b0d1136c3d",
+    strip_prefix = "rules_docker-0.7.0",
+    urls = ["https://github.com/bazelbuild/rules_docker/archive/v0.7.0.tar.gz"],
+)
+
+
 load(
     "@io_bazel_rules_docker//repositories:repositories.bzl",
     container_repositories = "repositories",

第2个错误是某些包找不到,发现是网络不好导致的,多试几次就过了。

https://github.com/tensorflow/tensorflow/issues/24135

第3个错误是找不到 //tools/target_cpu包,需要修改文件 tensorflow/compiler/aot/tfcompile.bzl

diff --git a/tensorflow/compiler/aot/tfcompile.bzl b/tensorflow/compiler/aot/tfcompile.bzl
index e7f3c0a..60d09f8 100644
--- a/tensorflow/compiler/aot/tfcompile.bzl
+++ b/tensorflow/compiler/aot/tfcompile.bzl
@@ -183,10 +183,10 @@ def tf_library(
     # Pass --target_cpu=haswell to tfcompile if compiling for Haswell (bazel
     # build --cpu=haswell).  We put it at the beginning of the flags list so
     # that tfcompile_flags can override if if desired.
-    flags = select({
-        "//tools/target_cpu:haswell": "--target_cpu=haswell ",
-        "//conditions:default": "",
-    }) + flags
+    #flags = select({
+    #    "//tools/target_cpu:haswell": "--target_cpu=haswell ",
+    #    "//conditions:default": "",
+    #}) + flags

     if enable_xla_hlo_profiling:
         profiling_flag = "--xla_hlo_profile"

修改完之后,可以测试一下

bazel build //tensorflow/compiler/aot:tfcompile
bazel test //tensorflow/compiler/aot:all_tests
bazel test tensorflow/compiler/aot/tests:tfcompile_test

运行官方示例:

https://www.tensorflow.org/xla/tfcompile

一共有4步,实际上不需要这么复杂:

1 首先在文件:tensorflow/compiler/aot/tests/BUILD 中添加

diff --git a/tensorflow/compiler/aot/tests/BUILD b/tensorflow/compiler/aot/tests/BUILD
index 7146fe4..caaf65f 100644
--- a/tensorflow/compiler/aot/tests/BUILD
+++ b/tensorflow/compiler/aot/tests/BUILD
@@ -248,6 +248,27 @@ tf_library(
     ],
 )

+tf_library(
+    name = "my_graph_tfmatmul",
+    config = "test_graph_tfmatmul.config.pbtxt",
+    cpp_class = "foo::bar::MatMulComp",
+    graph = "test_graph_tfmatmul.pb",
+)
+
+cc_binary(
+    name = "my_binary",
+    srcs = [
+        "my_code.cc",  # include test_graph_tfmatmul.h to access the generated header
+    ],
+    deps = [
+        ":my_graph_tfmatmul",  # link in the generated object file
+        "//third_party/eigen3",
+    ],
+    linkopts = [
+          "-lpthread",
+    ]
+)
+
 tf_cc_test(
     name = "tfcompile_test",
     srcs = ["tfcompile_test.cc"],

2 添加如下文件:tensorflow/compiler/aot/tests/http://my_code.cc

#define EIGEN_USE_THREADS
#define EIGEN_USE_CUSTOM_THREAD_POOL

#include 
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "tensorflow/compiler/aot/tests/my_graph_tfmatmul.h" // generated

int main(int argc, char** argv) {
  Eigen::ThreadPool tp(2);  // Size the thread pool as appropriate.
  Eigen::ThreadPoolDevice device(&tp, tp.NumThreads());

  foo::bar::MatMulComp matmul;
  matmul.set_thread_pool(&device);

  // Set up args and run the computation.
  const float args[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
  std::copy(args + 0, args + 6, matmul.arg0_data());
  std::copy(args + 6, args + 12, matmul.arg1_data());
  matmul.Run();

  // Check result
  if (matmul.result0(0, 0) == 58) {
    std::cout << "Success" << std::endl;
  } else {
    std::cout << "Failed. Expected value 58 at 0,0. Got:"
              << matmul.result0(0, 0) << std::endl;
  }

  return 0;
}

3 编译 & 运行

bazel build //tensorflow/compiler/aot/tests:my_binary

bazel-bin/tensorflow/compiler/aot/tests/my_binary

original refer