tensorflow: Custom Reader Op results in undefined symbol: _ZTIN10tensorflow8OpKernelE on library load

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Linux Ubuntu 16.04
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
  • TensorFlow installed from (source or binary): Docker container, also tried source
  • TensorFlow version: 1.12.0
  • Python version: 2.7
  • Installed using virtualenv? pip? conda?: pip
  • Bazel version (if compiling from source): 0.18
  • GCC/Compiler version (if compiling from source): 5.4
  • CUDA/cuDNN version: 9/7
  • GPU model and memory: 1080ti

I have a custom Reader op that inherits ReaderBase class (tensorflow/core/framework/reader_base.h). The op compiled and worked as expected under TF 1.3 and earlier.

I have now upgraded to TF1.12.0. Shared library for the custom op builds successfully, but loading it with import tensorFlow as tf; tf.load_op_library() fails due to missing symbols: tensorflow.python.framework.errors_impl.NotFoundError: ./libjson_record_reader_op.so: undefined symbol: _ZTIN10tensorflow8OpKernelE

Actually, at first it was reporting undefined symbol ReaderBaseE, but now reporting OpKernelE.

I tried building the op in the following Docker containers with the following tags: 1.12.0, 1.12.0-devel, 1.12.0-gpu-devel.

I have also built TF from source with bazel 0.18 and installed via pip (also in tensorflow/tensorflow-devel-gpu container) with the same result as above.

Building custom op as per current instructions in the docs using g++. I tried building with g++ 5.5, 5.4, 4.8, 4.9 with the same result.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 19 (6 by maintainers)

Most upvoted comments

No, sorry, I was not able to make it work and moved to PyTorch since.

@agnz could you add -D_GLIBCXX_USE_CXX11_ABI=0 to your g++ flag?

@yifeif - Sorry for a very delayed response.

This flag is already automatically added from TF_CFLAGS…

Please see below a full set of steps to reproduce the problem. Steps are taken from this guide: https://github.com/tensorflow/custom-op

docker pull tensorflow/tensorflow:custom-op
docker run -it tensorflow/tensorflow:custom-op /bin/bash

Then inside the container

root@be9528464b95:/custom-op#  cd custom-op
root@be9528464b95:/custom-op# make pip_pkg
g++ -I/usr/local/lib/python2.7/dist-packages/tensorflow/include -D_GLIBCXX_USE_CXX11_ABI=0 -fPIC -O2 -std=c++11 -o tensorflow_zero_out/python/ops/_zero_out_ops.so tensorflow_zero_out/cc/kernels/zero_out_kernels.cc tensorflow_zero_out/cc/ops/zero_out_ops.cc -shared -L/usr/local/lib/python2.7/dist-packages/tensorflow -ltensorflow_framework
### intermediate output removed
root@be9528464b95:/custom-op# pip install artifacts/*.whl
root@be9528464b95:/custom-op# cd ..
root@be9528464b95:/# python -c "import tensorflow as tf;import tensorflow_zero_out as zero_out_module;print(zero_out_module.zero_out([[1,2], [3,4]]).eval(session=tf.Session()))"
[[1 0]
 [0 0]]
root@be9528464b95:/custom-op# pip uninstall tensorflow_zero_out

Everything works as expected. Now modifying zero_out_kernel.cc to include an additional SimpleReader custom op that inherits from ReaderBase (see source below). It does absolutely nothing and is used just for demonstration purposes.

root@be9528464b95:/custom-op# git diff tensorflow_zero_out/cc/kernels/zero_out_kernels.cc
diff --git a/tensorflow_zero_out/cc/kernels/zero_out_kernels.cc b/tensorflow_zero_out/cc/kernels/zero_out_kernels.cc
index 5627142..3338574 100644
--- a/tensorflow_zero_out/cc/kernels/zero_out_kernels.cc
+++ b/tensorflow_zero_out/cc/kernels/zero_out_kernels.cc
@@ -14,6 +14,8 @@ limitations under the License.
 ==============================================================================*/

 #include "tensorflow/core/framework/op_kernel.h"
+#include "tensorflow/core/framework/reader_op_kernel.h"
+#include "tensorflow/core/framework/reader_base.h"

 using namespace tensorflow;

@@ -44,3 +46,42 @@ class ZeroOutOp : public OpKernel {
 };

 REGISTER_KERNEL_BUILDER(Name("ZeroOut").Device(DEVICE_CPU), ZeroOutOp);
+
+
+class SimpleReader : public ReaderBase {
+ public:
+  SimpleReader(const string& node_name)
+      : ReaderBase(strings::StrCat("SimpleReader '", node_name, "'")) {}
+
+  Status OnWorkStartedLocked() override {
+    return Status::OK();
+  }
+
+  Status OnWorkFinishedLocked() override {
+    return Status::OK();
+  }
+
+  Status ReadLocked(string* key, string* value, bool* produced,
+                    bool* at_end) override {
+    return Status::OK();
+  }
+
+  Status ResetLocked() override {
+    return ReaderBase::ResetLocked();
+  }
+
+};
+
+class SimpleReaderOp : public ReaderOpKernel {
+ public:
+  explicit SimpleReaderOp(OpKernelConstruction* context)
+      : ReaderOpKernel(context) {
+    SetReaderFactory([this]() {
+      return new SimpleReader(name());
+    });
+  }
+};
+
+REGISTER_KERNEL_BUILDER(Name("SimpleReader").Device(DEVICE_CPU),
+                        SimpleReaderOp);

Trying to compile and run zero_out op again - this part is identical to the first set of steps, except an additional op has been compiled.

root@be9528464b95:/custom-op# vim tensorflow_zero_out/cc/kernels/zero_out_kernels.cc
root@be9528464b95:/custom-op# make pip_pkg
g++ -I/usr/local/lib/python2.7/dist-packages/tensorflow/include -D_GLIBCXX_USE_CXX11_ABI=0 -fPIC -O2 -std=c++11 -o tensorflow_zero_out/python/ops/_zero_out_ops.so tensorflow_zero_out/cc/kernels/zero_out_kernels.cc tensorflow_zero_out/cc/ops/zero_out_ops.cc -shared -L/usr/local/lib/python2.7/dist-packages/tensorflow -ltensorflow_framework
root@be9528464b95:/custom-op# pip install artifacts/*.whl
root@be9528464b95:/custom-op# python -c "import tensorflow as tf;import tensorflow_zero_out as zero_out_module;print(zero_out_module.zero_out([[1,2], [3,4]]).eval(session=tf.Session()))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "tensorflow_zero_out/__init__.py", line 19, in <module>
    from tensorflow_zero_out.python.ops.zero_out_ops import zero_out
  File "tensorflow_zero_out/python/ops/zero_out_ops.py", line 25, in <module>
    resource_loader.get_path_to_datafile('_zero_out_ops.so'))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/load_library.py", line 60, in load_op_library
    lib_handle = py_tf.TF_LoadLibrary(library_filename)
tensorflow.python.framework.errors_impl.NotFoundError: tensorflow_zero_out/python/ops/_zero_out_ops.so: undefined symbol: _ZTIN10tensorflow10ReaderBaseE