Repository navigation
Conversation
c711e06 to
06f6eb0
Compare
narendasan
left a comment
There was a problem hiding this comment.
Can you add an example with how to use this to the docs?
983cf29 to
2421c97
Compare
7aeb2ae to
bb5fc11
Compare
589924d to
65e075c
Compare
27b9429 to
ff66837
Compare
| 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
https://github.com/pytorch/TensorRT/pull/4752/changes#diff-4d42b181b269feac334e4735a62a27fa4ec341f9257781117b610e1b6bafb44cR346
It shows the region marker currently only supports in torch_trt.compile()
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.
eebf847 to
8620c78
Compare
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
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.regions={selector: rule}withExecuteInTorch,FusedOpandFusedKernel. 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")andtorch_tensorrt.dynamo.compile(program), for annn.Moduleand for an exported program (strict or not), including a program saved and loaded in another process.torch.compile(model, backend="torch_tensorrt")andtorch_tensorrt.compile(model, ir="torch_compile"). A graph with regions that cannot be compiled raises instead of falling back to PyTorch.torch.compilebackends 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)MutableTorchTensorRTModule, CPU weight offload, autocast, resource partitioning and weight streaming.execute_in_torchorfused_opregions, and saving a model compiled with regions. Both are left for follow-up PRs.run_decompositions().Docs: tutorial
docsrc/tutorials/execute_in_torch.rst, API pagedocsrc/py_api/region.rst, and four examples underexamples/dynamo/(execute_in_torch.py,fused_op.py,fused_kernel.py,region_rules.py).Type of change
Checklist: