High CPU Usage?

willy: <p>I tried AMP on my training pipeline. While the memory usage certainly decreased by a factor of 2, the overall runtime seems to be the same?</p>
<p>I ran some testing with profiler and it seems like the gradient scaling step takes over 300ms of CPU time? Seems like gradient scaling defeats the purpose of all the speed up we receive from AMP?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/8/c8bcfb06455c65510f43ed8e7e014d8d8d883dfa.png" data-download-href="https://discuss.pytorch.org/uploads/default/c8bcfb06455c65510f43ed8e7e014d8d8d883dfa" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/8/c8bcfb06455c65510f43ed8e7e014d8d8d883dfa.png" alt="image" data-base62-sha1="sDOnKz7e2QaCO4HgBJlXvvubiMG" width="690" height="97" data-dominant-color="101010"><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">2249×317 11.9 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>Also, while I observed similar times for AMP vs. regular training, the reported CUDA time + CPU time from profile seems to suggest AMP takes twice as long?</p>
<p>Also, unrelated to AMP, seems like I have a step aten::mul_ taking a couple of milliseconds that appears to be from ADAM? So is the Adam optimizer done on the CPU, which explains this extra CPU time?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/2/528ccb7621b04f0d22eb7d2b53fc430aaf016f82.png" data-download-href="https://discuss.pytorch.org/uploads/default/528ccb7621b04f0d22eb7d2b53fc430aaf016f82" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/2/528ccb7621b04f0d22eb7d2b53fc430aaf016f82.png" alt="image" data-base62-sha1="bMgNQvGJiBjFBhYUoGtI5R3kblg" width="690" height="105" data-dominant-color="111111"><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">2157×329 12.9 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>Did you check the end2end runtime in FP32 vs. AMP training?<br>
If so, how long look each iteration (after a warmup phase) and which device are you using?</p>

willy: <p>Yes, I did. The reported stats are the ones after warmup phase. I am using RTX 3060.</p>
<p>I also discovered that I get a marginal (roughly 25% speedup) with amp using<br>
torch.backends.cudnn.benchmark = True</p>
<p>Just curious on why this is the case.</p>

ptrblck: <p>By default TF32 is already used, so the TensorCores on your device will already be utilized, which might not leave a lot of performance benefits left for AMP. <a href="https://github.com/pytorch/pytorch/issues/57806">This issue</a> with the related <a href="https://discuss.pytorch.org/t/torch-cuda-amp-cannot-speed-up-on-a100/120525/2">double post</a> might be interesting for you.<br>
In any case, you could create profiles with a visual profiler such as Nsight Systems and check for other bottlenecks, such as data loading, which might be the current bottleneck.<br>
Also refer to the <a href="https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html">Performance Tuning Guide</a> and, if possible, you could also try out the latest CUDA + cudnn versions by building from source, which could yield additional performance improvements.</p>

willy: <p>Thanks for the information!</p>
<p>So TensorCores are used by default in PyTorch already? I did use the Nsight Systems but the results were very difficult to analyze. It could be due to the fact that TensorCores are already being used.</p>
<p>Just some quick follow up questions:</p>
<ol>
<li>
<p>why does torch.backends.cudnn.benchmark = True improve speed up? Does it utilize CUDA cores in conjunction with tensor cores? I am seeing more kernels being used when monitoring with Nsight Systems. Also seems like this optimization only improves performance of AMP code, but not regular. Do you know why this is the case?</p>
</li>
<li>
<p>If tensor cores are used by default, are there any advantages of using AMP (other than for optimization in question 1)? because it seems like the gradient scaler already takes a lot of CPU time. I am also afraid of AMP potentially causing instabilities in network training.</p>
</li>
<li>
<p>Are newer version of CUDA supported by pytorch? I am using CUDA 11.1.0, which is the latest CUDA supported by pytorch according to the website.</p>
</li>
<li>
<p>One of the post you listed mentioned using last channel format for additional performance increase. However, when I did this with AMP, I actually observed a performance decrease. Can this occur or is something wrong with my setup?</p>
</li>
</ol>
<p>Thanks again for the help.</p>

ptrblck: <aside class="quote no-group" data-username="willy" data-post="5" data-topic="125159">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/willy/48/44276_2.png" class="avatar"> willy:</div>
<blockquote>
<p>So TensorCores are used by default in PyTorch already?</p>
</blockquote>
</aside>
<p>On Ampere GPUs, yes, since the <a href="https://developer.nvidia.com/blog/accelerating-ai-training-with-tf32-tensor-cores/">TF32 type</a> is used.</p>
<ol>
<li>
<p><code>cudnn.benchmark = True</code> will profile different algorithms for each convolution and select the fastest one. The algorithm selection will be stored in a cache for each new input shape. The <a href="https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html#enable-cudnn-auto-tuner">performance tuning guide</a> explains it in more detail. It’s also not specific to AMP.</p>
</li>
<li>
<p>Yes, potentially an additional speedup can be seen, which might not be as large as pure FP32 training vs. AMP.</p>
</li>
<li>
<p>No, all CUDA versions are supported. The pip wheels and conda binaries are shipped with CUDA10.2 and 11.1 at the moment, but you can build from source with any toolkit &gt;=10.0.</p>
</li>
<li>
<p>The channels-last memory format could yield a speedup when using AMP (refer to the performance guide). If you are seeing a slowdown, please post the model definition and input shapes, which would reproduce the issue.</p>
</li>
</ol>

willy: <p>Okay, thank you for the help!</p>