2

我的目标是在 C++ 应用程序中运行 tensorrt 优化的 tensorflow 图。我正在使用 tensorflow 1.8 和 tensorrt 4。使用 python api,我能够优化图形并看到很好的性能提升。

尝试在 c++ 中运行图形失败并出现以下错误:

Not found: Op type not registered 'TRTEngineOp' in binary running on e15ff5301262. Make sure the Op and Kernel are registered in the binary running in this process.

其他,非张量图工作。我在使用 python api 时遇到了类似的错误,但通过导入 tensorflow.contrib.tensorrt 解决了它。从错误中,我相当确定内核和操作没有注册,但我不知道如何在构建 tensorflow 后在应用程序中这样做。附带说明一下,我不能使用 bazel,但需要使用 cmake。到目前为止,我链接到libtensorflow_cc.soand libtensorflow_framework.so

有人能帮我一下吗?谢谢!

更新:使用 c 或 c++ api 加载_trt_engine_op.so不会在加载时抛出错误,但无法运行

Invalid argument: No OpKernel was registered to support Op 'TRTEngineOp' with these attrs.  Registered devices: [CPU,GPU], Registered kernels:
  <no registered kernels>

     [[Node: my_trt_op3 = TRTEngineOp[InT=[DT_FLOAT, DT_FLOAT], OutT=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT], input_nodes=["tower_0/down_0/conv_0/Conv2D-0-TransposeNHWCToNCHW-LayoutOptimizer", "tower_0/down_0/conv_skip/Conv2D-0-TransposeNHWCToNCHW-LayoutOptimizer"], output_nodes=["tower_0/down_0/conv_skip/Relu", "tower_0/down_1/conv_skip/Relu", "tower_0/down_2/conv_skip/Relu", "tower_0/down_3/conv_skip/Relu"], serialized_engine="\220{I\000...00\000\000"](tower_0/down_0/conv_0/Conv2D-0-TransposeNHWCToNCHW-LayoutOptimizer, tower_0/down_0/conv_skip/Conv2D-0-TransposeNHWCToNCHW-LayoutOptimizer)]]
4

6 回答 6

3

解决Tensorflow 1.8上出现错误“未找到:操作类型未注册'TRTEngineOp'”问题的另一种方法:

1)在文件tensorflow/contrib/tensorrt/BUILD中,添加具有以下内容的新部分:

cc_library(
name = "trt_engine_op_kernel_cc",
srcs = [
    "kernels/trt_calib_op.cc",
    "kernels/trt_engine_op.cc",
    "ops/trt_calib_op.cc",
    "ops/trt_engine_op.cc",
    "shape_fn/trt_shfn.cc",
],
hdrs = [
    "kernels/trt_calib_op.h",
    "kernels/trt_engine_op.h",
    "shape_fn/trt_shfn.h",
],
copts = tf_copts(),
visibility = ["//visibility:public"],
deps = [
    ":trt_logging",
    ":trt_plugins",
    ":trt_resources",
    "//tensorflow/core:gpu_headers_lib",
    "//tensorflow/core:lib_proto_parsing",
    "//tensorflow/core:stream_executor_headers_lib",
] + if_tensorrt([
    "@local_config_tensorrt//:nv_infer",
]) + tf_custom_op_library_additional_deps(),
alwayslink = 1,  # buildozer: disable=alwayslink-with-hdrs
)

2) 将//tensorflow/contrib/tensorrt:trt_engine_op_kernel_cc依赖项添加到您要构建的相应 BAZEL 项目中

PS:无需_trt_engine_op.so使用 TF_LoadLibrary加载库

于 2018-05-22T06:26:53.277 回答
1

这是我对这个问题的发现(和某种解决方案)(Tensorflow 1.8.0,TensorRT 3.0.4):

我想将 tensorrt 支持包含到一个库中,该库从给定*.pb文件加载图表。

仅仅添加//tensorflow/contrib/tensorrt:trt_engine_op_kernel到我的 Bazel BUILD 文件对我来说并没有成功。我仍然收到一条消息,指出未注册的操作:

2018-05-21 12:22:07.286665: E tensorflow/core/framework/op_kernel.cc:1242] OpKernel ('op: "TRTCalibOp" device_type: "GPU"') for unknown op: TRTCalibOp
2018-05-21 12:22:07.286856: E tensorflow/core/framework/op_kernel.cc:1242] OpKernel ('op: "TRTEngineOp" device_type: "GPU"') for unknown op: TRTEngineOp
2018-05-21 12:22:07.296024: E tensorflow/examples/tf_inference_lib/cTfInference.cpp:56] Not found: Op type not registered 'TRTEngineOp' in binary running on ***. 
Make sure the Op and Kernel are registered in the binary running in this process.

解决方案是,我必须使用 C_API 在我的 C++ 代码中加载 Ops 库(tf_custom_op_library):

#include "tensorflow/c/c_api.h"
...
TF_Status status = TF_NewStatus();
TF_LoadLibrary("_trt_engine_op.so", status);

共享对象_trt_engine_op.so是为 bazel 目标创建的//tensorflow/contrib/tensorrt:python/ops/_trt_engine_op.so

bazel build --config=opt --config=cuda --config=monolithic \
     //tensorflow/contrib/tensorrt:python/ops/_trt_engine_op.so

现在我只需要确保 _trt_engine_op.so 在需要时可用,例如通过LD_LIBRARY_PATH.

如果有人有想法,如何以更优雅的方式做到这一点(为什么我们有 2 个必须构建的人工制品?我们不能只有一个吗?),我对每一个建议都很高兴。

tldr

  1. 添加//tensorflow/contrib/tensorrt:trt_engine_op_kernel为您要构建的相应 BAZEL 项目的依赖项

  2. _trt_engine_op.so使用 C-API 在您的代码中加载 ops-library 。

于 2018-05-21T12:47:44.097 回答
1

对于 Tensorflow r1.8,下面显示的两个 BUILD 文件中的添加和libtensorflow_cc.so使用单体选项构建对我有用。

diff --git a/tensorflow/BUILD b/tensorflow/BUILD
index cfafffd..fb8eb31 100644
--- a/tensorflow/BUILD
+++ b/tensorflow/BUILD
@@ -525,6 +525,8 @@ tf_cc_shared_object(
         "//tensorflow/cc:scope",
         "//tensorflow/cc/profiler",
         "//tensorflow/core:tensorflow",
+        "//tensorflow/contrib/tensorrt:trt_conversion",
+        "//tensorflow/contrib/tensorrt:trt_engine_op_kernel",
     ],
 )

diff --git a/tensorflow/contrib/tensorrt/BUILD b/tensorflow/contrib/tensorrt/BUILD
index fd3582e..a6566b9 100644
--- a/tensorflow/contrib/tensorrt/BUILD
+++ b/tensorflow/contrib/tensorrt/BUILD
@@ -76,6 +76,8 @@ cc_library(
     srcs = [
         "kernels/trt_calib_op.cc",
         "kernels/trt_engine_op.cc",
+        "ops/trt_calib_op.cc",
+        "ops/trt_engine_op.cc",
     ],
     hdrs = [
         "kernels/trt_calib_op.h",
@@ -86,6 +88,7 @@ cc_library(
     deps = [
         ":trt_logging",
         ":trt_resources",
+        ":trt_shape_function",
         "//tensorflow/core:gpu_headers_lib",
         "//tensorflow/core:lib_proto_parsing",
         "//tensorflow/core:stream_executor_headers_lib",
于 2018-07-08T01:21:56.010 回答
0

解决方案:添加导入

从 tensorflow.python.compiler.tensorrt 导入 trt_convert 作为 trt

链接讨论:https ://github.com/tensorflow/tensorflow/issues/26525

于 2019-09-10T02:57:37.057 回答
0

这是我的解决方案,tensorflow 是 1.14。在您的 BUILD 文件、exp、tensorflow/examples/your_workspace/BUILD 中:

tf_cc_binary

scrs= [...,"//tensorflow/compiler/tf2tensorrt:ops/trt_engine_op.cc"]
deps=[...,"//tensorflow/compiler/tf2tensorrt:trt_op_kernels"]
于 2019-11-12T08:47:59.330 回答
0

正如您所提到的,当您添加//tensorflow/contrib/tensorrt:trt_engine_op_kernel到依赖项列表时它应该可以工作。目前 Tensorflow-TensorRT 集成仍在进行中,可能仅适用于 python API;对于 C++,您需要调用ConvertGraphDefToTensorRT()fromtensorflow/contrib/tensorrt/convert/convert_graph.h进行转换。

如果您有任何问题,请告诉我。

于 2018-05-09T22:01:31.900 回答