Skip to content

Python runtime rework - #4222

Merged
cehongwang merged 2 commits into
mainfrom
python-runtime-rework-patches
May 21, 2026
Merged

cehongwang merged 2 commits into
mainfrom
python-runtime-rework-patches

Conversation

@cehongwang

@cehongwang cehongwang commented Apr 29, 2026 •

Copy link
Copy Markdown
Collaborator

Unify Python and C++ TensorRT runtimes

PythonTorchTensorRTModule is removed. Both runtimes now live behind the same TorchTensorRTModule, with a single use_python_runtime flag (on CompilationSettings) selecting which path to take at engine setup.

What's changed
One module, two backends. TorchTensorRTModule.setup_engine() constructs either torch.classes.tensorrt.Engine (C++) or TRTEngine (Python) and binds the matching op to self.execute_engine_op. forward() just calls the bound op — no per-iteration branching, no separate module class.
Two equivalent ops. tensorrt::execute_engine (C++) and tensorrt::execute_engine_python (Python) are both registered and have the same signature, fake kernel, and semantics. The Python op is registered unconditionally so it's available even when the C++ runtime is loaded.

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes
  • I have added the relevant labels to my PR in so that relevant reviewers are notified

@meta-cla meta-cla Bot added the cla signed label Apr 29, 2026
@github-actions github-actions Bot added documentation Improvements or additions to documentation component: tests Issues re: Tests component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: runtime component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Apr 29, 2026
@github-actions
github-actions Bot requested a review from zewenli98 April 29, 2026 00:01

@narendasan narendasan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this PR is clean yet

Comment thread core/runtime/register_jit_hooks.cpp Resolved
Comment thread py/torch_tensorrt/dynamo/_exporter.py Resolved
@cehongwang
cehongwang force-pushed the python-runtime-rework-patches branch from daad718 to a328e80 Compare April 29, 2026 20:59

@zewenli98 zewenli98 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this PR based on #4164? Why do we need two separate PRs?
Can you complete Description of the PRs so that other folks can understand why and what the PRs are doing?

@cehongwang
cehongwang force-pushed the python-runtime-rework-patches branch from a328e80 to 00e4ea2 Compare April 30, 2026 21:04
@cehongwang cehongwang changed the title Python runtime rework patches Python runtime rework Apr 30, 2026
@cehongwang
cehongwang force-pushed the python-runtime-rework-patches branch from 00e4ea2 to 168bdea Compare April 30, 2026 22:08
@cehongwang

Copy link
Copy Markdown
Collaborator Author

I will revert the commit 168bdea547a614b26c256527f65217e2d0fbf222 once the pytorch changes are landed.

@cehongwang
cehongwang force-pushed the python-runtime-rework-patches branch 3 times, most recently from 2b3b974 to c6b8edf Compare May 4, 2026 23:59

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to Python style guidelines:

--- /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/runtime/_TorchTensorRTModule.py	2026-05-04 23:59:29.343126+00:00
+++ /home/runner/work/TensorRT/TensorRT/py/torch_tensorrt/dynamo/runtime/_TorchTensorRTModule.py	2026-05-04 23:59:48.643676+00:00
@@ -38,10 +38,11 @@
    Optional[SerializedTensorRTEngineFmt],
    List[str],
    List[str],
]

+
class TorchTensorRTModule(torch.nn.Module):  # type: ignore[misc]
    """``nn.Module`` that runs a TensorRT engine inside PyTorch.

    When the C++ Torch-TensorRT runtime is available, execution uses
    ``torch.classes.tensorrt.Engine`` and ``torch.ops.tensorrt.execute_engine``.
@@ -157,10 +158,11 @@
            if k == "engine":
                object.__setattr__(result, k, v)  # shallow: reuse the same C++ Engine
            else:
                object.__setattr__(result, k, copy.deepcopy(v, memo))
        return result
+
    def _resolve_target_device(self) -> torch.device:
        """Resolve the engine's target CUDA device from compilation settings."""
        if self.settings.device is not None:
            return torch.device(f"cuda:{self.settings.device.gpu_id}")
        return torch.device(f"cuda:{torch.cuda.current_device()}")
--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/partitioning/test_000_resource_partitioning.py	2026-05-04 23:59:29.378025+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/partitioning/test_000_resource_partitioning.py	2026-05-04 23:59:52.160386+00:00
@@ -17,10 +17,11 @@
    ResourcePartitioner,
)

# Fixed RSS value to make memory-budget calculations deterministic.
_FIXED_RSS_BYTES = 512 * 1024 * 1024  # 512 MB
+

class TestResourcePartitioning(TestCase):
    def test_atomic_subgraph_correction(self):
        class net(nn.Module):
            def __init__(self):
@@ -110,7 +111,8 @@
            # The fusion should be fixed after the step
            partitioner._verify_all_fusion_nodes_in_same_subgraph(new_subgraphs)

            break

+
if __name__ == "__main__":
    run_tests()
--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/partitioning/test_001_resource_partitioning.py	2026-05-04 23:59:29.378025+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/partitioning/test_001_resource_partitioning.py	2026-05-04 23:59:52.471763+00:00
@@ -25,10 +25,11 @@
    resource_partition,
)

# Fixed RSS value used across all tests to make memory-budget calculations deterministic.
_FIXED_RSS_BYTES = 512 * 1024 * 1024  # 512 MB
+

class TestResourcePartitioning(TestCase):
    def test_resource_partitioning(self):
        class net(nn.Module):
            def __init__(self):
@@ -413,7 +414,8 @@
            == 4
        ), "The graph should have 4 accelerated subgraphs"

        torch._dynamo.reset()

+
if __name__ == "__main__":
    run_tests()

@cehongwang
cehongwang force-pushed the python-runtime-rework-patches branch 5 times, most recently from 42972f0 to 29673a1 Compare May 5, 2026 23:52

@narendasan narendasan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool I think this is looking good. One thing I think this shows is maybe we can keep the use_python_runtime but only register with torch in python only? then we arent losing user flexibility

Comment thread core/runtime/TRTEngine.h Resolved
ABI_VERSION = "9"


class SerializedInfoIndex(IntEnum):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feat. request for later is to move this into some sort of data that both C++ and python can load at build time

@cehongwang
cehongwang requested a review from zewenli98 May 19, 2026 20:10

@zewenli98 zewenli98 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall LGTM

Comment thread docsrc/py_api/runtime.rst
Comment on lines +33 to +35
Execution uses the C++ runtime engine when it is installed in the build; otherwise the
Python runtime engine is used. There is no separate process-wide backend switch
in ``torch_tensorrt.runtime``.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If c++ is built, is it able to switch to python runtime?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

elif (
not ENABLED_FEATURES.torch_tensorrt_runtime and not settings.use_python_runtime
):
if not ENABLED_FEATURES.torch_tensorrt_runtime:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean torch_tensorrt_cpp_runtime? the name is kind of confusing

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think torch_tensorrt_runtime is the cpp runtime. I did not invent the name. It's been there for a while and I am just using it

Comment thread core/runtime/TRTEngine.h
int64_t get_streamable_device_memory_budget();
int64_t get_automatic_device_memory_budget();
std::vector<at::Tensor> infer_outputs(std::vector<std::vector<int64_t>> input_shapes);
void set_pre_allocated_outputs(bool enable);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is it removed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's never used

@cehongwang
cehongwang force-pushed the python-runtime-rework-patches branch from 3cd0a03 to 3538005 Compare May 21, 2026 19:43
@cehongwang
cehongwang merged commit ae28da2 into main May 21, 2026
30 of 32 checks passed
@cehongwang
cehongwang deleted the python-runtime-rework-patches branch May 21, 2026 19:45
shoumikhin added a commit to shoumikhin/TensorRT that referenced this pull request May 21, 2026
Rebased on top of main (post pytorch#4222 Python runtime rework). The original
PR pytorch#4232 modified _PythonTorchTensorRTModule.py which was removed by
pytorch#4222; the same stream-selection logic has been ported into the new
_TRTEngine class.

Behavior:
- If the caller is on the default CUDA stream, keep legacy behavior: run
  the engine on a dedicated pool stream and synchronise via wait_stream.
- If the caller is on a non-default stream (e.g. attached to a CUDA Green
  Context for SM partitioning), honor it: reuse the caller's stream for
  the engine and skip the wait_stream pair (saving extra syncs and
  preserving the caller's scheduling choice end to end).
- When CUDA graphs are enabled and the engine stream changes between
  invocations, trigger graph recapture via runtime_states.context_changed
  so we don't replay a graph recorded against a stale stream.

Implementation:
- core/runtime/execute_engine.cpp: same logic in C++ (cudagraphs replay
  path + standard path).
- py/torch_tensorrt/dynamo/runtime/_TRTEngine.py: new _prepare_streams
  helper called from both _execute_standard and _execute_output_allocator.
- py/torch_tensorrt/dynamo/runtime/_CudaGraphsTorchTensorRTModule.py:
  same logic in the cudagraphs wrapper module.
shoumikhin added a commit to shoumikhin/TensorRT that referenced this pull request May 21, 2026
Rebased on top of main (post pytorch#4222 Python runtime rework). The original
PR pytorch#4232 modified _PythonTorchTensorRTModule.py which was removed by
pytorch#4222; the same stream-selection logic has been ported into the new
_TRTEngine class.

Behavior:
- If the caller is on the default CUDA stream, keep legacy behavior: run
  the engine on a dedicated pool stream and synchronise via wait_stream.
- If the caller is on a non-default stream (e.g. attached to a CUDA Green
  Context for SM partitioning), honor it: reuse the caller's stream for
  the engine and skip the wait_stream pair (saving extra syncs and
  preserving the caller's scheduling choice end to end).
- When CUDA graphs are enabled and the engine stream changes between
  invocations, trigger graph recapture via runtime_states.context_changed
  so we don't replay a graph recorded against a stale stream.

Implementation:
- core/runtime/execute_engine.cpp: same logic in C++ (cudagraphs replay
  path + standard path).
- py/torch_tensorrt/dynamo/runtime/_TRTEngine.py: new _prepare_streams
  helper called from both _execute_standard and _execute_output_allocator.
- py/torch_tensorrt/dynamo/runtime/_CudaGraphsTorchTensorRTModule.py:
  same logic in the cudagraphs wrapper module.
shoumikhin added a commit to shoumikhin/TensorRT that referenced this pull request May 27, 2026
Rebased on top of main (post pytorch#4222 Python runtime rework). The original
PR pytorch#4232 modified _PythonTorchTensorRTModule.py which was removed by
pytorch#4222; the same stream-selection logic has been ported into the new
_TRTEngine class.

Behavior:
- If the caller is on the default CUDA stream, keep legacy behavior: run
  the engine on a dedicated pool stream and synchronise via wait_stream.
- If the caller is on a non-default stream (e.g. attached to a CUDA Green
  Context for SM partitioning), honor it: reuse the caller's stream for
  the engine and skip the wait_stream pair (saving extra syncs and
  preserving the caller's scheduling choice end to end).
- When CUDA graphs are enabled and the engine stream changes between
  invocations, trigger graph recapture via runtime_states.context_changed
  so we don't replay a graph recorded against a stale stream.

Implementation:
- core/runtime/execute_engine.cpp: same logic in C++ (cudagraphs replay
  path + standard path).
- py/torch_tensorrt/dynamo/runtime/_TRTEngine.py: new _prepare_streams
  helper called from both _execute_standard and _execute_output_allocator.
- py/torch_tensorrt/dynamo/runtime/_CudaGraphsTorchTensorRTModule.py:
  same logic in the cudagraphs wrapper module.
shoumikhin added a commit to shoumikhin/TensorRT that referenced this pull request May 27, 2026
Rebased on top of main (post pytorch#4222 Python runtime rework). The original
PR pytorch#4232 modified _PythonTorchTensorRTModule.py which was removed by
pytorch#4222; the same stream-selection logic has been ported into the new
_TRTEngine class.

Behavior:
- If the caller is on the default CUDA stream, keep legacy behavior: run
  the engine on a dedicated pool stream and synchronise via wait_stream.
- If the caller is on a non-default stream (e.g. attached to a CUDA Green
  Context for SM partitioning), honor it: reuse the caller's stream for
  the engine and skip the wait_stream pair (saving extra syncs and
  preserving the caller's scheduling choice end to end).
- When CUDA graphs are enabled and the engine stream changes between
  invocations, trigger graph recapture via runtime_states.context_changed
  so we don't replay a graph recorded against a stale stream.

Implementation:
- core/runtime/execute_engine.cpp: same logic in C++ (cudagraphs replay
  path + standard path).
- py/torch_tensorrt/dynamo/runtime/_TRTEngine.py: new _prepare_streams
  helper called from both _execute_standard and _execute_output_allocator.
- py/torch_tensorrt/dynamo/runtime/_CudaGraphsTorchTensorRTModule.py:
  same logic in the cudagraphs wrapper module.
shoumikhin added a commit to shoumikhin/TensorRT that referenced this pull request May 27, 2026
Rebased on top of main (post pytorch#4222 Python runtime rework). The original
PR pytorch#4232 modified _PythonTorchTensorRTModule.py which was removed by
pytorch#4222; the same stream-selection logic has been ported into the new
_TRTEngine class.

Behavior:
- If the caller is on the default CUDA stream, keep legacy behavior: run
  the engine on a dedicated pool stream and synchronise via wait_stream.
- If the caller is on a non-default stream (e.g. attached to a CUDA Green
  Context for SM partitioning), honor it: reuse the caller's stream for
  the engine and skip the wait_stream pair (saving extra syncs and
  preserving the caller's scheduling choice end to end).
- When CUDA graphs are enabled and the engine stream changes between
  invocations, trigger graph recapture via runtime_states.context_changed
  so we don't replay a graph recorded against a stale stream.

Implementation:
- core/runtime/execute_engine.cpp: same logic in C++ (cudagraphs replay
  path + standard path).
- py/torch_tensorrt/dynamo/runtime/_TRTEngine.py: new _prepare_streams
  helper called from both _execute_standard and _execute_output_allocator.
- py/torch_tensorrt/dynamo/runtime/_CudaGraphsTorchTensorRTModule.py:
  same logic in the cudagraphs wrapper module.
shoumikhin added a commit to shoumikhin/TensorRT that referenced this pull request May 27, 2026
Rebased on top of main (post pytorch#4222 Python runtime rework). The original
PR pytorch#4232 modified _PythonTorchTensorRTModule.py which was removed by
pytorch#4222; the same stream-selection logic has been ported into the new
_TRTEngine class.

Behavior:
- If the caller is on the default CUDA stream, keep legacy behavior: run
  the engine on a dedicated pool stream and synchronise via wait_stream.
- If the caller is on a non-default stream (e.g. attached to a CUDA Green
  Context for SM partitioning), honor it: reuse the caller's stream for
  the engine and skip the wait_stream pair (saving extra syncs and
  preserving the caller's scheduling choice end to end).
- When CUDA graphs are enabled and the engine stream changes between
  invocations, trigger graph recapture via runtime_states.context_changed
  so we don't replay a graph recorded against a stale stream.

Implementation:
- core/runtime/execute_engine.cpp: same logic in C++ (cudagraphs replay
  path + standard path).
- py/torch_tensorrt/dynamo/runtime/_TRTEngine.py: new _prepare_streams
  helper called from both _execute_standard and _execute_output_allocator.
- py/torch_tensorrt/dynamo/runtime/_CudaGraphsTorchTensorRTModule.py:
  same logic in the cudagraphs wrapper module.
shoumikhin added a commit to shoumikhin/TensorRT that referenced this pull request May 27, 2026
Rebased on top of main (post pytorch#4222 Python runtime rework). The original
PR pytorch#4232 modified _PythonTorchTensorRTModule.py which was removed by
pytorch#4222; the same stream-selection logic has been ported into the new
_TRTEngine class.

Behavior:
- If the caller is on the default CUDA stream, keep legacy behavior: run
  the engine on a dedicated pool stream and synchronise via wait_stream.
- If the caller is on a non-default stream (e.g. attached to a CUDA Green
  Context for SM partitioning), honor it: reuse the caller's stream for
  the engine and skip the wait_stream pair (saving extra syncs and
  preserving the caller's scheduling choice end to end).
- When CUDA graphs are enabled and the engine stream changes between
  invocations, trigger graph recapture via runtime_states.context_changed
  so we don't replay a graph recorded against a stale stream.

Implementation:
- core/runtime/execute_engine.cpp: same logic in C++ (cudagraphs replay
  path + standard path).
- py/torch_tensorrt/dynamo/runtime/_TRTEngine.py: new _prepare_streams
  helper called from both _execute_standard and _execute_output_allocator.
- py/torch_tensorrt/dynamo/runtime/_CudaGraphsTorchTensorRTModule.py:
  same logic in the cudagraphs wrapper module.
shoumikhin added a commit to shoumikhin/TensorRT that referenced this pull request May 27, 2026
Rebased on top of main (post pytorch#4222 Python runtime rework). The original
PR pytorch#4232 modified _PythonTorchTensorRTModule.py which was removed by
pytorch#4222; the same stream-selection logic has been ported into the new
_TRTEngine class.

Behavior:
- If the caller is on the default CUDA stream, keep legacy behavior: run
  the engine on a dedicated pool stream and synchronise via wait_stream.
- If the caller is on a non-default stream (e.g. attached to a CUDA Green
  Context for SM partitioning), honor it: reuse the caller's stream for
  the engine and skip the wait_stream pair (saving extra syncs and
  preserving the caller's scheduling choice end to end).
- When CUDA graphs are enabled and the engine stream changes between
  invocations, trigger graph recapture via runtime_states.context_changed
  so we don't replay a graph recorded against a stale stream.

Implementation:
- core/runtime/execute_engine.cpp: same logic in C++ (cudagraphs replay
  path + standard path).
- py/torch_tensorrt/dynamo/runtime/_TRTEngine.py: new _prepare_streams
  helper called from both _execute_standard and _execute_output_allocator.
- py/torch_tensorrt/dynamo/runtime/_CudaGraphsTorchTensorRTModule.py:
  same logic in the cudagraphs wrapper module.
shoumikhin added a commit to shoumikhin/TensorRT that referenced this pull request May 27, 2026
Rebased on top of main (post pytorch#4222 Python runtime rework). The original
PR pytorch#4232 modified _PythonTorchTensorRTModule.py which was removed by
pytorch#4222; the same stream-selection logic has been ported into the new
_TRTEngine class.

Behavior:
- If the caller is on the default CUDA stream, keep legacy behavior: run
  the engine on a dedicated pool stream and synchronise via wait_stream.
- If the caller is on a non-default stream (e.g. attached to a CUDA Green
  Context for SM partitioning), honor it: reuse the caller's stream for
  the engine and skip the wait_stream pair (saving extra syncs and
  preserving the caller's scheduling choice end to end).
- When CUDA graphs are enabled and the engine stream changes between
  invocations, trigger graph recapture via runtime_states.context_changed
  so we don't replay a graph recorded against a stale stream.

Implementation:
- core/runtime/execute_engine.cpp: same logic in C++ (cudagraphs replay
  path + standard path).
- py/torch_tensorrt/dynamo/runtime/_TRTEngine.py: new _prepare_streams
  helper called from both _execute_standard and _execute_output_allocator.
- py/torch_tensorrt/dynamo/runtime/_CudaGraphsTorchTensorRTModule.py:
  same logic in the cudagraphs wrapper module.
BowenFu added a commit to BowenFu/Torch-TensorRT that referenced this pull request May 29, 2026
…e rework

PR pytorch#4222 merged PythonTorchTensorRTModule into TorchTensorRTModule and now
wraps trt.ICudaEngine in TRTEngine. Update the integration helpers:

- Drop the removed _PythonTorchTensorRTModule import; engine collection
  now matches only TorchTensorRTModule.
- Add use_python_runtime=True to _trt_compile so the C++ runtime
  (which needs libtorchtrt_runtime.so to register torch.classes.tensorrt.Engine)
  is not required to run these tests.
- Unwrap engines[0].engine via .cuda_engine in the inspector helpers since
  the Python runtime now returns a TRTEngine wrapper rather than a raw
  trt.ICudaEngine.
tp5uiuc added a commit to tp5uiuc/TensorRT that referenced this pull request Jun 2, 2026
Drop the standalone test_001_cpp_runtime_rtx_features.py and extend the
existing python-runtime test files (test_000_runtime_cache.py,
test_001_dynamic_shapes_kernel_strategy.py, test_001_cuda_graph_strategy.py)
to also exercise use_python_runtime=False via parameterized.expand over
[(\"python\", True), (\"cpp\", False)]. Mirrors the pattern from the original
pytorch#4202; updated for the post-pytorch#4222 unified runtime where there is no
process-wide backend switch.

Setup / whitebox introspection tests stay python-only because they read
engine.runtime_config on the Python TRTEngine, which the C++
torch.classes.tensorrt.Engine does not expose. End-to-end inference,
persistence, and typo-rejection tests run on both runtimes.

Add a TestSerializationIndices class that verifies the new HAS_RUNTIME_CFG
flag + three TRTRuntimeConfig slots are registered identically on both
backends.

Add a LOG_INFO(\"[torch-TensorRT C++ runtime] TRTEngine constructed from
serialized info\") in the C++-only TRTEngine(std::vector<std::string>)
constructor. Running compile with use_python_runtime=False under
torch_tensorrt.logging.set_level(logging.INFO) produces exactly one such
log line; use_python_runtime=True produces zero.
tp5uiuc added a commit to tp5uiuc/TensorRT that referenced this pull request Jun 2, 2026
Drop the standalone test_001_cpp_runtime_rtx_features.py and extend the
existing python-runtime test files (test_000_runtime_cache.py,
test_001_dynamic_shapes_kernel_strategy.py, test_001_cuda_graph_strategy.py)
to also exercise use_python_runtime=False via parameterized.expand over
[(\"python\", True), (\"cpp\", False)]. Mirrors the pattern from the original
pytorch#4202; updated for the post-pytorch#4222 unified runtime where there is no
process-wide backend switch.

Setup / whitebox introspection tests stay python-only because they read
engine.runtime_config on the Python TRTEngine, which the C++
torch.classes.tensorrt.Engine does not expose. End-to-end inference,
persistence, and typo-rejection tests run on both runtimes.

Add a TestSerializationIndices class that verifies the new HAS_RUNTIME_CFG
flag + three TRTRuntimeConfig slots are registered identically on both
backends.

Add a LOG_INFO(\"[torch-TensorRT C++ runtime] TRTEngine constructed from
serialized info\") in the C++-only TRTEngine(std::vector<std::string>)
constructor. Running compile with use_python_runtime=False under
torch_tensorrt.logging.set_level(logging.INFO) produces exactly one such
log line; use_python_runtime=True produces zero.
tp5uiuc added a commit to tp5uiuc/TensorRT that referenced this pull request Jun 10, 2026
Two stale references on the Python-runtime branch of TorchTensorRTModule
were left over from earlier in this PR and broke every test that runs a
compiled module on the Python runtime:

- ``setup_engine`` and ``set_extra_state`` were calling
  ``torch.ops.tensorrt.execute_engine_python``, but the custom op is
  registered as ``tensorrt::execute_engine`` (in _TRTEngine.py). The
  ``_python`` suffix was an intermediate name during PR pytorch#4222's dev
  cycle and never made it to main. Fixed both call sites to use the
  single shared op name, matching the C++-runtime branch and the
  docstring at the top of the class.

- ``set_extra_state`` was still passing
  ``runtime_settings=self._runtime_settings`` to ``TRTEngine.__init__``,
  but the previous follow-up commit dropped that kwarg. Engine now
  constructs with default settings (matching what the caller assigned
  to ``self._runtime_settings`` two lines above) and applies any
  non-default settings via the post-load setter, same as the live
  ``setup_engine`` path.
apbose pushed a commit that referenced this pull request Jul 15, 2026
test_native_nccl.py:
- Fix test_distributed_mode_subgroup and test_distributed_mode_context_switch
  regressions introduced by PR #4222 (Python runtime rework): add
  dist.barrier(group=subgroup) before first TRT forward so PyTorch's lazy
  ncclComm_t is initialized and bind_nccl_comm() sees a non-zero getCommPtr().
- Remove all use_python_runtime=True flags (deprecated no-op in 2.13).
- Add _cpp_runtime_available() guard to TestPythonRuntimePickle so it skips
  in C++ builds where _TRTEngine's custom op is never registered.
- Fix _multirank_scatter_correctness / _multirank_gather_correctness in
  run_multirank_tests(): wrap with lambdas that loop over root ranks to match
  their (root, rank, world_size, device) signature (broken since PR #4321).
- Remove _multirank_distributed_mode_subgroup_python and
  _multirank_distributed_mode_context_switch_python (cannot work when C++
  extension is loaded); collapse _multirank_pg_migration to cpp-only.

_nccl_utils.py:
- setup_nccl_for_torch_tensorrt(): drop ensure_nccl_symlink() call and
  pre-load libnccl.so.2 directly instead of via the unversioned symlink.
  TRT 11.0+ (ncclWrapper.cpp) tries dlopen("libnccl.so.2") first, matching
  PyTorch's soname exactly; glibc reuses the already-loaded handle so no
  symlink is needed. ensure_nccl_symlink() is kept for TRT < 11.0 users.
- Update module docstring to reflect confirmed TRT 11.0+ behavior.
apbose pushed a commit that referenced this pull request Jul 15, 2026
test_native_nccl.py:
- Fix test_distributed_mode_subgroup and test_distributed_mode_context_switch
  regressions introduced by PR #4222 (Python runtime rework): add
  dist.barrier(group=subgroup) before first TRT forward so PyTorch's lazy
  ncclComm_t is initialized and bind_nccl_comm() sees a non-zero getCommPtr().
- Remove all use_python_runtime=True flags (deprecated no-op in 2.13).
- Add _cpp_runtime_available() guard to TestPythonRuntimePickle so it skips
  in C++ builds where _TRTEngine's custom op is never registered.
- Fix _multirank_scatter_correctness / _multirank_gather_correctness in
  run_multirank_tests(): wrap with lambdas that loop over root ranks to match
  their (root, rank, world_size, device) signature (broken since PR #4321).
- Remove _multirank_distributed_mode_subgroup_python and
  _multirank_distributed_mode_context_switch_python (cannot work when C++
  extension is loaded); collapse _multirank_pg_migration to cpp-only.

_nccl_utils.py:
- setup_nccl_for_torch_tensorrt(): drop ensure_nccl_symlink() call and
  pre-load libnccl.so.2 directly instead of via the unversioned symlink.
  TRT 11.0+ (ncclWrapper.cpp) tries dlopen("libnccl.so.2") first, matching
  PyTorch's soname exactly; glibc reuses the already-loaded handle so no
  symlink is needed. ensure_nccl_symlink() is kept for TRT < 11.0 users.
- Update module docstring to reflect confirmed TRT 11.0+ behavior.
apbose pushed a commit that referenced this pull request Jul 20, 2026
test_native_nccl.py:
- Fix test_distributed_mode_subgroup and test_distributed_mode_context_switch
  regressions introduced by PR #4222 (Python runtime rework): add
  dist.barrier(group=subgroup) before first TRT forward so PyTorch's lazy
  ncclComm_t is initialized and bind_nccl_comm() sees a non-zero getCommPtr().
- Remove all use_python_runtime=True flags (deprecated no-op in 2.13).
- Add _cpp_runtime_available() guard to TestPythonRuntimePickle so it skips
  in C++ builds where _TRTEngine's custom op is never registered.
- Fix _multirank_scatter_correctness / _multirank_gather_correctness in
  run_multirank_tests(): wrap with lambdas that loop over root ranks to match
  their (root, rank, world_size, device) signature (broken since PR #4321).
- Remove _multirank_distributed_mode_subgroup_python and
  _multirank_distributed_mode_context_switch_python (cannot work when C++
  extension is loaded); collapse _multirank_pg_migration to cpp-only.

_nccl_utils.py:
- setup_nccl_for_torch_tensorrt(): drop ensure_nccl_symlink() call and
  pre-load libnccl.so.2 directly instead of via the unversioned symlink.
  TRT 11.0+ (ncclWrapper.cpp) tries dlopen("libnccl.so.2") first, matching
  PyTorch's soname exactly; glibc reuses the already-loaded handle so no
  symlink is needed. ensure_nccl_symlink() is kept for TRT < 11.0 users.
- Update module docstring to reflect confirmed TRT 11.0+ behavior.
BowenFu added a commit to BowenFu/Torch-TensorRT that referenced this pull request Aug 11, 2026
…e rework

PR pytorch#4222 merged PythonTorchTensorRTModule into TorchTensorRTModule and now
wraps trt.ICudaEngine in TRTEngine. Update the integration helpers:

- Drop the removed _PythonTorchTensorRTModule import; engine collection
  now matches only TorchTensorRTModule.
- Add use_python_runtime=True to _trt_compile so the C++ runtime
  (which needs libtorchtrt_runtime.so to register torch.classes.tensorrt.Engine)
  is not required to run these tests.
- Unwrap engines[0].engine via .cuda_engine in the inspector helpers since
  the Python runtime now returns a TRTEngine wrapper rather than a raw
  trt.ICudaEngine.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: runtime component: tests Issues re: Tests documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants