Skip to content

    [1]:

    # Copyright 2019 NVIDIA Corporation. All Rights Reserved.
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #     http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    # ==============================================================================

# Torch-TensorRT Getting Started - LeNet

## Overview

In the practice of developing machine learning models, there are few tools as approachable as PyTorch for developing and experimenting in designing machine learning models. The power of PyTorch comes from its deep integration into Python, its flexibility and its approach to automatic differentiation and execution (eager execution). However, when moving from research into production, the requirements change and we may no longer want that deep Python integration and we want optimization to get the best performance we can on our deployment platform. In PyTorch 1.0, TorchScript was introduced as a method to separate your PyTorch model from Python, make it portable and optimizable. TorchScript uses PyTorch’s JIT compiler to transform your normal PyTorch code which gets interpreted by the Python interpreter to an intermediate representation (IR) which can have optimizations run on it and at runtime can get interpreted by the PyTorch JIT interpreter. For PyTorch this has opened up a whole new world of possibilities, including deployment in other languages like C++. It also introduces a structured graph based format that we can use to do down to the kernel level optimization of models for inference.

When deploying on NVIDIA GPUs TensorRT, NVIDIA’s Deep Learning Optimization SDK and Runtime is able to take models from any major framework and specifically tune them to perform better on specific target hardware in the NVIDIA family be it an A100, TITAN V, Jetson Xavier or NVIDIA’s Deep Learning Accelerator. TensorRT performs a couple sets of optimizations to achieve this. TensorRT fuses layers and tensors in the model graph, it then uses a large kernel library to select implementations that perform best on the target GPU. TensorRT also has strong support for reduced operating precision execution which allows users to leverage the Tensor Cores on Volta and newer GPUs as well as reducing memory and computation footprints on device.

Torch-TensorRT is a compiler that uses TensorRT to optimize TorchScript code, compiling standard TorchScript modules into ones that internally run with TensorRT optimizations. This enables you to continue to remain in the PyTorch ecosystem, using all the great features PyTorch has such as module composability, its flexible tensor implementation, data loaders and more. Torch-TensorRT is available to use with both PyTorch and LibTorch.

### Learning objectives

This notebook demonstrates the steps for compiling a TorchScript module with Torch-TensorRT on a simple LeNet network.

## Content

1. Requirements

2. Creating TorchScript modules

3. Compiling with Torch-TensorRT

## 1. Requirements

Follow the steps in `notebooks/README` to prepare a Docker container, within which you can run this notebook.

    [1]:

    !pip install ipywidgets --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org
    !nvidia-smi

    Looking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com
    Collecting ipywidgets
      Downloading ipywidgets-7.6.5-py2.py3-none-any.whl (121 kB)
         |████████████████████████████████| 121 kB 12.7 MB/s eta 0:00:01
    Requirement already satisfied: traitlets>=4.3.1 in /opt/conda/lib/python3.8/site-packages (from ipywidgets) (5.1.1)
    Collecting jupyterlab-widgets>=1.0.0
      Downloading jupyterlab_widgets-1.0.2-py3-none-any.whl (243 kB)
         |████████████████████████████████| 243 kB 115.0 MB/s eta 0:00:01
    Requirement already satisfied: ipython-genutils~=0.2.0 in /opt/conda/lib/python3.8/site-packages (from ipywidgets) (0.2.0)
    Requirement already satisfied: nbformat>=4.2.0 in /opt/conda/lib/python3.8/site-packages (from ipywidgets) (5.1.3)
    Requirement already satisfied: ipykernel>=4.5.1 in /opt/conda/lib/python3.8/site-packages (from ipywidgets) (6.7.0)
    Requirement already satisfied: ipython>=4.0.0 in /opt/conda/lib/python3.8/site-packages (from ipywidgets) (7.31.0)
    Collecting widgetsnbextension~=3.5.0
      Downloading widgetsnbextension-3.5.2-py2.py3-none-any.whl (1.6 MB)
         |████████████████████████████████| 1.6 MB 122.5 MB/s eta 0:00:01
    Requirement already satisfied: tornado<7.0,>=4.2 in /opt/conda/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets) (6.1)
    Requirement already satisfied: debugpy<2.0,>=1.0.0 in /opt/conda/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets) (1.5.1)
    Requirement already satisfied: jupyter-client<8.0 in /opt/conda/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets) (7.1.2)
    Requirement already satisfied: nest-asyncio in /opt/conda/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets) (1.5.4)
    Requirement already satisfied: matplotlib-inline<0.2.0,>=0.1.0 in /opt/conda/lib/python3.8/site-packages (from ipykernel>=4.5.1->ipywidgets) (0.1.3)
    Requirement already satisfied: setuptools>=18.5 in /opt/conda/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (59.5.0)
    Requirement already satisfied: backcall in /opt/conda/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (0.2.0)
    Requirement already satisfied: pexpect>4.3 in /opt/conda/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (4.8.0)
    Requirement already satisfied: pickleshare in /opt/conda/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (0.7.5)
    Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /opt/conda/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (3.0.24)
    Requirement already satisfied: jedi>=0.16 in /opt/conda/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (0.18.1)
    Requirement already satisfied: decorator in /opt/conda/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (5.1.0)
    Requirement already satisfied: pygments in /opt/conda/lib/python3.8/site-packages (from ipython>=4.0.0->ipywidgets) (2.11.1)
    Requirement already satisfied: parso<0.9.0,>=0.8.0 in /opt/conda/lib/python3.8/site-packages (from jedi>=0.16->ipython>=4.0.0->ipywidgets) (0.8.3)
    Requirement already satisfied: pyzmq>=13 in /opt/conda/lib/python3.8/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (22.3.0)
    Requirement already satisfied: entrypoints in /opt/conda/lib/python3.8/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (0.3)
    Requirement already satisfied: jupyter-core>=4.6.0 in /opt/conda/lib/python3.8/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (4.9.1)
    Requirement already satisfied: python-dateutil>=2.1 in /opt/conda/lib/python3.8/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (2.8.2)
    Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /opt/conda/lib/python3.8/site-packages (from nbformat>=4.2.0->ipywidgets) (4.4.0)
    Requirement already satisfied: attrs>=17.4.0 in /opt/conda/lib/python3.8/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (21.4.0)
    Requirement already satisfied: importlib-resources>=1.4.0 in /opt/conda/lib/python3.8/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (5.4.0)
    Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /opt/conda/lib/python3.8/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (0.18.1)
    Requirement already satisfied: zipp>=3.1.0 in /opt/conda/lib/python3.8/site-packages (from importlib-resources>=1.4.0->jsonschema!=2.5.0,>=2.4->nbformat>=4.2.0->ipywidgets) (3.7.0)
    Requirement already satisfied: ptyprocess>=0.5 in /opt/conda/lib/python3.8/site-packages (from pexpect>4.3->ipython>=4.0.0->ipywidgets) (0.7.0)
    Requirement already satisfied: wcwidth in /opt/conda/lib/python3.8/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=4.0.0->ipywidgets) (0.2.5)
    Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.8/site-packages (from python-dateutil>=2.1->jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (1.16.0)
    Requirement already satisfied: notebook>=4.4.1 in /opt/conda/lib/python3.8/site-packages (from widgetsnbextension~=3.5.0->ipywidgets) (6.4.1)
    Requirement already satisfied: terminado>=0.8.3 in /opt/conda/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.12.1)
    Requirement already satisfied: prometheus-client in /opt/conda/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.13.0)
    Requirement already satisfied: jinja2 in /opt/conda/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (3.0.3)
    Requirement already satisfied: argon2-cffi in /opt/conda/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (21.3.0)
    Requirement already satisfied: nbconvert in /opt/conda/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (6.4.0)
    Requirement already satisfied: Send2Trash>=1.5.0 in /opt/conda/lib/python3.8/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.8.0)
    Requirement already satisfied: argon2-cffi-bindings in /opt/conda/lib/python3.8/site-packages (from argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (21.2.0)
    Requirement already satisfied: cffi>=1.0.1 in /opt/conda/lib/python3.8/site-packages (from argon2-cffi-bindings->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.15.0)
    Requirement already satisfied: pycparser in /opt/conda/lib/python3.8/site-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (2.21)
    Requirement already satisfied: MarkupSafe>=2.0 in /opt/conda/lib/python3.8/site-packages (from jinja2->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (2.0.1)
    Requirement already satisfied: testpath in /opt/conda/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.5.0)
    Requirement already satisfied: defusedxml in /opt/conda/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.7.1)
    Requirement already satisfied: jupyterlab-pygments in /opt/conda/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.1.2)
    Requirement already satisfied: nbclient<0.6.0,>=0.5.0 in /opt/conda/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.5.10)
    Requirement already satisfied: bleach in /opt/conda/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (4.1.0)
    Requirement already satisfied: pandocfilters>=1.4.1 in /opt/conda/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.5.0)
    Requirement already satisfied: mistune<2,>=0.8.1 in /opt/conda/lib/python3.8/site-packages (from nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.8.4)
    Requirement already satisfied: webencodings in /opt/conda/lib/python3.8/site-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.5.1)
    Requirement already satisfied: packaging in /opt/conda/lib/python3.8/site-packages (from bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (21.3)
    Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /opt/conda/lib/python3.8/site-packages (from packaging->bleach->nbconvert->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (3.0.6)
    Installing collected packages: widgetsnbextension, jupyterlab-widgets, ipywidgets
    Successfully installed ipywidgets-7.6.5 jupyterlab-widgets-1.0.2 widgetsnbextension-3.5.2
    WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
    Thu Feb 10 22:01:27 2022
    +-----------------------------------------------------------------------------+
    | NVIDIA-SMI 510.39.01    Driver Version: 510.39.01    CUDA Version: 11.6     |
    |-------------------------------+----------------------+----------------------+
    | GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
    | Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
    |                               |                      |               MIG M. |
    |===============================+======================+======================|
    |   0  NVIDIA GeForce ...  On   | 00000000:09:00.0 Off |                  N/A |
    |  0%   42C    P8    20W / 320W |      0MiB / 10240MiB |      0%      Default |
    |                               |                      |                  N/A |
    +-------------------------------+----------------------+----------------------+

    +-----------------------------------------------------------------------------+
    | Processes:                                                                  |
    |  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
    |        ID   ID                                                   Usage      |
    |=============================================================================|
    |  No running processes found                                                 |
    +-----------------------------------------------------------------------------+

## 2. Creating TorchScript modules

Here we create two submodules for a feature extractor and a classifier and stitch them together in a single LeNet module. In this case this is overkill but modules give us granular control over our program including where we decide to optimize and where we don’t. It is also the unit that the TorchScript compiler operates on. So you can decide to only convert/optimize the feature extractor and leave the classifier in standard PyTorch or you can convert the whole thing. When compiling your module to TorchScript, there are two paths: Tracing and Scripting.

    [2]:

    import torch
    from torch import nn
    import torch.nn.functional as F

    class LeNetFeatExtractor(nn.Module):
        def __init__(self):
            super(LeNetFeatExtractor, self).__init__()
            self.conv1 = nn.Conv2d(1, 128, 3)
            self.conv2 = nn.Conv2d(128, 16, 3)

        def forward(self, x):
            x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
            x = F.max_pool2d(F.relu(self.conv2(x)), 2)
            return x

    class LeNetClassifier(nn.Module):
        def __init__(self):
            super(LeNetClassifier, self).__init__()
            self.fc1 = nn.Linear(16 * 6 * 6, 120)
            self.fc2 = nn.Linear(120, 84)
            self.fc3 = nn.Linear(84, 10)

        def forward(self, x):
            x = torch.flatten(x,1)
            x = F.relu(self.fc1(x))
            x = F.relu(self.fc2(x))
            x = self.fc3(x)
            return x

    class LeNet(nn.Module):
        def __init__(self):
            super(LeNet, self).__init__()
            self.feat = LeNetFeatExtractor()
            self.classifer = LeNetClassifier()

        def forward(self, x):
            x = self.feat(x)
            x = self.classifer(x)
            return x

Let us define a helper function to benchmark a model.

    [3]:

    import time
    import numpy as np

    import torch.backends.cudnn as cudnn
    cudnn.benchmark = True

    def benchmark(model, input_shape=(1024, 1, 32, 32), dtype='fp32', nwarmup=50, nruns=1000):
        input_data = torch.randn(input_shape)
        input_data = input_data.to("cuda")
        if dtype=='fp16':
            input_data = input_data.half()

        print("Warm up ...")
        with torch.no_grad():
            for _ in range(nwarmup):
                features = model(input_data)
        torch.cuda.synchronize()
        print("Start timing ...")
        timings = []
        with torch.no_grad():
            for i in range(1, nruns+1):
                start_time = time.time()
                features = model(input_data)
                torch.cuda.synchronize()
                end_time = time.time()
                timings.append(end_time - start_time)
                if i%100==0:
                    print('Iteration %d/%d, ave batch time %.2f ms'%(i, nruns, np.mean(timings)*1000))

        print("Input shape:", input_data.size())
        print("Output features size:", features.size())

        print('Average batch time: %.2f ms'%(np.mean(timings)*1000))

### PyTorch model

    [4]:

    model = LeNet()
    model.to("cuda").eval()

    [4]:

    LeNet(
      (feat): LeNetFeatExtractor(
        (conv1): Conv2d(1, 128, kernel_size=(3, 3), stride=(1, 1))
        (conv2): Conv2d(128, 16, kernel_size=(3, 3), stride=(1, 1))
      )
      (classifer): LeNetClassifier(
        (fc1): Linear(in_features=576, out_features=120, bias=True)
        (fc2): Linear(in_features=120, out_features=84, bias=True)
        (fc3): Linear(in_features=84, out_features=10, bias=True)
      )
    )

    [5]:

    benchmark(model)

    Warm up ...
    Start timing ...
    Iteration 100/1000, ave batch time 5.56 ms
    Iteration 200/1000, ave batch time 5.56 ms
    Iteration 300/1000, ave batch time 5.56 ms
    Iteration 400/1000, ave batch time 5.56 ms
    Iteration 500/1000, ave batch time 5.56 ms
    Iteration 600/1000, ave batch time 5.56 ms
    Iteration 700/1000, ave batch time 5.56 ms
    Iteration 800/1000, ave batch time 5.56 ms
    Iteration 900/1000, ave batch time 5.56 ms
    Iteration 1000/1000, ave batch time 5.56 ms
    Input shape: torch.Size([1024, 1, 32, 32])
    Output features size: torch.Size([1024, 10])
    Average batch time: 5.56 ms

When compiling your module to TorchScript, there are two paths: Tracing and Scripting.

### Tracing

Tracing follows the path of execution when the module is called and records what happens. This recording is what the TorchScript IR will describe. To trace an instance of our LeNet module, we can call torch.jit.trace with an example input.

    [6]:

    traced_model = torch.jit.trace(model, torch.empty([1,1,32,32]).to("cuda"))
    traced_model

    [6]:

    LeNet(
      original_name=LeNet
      (feat): LeNetFeatExtractor(
        original_name=LeNetFeatExtractor
        (conv1): Conv2d(original_name=Conv2d)
        (conv2): Conv2d(original_name=Conv2d)
      )
      (classifer): LeNetClassifier(
        original_name=LeNetClassifier
        (fc1): Linear(original_name=Linear)
        (fc2): Linear(original_name=Linear)
        (fc3): Linear(original_name=Linear)
      )
    )

    [7]:

    benchmark(traced_model)

    Warm up ...
    Start timing ...
    Iteration 100/1000, ave batch time 5.56 ms
    Iteration 200/1000, ave batch time 5.56 ms
    Iteration 300/1000, ave batch time 5.56 ms
    Iteration 400/1000, ave batch time 5.56 ms
    Iteration 500/1000, ave batch time 5.56 ms
    Iteration 600/1000, ave batch time 5.56 ms
    Iteration 700/1000, ave batch time 5.56 ms
    Iteration 800/1000, ave batch time 5.56 ms
    Iteration 900/1000, ave batch time 5.56 ms
    Iteration 1000/1000, ave batch time 5.56 ms
    Input shape: torch.Size([1024, 1, 32, 32])
    Output features size: torch.Size([1024, 10])
    Average batch time: 5.56 ms

### Scripting

Scripting actually inspects your code with a compiler and generates an equivalent TorchScript program. The difference is that since tracing simply follows the execution of your module, it cannot pick up control flow for instance, it will only follow the code path that a particular input triggers. By working from the Python code, the compiler can include these components. We can run the script compiler on our LeNet module by calling torch.jit.script.

    [8]:

    model = LeNet().to("cuda").eval()
    script_model = torch.jit.script(model)

    [9]:

    script_model

    [9]:

    RecursiveScriptModule(
      original_name=LeNet
      (feat): RecursiveScriptModule(
        original_name=LeNetFeatExtractor
        (conv1): RecursiveScriptModule(original_name=Conv2d)
        (conv2): RecursiveScriptModule(original_name=Conv2d)
      )
      (classifer): RecursiveScriptModule(
        original_name=LeNetClassifier
        (fc1): RecursiveScriptModule(original_name=Linear)
        (fc2): RecursiveScriptModule(original_name=Linear)
        (fc3): RecursiveScriptModule(original_name=Linear)
      )
    )

    [10]:

    benchmark(script_model)

    Warm up ...
    Start timing ...
    Iteration 100/1000, ave batch time 5.56 ms
    Iteration 200/1000, ave batch time 5.56 ms
    Iteration 300/1000, ave batch time 5.56 ms
    Iteration 400/1000, ave batch time 5.56 ms
    Iteration 500/1000, ave batch time 5.56 ms
    Iteration 600/1000, ave batch time 5.56 ms
    Iteration 700/1000, ave batch time 5.56 ms
    Iteration 800/1000, ave batch time 5.56 ms
    Iteration 900/1000, ave batch time 5.56 ms
    Iteration 1000/1000, ave batch time 5.56 ms
    Input shape: torch.Size([1024, 1, 32, 32])
    Output features size: torch.Size([1024, 10])
    Average batch time: 5.56 ms

## 3. Compiling with Torch-TensorRT

### TorchScript traced model

First, we compile the TorchScript traced model with Torch-TensorRT. Notice the performance impact.

    [13]:

    import torch_tensorrt

    # We use a batch-size of 1024, and half precision
    trt_ts_module = torch_tensorrt.compile(traced_model, inputs=[torch_tensorrt.Input(
                min_shape=[1024, 1, 32, 32],
                opt_shape=[1024, 1, 33, 33],
                max_shape=[1024, 1, 34, 34],
                dtype=torch.half
                )],
                enabled_precisions = {torch.half})

    input_data = torch.randn((1024, 1, 32, 32))
    input_data = input_data.half().to("cuda")

    input_data = input_data.half()
    result = trt_ts_module(input_data)
    torch.jit.save(trt_ts_module, "trt_ts_module.ts")

    WARNING: [Torch-TensorRT] - For input x.1, found user specified input dtype as Float16, however when inspecting the graph, the input type expected was inferred to be Float
    The compiler is going to use the user setting Float16
    This conflict may cause an error at runtime due to partial compilation being enabled and therefore
    compatibility with PyTorch's data type convention is required.
    If you do indeed see errors at runtime either:
    - Remove the dtype spec for x.1
    - Disable partial compilation by setting require_full_compilation to True
    WARNING: [Torch-TensorRT] - Dilation not used in Max pooling converter
    WARNING: [Torch-TensorRT] - Dilation not used in Max pooling converter
    WARNING: [Torch-TensorRT TorchScript Conversion Context] - Max value of this profile is not valid

    [14]:

    benchmark(trt_ts_module, input_shape=(1024, 1, 32, 32), dtype="fp16")

    Warm up ...
    Start timing ...
    Iteration 100/1000, ave batch time 1.41 ms
    Iteration 200/1000, ave batch time 1.40 ms
    Iteration 300/1000, ave batch time 1.40 ms
    Iteration 400/1000, ave batch time 1.39 ms
    Iteration 500/1000, ave batch time 1.40 ms
    Iteration 600/1000, ave batch time 1.40 ms
    Iteration 700/1000, ave batch time 1.40 ms
    Iteration 800/1000, ave batch time 1.40 ms
    Iteration 900/1000, ave batch time 1.40 ms
    Iteration 1000/1000, ave batch time 1.40 ms
    Input shape: torch.Size([1024, 1, 32, 32])
    Output features size: torch.Size([1024, 10])
    Average batch time: 1.40 ms

### TorchScript script model

Next, we compile the TorchScript script model with Torch-TensorRT. Notice the performance impact.

    [16]:

    import torch_tensorrt

    trt_script_module = torch_tensorrt.compile(script_model, inputs = [torch_tensorrt.Input(
                min_shape=[1024, 1, 32, 32],
                opt_shape=[1024, 1, 33, 33],
                max_shape=[1024, 1, 34, 34],
                dtype=torch.half
                )],
                enabled_precisions={torch.half})

    input_data = torch.randn((1024, 1, 32, 32))
    input_data = input_data.half().to("cuda")

    input_data = input_data.half()
    result = trt_script_module(input_data)
    torch.jit.save(trt_script_module, "trt_script_module.ts")

    WARNING: [Torch-TensorRT] - For input x.1, found user specified input dtype as Float16, however when inspecting the graph, the input type expected was inferred to be Float
    The compiler is going to use the user setting Float16
    This conflict may cause an error at runtime due to partial compilation being enabled and therefore
    compatibility with PyTorch's data type convention is required.
    If you do indeed see errors at runtime either:
    - Remove the dtype spec for x.1
    - Disable partial compilation by setting require_full_compilation to True
    WARNING: [Torch-TensorRT] - Dilation not used in Max pooling converter
    WARNING: [Torch-TensorRT] - Dilation not used in Max pooling converter
    WARNING: [Torch-TensorRT TorchScript Conversion Context] - Max value of this profile is not valid

    [17]:

    benchmark(trt_script_module, input_shape=(1024, 1, 32, 32), dtype="fp16")

    Warm up ...
    Start timing ...
    Iteration 100/1000, ave batch time 1.43 ms
    Iteration 200/1000, ave batch time 1.41 ms
    Iteration 300/1000, ave batch time 1.40 ms
    Iteration 400/1000, ave batch time 1.42 ms
    Iteration 500/1000, ave batch time 1.42 ms
    Iteration 600/1000, ave batch time 1.41 ms
    Iteration 700/1000, ave batch time 1.41 ms
    Iteration 800/1000, ave batch time 1.40 ms
    Iteration 900/1000, ave batch time 1.40 ms
    Iteration 1000/1000, ave batch time 1.40 ms
    Input shape: torch.Size([1024, 1, 32, 32])
    Output features size: torch.Size([1024, 10])
    Average batch time: 1.40 ms

## Conclusion

In this notebook, we have walked through the complete process of compiling TorchScript models with Torch-TensorRT and test the performance impact of the optimization.

### What’s next

Now it’s time to try Torch-TensorRT on your own model. Fill out issues at https://github.com/NVIDIA/Torch-TensorRT . Your involvement will help future development of Torch-TensorRT.