How to get fast inference with Pytorch and MXNet model using GPU?

GB_K: <p>Hello PyTorchers.<br>
I used the word PyTorcher meaning the person uses Pytorch.<br>
I am not sure this expression is appropriate, so comment to me about this, please XD</p>
<hr>
<p>For my task (face detection), I am using two deep learning framework, Pytorch and MXNet.</p>
<p>Please look at the diagram below to help your understanding.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/5/15da06e6d0870cea9080b3600bc13a0a3d5c5f80.png" data-download-href="https://discuss.pytorch.org/uploads/default/15da06e6d0870cea9080b3600bc13a0a3d5c5f80" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/5/15da06e6d0870cea9080b3600bc13a0a3d5c5f80_2_690x215.png" alt="image" data-base62-sha1="37j98VYuM7qF2ngf6PuCPvfXR72" width="690" height="215" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/5/15da06e6d0870cea9080b3600bc13a0a3d5c5f80_2_690x215.png, https://discuss.pytorch.org/uploads/default/original/3X/1/5/15da06e6d0870cea9080b3600bc13a0a3d5c5f80.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/5/15da06e6d0870cea9080b3600bc13a0a3d5c5f80.png 2x" data-dominant-color="BBA694"><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">956×299 11 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>
<p>When the code was first implemented, GPU was used for the process above.<br>
But the inference time was not that fast.</p>
<p>To compare inference time, I later have created other codes that use CPU only(installed No CUDA libraries).</p>
<p>I found that there was no difference between GPU processing and CPU processing in terms of inference time. (I measure FPS but there was no dramatic difference.)</p>
<ol>
<li>Could you give me ideas to use GPU efficiently for fast inference?</li>
<li>Is this normal that the Volatile GPU-util is almost 0% when PyTorch model is on inference mode?</li>
<li>I capture the RTSP frame using <em>VideoStream</em> class from <em>imutils</em> package which use <em>cv2</em> and <em>thread</em> package. Do you have any idea for fast realtime ( &lt; 2 sec delay) visualization?</li>
</ol>
<p>I will definitely respond to all of your comments.</p>
<p>Really thank you so much in advance.</p>

ptrblck: <p>How did you implement the communication between MXNet and PyTorch?</p>
<p>The GPU utilization might be low, if your workload is small or if your application has a bottleneck in another part of the code.<br>
How large is your input and how fast is the data loading (including MXNet processing) compared to the forward pass of your PyTorch model?</p>

GB_K: <p>Thank you <a class="mention" href="/u/ptrblck">@ptrblck</a>.</p>
<p>For your question,</p>
<ol>
<li>
<p>I just get numpy.array features from MXNet1 and MXnet2 as a following figure.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/b/2bdde844452ef510c4de710e2ef1380ad957cdc3.png" data-download-href="https://discuss.pytorch.org/uploads/default/2bdde844452ef510c4de710e2ef1380ad957cdc3" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/b/2bdde844452ef510c4de710e2ef1380ad957cdc3_2_690x214.png" alt="image" data-base62-sha1="6g3XR21SsROxW7v9E4eQxltpcwH" width="690" height="214" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/b/2bdde844452ef510c4de710e2ef1380ad957cdc3_2_690x214.png, https://discuss.pytorch.org/uploads/default/original/3X/2/b/2bdde844452ef510c4de710e2ef1380ad957cdc3.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/b/2bdde844452ef510c4de710e2ef1380ad957cdc3.png 2x" data-dominant-color="B9A492"><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">1010×314 61.5 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><br>
I put all the models on the same GPU (GPU:0) as you can see below.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/4/0/4074756c7724e00086e3c60ab7411c238c240cc7.png" alt="image" data-base62-sha1="9cc4jTT9hs8R11CPlbgMbSkMIqb" width="647" height="400"></p>
</li>
<li>
<p>Input size is not that big. Using cv2 based library, I get frame using rtsp and resize the frame into (270, 470, 3) and throw it to the model.</p>
</li>
<li>
<p>I need to measure the processing time at each step. Is it recommend to use time.time() before and after the code block?</p>
</li>
</ol>

ptrblck: <p>Thanks for the information.</p>
<ol>
<li>In that case you are adding synchronizations, as numpy uses CPU arrays. Your workflow would therefore probably be:</li>
</ol>
<pre><code class="lang-python">load data on CPU -&gt; transfer to GPU and use MXNet model -&gt; transfer back to CPU -&gt; transform to PyTorch tensor and transfer back to GPU -&gt; use forward pass of PyTorch model -&gt; prediction
</code></pre>
<p>which most likely won’t benefit a lot from the GPU. You could try to use both models on the CPU only and compare the processing time. Since you are synchronizing and also transferring the data between the host and device multiple times, the GPU utilization could be low.</p>
<ol start="2">
<li>
<p>Especially for small inputs, the workload on the GPU is small and you might see the overhead of the kernel launch times as well as the data transfer.</p>
</li>
<li>
<p>Note that CUDA operations in PyTorch are asynchronous, so you would need to synchronize the code manually to profile the desired operation before starting and stopping the timer via <code>torch.cuda.synchronize()</code>.</p>
</li>
</ol>

GB_K: <p>Thank you so much <a class="mention" href="/u/ptrblck">@ptrblck</a>.<br>
Your comments really helpful everytime.</p>
<p>You made that I want to contribute to Pytorch forum.<br>
I will do my best with Pytorch and share my knowledge with Pytorchers.</p>
<p>From South Korea</p>
<p>Thanks.</p>