Cannot load customized op shared lib in tensorflow
<p>I tried to add a customized op to tensorflow, but I cannot load it from python. The question is similar to the closed <a href="https://github.com/tensorflow/tensorflow/issues/2455" rel="nofollow">issue</a> in github, but the solution there did not solve my problem.</p>

<p>Operating System: macOS 10.12</p>

<p>Installed version of CUDA and cuDNN: None</p>

<p>TensorFlow 0.11.0 installed from source.</p>

<p>I followed the <a href="https://www.tensorflow.org/versions/r0.11/how_tos/adding_an_op/index.html" rel="nofollow">add new op</a> tutorial, adding zero_out.cc file:</p>

<pre><code>#include "tensorflow/core/framework/op.h"

REGISTER_OP("ZeroOut")
    .Input("to_zero: int32")
    .Output("zeroed: int32");

#include "tensorflow/core/framework/op_kernel.h"

using namespace tensorflow;

class ZeroOutOp : public OpKernel {
 public:
  explicit ZeroOutOp(OpKernelConstruction* context) : OpKernel(context) {}

  void Compute(OpKernelContext* context) override {
    // Grab the input tensor
    const Tensor&amp; input_tensor = context-&gt;input(0);
    auto input = input_tensor.flat&lt;int32&gt;();

    // Create an output tensor
    Tensor* output_tensor = NULL;
    OP_REQUIRES_OK(context, context-&gt;allocate_output(0, input_tensor.shape(),
                                                     &amp;output_tensor));
    auto output = output_tensor-&gt;flat&lt;int32&gt;();

    // Set all but the first element of the output tensor to 0.
    const int N = input.size();
    for (int i = 1; i &lt; N; i++) {
      output(i) = 0;
    }

    // Preserve the first input value if possible.
    if (N &gt; 0) output(0) = input(0);
  }
};

REGISTER_KERNEL_BUILDER(Name("ZeroOut").Device(DEVICE_CPU), ZeroOutOp);
</code></pre>

<p>and bazel BUILD file:</p>

<pre><code>load("//tensorflow:tensorflow.bzl", "tf_custom_op_library")

tf_custom_op_library(
    name = "zero_out.so",
    srcs = ["zero_out.cc"]
)
</code></pre>

<p>then I run:</p>

<pre><code>bazel build -c opt //tensorflow/core/user_ops:zero_out.so
</code></pre>

<p>output:</p>

<pre><code>INFO: Waiting for response from Bazel server (pid 28589)...
INFO: Found 1 target...
Target //tensorflow/core/user_ops:zero_out.so up-to-date:
  bazel-bin/tensorflow/core/user_ops/zero_out.so
INFO: Elapsed time: 5.115s, Critical Path: 0.00s
</code></pre>

<p>The generated shared library located in bazel-bin. When I tried to load it like this:</p>

<pre><code>tf.load_op_library('/Users/dtong/code/data/tensorflow/bazel-bin/tensorflow/core/user_ops/zero_out.so')
</code></pre>

<p>the result:</p>

<pre><code>python(41716,0x7fffb7e123c0) malloc: *** error for object 0x7f9e9cd2de18: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
</code></pre>

<p>Well, I found a solution. Instead of building user op by bazel, use g++.</p>

<pre><code>g++ -v -std=c++11 -shared zero_out.cc -o zero_out.so -fPIC -I $TF_INC -O2 -undefined dynamic_lookup -D_GLIBCXX_USE_CXX11_ABI=0
</code></pre>

<p>It will work. The reason seems like that my gcc version is too high (v6.2.0).</p>

<p>The <code>-D_GLIBCXX_USE_CXX11_ABI=0</code> is from the notion of <a href="https://www.tensorflow.org/versions/r0.11/how_tos/adding_an_op/index.html" rel="nofollow">official site</a> in 'Building the Op library' section.</p>

<p>But I still cannot find a solution using bazel. I fired an <a href="https://github.com/tensorflow/tensorflow/issues/4989" rel="nofollow">issue</a> in GitHub.</p>

python tensorflow