编译TensorFlow自定义运算符(TF 2.0.0)

时间:2019-09-30 20:07:08

标签: tensorflow cmake compiler-errors

更新:我认为问题需要了解CMake而不是TensorFlow,因此我添加了CMake标签。

TL; DR:

  • 我的TF自定义运算符正在TF 1.14中工作。
  • 使用TF 2.0.0编译时:仅当我创建指向libtensorflow_framework.so的符号链接libtensorflow_framework.so.2时,它才起作用。
  • 我尝试使用tf.sysconfig.get_link_flags()tf.sysconfig.get_compile_flags()来获取库的路径:构建成功,但是运行时undefined symbol: _ZTIN10tensorflow8OpKernelE

在TF 2.0.0中使用custom TF operator时遇到问题。 CMakeLists.txt看起来像这样:

cmake_minimum_required(VERSION 3.9)

project(myproject LANGUAGES CXX CUDA)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CUDA_STANDARD 11)
set(CUDA_SEPARABLE_COMPILATION ON)

if(UNIX AND NOT APPLE)
    set(LINUX TRUE)
endif()

include_directories(${CUDA_INCLUDE})
execute_process(COMMAND python -c "import tensorflow as tf; print(tf.sysconfig.get_include())" OUTPUT_VARIABLE TF_INC)
execute_process(COMMAND python -c "import tensorflow as tf; print(tf.sysconfig.get_lib())" OUTPUT_VARIABLE TF_LIB)
string(REGEX REPLACE "\n$" "" TF_INC "${TF_INC}")
string(REGEX REPLACE "\n$" "" TF_LIB "${TF_LIB}")
include_directories(${TF_INC} ${TF_INC}/external/nsync/public)
link_directories(${TF_LIB})

add_library(my_operator SHARED src/*.cpp src/*.cu src/*.h)
target_compile_features(my_operator PUBLIC cxx_std_11)
target_link_libraries(my_operator PUBLIC tensorflow_framework)
set_target_properties(my_operator PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
set_target_properties(my_operator PROPERTIES PREFIX "")
target_compile_options(my_operator PUBLIC "-D GOOGLE_CUDA=1" "-D GLIBCXX_USE_CXX11_ABI=0")
# Workaround (TensorFlow bug)
target_compile_options(my_operator PUBLIC "-DNDEBUG")

if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.0)
    target_compile_options(my_operator PUBLIC "-D_GLIBCXX_USE_CXX11_ABI=0")
endif()

在TF 1.14中运行良好。升级到TF 2.0.0之后,我得到了一个ltensorflow_framework not found错误,这是因为它在我的virtualenv libtensorflow_framework.so中找不到~/.env/python3/lib/python3.5/site-packages/tensorflow_core/。为了使其正常工作,我必须创建一个符号链接libtensorflow_framework.so指向同一文件夹中存在的文件libtensorflow_framework.so.2。更好的解决方案是使用类似这样的方法来获取库的路径:

execute_process(COMMAND python -c "import tensorflow as tf; print(' '.join(tf.sysconfig.get_compile_flags()), end='')" OUTPUT_VARIABLE TF_CFLAGS)
execute_process(COMMAND python -c "import tensorflow as tf; print(' '.join(tf.sysconfig.get_link_flags()), end='')" OUTPUT_VARIABLE TF_CFLAGS)
# C++11 required for tensorflow
set(CMAKE_CXX_FLAGS "-std=c++11 ${TF_CFLAGS} ${CMAKE_CXX_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-as-needed ${TF_LFLAGS} ${CMAKE_SHARED_LINKER_FLAGS}") 

我在link_directories(${TF_LIB})行之后添加了此选项,但这没有用。我尝试将target_link_libraries(lattice_filter PUBLIC tensorflow_framework)替换为target_link_libraries(lattice_filter PUBLIC ${TF_LFLAGS}),但是它也不起作用。构建成功(没有链接或其他错误),但是运行代码时,我得到了undefined symbol: _ZTIN10tensorflow8OpKernelE

能请你帮忙吗? 提前非常感谢您!

0 个答案:

没有答案