Model is faster on CPU than GPU

Eardrum7: <p>I made a simple model to see if using my GPU would improve speed, it did not. I want to understand if my GPU works correctly or whether I need to change settings to have more benefit from GPU.</p>
<p>My GPU is recognized, but… in the task manager it seems to be never busy.</p>
<p>Would my GPU have more benefit if I changed settings (like batch size, or larger model, or more data)?</p>
<ul>
<li>My data shape is: (100000, 121)</li>
<li>batch size: 32</li>
<li>epochs: 10</li>
<li>model:</li>
</ul>
<pre><code class="lang-auto">PremiumPredictor(
   (fc1): Linear(in_features=121, out_features=64, bias=True)
   (fc2): Linear(in_features=64, out_features=1, bias=True)
   (relu): ReLU()
)
</code></pre>
<p>Takes 37s on GPU, and 19s on CPU.</p>

ptrblck: <aside class="quote no-group" data-username="Eardrum7" data-post="1" data-topic="207307">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/e/b3f665/48.png" class="avatar"> Eardrum7:</div>
<blockquote>
<p>but… in the task manager it seems to be never busy.</p>
</blockquote>
</aside>
<p>You would need to select the <code>compute</code> view in Windows’ Task Manager as the default view won’t show the utilization of compute applications.</p>
<aside class="quote no-group" data-username="Eardrum7" data-post="1" data-topic="207307">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/e/b3f665/48.png" class="avatar"> Eardrum7:</div>
<blockquote>
<p>GPU have more benefit if I changed settings (like batch size, or larger model, or more data)?</p>
</blockquote>
</aside>
<p>Yes, increasing the workload should increase the utilization as your current use case might be CPU-limited. If you want to use this small model, you might want to use CUDA Graphs as it will reduce the CPU workload.</p>

Eardrum7: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a> . I tried the compute view in the task manager but that doesn’t show anything.</p>
<p>I ran a different code snippet this time (see below), which maybe doesn’t show in compute? I only see a spike when I close/restart the python kernel in one or two of the Copy views.</p>
<p><code>nvidia-smi</code> in <code>cmd</code> gives me most hope that my GPU is doing something. Temperature increases from 45 to 76 Celsius and the Volatile GPU-Util goes from 0% to 100%</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/b/bbef46e5571e73860a3b72f505fe4e902e8c6065.png" data-download-href="https://discuss.pytorch.org/uploads/default/bbef46e5571e73860a3b72f505fe4e902e8c6065" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bbef46e5571e73860a3b72f505fe4e902e8c6065_2_314x249.png" alt="image" data-base62-sha1="qOxVZRew8kigcY6fsMJ5hAhmOkB" width="314" height="249" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bbef46e5571e73860a3b72f505fe4e902e8c6065_2_314x249.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bbef46e5571e73860a3b72f505fe4e902e8c6065_2_471x373.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bbef46e5571e73860a3b72f505fe4e902e8c6065_2_628x498.png 2x" data-dominant-color="F8F9F7"><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">840×667 15.4 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>
<pre><code class="lang-auto">import torch
import time

device = "cuda"

x = torch.randn(10000, 10000)

## CPU version
start_time = time.time()
_ = torch.matmul(x, x)
end_time = time.time()
print(f"CPU time: {(end_time - start_time):6.5f}s")

for _ in range(10):
    ## GPU version
    x = x.to(device)
    _ = torch.matmul(x, x)  # First operation to 'burn in' GPU
    # CUDA is asynchronous, so we need to use different timing functions
    start = torch.cuda.Event(enable_timing=True)
    end = torch.cuda.Event(enable_timing=True)
    start.record()
    _ = torch.matmul(x, x)
    end.record()
    torch.cuda.synchronize()  # Waits for everything to finish running on the GPU
    print(f"GPU time: {0.001 * start.elapsed_time(end):6.5f}s")  # Milliseconds to seconds
# CPU time: 20.77174s
# GPU time: 0.59403s
</code></pre>

ptrblck: <aside class="quote no-group" data-username="Eardrum7" data-post="3" data-topic="207307">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/e/b3f665/48.png" class="avatar"> Eardrum7:</div>
<blockquote>
<p><code>nvidia-smi</code> in <code>cmd</code> gives me most hope that my GPU is doing something. Temperature increases from 45 to 76 Celsius and the Volatile GPU-Util goes from 0% to 100%</p>
</blockquote>
</aside>
<p>Great, so please stick to it as we have seen a lot of confusion when Windows’ Task Manager is used.</p>

Eardrum7: <p>Wanted to add for other users:</p>
<p>In the task manager there is a <code>cuda</code>, which I my case shows work is being done, while the <code>compute</code> view shows nothing:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/0/20cd416ca7ad8426a17d3a150ec40df94528289e.png" data-download-href="https://discuss.pytorch.org/uploads/default/20cd416ca7ad8426a17d3a150ec40df94528289e" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/0/20cd416ca7ad8426a17d3a150ec40df94528289e.png" alt="image" data-base62-sha1="4Gb2ivpTDFQyaC8pYPlB2qkqaGq" width="643" height="500" data-dominant-color="F4F7F8"><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">845×657 21.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></p>

J_Johnson: <p>CPU cores are much faster than GPU cores, but there are only a few of them per CPU.</p>
<p>For example, an Intel i9 has 24 cores with up to 6 GHz speed.</p>
<p>An Nvidia 4090 has 512 Tensor Cores which are only 2.2GHz base clock.</p>
<p>So unless you’re giving enough concurrent operations to utilize at least 72 of those cores, the CPU will be faster.</p>
<p>Hence, a larger model or bigger batch size might see a more efficient use of your GPU.</p>

Eardrum7: <p>Thanks for your reply. And indeed, I see that with a larger model and/or larger batch size, GPU is faster.</p>
<p>Besides the speed of blasting through samples, there is the performance, and from what I read online, a smaller batch size is considered to generalize better right? So, that’s one reason to not go overboard with an extremely large batch size, I guess.</p>

J_Johnson: <p>It’s quite the opposite. Larger batch size is better for generalization. Usually memory constraints are the reason for smaller batches. But accumulating gradients before backprop across batches can mimic a larger batch size.</p>

Eardrum7: <p>I read online that large batch size leads to sharp minima, while small batch size leads to flat minima generally.</p>
<p>E.g. this post on medium which discusses an (old) paper: <a href="https://medium.com/geekculture/why-small-batch-sizes-lead-to-greater-generalization-in-deep-learning-a00a32251a4f" class="inline-onebox" rel="noopener nofollow ugc">Why Small Batch sizes lead to greater generalization in Deep Learning | by Devansh | Geek Culture | Medium</a></p>

J_Johnson: <p>Interesting paper. See page 7,</p>
<p>" It is often reported that when increasing the batch size for a problem, there exists a threshold after which there is a deterioration in the quality of the model. This behavior can be observed for the F2 and C1 networks in Figure 4. In both of these experiments, there is a batch size (≈ 15000 for F2 and ≈ 500 for C1) after which there is a large drop in testing accuracy."</p>
<p>In other words, accuracy increases up to a certain point as batch size increases, until around 15,000 and 500 respectively in these given models and datasets. If your batch size is 32, you’re probably still well below that threshold.</p>

pratikkorat26: <p>I do not understand one thing.</p>
<p>Recently, while working on my project, I trained a neural network with approximately 10K parameters. Initially, I used a batch size of 128, and the network converged quickly. To better utilize my GPU, I increased the batch size to 2048, but to my surprise, the convergence rate slowed down compared to 128.</p>
<p>May be do I need to reduce my learning rate, if I increase my batch size or vice versa for small batch sizes ?</p>
<p>One thing I learned is that larger batch sizes often lead to better generalization, while smaller batch sizes tend to result in faster convergence. How do I know which one to follow.</p>
<p>Can somebody explain this to me, Please ?</p>
<p>Thank you!</p>

J_Johnson: <p>The paper shared earlier by <a class="mention" href="/u/eardrum7">@Eardrum7</a> suggested that larger batch size(as in 10% or more of the total training data) showed a consistently lower performance drop.</p>
<p>However, I don’t know of any definitive studies that show how this behavior scales based on total dataset size. That would be interesting to see.</p>

Eardrum7: <p>With a larger batch size, you have fewer update steps per epoch. So, with the same learning rate you converge slower. If you want to use a larger batch size you can increase the learning rate and have the same convergence speed. <a href="https://arxiv.org/pdf/1706.02677" rel="noopener nofollow ugc">See this paper.</a></p>
<p>They show that you can have the same convergence speed, but due to larger batch size, much faster computation, resulting in less total time. You do have to be careful with the first updates, see Figure 2.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/a/ca9c852df656d6f4a1eca36a77139f2092795f2d.png" data-download-href="https://discuss.pytorch.org/uploads/default/ca9c852df656d6f4a1eca36a77139f2092795f2d" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/a/ca9c852df656d6f4a1eca36a77139f2092795f2d_2_690x247.png" alt="image" data-base62-sha1="sUnN6Lq0TsvjmGkZ6SxAw4xI7Zr" width="690" height="247" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/a/ca9c852df656d6f4a1eca36a77139f2092795f2d_2_690x247.png, https://discuss.pytorch.org/uploads/default/optimized/3X/c/a/ca9c852df656d6f4a1eca36a77139f2092795f2d_2_1035x370.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/a/ca9c852df656d6f4a1eca36a77139f2092795f2d.png 2x" data-dominant-color="F5F5F5"><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">1289×463 77 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>