首页 > 解决方案 > “tensorflow/core/framework/common_shape_fns.h:没有这样的文件或目录”在tensorflow中添加自定义操作时

问题描述

我正在尝试使用此Tensorflow Doc在Google Colab的 Tensorflow 中添加自定义操作。但是,在构建时出现此错误。

2021-04-05 04:24:26.500483: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
2021-04-05 04:24:29.436586: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.11.0
xor_op.cc:2:10: fatal error: tensorflow/core/framework/common_shape_fns.h: No such file or directory
 #include "tensorflow/core/framework/common_shape_fns.h"
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

构建命令是,

$ TF_LFLAGS=($(python -c 'import tensorflow as tf; print(" ".join(tf.sysconfig.get_link_flags()))'))
$ TF_CFLAGS=($(python -c 'import tensorflow as tf; print(" ".join(tf.sysconfig.get_compile_flags()))'))
$ 
$ g++ -std=c++14 -shared xor_op.cc -o xor_op.so -fPIC ${TF_CFLAGS[@]} ${TF_LFLAGS[@]} -O2

知道在这种情况下有什么问题吗?

标签: pythonc++tensorflowgoogle-colaboratorycustom-operator

解决方案


使用以下步骤在 Google Colab 中运行它。基本上,我们将在构建中删除变量(例如,TF_LFLAGS、TF_CFLAGS)的使用,并使用直接命令构建它。

  1. 添加.cpp 文件。
# create empty file and copy and paste code
! touch ./xor_op.cc

或者使用魔法操作,

%%writefile xor_op.cc

#And contents of the file
  1. 从 TensorFlow 获取编译标志。
tf.sysconfig.get_compile_flags()
# output: ['-I/usr/local/lib/python3.7/dist-packages/tensorflow/include',
# '-D_GLIBCXX_USE_CXX11_ABI=0']
  1. 从 TensorFlow 获取链接标志。
tf.sysconfig.get_link_flags()
# output: ['-L/usr/local/lib/python3.7/dist-packages/tensorflow',
# '-l:libtensorflow_framework.so.2']
  1. 构建操作。
! g++ -std=c++14 -shared \
    xor_op.cc \
    -o xor_op.so \
    -fPIC \
    -I/usr/local/lib/python3.7/dist-packages/tensorflow/include \
    -D_GLIBCXX_USE_CXX11_ABI=0 \
    -L/usr/local/lib/python3.7/dist-packages/tensorflow \
    -l:libtensorflow_framework.so.2 \
    -O2

现在这将创建预期的 .so 文件。gobrewers14在另一个问题的评论部分指出了这一点。


推荐阅读