Pytorch amp.gradscalar/amp.autocast attribute not found

mbutt: <p>Hi I am getting out of memory error while training ResNet18. So I tried to use pytorch automatic mixed precision but I get errors like amp.GradScalar/amp.autocast not an attribute. Importing amp doesn’t raise any error though. I have googled the error but could not resolve it. If someone could please help?</p>

ptrblck: <p>You have a typo in <code>GradScalar</code> as it should be <code>torch.cuda.amp.GradScaler</code>.<br>
In case you are trying to use it from the <code>torch.amp</code> namespace, note that it might not be available since the CPU uses <code>bfloat16</code> as the lower precision <code>dtype</code> and thus does not use gradient scaling.</p>

mbutt: <p>Sorry for the typo, it’s just here. I am now even confused if the memory error I am getting can even be resolved using this. the memory error is written like this “Memory cgroup out of memory”. I am not familiar with cgroup. and I can’t even find the cgconfig.conf file. I am working on a remote server through bash in Visual Studio Code.</p>

ptrblck: <p>I would guess your <code>cgroup</code> is running out of host RAM and it’s unrelated to any GPU memory usage.</p>

mbutt: <p>By host RAM do you mean server RAM? Or something else? I am sorry I am very new at this kind f thing. I should also mention that reducing the batch-size doesn’t make any difference, the error is raised at exact same iteration.</p>

ptrblck: <p>Yes, I meant the CPU RAM. I don’t know which setup you are using, but you would have to check if a <code>cgroup</code> is created which limits the RAM usage. Also just check your RAM via e.g. <code>htop</code> and see at which point your script crashes.</p>

mbutt: <p>Okay let me look at these things. Thank you.</p>

mbutt: <aside class="quote no-group" data-username="ptrblck" data-post="6" data-topic="161254">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>htop</code></p>
</blockquote>
</aside>
<p>So I tried htop. I am attaching screenshots in increasing time. (this happens even with batch size 1)</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/0/60d2b7708784f5316b780694965df60141317a93.png" data-download-href="https://discuss.pytorch.org/uploads/default/60d2b7708784f5316b780694965df60141317a93" title="1"><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/0/60d2b7708784f5316b780694965df60141317a93.png" alt="1" data-base62-sha1="dOxijbfp8RzVEavGlf8YyUXBlhp" width="690" height="438" data-dominant-color="272C2C"><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">1</span><span class="informations">1266×805 91.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>

mbutt: <p>Can’t put multiple images in one post.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/b/bb7cc7f6234776b359c915ab707e14667bcc792a.png" data-download-href="https://discuss.pytorch.org/uploads/default/bb7cc7f6234776b359c915ab707e14667bcc792a" title="2"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/b/bb7cc7f6234776b359c915ab707e14667bcc792a.png" alt="2" data-base62-sha1="qKAD0P6SlWircsLzvpk5yWjQbfc" width="690" height="431" data-dominant-color="272C2C"><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">2</span><span class="informations">1263×790 91.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>

mbutt: <p>Here the process gets killed.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/6/366d15e5b9ee4568ba6d2a82e8ac0d058bbf5b16.png" data-download-href="https://discuss.pytorch.org/uploads/default/366d15e5b9ee4568ba6d2a82e8ac0d058bbf5b16" title="3"><img src="https://discuss.pytorch.org/uploads/default/original/3X/3/6/366d15e5b9ee4568ba6d2a82e8ac0d058bbf5b16.png" alt="3" data-base62-sha1="7LtuSErniM0nQ52bb5obna7Vntc" width="690" height="454" data-dominant-color="272C2C"><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">3</span><span class="informations">1265×834 92.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>