Older version of PyTorch: with torch.autocast('cuda'): AttributeError: module 'torch' has no attribute 'autocast'

username: Mona_Jalal
cooked: <p>I have two machines that I need to check my code across one is Ubuntu 18.04 and the other is Ubuntu 20.04.</p>
<p>In Ubuntu 18.04 I have:</p>
<pre><code class="lang-auto">$ python
Python 3.8.0 (default, Dec  9 2021, 17:53:27)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.9.1+cu102'
</code></pre>
<p>However, the code that works in Ubuntu 20.04, throws this error:</p>
<pre><code class="lang-auto">    with torch.autocast('cuda'):
AttributeError: module 'torch' has no attribute 'autocast'
</code></pre>
<p>I have this version of PyTorch on Ubuntu 20.04:</p>
<pre><code class="lang-auto"> python
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.10.1+cu113'
</code></pre>
<p>Here’s the GPU info on Ubuntu 20.04:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/f/1f8c64d403981965b5102d0b527b4dcaa98779e1.png" data-download-href="https://discuss.pytorch.org/uploads/default/1f8c64d403981965b5102d0b527b4dcaa98779e1" title="Screen Shot 2022-02-01 at 2.45.51 AM"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/f/1f8c64d403981965b5102d0b527b4dcaa98779e1.png" alt="Screen Shot 2022-02-01 at 2.45.51 AM" data-base62-sha1="4v5APuOFdq202dvLe6QhILabcfD" width="690" height="479" data-dominant-color="282A2A"><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">Screen Shot 2022-02-01 at 2.45.51 AM</span><span class="informations">814×566 59.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></p>
<p>and here’s the GPU info on Ubuntu 18.04:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/7/27c900cdb2a757d32e09521d93eabd973f18baff.png" data-download-href="https://discuss.pytorch.org/uploads/default/27c900cdb2a757d32e09521d93eabd973f18baff" title="Screen Shot 2022-02-01 at 2.46.59 AM"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/7/27c900cdb2a757d32e09521d93eabd973f18baff.png" alt="Screen Shot 2022-02-01 at 2.46.59 AM" data-base62-sha1="5FXgErzN1y38T9dX0vWkv5xqQ7t" width="690" height="462" data-dominant-color="282929"><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">Screen Shot 2022-02-01 at 2.46.59 AM</span><span class="informations">822×551 59.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>
<p>Ideally I want the same code to run across two machines.</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="Mona_Jalal" data-post="1" data-topic="142993">
<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/mona_jalal/48/25715_2.png" class="avatar"> Mona_Jalal:</div>
<blockquote>
<p>Ideally I want the same code to run across two machines.</p>
</blockquote>
</aside>
<p>The best approach would be to use the same PyTorch release on both machines. If that’s not possible, and assuming you are using the GPU, use <code>torch.cuda.amp.autocast</code>.</p>

username: Mona_Jalal
cooked: <p>Thank you. I will spend some more time digging into this but</p>
<pre><code class="lang-auto">    with torch.cuda.amp.autocast:
AttributeError: __enter__
</code></pre>
<p>didn’t work as well. The error is unfortunately not super descriptive or guiding me how to fix it.</p>

username: ptrblck
cooked: <p>You have to “call” the decorator as given in the docs and examples:</p>
<pre><code class="lang-python">with torch.cuda.amp.autocast():
    ...
</code></pre>