torch2trt icon indicating copy to clipboard operation
torch2trt copied to clipboard

[torch2trt.py] Add pretty trace for verbose logging.

Open chaoz-dev opened this issue 3 years ago • 1 comments

Addresses #788.

This PR adds pretty tracing when running in verbose logging mode. Specifically, we print out extra information when converting such as where the torch op is called, the filename, the line number, and code context.

For example, running the below code snippet:

  import logging
  import tensorrt
  import torch
  import torch2trt


  logging.basicConfig(level=logging.INFO)
  torch.manual_seed(0)

  DEVICE = torch.device("cuda:0")


  def sub(a, b):
      return a - b

  class Model(torch.nn.Module):
      def __init__(self):
          super().__init__()

          self.layers = torch.nn.Sequential(
              torch.nn.Conv2d(3, 3, 3),
              torch.nn.BatchNorm2d(3),
              torch.nn.ReLU(),
          )

      def forward(self, t):
          t = self.layers(t)
          t = t + t
          t = torch.cat([t, t], 1)
          return sub(t, t)


  if __name__ == "__main__":
      t = torch.ones(3, 3, 3, 3).to(DEVICE)

      model = Model().eval().to(DEVICE)
      out = model(t)
      print(f'{out.shape}')

      model_trt = torch2trt.torch2trt(
          model, [t], log_level=tensorrt.Logger.VERBOSE, max_batch_size=3
      )
      out_trt = model_trt(t)
      print(f'{out_trt.shape}')

      assert torch.max(torch.abs(out - out_trt)) < 1e-6

We now see the following output as well:

Found 'torch.nn.Conv2d.forward' (converter available) in function 'forward:'
verbose.py: 27
>   t = self.layers(t)


Found 'torch.Tensor.dim' (no converter available) in function 'forward:'
verbose.py: 27
>   t = self.layers(t)


Found 'torch.nn.functional.batch_norm' (converter available) in function 'forward:'
verbose.py: 27
>   t = self.layers(t)


Found 'torch.nn.ReLU.forward' (converter available) in function 'forward:'
verbose.py: 27
>   t = self.layers(t)


Found 'torch.Tensor.__add__' (converter available) in function 'forward:'
verbose.py: 28
>   t = t + t


Found 'torch.cat' (converter available) in function 'forward:'
verbose.py: 29
>   t = torch.cat([t, t], 1)


Found 'torch.Tensor.__sub__' (converter available) in function 'sub:'
verbose.py: 14
>   return a - b

Note the use of tensorrt.Logger.VERBOSE here.

chaoz-dev avatar Aug 13 '22 02:08 chaoz-dev

Going to need to rebase this on master

chaoz-dev avatar Aug 13 '22 02:08 chaoz-dev

@jaybdub This PR is now ready for review. Should be rebased off of the latest master.

chaoz-dev avatar Nov 20 '22 21:11 chaoz-dev