Repository: pytorch
Issue: Auto tuner takes too much time in serialized model
Labels: oncall: jit,triaged

## 🐛 Bug

Serialized models have a huge overhead on a first run. With the model I'm using, it takes at least more than 3-4 minutes (than I'm just shutting it down, though it doesn't respond to `SiGSTOP` or `SIGINT`). I am providing the slightly trimmed version of the final model, but still you can see the huge overhead here too.

## To Reproduce

Steps to reproduce the behavior:

I'm providing the trimmed version of `poseresnet` (`posenet.py`):
```
import torch
import torch.nn as nn

BN_MOMENTUM = 0.1

class Bottleneck_down(nn.Module):
    expansion = 4

    def __init__(self, inplanes, planes, stride=1):
        super(Bottleneck_down, self).__init__()
        self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
        self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
                               padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(planes, momentum=BN_MOMENTUM)
        self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1,
                               bias=False)
        self.bn3 = nn.BatchNorm2d(planes * self.expansion,
                                  momentum=BN_MOMENTUM)
        self.relu = nn.ReLU(inplace=True)
        #self.downsample = downsample
        self.stride = stride
        self.downsample = nn.Sequential(
            nn.Conv2d(inplanes, planes * self.expansion,
                      kernel_size=1, stride=stride, bias=False),
            nn.BatchNorm2d(planes * self.expansion, momentum=BN_MOMENTUM),
        )

    def forward(self, x):
        residual = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        residual = self.downsample(x)

        out += residual
        out = self.relu(out)

        return out


resnet_spec = {50: (Bottleneck_down, [3, 4, 6, 3]),
               101: (Bottleneck_down, [3, 4, 23, 3]),
               152: (Bottleneck_down, [3, 8, 36, 3])}


class PoseResNet_2(nn.Module):
    def __init__(self, block, layers, num_joints,
                 num_input_channels=3,
                 deconv_with_bias=False,
                 num_deconv_layers=3,
                 num_deconv_filters=(256, 256, 256),
                 num_deconv_kernels=(4, 4, 4),
                 final_conv_kernel=1,
                 ):
        super().__init__()

        self.num_joints = num_joints
        self.num_input_channels = num_input_channels
        self.inplanes = 64

        self.deconv_with_bias = deconv_with_bias
        self.num_deconv_layers, self.num_deconv_filters, self.num_deconv_kernels = num_deconv_layers, num_deconv_filters, num_deconv_kernels

        self.conv1 = nn.Conv2d(num_input_channels, 64, kernel_size=7, stride=2, padding=3,
                               bias=False)
        self.bn1 = nn.BatchNorm2d(64, momentum=BN_MOMENTUM)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)


    def _make_layer(self, block, planes, blocks, stride=1):
        layers = []
        layers.append(block(self.inplanes, planes, stride))
        self.inplanes = planes * block.expansion
        for i in range(1, blocks):
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)


    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)

        return x


def get_pose_net_2():
    block_class, layers = resnet_spec[152]
    model = PoseResNet_2(
        block_class, layers, 17,
    )
    return model
```

and the experiment file itself:

```
import torch
import torch.nn as nn
import poseresnet
import time

torch.backends.cudnn.benchmark=True

device = torch.device("cuda:0")

module = poseresnet.get_pose_net_2().to(device)
module = torch.jit.script(module)
print("serialized")

module(torch.randn((4, 3, 304, 304), device=device))
module(torch.randn((4, 3, 304, 304), device=device))

torch.cuda.synchronize()
start = time.time()

for i in range(400):
    module(torch.randn((4, 3, 304, 304), device=device))

torch.cuda.synchronize()
print(time.time() - start)
```

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->

## Environment

 - PyTorch Version (e.g., 1.2.0):
 - OS (e.g., Linux):
 - GPU models and configuration: RTX 2080Ti