The first convolution operation took a long time

blazarx: <p>I tested the inference of the vgg16 network with the pytorch framework, and found that the first layer of network (convolutional layer) for the first inference took a very long time, probably dozens of times the total time of the second reasoning. I know this may be the first Once booted on the GPU, but I want to know what the hell are doing this time?<br>
Machine Configuration：GTX2080Ti<br>
key Code：<br>
cnt = 0<br>
for i in gpu_input_data:<br>
y = net(i)<br>
cnt += 1<br>
time.sleep(0.02)<br>
if cnt &gt; 20:<br>
break<br>
The timeline measured with pytorch’s profile is as follows：<br>
first time inference: 300ms, the average cost time the rest 19 times inference: 13ms</p>

ptrblck: <p>The first CUDA operation creates the CUDA context on the device, which adds some overhead.<br>
Also, since CUDA operations are asynchronously called, you would need to synchronize the code before starting and stopping a timer via <code>torch.cuda.synchronize()</code>.<br>
Additionally, if you are using <code>torch.backends.cudnn.benchmark = True</code> the first iteration using a new input shape will profile different cudnn kernels and select the fastest one, which also adds overhead.</p>

blazarx: <p>thanks for your reply！<br>
I turned on the print time after synchronization, and I got the same result. In addition, I used the torch.autograd.profiler.profile, I don’t know if this will introduce additional overhead in the first inference.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/7/a/7a4c273b27bc9d7e6f98918124c4d7dbc13eee9e.png" alt="11111" data-base62-sha1="hrTpm5DWosgSPqG2yZcAKO3j0Ae" width="486" height="418"></p>

ptrblck: <p>Thanks for the update. I’m not completely sure, but it looks as if all calls take approx. the same amount of time?<br>
If the difference in in the colors, note that I’m partly colorblind and cannot see these differences that clearly so you would have to explain what you expect and what you are seeing now.</p>

blazarx: <p>This image is a process of 20 inferences, the first of which takes longer than the others. As can be seen from the figure, the first reasoning takes a very long time after going through the first layer network. The following is the operations done by Pytorch each time the picture is enlarged.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/6/26502e65fb4cc80b65085a30051e89dfaa56a2d4.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/26502e65fb4cc80b65085a30051e89dfaa56a2d4" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/6/26502e65fb4cc80b65085a30051e89dfaa56a2d4_2_690x245.jpeg" alt="image" data-base62-sha1="5sVVKiw4dT7RoZJ2coCizffwxLu" width="690" height="245" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/6/26502e65fb4cc80b65085a30051e89dfaa56a2d4_2_690x245.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/2/6/26502e65fb4cc80b65085a30051e89dfaa56a2d4_2_1035x367.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/2/6/26502e65fb4cc80b65085a30051e89dfaa56a2d4_2_1380x490.jpeg 2x" data-dominant-color="D1CEC8"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1534×546 280 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

blazarx: <p>The interpretation of the previous picture involves 20 inferences, and the first one takes more time than the others.<img src="https://discuss.pytorch.org/uploads/default/original/3X/f/3/f3437ef00eafc795679cddbe1219dea46ad8b5db.jpeg" alt="Inked11111_LI" data-base62-sha1="yI0Dl3WkQih7dJbzTjrSmrsRhKX" width="486" height="418"></p>

blazarx: <p>Network structure of vgg16（I used）<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/a/ea0e1d0d4ea26ae30dff99544c00d936d1bb5b1f.png" data-download-href="https://discuss.pytorch.org/uploads/default/ea0e1d0d4ea26ae30dff99544c00d936d1bb5b1f" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/a/ea0e1d0d4ea26ae30dff99544c00d936d1bb5b1f_2_410x500.png" alt="image" data-base62-sha1="xoxXTDhsKhXHXoBQbwFbvCRRumH" width="410" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/a/ea0e1d0d4ea26ae30dff99544c00d936d1bb5b1f_2_410x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/e/a/ea0e1d0d4ea26ae30dff99544c00d936d1bb5b1f_2_615x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/a/ea0e1d0d4ea26ae30dff99544c00d936d1bb5b1f_2_820x1000.png 2x" data-dominant-color="292929"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1025×1250 148 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

ptrblck: <p>Is this the very first execution of this layer in this script or are you seeing this increased time in another epoch of the model?<br>
I would not recommend to profile the first execution for the aforementioned reasons (CUDA context creation, <code>cudnnFind</code> in benchmark mode, GPU startup/warmup time).</p>

blazarx: <p>This graph is not the first execution of the network, it is one of many inferences that follow. Thank you for your answer, I probably understand the extra cost of the first execution of the model. There is one more question I want to understand. According to your statement, the first execution of the model after it is loaded into the GPU will inevitably take time to perform some initialization operations. If it is unavoidable, can this time be shortened in some way.</p>

ptrblck: <p>It depends what is causing the slow down, i.e. the context creation cannot be avoided.<br>
Could you post a code snippet to reproduce this issue you are seeing so that we can profile it?</p>

blazarx: <p>You need to run the vgg.py You’ll get vgg.json , vgg.json In Chrome chrome://tracing/ The website can be parsed into visual graphics, and the core code of the test is in the test function.<br>
github project：<br>
</p><aside class="onebox allowlistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">
      <a href="https://github.com/xinhaixiangyunpiao/vgg/tree/master" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars1.githubusercontent.com/u/24819521?s=400&amp;v=4" class="thumbnail onebox-avatar" width="420" height="420">

<h3><a href="https://github.com/xinhaixiangyunpiao/vgg/tree/master" target="_blank" rel="noopener nofollow ugc">xinhaixiangyunpiao/vgg</a></h3>

<p>Contribute to xinhaixiangyunpiao/vgg development by creating an account on GitHub.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>