簡體   English   中英

TensorFlow 自定義 C++ op 帶資源句柄

[英]TensorFlow custom C++ op with resource handle

Python代碼:


import os
import sys
from subprocess import check_call
import tensorflow as tf


CC_NAME = "tf-resource-op.cc"
SO_NAME = "tf-resource-op.so"


def compile_so():
  use_cxx11_abi = hasattr(tf, 'CXX11_ABI_FLAG') and tf.CXX11_ABI_FLAG
  common_opts = ["-shared", "-O2"]
  common_opts += ["-std=c++11"]
  if sys.platform == "darwin":
    common_opts += ["-undefined", "dynamic_lookup"]
  tf_include = tf.sysconfig.get_include()  # e.g. "...python2.7/site-packages/tensorflow/include"
  tf_include_nsync = tf_include + "/external/nsync/public"  # https://github.com/tensorflow/tensorflow/issues/2412
  include_paths = [tf_include, tf_include_nsync]
  for include_path in include_paths:
    common_opts += ["-I", include_path]
  common_opts += ["-fPIC", "-v"]
  common_opts += ["-D_GLIBCXX_USE_CXX11_ABI=%i" % (1 if use_cxx11_abi else 0)]
  common_opts += ["-g"]
  opts = common_opts + [CC_NAME, "-o", SO_NAME]
  ld_flags = ["-L%s" % tf.sysconfig.get_lib(), "-ltensorflow_framework"]
  opts += ld_flags
  cmd_bin = "g++"
  cmd_args = [cmd_bin] + opts
  print("$ %s" % " ".join(cmd_args))
  check_call(cmd_args)


def main():
  if not os.path.exists(SO_NAME):
    compile_so()
  mod = tf.load_op_library(SO_NAME)
  handle = mod.open_fst_load(filename="foo.bar")
  new_states, scores = mod.open_fst_transition(handle=handle, inputs=[0], states=[0])

  with tf.Session() as session:
    # InternalError: ndarray was 1 bytes but TF_Tensor was 98 bytes
    # print("fst:", session.run(handle))

    out_new_states, out_scores = session.run((new_states, scores))
    print("output new states:", out_new_states)
    print("output scores:", out_scores)

    # When session unloads, crashes with assertion:
    # F .../site-packages/tensorflow/include/tensorflow/core/lib/core/refcount.h:79] Check failed: ref_.load() == 0 (1 vs. 0)  # nopep8


if __name__ == '__main__':
  import better_exchook
  better_exchook.install()
  main()

C++ 代碼:


#include <exception>
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/shape_inference.h"
#include "tensorflow/core/framework/resource_mgr.h"
#include "tensorflow/core/framework/resource_op_kernel.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/platform/macros.h"
#include "tensorflow/core/platform/mutex.h"
#include "tensorflow/core/platform/types.h"

using namespace tensorflow;


REGISTER_OP("OpenFstLoad")
.Attr("filename: string")
.Attr("container: string = ''")
.Attr("shared_name: string = ''")
.Output("handle: resource")
.SetIsStateful()
.SetShapeFn(shape_inference::ScalarShape)
.Doc("OpenFstLoad: loads FST, creates TF resource, persistent across runs in the session");


REGISTER_OP("OpenFstTransition")
.Input("handle: resource")
.Input("states: int32")
.Input("inputs: int32")
.Output("new_states: int32")
.Output("scores: float32")
.SetShapeFn([](::tensorflow::shape_inference::InferenceContext* c) {
  c->set_output(0, c->input(1));
  c->set_output(1, c->input(1));
  return Status::OK();
})
.Doc("OpenFstTransition: performs a transition");


struct OpenFstInstance : public ResourceBase {
  explicit OpenFstInstance(const string& filename) : filename_(filename) {}

  string DebugString() override {
    return strings::StrCat("OpenFstInstance[", filename_, "]");
  }

  const string filename_;
};


// https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/framework/resource_op_kernel.h
// TFUtil.TFArrayContainer
class OpenFstLoadOp : public ResourceOpKernel<OpenFstInstance> {
 public:
  explicit OpenFstLoadOp(OpKernelConstruction* context)
      : ResourceOpKernel(context) {
    OP_REQUIRES_OK(context, context->GetAttr("filename", &filename_));
  }

 private:
  virtual bool IsCancellable() const { return false; }
  virtual void Cancel() {}

  Status CreateResource(OpenFstInstance** ret) override EXCLUSIVE_LOCKS_REQUIRED(mu_) {
    try {
      *ret = new OpenFstInstance(filename_);
    } catch (std::exception& exc) {
      return errors::Internal("Could not load OpenFst ", filename_, ", exception: ", exc.what());
    }
    if(*ret == nullptr)
      return errors::ResourceExhausted("Failed to allocate");
    return Status::OK();
  }

  Status VerifyResource(OpenFstInstance* fst) override {
    if(fst->filename_ != filename_)
      return errors::InvalidArgument("Filename mismatch: expected ", filename_,
                                     " but got ", fst->filename_, ".");
    return Status::OK();
  }

  string filename_;
};

REGISTER_KERNEL_BUILDER(Name("OpenFstLoad").Device(DEVICE_CPU), OpenFstLoadOp);


class OpenFstTransitionOp : public OpKernel {
 public:
  using OpKernel::OpKernel;

  void Compute(OpKernelContext* context) override {
    OpenFstInstance* fst;
    OP_REQUIRES_OK(context, GetResourceFromContext(context, "handle", &fst));
    core::ScopedUnref unref(fst);

    const Tensor& states_tensor = context->input(1);
    auto states_flat = states_tensor.flat<int32>();

    const Tensor& inputs_tensor = context->input(2);
    auto inputs_flat = inputs_tensor.flat<int32>();

    OP_REQUIRES(
      context,
      TensorShapeUtils::IsVector(states_tensor.shape()) &&
      TensorShapeUtils::IsVector(inputs_tensor.shape()) &&
      states_flat.size() == inputs_flat.size(),
      errors::InvalidArgument(
        "Shape mismatch. states ", states_tensor.shape().DebugString(),
        " vs inputs ", inputs_tensor.shape().DebugString()));

    Tensor* output_new_states_tensor = NULL;
    OP_REQUIRES_OK(context, context->allocate_output(0, states_tensor.shape(), &output_new_states_tensor));
    auto output_new_states_flat = output_new_states_tensor->flat<int32>();
    Tensor* output_scores_tensor = NULL;
    OP_REQUIRES_OK(context, context->allocate_output(1, states_tensor.shape(), &output_scores_tensor));
    auto output_scores_flat = output_scores_tensor->flat<float>();

    for(int i = 0; i < inputs_flat.size(); ++i) {
      output_new_states_flat(i) = -1;  // TODO
      output_scores_flat(i) = -1.;  // TODO
    }
  }
};

REGISTER_KERNEL_BUILDER(Name("OpenFstTransition").Device(DEVICE_CPU), OpenFstTransitionOp);

一些問題:

運行print("fst:", session.run(handle))會拋出異常InternalError: ndarray was 1 bytes but TF_Tensor was 98 bytes 為什么? 這是什么意思?

當 session 卸載時,它會因斷言而崩潰: F.../site-packages/tensorflow/include/tensorflow/core/lib/core/refcount.h:79] Check failed: ref_.load() == 0 (1 vs. 0) 堆棧跟蹤:

2   libsystem_c.dylib               0x00007fff6687d1ae abort + 127
3   libtensorflow_framework.so      0x0000000107382e70 tensorflow::internal::LogMessageFatal::~LogMessageFatal() + 32
4   libtensorflow_framework.so      0x0000000107382e80 tensorflow::internal::LogMessageFatal::~LogMessageFatal() + 16
5   tf-resource-op.so               0x0000000128093d82 tensorflow::core::RefCounted::~RefCounted() + 162
6   tf-resource-op.so               0x0000000128095e2e OpenFstInstance::~OpenFstInstance() + 46 (tf-resource-op.cc:40)
7   libtensorflow_framework.so      0x000000010726a1f3 tensorflow::ResourceMgr::DoDelete(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long long, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 307
8   libtensorflow_framework.so      0x000000010726a433 tensorflow::ResourceMgr::DoDelete(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::type_index, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 99
9   tf-resource-op.so               0x000000012809457b tensorflow::ResourceOpKernel<OpenFstInstance>::~ResourceOpKernel() + 91 (resource_op_kernel.h:60)
10  tf-resource-op.so               0x0000000128094694 OpenFstLoadOp::~OpenFstLoadOp() + 52 (tf-resource-op.cc:53)
11  libtensorflow_framework.so      0x0000000107264d4f tensorflow::OpSegment::Item::~Item() + 63
12  libtensorflow_framework.so      0x000000010726558f tensorflow::OpSegment::RemoveHold(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 303
13  _pywrap_tensorflow_internal.so  0x0000000113b7b712 tensorflow::DirectSession::~DirectSession() + 274
14  _pywrap_tensorflow_internal.so  0x0000000113b7bade tensorflow::DirectSession::~DirectSession() + 14

我想OpenFstInstance object 的引用計數有問題。 但為什么? 我該如何解決?

(相關的是這個問題。)

-DNDEBUG添加到構建標志可解決此問題。 此解決方法在 TF 問題 17316中進行了說明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM