How to get layer execution time on an AI model saved as .pth file?
I'm trying to run a Resnet-like image classification model on a CPU, and want to know the breakdown of time it takes to run each layer of the model.
The issue I'm facing is the github link https://github.com/facebookresearch/semi-supervised-ImageNet1K-models has the model saved as a .pth file. It is very large (100s of MB), and I don't know exactly how it differs from pytorch except it's binary.
I load the model from this file using the following script. But I don't see a way to modify the model or insert the t = time.time() variables/statements in between model layers to break down the time in each layer.
Questions:

Would running the model in the following script give a correct estimate of end-to-end time (t2-t1) it takes to run the model on the CPU, or would it also include pytorch compilation time?

How to insert time statements between consecutive layers to get a breakdown?

There is no inference/training script at the github link and only has the .pth file. So how exactly is one supposed to run inference or training? How to insert additional layers between consecutive layers of the .pth model and save them?


#!/usr/bin/env python
import torch torchvision time

model=torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext50_32x4d_swsl', force_reload=False)
in = torch.randn(1, 3, 224, 224)
t1 = time.time()
out = model.forward(in)
t2 = time.time()
```**strong text**


A simple way to implement such a requirement is by registering forward hooks on each module of the model which updates a global variable for storing the time and computes the time difference between the last and current computations.
For example:
import torch
import torchvision
import time

global_time = None
exec_times = []


def store_time(self, input, output):
    global global_time, exec_times
    exec_times.append(time.time() - global_time)
    global_time = time.time()


model = torch.hub.load('facebookresearch/semi-supervised-ImageNet1K-models', 'resnext50_32x4d_swsl', force_reload=False)
x = torch.randn(1, 3, 224, 224)

# Register a hook for each module for computing the time difference
for module in model.modules():
    module.register_forward_hook(store_time)

global_time = time.time()
out = model(x)
t2 = time.time()

for module, t in zip(model.modules(), exec_times):
    print(f&quot;{module.__class__}: {t}&quot;)

The output I get is:
&lt;class 'torchvision.models.resnet.ResNet'&gt;: 0.004999876022338867
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.002006053924560547
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0009946823120117188
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.007998466491699219
&lt;class 'torch.nn.modules.pooling.MaxPool2d'&gt;: 0.0010004043579101562
&lt;class 'torch.nn.modules.container.Sequential'&gt;: 0.0020003318786621094
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.0010023117065429688
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.017997026443481445
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0010018348693847656
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0009999275207519531
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.003000497817993164
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.003999948501586914
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.001997232437133789
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.004001140594482422
&lt;class 'torch.nn.modules.container.Sequential'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.001999378204345703
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.003001689910888672
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0020008087158203125
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0009992122650146484
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0019991397857666016
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0010001659393310547
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0009999275207519531
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.002998828887939453
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0010013580322265625
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.0029997825622558594
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.002999544143676758
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0010006427764892578
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.001001119613647461
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0019979476928710938
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0010018348693847656
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0010001659393310547
&lt;class 'torch.nn.modules.container.Sequential'&gt;: 0.00299835205078125
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.002004384994506836
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0009975433349609375
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.005999088287353516
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0020003318786621094
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0010001659393310547
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0020017623901367188
&lt;class 'torch.nn.modules.container.Sequential'&gt;: 0.0009970664978027344
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0029997825622558594
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.0010008811950683594
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.00500035285949707
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0009984970092773438
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0020020008087158203
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0019979476928710938
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0010018348693847656
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.00099945068359375
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.001001119613647461
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.002997875213623047
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0010013580322265625
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.002000570297241211
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.001997232437133789
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0010008811950683594
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.001001596450805664
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.00099945068359375
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.002998828887939453
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0010020732879638672
&lt;class 'torch.nn.modules.container.Sequential'&gt;: 0.0010020732879638672
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.001995563507080078
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.002001523971557617
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0010001659393310547
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0010008811950683594
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0029985904693603516
&lt;class 'torch.nn.modules.container.Sequential'&gt;: 0.0009989738464355469
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0010068416595458984
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.004993438720703125
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0010013580322265625
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0010001659393310547
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0010018348693847656
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.001997709274291992
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0019991397857666016
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.0029990673065185547
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0030128955841064453
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0019872188568115234
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0029993057250976562
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0010008811950683594
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0010006427764892578
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0009992122650146484
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.003001689910888672
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0019986629486083984
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0010008811950683594
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.002000093460083008
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0019986629486083984
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0020012855529785156
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0019981861114501953
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0030014514923095703
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0029985904693603516
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0010008811950683594
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0010013580322265625
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0009989738464355469
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0
&lt;class 'torch.nn.modules.container.Sequential'&gt;: 0.002998828887939453
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.002000570297241211
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.003000497817993164
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0020020008087158203
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0009982585906982422
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0009996891021728516
&lt;class 'torch.nn.modules.container.Sequential'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0029990673065185547
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0020003318786621094
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.0010025501251220703
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0019981861114501953
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0019996166229248047
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0019996166229248047
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0
&lt;class 'torchvision.models.resnet.Bottleneck'&gt;: 0.0030002593994140625
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0020012855529785156
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.0
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0
&lt;class 'torch.nn.modules.conv.Conv2d'&gt;: 0.006000518798828125
&lt;class 'torch.nn.modules.batchnorm.BatchNorm2d'&gt;: 0.0019979476928710938
&lt;class 'torch.nn.modules.activation.ReLU'&gt;: 0.0
&lt;class 'torch.nn.modules.pooling.AdaptiveAvgPool2d'&gt;: 0.002003192901611328
&lt;class 'torch.nn.modules.linear.Linear'&gt;: 0.0019965171813964844

Process finished with exit code 0



