Why no direct relation between CPU or GPU inference time and number of parameters in models?

Neda: <p>I am wondering why CPU inference time varies for Vgg16 and ResNet18. I am using the following script to measure the inference time on CPU for three different modes which I did train from scratch for my custom dataset.</p>
<p>inference time: ResNet18 = 12.88 millisecond, Vgg16 = 66.85 millisecond, and my propsoed model = 11.72 milisecond</p>
<p>Also, the number of parameters for each model are as follows:<br>
ResNet18 : 11.1 M<br>
Vgg16: 13.4 M<br>
proposed model: 33 K</p>
<p>The question is why ResNet18 with 11.1 M parameters takes ~13 ms, however, the proposed model with 33 K takes ~12 ms?</p>
<p>P.S. I measure inference time for one image, 100 times, and then I report the average.</p>
<p>Here is my snippet: Am I missing something here?</p>
<pre><code class="lang-auto">from ResNet_model_ResNet18  import model
#from vgg_model_vgg16 import model

import torch
import torch.optim as optim
import torch.nn as nn
import time
from PIL import Image
import torchvision.transforms.functional as TF

criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=1e-4)

def main():

    epoch = 800
    PATH = 'C:/test_inference_time/resnet18_epoch{}.pth'.format(epoch)
    checkpoint = torch.load(PATH, map_location = 'cpu')
    model.load_state_dict(checkpoint['model_state_dict'])
    optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
    epoch = checkpoint['epoch']
    
    model.eval()
    with torch.no_grad():

     for step in range(0, 100):
         
        t0 = time.time_ns()
        
        width = 32
        height = 32
        t_image = Image.open('C://test_inference_time//test//01a1d54e8b64e81d_b44.png')
        t_image = t_image.resize((width, height), Image.BILINEAR)   
        t_image = TF.to_tensor(t_image)
        t_image = t_image.unsqueeze_(0)

        t_image = t_image     #.to(device)

        t1 =  time.time_ns() - t0    #time of load image

        logits= model(t_image)
        _, predicted = torch.max(logits, 1)

        t2 = time.time_ns() - t0      # time of apply model and prediction  

        t3 = t2 - t1

        print('{:.00f} minutes'.format((t3) / 6e10), '{:.00f} second'.format((t3) / 1e9), '{:.00f} milisecond'.format((t3) / 1e+6), '{:.00f} microsecond'.format((t3) / 1000),'{:.00f} nanosec'.format((t3)% 1e9))

if __name__ == '__main__':
  main()
  
</code></pre>
<p>Also, I did measure inference time on GPU for the same models, and I am wondering to see inference time for ResNet18 = 10.21 ms, Vgg16 = 5.49 ms, and proposed model = 4.76 ms</p>
<p>Thanks in advance,<br>
Neda</p>

ptrblck: <p>Your code looks generally fine for profiling the CPU.<br>
The inference speed might not linearly depend on the number of parameters.<br>
E.g. convolution layers have very few parameters (just the kernels, which are often small and the bias which is also usually small), while the actual operation might be quite expensive.<br>
Also, some operations (e.g. 3x3 kernels) could have been highly optimized in the underlying library (e.g. MKL-DNN).</p>
<p>If you are timing the models on the GPU, don’t forget to add <code>torch.cuda.synchronize()</code> calls before starting and stopping the timers.</p>

Neda: <p>I just post my second question here because as you mentioned, the inference time is not only depends on number of parameters, and one of the main factor has effect on inference time is number of operation.<br>
I did count the number of parameters in the model by the following script.</p>
<pre><code class="lang-auto">def count_parameters(model):
        return sum(p.numel() for p in model.parameters() if p.requires_grad)
    
    print("number of parameters in the model is: ", count_parameters(model))
</code></pre>
<p><a class="mention" href="/u/ptrblck">@ptrblck</a> I was wondering how can I count the number of operations in the model?<br>
Is there any recommended way to do this?<br>
Thanks</p>

ptrblck: <p>I’m not sure, if there is an easy way to calculate the number of operations.<br>
You could compute the theoretical number of operations, e.g. as done <a href="https://towardsdatascience.com/a-comprehensive-introduction-to-different-types-of-convolutions-in-deep-learning-669281e58215">here</a>.<br>
However, this would not necessarily give you the performance of this computation, as it depends highly on the optimization of the performed algorithm.</p>
<p>If you are interested in the performance of a specific method, I think your best bet would be to time it.</p>

Neda: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a> for sharing the link.</p>

ham952: <p>Here is an example for calculation of Operations , hope this helps : <img src="https://discuss.pytorch.org/uploads/default/original/3X/6/d/6d29ea9c5d3ea586b596cc019ac0c11f5d384e35.png" alt="test" data-base62-sha1="fzHQGQmOAmdxz4s2zbMAyykOCUt" width="598" height="296"></p>