|
| 1 | +""" |
| 2 | +.. _torch_compile_advanced_usage: |
| 3 | +
|
| 4 | +Torch Compile Advanced Usage |
| 5 | +====================================================== |
| 6 | +
|
| 7 | +This interactive script is intended as an overview of the process by which `torch_tensorrt.compile(..., ir="torch_compile", ...)` works, and how it integrates with the `torch.compile` API.""" |
| 8 | + |
| 9 | +# %% |
| 10 | +# Imports and Model Definition |
| 11 | +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 12 | + |
| 13 | +import torch |
| 14 | +import torch_tensorrt |
| 15 | + |
| 16 | +# %% |
| 17 | + |
| 18 | +# We begin by defining a model |
| 19 | +class Model(torch.nn.Module): |
| 20 | + def __init__(self) -> None: |
| 21 | + super().__init__() |
| 22 | + self.relu = torch.nn.ReLU() |
| 23 | + |
| 24 | + def forward(self, x: torch.Tensor, y: torch.Tensor): |
| 25 | + x_out = self.relu(x) |
| 26 | + y_out = self.relu(y) |
| 27 | + x_y_out = x_out + y_out |
| 28 | + return torch.mean(x_y_out) |
| 29 | + |
| 30 | + |
| 31 | +# %% |
| 32 | +# Compilation with `torch.compile` Using Default Settings |
| 33 | +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 34 | + |
| 35 | +# Define sample float inputs and initialize model |
| 36 | +sample_inputs = [torch.rand((5, 7)).cuda(), torch.rand((5, 7)).cuda()] |
| 37 | +model = Model().eval().cuda() |
| 38 | + |
| 39 | +# %% |
| 40 | + |
| 41 | +# Next, we compile the model using torch.compile |
| 42 | +# For the default settings, we can simply call torch.compile |
| 43 | +# with the backend "torch_tensorrt", and run the model on an |
| 44 | +# input to cause compilation, as so: |
| 45 | +optimized_model = torch.compile(model, backend="torch_tensorrt") |
| 46 | +optimized_model(*sample_inputs) |
| 47 | + |
| 48 | +# %% |
| 49 | +# Compilation with `torch.compile` Using Custom Settings |
| 50 | +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 51 | + |
| 52 | +# First, we use Torch utilities to clean up the workspace |
| 53 | +# after the previous compile invocation |
| 54 | +torch._dynamo.reset() |
| 55 | + |
| 56 | +# Define sample half inputs and initialize model |
| 57 | +sample_inputs_half = [ |
| 58 | + torch.rand((5, 7)).half().cuda(), |
| 59 | + torch.rand((5, 7)).half().cuda(), |
| 60 | +] |
| 61 | +model_half = Model().eval().cuda() |
| 62 | + |
| 63 | +# %% |
| 64 | + |
| 65 | +# If we want to customize certain options in the backend, |
| 66 | +# but still use the torch.compile call directly, we can provide |
| 67 | +# custom options to the backend via the "options" keyword |
| 68 | +# which takes in a dictionary mapping options to values. |
| 69 | +# |
| 70 | +# For accepted backend options, see the CompilationSettings dataclass: |
| 71 | +# py/torch_tensorrt/dynamo/backend/_settings.py |
| 72 | +backend_kwargs = { |
| 73 | + "enabled_precisions": {torch.half}, |
| 74 | + "debug": True, |
| 75 | + "min_block_size": 2, |
| 76 | + "torch_executed_ops": {"torch.ops.aten.sub.Tensor"}, |
| 77 | + "optimization_level": 4, |
| 78 | + "use_python_runtime": False, |
| 79 | +} |
| 80 | + |
| 81 | +# Run the model on an input to cause compilation, as so: |
| 82 | +optimized_model_custom = torch.compile( |
| 83 | + model_half, backend="torch_tensorrt", options=backend_kwargs |
| 84 | +) |
| 85 | +optimized_model_custom(*sample_inputs_half) |
| 86 | + |
| 87 | +# %% |
| 88 | +# Cleanup |
| 89 | +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 90 | + |
| 91 | +# Finally, we use Torch utilities to clean up the workspace |
| 92 | +torch._dynamo.reset() |
| 93 | + |
| 94 | +# %% |
| 95 | +# Cuda Driver Error Note |
| 96 | +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 97 | +# |
| 98 | +# Occasionally, upon exiting the Python runtime after Dynamo compilation with `torch_tensorrt`, |
| 99 | +# one may encounter a Cuda Driver Error. This issue is related to https://github.com/NVIDIA/TensorRT/issues/2052 |
| 100 | +# and can be resolved by wrapping the compilation/inference in a function and using a scoped call, as in:: |
| 101 | +# |
| 102 | +# if __name__ == '__main__': |
| 103 | +# compile_engine_and_infer() |
0 commit comments