2016-10-14 6 views
0

tensorflowにカスタマイズされたopを追加しようとしましたが、Pythonからロードできません。問題はgithubのclosed issueと似ていますが、そこの解決策は私の問題を解決しませんでした。tensorflowでカスタマイズされたop共有ライブラリを読み込めません

オペレーティングシステム:CUDAとcuDNNのMacOSの10.12

インストールバージョン:ソースからインストールなし

TensorFlow 0.11.0。

私はzero_out.ccファイルの追加、add new opチュートリアルに従っ:その後、私は実行

load("//tensorflow:tensorflow.bzl", "tf_custom_op_library") 

tf_custom_op_library(
    name = "zero_out.so", 
    srcs = ["zero_out.cc"] 
) 

bazel build -c opt //tensorflow/core/user_ops:zero_out.so 

出力:

#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& input_tensor = context->input(0); 
    auto input = input_tensor.flat<int32>(); 

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

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

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

REGISTER_KERNEL_BUILDER(Name("ZeroOut").Device(DEVICE_CPU), ZeroOutOp); 

とbazelのビルドファイルを

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 

生成された共有ライブラリは、bazel-binにあります。

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

結果:私はこのようにそれをロードしようとしたとき

まあ
python(41716,0x7fffb7e123c0) malloc: *** error for object 0x7f9e9cd2de18: pointer being freed was not allocated 
*** set a breakpoint in malloc_error_break to debug 

答えて

0

、私は解決策を見つけました。 bazelでユーザーopを構築する代わりに、g ++を使用します。

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 

これが機能します。理由は、gccのバージョンが高すぎるようです(v6.2.0)。

-D_GLIBCXX_USE_CXX11_ABI=0は、「Opライブラリの構築」セクションのofficial siteという概念からのものです。

しかし、私はまだbazelを使用して解決策を見つけることができません。私はGitHubでissueを解雇した。

関連する問題