部署 Torch-TensorRT 程序¶
编译和保存 Torch-TensorRT 程序后,不再严格依赖于完整的 Torch-TensorRT 库。运行编译后的程序只需要运行时。因此,除了将完整的 Torch-TensorRT 编译器与您的应用程序一起发布外,您还有几个其他选择来部署您的程序。
Torch-TensorRT 软件包 / libtorchtrt.so¶
编译程序后,您可以使用标准 PyTorch API 运行它。唯一的要求是该软件包必须在 python 中导入或在 C++ 中链接。
运行时库¶
C++ 发行版中包含 libtorchtrt_runtime.so
。该库仅包含运行 Torch-TensorRT 程序所需的组件。您可以将 libtorchtrt_runtime.so
链接到您的部署程序,而不是链接 libtorchtrt.so
或导入 torch_tensorrt
,或者使用 DL_OPEN
或 LD_PRELOAD
。对于 python,您可以使用 torch.ops.load_library("libtorchtrt_runtime.so")
加载运行时。然后,您可以继续通过 PyTorch API 使用程序,就像您通常使用它们一样。
注意
如果您在 x86 上使用 Python 上的标准 PyTorch 发行版,您可能需要 libtorchtrt_runtime.so
的 pre-cxx11-abi 变体,请查看 安装 文档以了解更多详细信息。
注意
如果您链接了 libtorchtrt_runtime.so
,使用以下标志可能会有所帮助 -Wl,--no-as-needed -ltorchtrt -Wl,--as-needed
,因为对于大多数 Torch-TensorRT 运行时应用程序,没有直接符号依赖关系到 Torch-TensorRT 运行时的任何内容
有关如何使用 libtorchtrt_runtime.so
的示例,请查看此处:https://github.com/pytorch/TensorRT/tree/master/examples/torchtrt_runtime_example
插件库¶
如果您使用 Torch-TensorRT 作为 TensorRT 引擎的转换器,并且您的引擎使用 Torch-TensorRT 提供的插件,Torch-TensorRT 会提供 libtorchtrt_plugins.so
库,其中包含 Torch-TensorRT 在编译期间使用的 TensorRT 插件的实现。这个库可以像其他 TensorRT 插件库一样 DL_OPEN
或 LD_PRELOAD
。