更新:我认为问题需要了解CMake而不是TensorFlow,因此我添加了CMake标签。
TL; DR:
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
。
能请你帮忙吗? 提前非常感谢您!