Skip to content

Python runtime rework #4222

Open
cehongwang wants to merge 2 commits intomainfrom
python-runtime-rework-patches
Open

Python runtime rework #4222
cehongwang wants to merge 2 commits intomainfrom
python-runtime-rework-patches

Conversation

@cehongwang
Copy link
Copy Markdown
Collaborator

@cehongwang cehongwang commented Apr 29, 2026

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
Copy link
Copy Markdown
Collaborator

@narendasan narendasan left a comment

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

: TRTEngine::ResourceAllocationStrategy::kStatic);
})
.def_readwrite("use_pre_allocated_outputs", &TRTEngine::use_pre_allocated_outputs)
.def_readwrite("pre_allocated_outputs", &TRTEngine::pre_allocated_outputs)
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 do we need both?

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.

The first one is the flag, and the second one is the tensor. There is a test that compares the output tensor pointer to verify it is the same tensor. So the second one is needed

Comment thread docsrc/tutorials/deployment/distributed_inference.rst
Comment thread py/torch_tensorrt/dynamo/runtime/_serialized_engine_layout.py
Comment thread py/torch_tensorrt/dynamo/_exporter.py
Comment thread scripts/remove_set_runtime_backend_with.py Outdated
@cehongwang cehongwang force-pushed the python-runtime-rework-patches branch from daad718 to a328e80 Compare April 29, 2026 20:59
Copy link
Copy Markdown
Collaborator

@zewenli98 zewenli98 left a comment

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
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

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 2 times, most recently from b04e443 to edd97c1 Compare May 5, 2026 23:24
@cehongwang cehongwang force-pushed the python-runtime-rework-patches branch 2 times, most recently from 15503b0 to 42972f0 Compare May 5, 2026 23:43
@cehongwang cehongwang force-pushed the python-runtime-rework-patches branch from 42972f0 to 29673a1 Compare May 5, 2026 23:52
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