Skip to content

[2.16] Consolidate C++ TRTEngine construction on the zero-copy path #4883

Open

Description

@cehongwang

Follow-up to the compile-memory stack (#4794, #4795, #4796), scheduled for 2.16.

#4794 added a zero-copy way to build the C++ runtime engine: torch.classes.tensorrt.Engine.from_engine_tensor(info, plan_tensor) reads the TensorRT plan in place from a uint8 CPU tensor. The older construction paths are still there, so we now have several ways to create a TRTEngine that differ only in how the plan is passed in:

C++ constructors (core/runtime/TRTEngine.h)

  • TRTEngine(const std::string& serialized_engine, const RTDevice&, in/out binding names, ...): used by the TorchScript path (core/compiler.cpp:44)
  • TRTEngine(std::vector<std::string> serialized_info): TorchBind __init__ and the pickle __setstate__ (core/runtime/register_jit_hooks.cpp:220)
  • TRTEngine(const std::vector<std::string>& serialized_info, std::string_view serialized_engine): used by from_engine_tensor
  • TRTEngine(const std::string& mod_name, std::string_view serialized_engine, const RTDevice&, ...): the one the others delegate to

TorchBind entry points (core/runtime/register_jit_hooks.cpp)

  • Engine(info): plan inside the std::vector<std::string>, copied twice on the way in
  • Engine.from_engine_tensor(info, plan_tensor): zero copy

Python (py/torch_tensorrt/dynamo/runtime/_serialized_engine_layout.py, create_cpp_engine) chooses between the two: Engine(info) when the plan is a str or empty, from_engine_tensor otherwise.

Since the zero-copy path covers every case, the other paths only add surface area and a way to reintroduce plan-sized copies.

Proposal

  • Make the std::string_view constructor the single real constructor, and have every caller (TorchScript compiler.cpp, pickle __setstate__, ExecuTorch backend) pass a view of a plan it already holds.
  • Expose one TorchBind constructor that takes the plan as a tensor (or make __init__ accept it) and remove the copying Engine(info) path from create_cpp_engine, keeping backward compatibility for serialized artifacts that store the plan inside serialized_info.
  • Remove the constructors that end up unused.

cc @narendasan

Activity

  1. added this to the v2.16.0 milestone on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions