2016-10-14 2 views
0

tensorflow에 사용자 정의 된 op를 추가하려고했지만 파이썬에서로드 할 수 없습니다. 질문은 github의 닫힌 issue과 비슷하지만 문제가 해결되지 않았습니다.tensorflow에서 사용자 정의 된 op 공유 라이브러리를로드 할 수 없습니다.

운영 체제 : CUDA와 cuDNN의 맥 OS 10.12

설치된 버전 : 없음 소스에서 설치

TensorFlow 0.11.0.

#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 빌드 파일 :

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

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

그때 내가 실행

은 내가 add new op 튜토리얼 추가 zero_out.cc 파일을 다음

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

출력 :

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로 사용자 연산을 작성하는 대신 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이라는 개념을 사용합니다.

하지만 여전히 바젤을 사용하는 솔루션을 찾을 수 없습니다. 나는 GitHub에서 issue를 해고했다.

관련 문제