Skip to content

feat: region annotation for backend dispatching - #4752

Open
bowang007 wants to merge 1 commit into
mainfrom
region_annotation
Open

bowang007 wants to merge 1 commit into
mainfrom
region_annotation

Conversation

@bowang007

@bowang007 bowang007 commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Adds region annotations to the Dynamo frontend (experimental, torch_tensorrt.region). A region is a group of operations the user places: Torch-TensorRT keeps it in PyTorch or replaces it with an operator or kernel the user supplies, and compiles the rest of the model as usual.

Two ways to mark a region

  • Scopes written in forward:
    • execute_in_torch(backend="eager" | "inductor") keeps the operations in PyTorch.
    • fused_op(op, inputs=...) replaces them with a registered custom operator that runs in PyTorch.
    • fused_kernel(kernel, inputs=..., ...) replaces them with a Triton or CUDA kernel that becomes a TensorRT plugin.
  • Rules passed at compile time, regions={selector: rule} with ExecuteInTorch, FusedOp and FusedKernel. A selector is a module path ("layers.*.mlp"), a module class or a module object, so a model from a library and a program that is already exported need no source change.

Compile paths that honor regions

  • torch_tensorrt.compile(model, ir="dynamo") and torch_tensorrt.dynamo.compile(program), for an nn.Module and for an exported program (strict or not), including a program saved and loaded in another process.
  • torch.compile(model, backend="torch_tensorrt") and torch_tensorrt.compile(model, ir="torch_compile"). A graph with regions that cannot be compiled raises instead of falling back to PyTorch.
  • Other torch.compile backends and TorchScript tracing ignore scopes with a warning. ignore_regions() traces annotated code as ordinary operations, for tools that do not know the region markers.

How it works

A scope records two marker operators in the trace; a rule inserts the same markers around the operations of the module it selects. Capture extracts each region into a child graph that decomposition leaves whole. It then becomes a PyTorch call, a call of the custom operator, or a generated plugin operator inside an engine. The partitioners keep PyTorch regions out of TensorRT, and placement is audited around partitioning and on the finished module.

Not supported yet (each raises a RegionError)

  • Dynamic shapes, writes to buffers or inputs, training mode, nested scopes and complex tensors in a model with regions.
  • Refit, MutableTorchTensorRTModule, CPU weight offload, autocast, resource partitioning and weight streaming.
  • CUDA graphs over execute_in_torch or fused_op regions, and saving a model compiled with regions. Both are left for follow-up PRs.
  • Engine-only output, cross-compilation and programs that went through run_decompositions().

Docs: tutorial docsrc/tutorials/execute_in_torch.rst, API page docsrc/py_api/region.rst, and four examples under examples/dynamo/ (execute_in_torch.py, fused_op.py, fused_kernel.py, region_rules.py).

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

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 Sep 30, 2026
@bowang007
bowang007 marked this pull request as draft September 30, 2026 00:00
@github-actions github-actions Bot added component: tests Issues re: Tests component: lowering Issues re: The lowering / preprocessing passes component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Sep 30, 2026
@lanluo-nvidia lanluo-nvidia added this to the v2.15.0 milestone Sep 30, 2026
@narendasan narendasan modified the milestones: v2.15.0, v2.16.0 Oct 6, 2026
@github-actions github-actions Bot added the component: build system Issues re: Build system label Oct 13, 2026
@bowang007
bowang007 marked this pull request as ready for review October 13, 2026 01:17

@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.

Can you add an example with how to use this to the docs?

Comment thread py/torch_tensorrt/region/README.md Resolved
Comment thread py/torch_tensorrt/region/README.md Resolved
@github-actions github-actions Bot added documentation Improvements or additions to documentation component: runtime labels Oct 21, 2026
Comment thread examples/dynamo/fuse_in_plugin.py Resolved
Comment thread examples/dynamo/fuse_in_plugin.py Resolved
@bowang007
bowang007 force-pushed the region_annotation branch 3 times, most recently from 983cf29 to 2421c97 Compare October 29, 2026 01:44
@bowang007
bowang007 force-pushed the region_annotation branch 2 times, most recently from 7aeb2ae to bb5fc11 Compare November 3, 2026 01:26
@github-actions
github-actions Bot requested a review from cehongwang November 3, 2026 22:14
@github-actions github-actions Bot added the component: conversion Issues re: Conversion stage label Nov 4, 2026
@bowang007
bowang007 force-pushed the region_annotation branch 2 times, most recently from 27b9429 to ff66837 Compare November 4, 2026 21:30
@narendasan
narendasan requested a review from zewenli98 November 10, 2026 18:27
Comment on lines +36 to +47
class Model(torch.nn.Module):
def __init__(self, backend="eager"):
super().__init__()
self.backend = backend

def forward(self, x, bias, scale):
x = x + 1.0
with torch_tensorrt.region.execute_in_torch(backend=self.backend):
x = x + bias
x = torch.relu(x)
x = x * scale
return x - 0.5

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.

Since with torch_tensorrt.region.execute_in_torch(backend=self.backend): is inserted in the source codes, how to make sure torch.compile(..., backend="torch_tensorrt") and torch export + dynamo compile lead to the same result?

If users have an exported program but don't have access to source codes, is there a way to achieve the functionality?

@zewenli98 zewenli98 Nov 18, 2026 •

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.

A region is a group of operations the user places: Torch-TensorRT keeps
it in PyTorch or replaces it with an operator or kernel the user
supplies, and compiles the rest of the model as usual.

API (torch_tensorrt.region, experimental)
- Scopes written in forward: execute_in_torch (backend "eager" or
  "inductor"), fused_op (a registered custom operator that stays in
  PyTorch) and fused_kernel (a Triton or CUDA kernel that becomes a
  TensorRT plugin).
- Rules passed at compile time, regions={selector: rule}, with
  ExecuteInTorch, FusedOp and FusedKernel. A selector is a module path,
  a module class or a module object, so a model from a library and a
  program that is already exported need no source change.
- ignore_regions() traces annotated code as ordinary operations, for
  tools that do not know the region markers.

Entry paths
- torch_tensorrt.compile(ir="dynamo") and torch_tensorrt.dynamo.compile,
  for an nn.Module and for an exported program, strict or not, also one
  saved and loaded in another process.
- torch.compile with the Torch-TensorRT backend ("torch_tensorrt",
  "tensorrt", ir="torch_compile"). A graph with regions that cannot be
  compiled raises instead of falling back to PyTorch.
- Other torch.compile backends and TorchScript tracing ignore scopes
  with a warning.

How it works
- A scope records two marker operators in the trace; a rule inserts the
  same markers around the operations of the module it selects. The
  operations between the markers are the region.
- Capture extracts each region into a child graph that decomposition
  leaves whole. It then becomes a PyTorch call, a call of the custom
  operator, or a generated plugin operator inside an engine. The
  partitioners keep PyTorch regions out of TensorRT, and placement is
  audited around partitioning and on the finished module.

Not supported yet, each with a RegionError
- Dynamic shapes, writes to buffers or inputs, training mode, nested
  scopes and complex tensors in a model with regions.
- Refit, MutableTorchTensorRTModule, CPU weight offload, autocast,
  resource partitioning and weight streaming.
- CUDA graphs over execute_in_torch or fused_op regions, and saving a
  model compiled with regions.
- Engine-only output, cross-compilation and decomposed programs.

Also in this change
- kernels: looking up a free plugin name no longer makes TensorRT log
  an error for every generated plugin.
- remove_detach no longer takes a placeholder named "detach" for a call.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci: full cla signed component: api [Python] Issues re: Python API component: build system Issues re: Build system 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: lowering Issues re: The lowering / preprocessing passes component: runtime component: tests Issues re: Tests component: torch_compile documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants