How to get PyTorch to use Ampere GPU (GPU util < 15%)?

markuf: <p>Hi, I’m trying to train a model that uses mlagents. After much debugging on this site and stackoverflow, I was able to get everything set up and working. The problem is that as soon as I started training, my GPU wasn’t being fully utilized (much at all). It’s a dynamic range but stays around the 10% range. I’m not really sure if that’s normal as this is my first time training a model using my GPU, it also dips to 0% at times when theres no activity. My CPU on the hand was being fully utilized.</p>
<p>If this isn’t supposed to be the case, then I suspect that the problem is related to my CUDA version? I’m using an RTX3090 but my env is as follows:</p>
<pre><code class="lang-auto">Ubuntu 22.04
python: 3.10.4
mlagents: 0.30.0
mlagents: 0.30.0
torch: 1.11.0
gym: 0.26.0
torchaudio: 0.11.0
torchvision: 0.12.0
cudatoolkit: 11.5.2
</code></pre>
<p>I was able to get this mlagents example working only because of <a href="https://stackoverflow.com/questions/75017489/install-older-version-of-pytorch-with-gpu-support-using-conda" rel="noopener nofollow ugc">this stackoverflow answer</a>.</p>
<p>The reason why I think this is a CUDA related error is because the 3090 apparently doesn’t support CUDA 11.5. However, a mod from here in a different post said that’s not a problem if we install torch with the binaries (which is what I thought I already did from that stackoverflow post). Now I realize it doesn’t seem like any binaries were installed with torch in that command.</p>
<p>In addition, nvcc isn’t installed on my machine because the toolkit for CUDA 11.5 requires Ubuntu 20.04 when I have 22.04, and I don’t really want to downgrade (if I can avoid it).</p>
<p><strong>Here’s a picture of  nvidia-smi while training:</strong><br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/2/52371afc29fe7024f8b107e160eaa965ac8b3174.png" data-download-href="https://discuss.pytorch.org/uploads/default/52371afc29fe7024f8b107e160eaa965ac8b3174" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/2/52371afc29fe7024f8b107e160eaa965ac8b3174.png" alt="image" data-base62-sha1="bJjdlTAmS2emygHiD7hMYEscZtq" width="690" height="369" data-dominant-color="3D3D3D"><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">1823×977 63.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>
It does look like the training process (soccertwo/bin/python.310) is running on the GPU, but it’s not that high.</p>
<p>Running torch.version.cuda in the same env: ‘11.5’</p>
<p><strong>Update</strong> So I ran into this <a href="https://discuss.pytorch.org/t/error-installing-with-python-3-8-and-cuda-11-5/134914">old post</a> where ptrblck mentions PyTorch binaries doesn’t come with CUDA 11.5, and this might be my problem. Does this mean I need to downgrade to a lower CUDA version and then the PyTorch binaries will work with it?</p>

ptrblck: <aside class="quote no-group" data-username="markuf" data-post="1" data-topic="188833">
<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/m/9fc348/48.png" class="avatar"> markuf:</div>
<blockquote>
<p>The reason why I think this is a CUDA related error is because the 3090 apparently doesn’t support CUDA 11.5.</p>
</blockquote>
</aside>
<p>That’s not the case as every CUDA 11 release supports your 3090. Where did you get this wrong information from?</p>
<aside class="quote no-group" data-username="markuf" data-post="1" data-topic="188833">
<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/m/9fc348/48.png" class="avatar"> markuf:</div>
<blockquote>
<p>In addition, nvcc isn’t installed on my machine because the toolkit for CUDA 11.5 requires Ubuntu 20.04 when I have 22.04, and I don’t really want to downgrade (if I can avoid it).</p>
</blockquote>
</aside>
<p>The PyTorch binaries ship with their own CUDA dependencies and your locally installed CUDA toolkit will be used if you build PyTorch from source or a custom CUDA extension.</p>
<aside class="quote no-group" data-username="markuf" data-post="1" data-topic="188833">
<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/m/9fc348/48.png" class="avatar"> markuf:</div>
<blockquote>
<p><strong>Update</strong> So I ran into this <a href="https://discuss.pytorch.org/t/error-installing-with-python-3-8-and-cuda-11-5/134914">old post</a> where ptrblck mentions PyTorch binaries doesn’t come with CUDA 11.5, and this might be my problem.</p>
</blockquote>
</aside>
<p>No, since your locally installed CUDA toolkit won’t be used and PyTorch will use the one you’ve selected during the installation.</p>
<p>To check why your GPU shows a low utilization you could profile your code with e.g. Nsight Systems to check if e.g. your CPU is too slow and creates a bottleneck. You could also enable TF32 for matmuls via <code>torch.backends.cuda.matmul.allow_tf32 = True</code> and generally you should update PyTorch to the latest version.</p>

markuf: <p>Thanks <span class="mention">@ptrblc</span>! I assumed this whole time I was doing something wrong, but it’s a relief knowing that everything till this point’s correct. I’ll take a look at the Nsight Systems documentation.</p>
<p>For allowing tf32 (or flags in PyTorch in general), is it enough to just open Python, import torch and paste the command? If so, I’m not noticing any difference in GPU utilization after resuming training.</p>
<p>Unfortunately, I have no choice but to use this outdated version for class <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=12" title=":frowning:" class="emoji" alt=":frowning:" loading="lazy" width="20" height="20">.</p>

ptrblck: <p>Yes, you can directly call the line of code in your script to enable TF32 for matrix multiplications.<br>
Note however, that this would potentially speed up matmuls and could even show a lower GPU utilization if the real bottleneck was coming from another part of the pipeline.</p>
<p>I’ve written a small tutorial <a href="https://dev-discuss.pytorch.org/t/using-nsight-systems-to-profile-gpu-workload/59">here</a> on how to profile PyTorch using <code>nsys</code>. Once you’ve created the profile check if whitespaces between kernels are visible and if the kernel launches are close to the actual kernel execution, which would point towards a CPU-limited workload.</p>