Tensorflow XLA AOT:Eigen 相关错误构建项目

Tensorflow XLA AOT: Eigen related Error Building Project

我目前正在尝试第一次提前完成 tensorflow XLA 编译工作流程,我在尝试构建包含 AOT 编译对象的最终可执行二进制文件时遇到了问题。

我使用 tutorial here 生成了 test_graph_tfgather.pbtest_graph_tfgather.config.pbtxt 文件。然后我直接使用tfcompile工具生成了MyClass.oMyClass.h。到目前为止一切顺利。

我现在正在构建一个包含此已编译模型的简单 makefile 项目,但我遇到了一些与 Eigen 相关的错误。这可能是因为我的电脑上安装了不同版本的 eigen3 吗?由于本征错误,我还不得不注释掉 Eigen::ThreadPool 行,因此某些版本未匹配可能是问题所在。有没有人以前见过这个问题或者有没有人知道如何让它工作?

谢谢。

构建错误:

g++ -c -std=c++11 -I . -I /usr/include/eigen3 -I /home/user/tensorflow_xla/tensorflow -I /usr/include main.cpp
In file included from /home/user/tensorflow_xla/tensorflow/tensorflow/compiler/xla/types.h:22:0,
                 from /home/user/tensorflow_xla/tensorflow/tensorflow/compiler/xla/executable_run_options.h:20,
                 from /home/user/tensorflow_xla/tensorflow/tensorflow/compiler/tf2xla/xla_compiled_cpu_function.h:22,
                 from MyClass.h:14,
                 from main.cpp:6:
/home/user/tensorflow_xla/tensorflow/tensorflow/core/framework/numeric_types.h: In static member function ‘static tensorflow::bfloat16 Eigen::NumTraits<tensorflow::bfloat16>::infinity()’:
/home/user/tensorflow_xla/tensorflow/tensorflow/core/framework/numeric_types.h:79:28: error: ‘infinity’ is not a member of ‘Eigen::NumTraits<float>’
     return FloatToBFloat16(NumTraits<float>::infinity());
                            ^
/home/user/tensorflow_xla/tensorflow/tensorflow/core/framework/numeric_types.h: In static member function ‘static tensorflow::bfloat16 Eigen::NumTraits<tensorflow::bfloat16>::quiet_NaN()’:
/home/user/tensorflow_xla/tensorflow/tensorflow/core/framework/numeric_types.h:83:28: error: ‘quiet_NaN’ is not a member of ‘Eigen::NumTraits<float>’
     return FloatToBFloat16(NumTraits<float>::quiet_NaN());
                            ^
/home/user/tensorflow_xla/tensorflow/tensorflow/core/framework/numeric_types.h: At global scope:
/home/user/tensorflow_xla/tensorflow/tensorflow/core/framework/numeric_types.h:95:34: error: ‘log’ is not a template function
     const tensorflow::bfloat16& x) {
                                  ^
/home/user/tensorflow_xla/tensorflow/tensorflow/core/framework/numeric_types.h:101:34: error: ‘exp’ is not a template function
     const tensorflow::bfloat16& x) {
                                  ^
/home/user/tensorflow_xla/tensorflow/tensorflow/core/framework/numeric_types.h:107:34: error: ‘abs’ is not a template function
     const tensorflow::bfloat16& x) {
                                  ^
Makefile:10: recipe for target 'main.o' failed

main.cpp 来源:

#define EIGEN_USE_THREADS
#define EIGEN_USE_CUSTOM_THREAD_POOL

#include <iostream>
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "MyClass.h" // generated

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

  MyClass 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;
}

生成文件

EIGEN_INC=-I /usr/include/eigen3
TF_INC=-I /home/user/tensorflow_xla/tensorflow
CPPFLAGS=-c -std=c++11

xla_hw: main.o MyClass.o
    g++ -o xla_hw main.o MyClass.o

main.o: main.cpp
    g++ $(CPPFLAGS) -I . $(TF_INC) $(EIGEN_INC) -I /usr/include main.cpp

我现在已经解决了这个问题,原来有一个特定版本的 eigen3 包含在 tensorflow 中,你需要使用这个版本才能工作。构建 tensorflow 后,正确版本的 eigen3 位于 <tensorflow path>bazel-tensorflow/external/eigen_archive

下面是工作生成文件,其中包含正确的 Eigen 路径以及 link 项目所需的库。

TF_INC=-I /home/user/tensorflow_xla/tensorflow/bazel-tensorflow/external/eigen_archive -I /home/user/tensorflow_xla/tensorflow

TF_LIBS=-L/home/user/tensorflow_xla/tensorflow/bazel-bin/tensorflow/compiler/tf2xla/ -lxla_compiled_cpu_function -L/home/user/tensorflow_xla/tensorflow/bazel-bin/tensorflow/compiler/aot -lruntime

CPPFLAGS=-c -std=c++11

xla_hw: main.o MyClass.o
    g++ -o xla_hw main.o MyClass.o $(TF_LIBS)

main.o: main.cpp
    g++ $(CPPFLAGS) -I . $(TF_INC) -I /usr/include main.cpp