CPU x10 faster than GPU: Recommendations for GPU implementation speed up

juanmed: <p>Hello,</p>
<p>I am coding a drone control algorithm (using modern control theory, not reinforcement learning) and was testing Pytorch as a replacement for Numpy. The algorithm receives as inputs the state of the drone and a desired trajectory, and computes the inputs for the drone to follow the trajectory. It must compute this at least at 100Hz.</p>
<p>The main purpose for trying Pytorch is to see if there would be any gains of using the GPU, since most of the operations are matrix-vector operations.</p>
<p>Links to source code of both controllers: <a href="https://github.com/juanmed/riseq_uav/blob/dev/torch/riseq_control/scripts/geometric_controller.py" rel="noopener nofollow ugc">numpy implementation</a>, <a href="https://github.com/juanmed/riseq_uav/blob/dev/torch/riseq_control/scripts/geometric_controller_gpu.py" rel="noopener nofollow ugc">pytorch implementation</a> and <a href="https://github.com/juanmed/riseq_uav/blob/b5cfce8cea18c1843d78649e24529f205aa53a60/riseq_control/scripts/riseq_high_level_control_publisher_async.py#L206" rel="noopener nofollow ugc">the script where I call</a> each of them.</p>
<p>During my testing I found out that the same control algorithm written using numpy and running on the CPU is at least 10x faster than the pytorch implementation running on the GPU (using only torch functions). I tried both in a desktop computer and on a Jetson Nano with quite similar and interesting results:</p>
<p><strong>Two tests on desktop:</strong></p>
<ul>
<li>Intel Core i5</li>
<li>GeForce GTX 1050</li>
<li>CUDA10</li>
<li>Pytorch 1.2.</li>
</ul>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/4/4ead26c5e3bb49ba8a269a706e98a43a255378c6.png" data-download-href="https://discuss.pytorch.org/uploads/default/4ead26c5e3bb49ba8a269a706e98a43a255378c6" title="bigcpugpupcputimes2"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/4/4ead26c5e3bb49ba8a269a706e98a43a255378c6_2_690x380.png" alt="bigcpugpupcputimes2" data-base62-sha1="be0dvUWf6ulkLr0W3Pm8RpuhP26" width="690" height="380" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/4/4ead26c5e3bb49ba8a269a706e98a43a255378c6_2_690x380.png, https://discuss.pytorch.org/uploads/default/optimized/2X/4/4ead26c5e3bb49ba8a269a706e98a43a255378c6_2_1035x570.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/4/4ead26c5e3bb49ba8a269a706e98a43a255378c6_2_1380x760.png 2x" data-dominant-color="FAF4F6"><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">bigcpugpupcputimes2</span><span class="informations">1855×1022 35.3 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> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/d/dbc7605d36a702850569a0e000cc8c4088860578.png" data-download-href="https://discuss.pytorch.org/uploads/default/dbc7605d36a702850569a0e000cc8c4088860578" title="bigcpugpupcputimes"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/d/dbc7605d36a702850569a0e000cc8c4088860578_2_690x380.png" alt="bigcpugpupcputimes" data-base62-sha1="vmfJ8B3OGjMusafwh6YOvQWcGKI" width="690" height="380" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/d/dbc7605d36a702850569a0e000cc8c4088860578_2_690x380.png, https://discuss.pytorch.org/uploads/default/optimized/2X/d/dbc7605d36a702850569a0e000cc8c4088860578_2_1035x570.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/d/dbc7605d36a702850569a0e000cc8c4088860578_2_1380x760.png 2x" data-dominant-color="F8F5F7"><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">bigcpugpupcputimes</span><span class="informations">1855×1022 35.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></p>
<p><strong>Two tests on Jetson Nano</strong></p>
<ul>
<li>ARMv8</li>
<li>Nvidia Tegra X1</li>
<li>Pytorch 1.2</li>
</ul>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/7/7dcf292e809f998334cb4358d91b327a7fd29bdd.png" data-download-href="https://discuss.pytorch.org/uploads/default/7dcf292e809f998334cb4358d91b327a7fd29bdd" title="jetsoncpugpupcputimes2"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/7/7dcf292e809f998334cb4358d91b327a7fd29bdd_2_690x380.png" alt="jetsoncpugpupcputimes2" data-base62-sha1="hWXwtVq1gFvEuAoWvzQznbPJEDz" width="690" height="380" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/7/7dcf292e809f998334cb4358d91b327a7fd29bdd_2_690x380.png, https://discuss.pytorch.org/uploads/default/optimized/2X/7/7dcf292e809f998334cb4358d91b327a7fd29bdd_2_1035x570.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/7/7dcf292e809f998334cb4358d91b327a7fd29bdd_2_1380x760.png 2x" data-dominant-color="FDF7F7"><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">jetsoncpugpupcputimes2</span><span class="informations">1855×1022 27.3 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> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/a/a93c7936853907b772e23e27ee851e8b6ee0b1fe.png" data-download-href="https://discuss.pytorch.org/uploads/default/a93c7936853907b772e23e27ee851e8b6ee0b1fe" title="jetsoncpugpupcputimes"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/a/a93c7936853907b772e23e27ee851e8b6ee0b1fe_2_690x380.png" alt="jetsoncpugpupcputimes" data-base62-sha1="o98fTqZm514llLGmjZ1vYBan50y" width="690" height="380" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/a/a93c7936853907b772e23e27ee851e8b6ee0b1fe_2_690x380.png, https://discuss.pytorch.org/uploads/default/optimized/2X/a/a93c7936853907b772e23e27ee851e8b6ee0b1fe_2_1035x570.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/a/a93c7936853907b772e23e27ee851e8b6ee0b1fe_2_1380x760.png 2x" data-dominant-color="FBF5F5"><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">jetsoncpugpupcputimes</span><span class="informations">1855×1022 28.2 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>The graphs, one for each tests, shows the computation time distribution of the code running either on a)numpy on CPU (blue) b) Pytorch on CPU (green) and c) Pytorch on GPU (red)</p>
<p>In both hardware configurations, numpy on CPU was at least x10 faster that pytorch on GPU. Also, Pytorch on CPU is faster than  on GPU. In the case of the desktop, Pytorch on CPU can be, on average, faster than numpy on CPU. Finally (and unluckily for me) Pytorch on GPU running in Jetson Nano cannot achieve 100Hz throughput.</p>
<p>What I am interested on is actually <em>getting the Pytorch GPU on Jetson speed to reach a performance similar than its CPU speed on Jetson Nano (&gt;=100Hz throughput)</em>,  since I cannot attach a desktop to a drone.</p>
<p>Reading around it seems some issues could be</p>
<ul>
<li>Data transfer between CPU and GPU can be very expensive,</li>
<li>Tensor type and dtype used</li>
</ul>
<p>I am not sure how to dramatically improve from this. Currently all operations are done on Tensor.FloatTensor, and I load all the data to GPU at the beginning of each iteration, all computation gets done only on GPU, and I offload from GPU only at the end when all the results are ready.</p>
<p>I am aware this is not the main purpose for which pytorch was created but I would like to get advice on how to optimize the performance of Pytorch on GPU, on an smaller platform like Jetson Nano, and hopefully get a x10 increase in performance.</p>
<p>Any advice will be very welcome!</p>
<p>Juan</p>
<p>For reference, I measured executions times following:</p>
<p>For GPU:</p>
<pre data-code-wrap="python"><code class="lang-python">        start = torch.cuda.Event(enable_timing=True)
        end = torch.cuda.Event(enable_timing=True)

        start.record()
        # I put my code here
        end.record()
        torch.cuda.synchronize()
       execution_time = start.elapsed_time(end)
</code></pre>
<p>For CPU:</p>
<pre><code class="lang-auto">       start = time.time()
       # I put my code here
       end = time.time()
       execution_time = end - start
</code></pre>

dambo: <p>It is practically impossible to help you without taking a look at the code.<br>
Also, are you utilizing batches for inference on the GPU?</p>

juanmed: <p><a class="mention" href="/u/dambo">@dambo</a>, thanks for your reply. I just edited the post to include links to the source code at the top.</p>
<p>Regarding batches, I am actually not using them. I have used pytorch for machine learning in vision, where a dataset is ready beforehand. However in this case the data comes in the instant it should be processed, so I am not sure if batch processing is possible.</p>

rwightman: <p>Using the GPU typically involves extra latency, especially when done via friendly APIs in Python. For large NN that is typically dwarfed by number of FLOPS so no big deal. For real-time control, with minimal flops and a low latency / high frequency demand it is likely not the best fit.</p>
<p>I’d also question why Python? If you implemented that loop in C or C++ and replotted your graph, it’d be hard to compare any of the existing measures on the same scale. There is so much extra overhead under the covers of any Python app, you’d likely get a 100x+ increase on the CPU writing it in C++ for an algo like that.</p>

juanmed: <p><a class="mention" href="/u/rwightman">@rwightman</a> Thanks for your reply.</p>
<p>When you mention to “implement that loop in C or C++”, do you mean purely C++ running on CPU or using the torch C++ frontend on GPU? If the answer is the latter,  I totally agree a C++ will be faster than python running on the CPU, but since I have not tried the torch C++ frontend, I am not sure whether an implementation of the controller using it will have a significant impact on execution speed on the GPU.</p>
<p>About Python,  I simply wanted to try. I am used to it for machine learning and also have seen some use cases for pytorch+python on optimal control (and general optimization) with interesting results. Nonetheless, optimization sees benefits on Pytorch thanks to automatic differentiation. Also, coming from a C background, coding in Python is much easier.</p>

juanmed: <p><a class="mention" href="/u/dambo">@dambo</a> <a class="mention" href="/u/rwightman">@rwightman</a> I performed some other timing tests to try to understand if bottleneck could be in CPU to GPU transfer:</p>
<ul>
<li>Move 6x 3-element np.arrays from CPU to GPU</li>
<li>Move a single 6x3 np.array from CPU to GPU</li>
<li>Create  6x 3-element torch.tensor directy on GPU whose elements are np.float variables</li>
<li>Create 6x 3-element torch.tensor directly on GPU whose elements are constant floats (hard coded)</li>
</ul>
<p>I did these 4 tests using np.float16, np.float32 and np.float64, as well as on my desktop and a Jetson Nano. The findings are quiet interesting and counter-intuitive:</p>
<ul>
<li>Faster operation is of a single 6x3 np.array from CPU to GPU, both on desktop and Jetson Nano. It is  around 4x faster than the rest of operations and even faster than creating 6x 3-element torch.tensor directly on GPU.</li>
<li>Using np.float64 is slightly faster than both np.float32 and np.float16</li>
<li>Transfer are 10x faster on the desktop than on Jetson Nano</li>
<li>Effectively, tranfering 6x 3-element np.arrays has transfer times as high as 5.5ms - 6.5ms, which is already too much for calculations at 100Hz in which the limit is 10ms.</li>
</ul>
<p>It seems that one approach to improve performance is by transferring all data in a single np.array rather than small pieces on several individual np.arrays.</p>
<p>However, does this makes sense at all? Is it really possible that transferring a 6x3 np.array is faster than creating 6 torch.tensors directly on GPU? Sounds to good to be true. I understand that batch processing is one of the keypoints on using a GPU… but “batch transferring”… not really sure.</p>
<p>Any further comments are appreciate.</p>
<p>Below are the graphs:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/8/8671d1ecbf316e5e4eca336f93dd33579145c338.png" data-download-href="https://discuss.pytorch.org/uploads/default/8671d1ecbf316e5e4eca336f93dd33579145c338" title="jetsonfloat16"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/8/8671d1ecbf316e5e4eca336f93dd33579145c338_2_690x380.png" alt="jetsonfloat16" data-base62-sha1="jblQ2i1Py6RNkmt1S7KBm5bar4k" width="690" height="380" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/8/8671d1ecbf316e5e4eca336f93dd33579145c338_2_690x380.png, https://discuss.pytorch.org/uploads/default/optimized/2X/8/8671d1ecbf316e5e4eca336f93dd33579145c338_2_1035x570.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/8/8671d1ecbf316e5e4eca336f93dd33579145c338_2_1380x760.png 2x" data-dominant-color="FCFAFC"><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">jetsonfloat16</span><span class="informations">1855×1022 32.2 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> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/c/c10e42756d2d389bf3ffe11beb22b017fc907912.png" data-download-href="https://discuss.pytorch.org/uploads/default/c10e42756d2d389bf3ffe11beb22b017fc907912" title="jetsonfloat64"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/c/c10e42756d2d389bf3ffe11beb22b017fc907912_2_690x380.png" alt="jetsonfloat64" data-base62-sha1="rxQHzMJdISpkhCiG34NtF5XHHge" width="690" height="380" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/c/c10e42756d2d389bf3ffe11beb22b017fc907912_2_690x380.png, https://discuss.pytorch.org/uploads/default/optimized/2X/c/c10e42756d2d389bf3ffe11beb22b017fc907912_2_1035x570.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/c/c10e42756d2d389bf3ffe11beb22b017fc907912_2_1380x760.png 2x" data-dominant-color="FCFBFB"><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">jetsonfloat64</span><span class="informations">1855×1022 30.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> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/2/241cc456d7b26407efde2ad24eefc846a98b6a07.png" data-download-href="https://discuss.pytorch.org/uploads/default/241cc456d7b26407efde2ad24eefc846a98b6a07" title="jetsonfloat32"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/2/241cc456d7b26407efde2ad24eefc846a98b6a07_2_690x380.png" alt="jetsonfloat32" data-base62-sha1="59sOXa5pWh740lSvsi96BKjBhWL" width="690" height="380" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/2/241cc456d7b26407efde2ad24eefc846a98b6a07_2_690x380.png, https://discuss.pytorch.org/uploads/default/optimized/2X/2/241cc456d7b26407efde2ad24eefc846a98b6a07_2_1035x570.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/2/241cc456d7b26407efde2ad24eefc846a98b6a07_2_1380x760.png 2x" data-dominant-color="FDFBFB"><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">jetsonfloat32</span><span class="informations">1855×1022 33 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> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/5/5d6f3b5eb9406a4cb03a555b8aa9c24534f4e03d.png" data-download-href="https://discuss.pytorch.org/uploads/default/5d6f3b5eb9406a4cb03a555b8aa9c24534f4e03d" title="desktopfloat64"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/5/5d6f3b5eb9406a4cb03a555b8aa9c24534f4e03d_2_690x380.png" alt="desktopfloat64" data-base62-sha1="dkyIw6kTAXAmXWT8oQBLhPj9S8J" width="690" height="380" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/5/5d6f3b5eb9406a4cb03a555b8aa9c24534f4e03d_2_690x380.png, https://discuss.pytorch.org/uploads/default/optimized/2X/5/5d6f3b5eb9406a4cb03a555b8aa9c24534f4e03d_2_1035x570.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/5/5d6f3b5eb9406a4cb03a555b8aa9c24534f4e03d_2_1380x760.png 2x" data-dominant-color="FDFCFC"><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">desktopfloat64</span><span class="informations">1855×1022 39.3 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> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/5/556bb8137618b26fcd80112fce99543d83e5cb50.png" data-download-href="https://discuss.pytorch.org/uploads/default/556bb8137618b26fcd80112fce99543d83e5cb50" title="desktopfloat16"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/5/556bb8137618b26fcd80112fce99543d83e5cb50_2_690x380.png" alt="desktopfloat16" data-base62-sha1="cbFn3THsFl3LfAz9Gllzm99mTny" width="690" height="380" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/5/556bb8137618b26fcd80112fce99543d83e5cb50_2_690x380.png, https://discuss.pytorch.org/uploads/default/optimized/2X/5/556bb8137618b26fcd80112fce99543d83e5cb50_2_1035x570.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/5/556bb8137618b26fcd80112fce99543d83e5cb50_2_1380x760.png 2x" data-dominant-color="FDFCFD"><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">desktopfloat16</span><span class="informations">1855×1022 37.7 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> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/6/64e9f8f2275ad34bbf879558c3aacc79c355acaa.png" data-download-href="https://discuss.pytorch.org/uploads/default/64e9f8f2275ad34bbf879558c3aacc79c355acaa" title="desktopfloat32"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/6/64e9f8f2275ad34bbf879558c3aacc79c355acaa_2_690x380.png" alt="desktopfloat32" data-base62-sha1="eoJ1TZNML8UjBSeX6e6GKzkeo5A" width="690" height="380" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/6/64e9f8f2275ad34bbf879558c3aacc79c355acaa_2_690x380.png, https://discuss.pytorch.org/uploads/default/optimized/2X/6/64e9f8f2275ad34bbf879558c3aacc79c355acaa_2_1035x570.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/6/64e9f8f2275ad34bbf879558c3aacc79c355acaa_2_1380x760.png 2x" data-dominant-color="FDFCFD"><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">desktopfloat32</span><span class="informations">1855×1022 37.8 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>

juanmed: <p>I added two more tests:</p>
<ul>
<li>Creating a 6x3 torch.tensor using torch.rand(6,3)</li>
<li>Creating a 6x3 torch.tensor directly on GPU using variables</li>
<li>Creating a 6x3 torch.tensor direcly on GPU using constants</li>
</ul>
<p>It seems that</p>
<ul>
<li>Creating a torch.rand(6,3) directly on GPU is the fastest operation</li>
<li>However, creating a 6x3 np.array and then transfering it to GPU is yet faster than creating a 6x3 torch.tensor directly on GPU using variables or constants. This is counter intuitive.</li>
</ul>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/c/c3d90a281e898d94126aa3e74f8962fcc03c6c49.png" data-download-href="https://discuss.pytorch.org/uploads/default/c3d90a281e898d94126aa3e74f8962fcc03c6c49" title="jetsonfloat64"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/c/c3d90a281e898d94126aa3e74f8962fcc03c6c49_2_690x380.png" alt="jetsonfloat64" data-base62-sha1="rWy6WTBGqnnICH2pBoD1HGUvczn" width="690" height="380" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/c/c3d90a281e898d94126aa3e74f8962fcc03c6c49_2_690x380.png, https://discuss.pytorch.org/uploads/default/optimized/2X/c/c3d90a281e898d94126aa3e74f8962fcc03c6c49_2_1035x570.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/c/c3d90a281e898d94126aa3e74f8962fcc03c6c49_2_1380x760.png 2x" data-dominant-color="FBFBF9"><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">jetsonfloat64</span><span class="informations">1855×1022 40.7 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> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/c/cde7ac7825b8578f7d49b4dc68b1e55b699e7c2b.png" data-download-href="https://discuss.pytorch.org/uploads/default/cde7ac7825b8578f7d49b4dc68b1e55b699e7c2b" title="desktopfloat64"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/c/cde7ac7825b8578f7d49b4dc68b1e55b699e7c2b_2_690x380.png" alt="desktopfloat64" data-base62-sha1="tnweSQ629XqYhF6CgXojzAuSYyf" width="690" height="380" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/c/cde7ac7825b8578f7d49b4dc68b1e55b699e7c2b_2_690x380.png, https://discuss.pytorch.org/uploads/default/optimized/2X/c/cde7ac7825b8578f7d49b4dc68b1e55b699e7c2b_2_1035x570.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/c/cde7ac7825b8578f7d49b4dc68b1e55b699e7c2b_2_1380x760.png 2x" data-dominant-color="FCFCFB"><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">desktopfloat64</span><span class="informations">1855×1022 46.6 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>

rwightman: <p>I’m not understanding what you mean ‘directly on GPU using constants’? There are no constants in Python and any variable in Python exists in the CPU and has to be transferred at some point to the GPU. You can make sure that those tensors are defined and moved to GPU outside of your high frequency loops and not modified, that is as close to constant as you get.</p>
<p>I’m not an expert on the nitty gritty details of CUDA kernels and the specific Pytorch mechanics relating to their handling. I believe a typical kernel launch latency is approx 10us, down to about 5us and up to I’m not sure where. But I’ve seen traces with 50us, etc.</p>
<p>So, if you are doing convolutions or mm with tens of thousands, hundreds of thousand elements, you don’t notice that. But for a tight loop of small operations that could only partially leverage the full parallalelization ability on a modern CPU, you could probably crank out  thousands of iterations of your loop (C/C++ optimized) in the time it takes to launch a kernel on the GPU.</p>
<p>If you want to keep it a little higher level, maybe use Eigen, you’d get a bit of cross platform ability in the sense that it may be able to leverage a bit of NEON or SSE depending on what platform you compile for.</p>

juanmed: <p>You are right, I meant to say ‘hard-coded floats’. Thanks for the insights on CUDA kernels and Eigen suggestion.</p>