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

I have two machines that I need to check my code across one is Ubuntu 18.04 and the other is Ubuntu 20.04.

In Ubuntu 18.04 I have:

$ 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.
>>> import torch
>>> torch.__version__
'1.9.1+cu102'

However, the code that works in Ubuntu 20.04, throws this error:

    with torch.autocast('cuda'):
AttributeError: module 'torch' has no attribute 'autocast'

I have this version of PyTorch on Ubuntu 20.04:

 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.
>>> import torch
>>> torch.__version__
'1.10.1+cu113'

Here’s the GPU info on Ubuntu 20.04:

and here’s the GPU info on Ubuntu 18.04:

Ideally I want the same code to run across two machines.

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 torch.cuda.amp.autocast.

1 Like

Thank you. I will spend some more time digging into this but

    with torch.cuda.amp.autocast:
AttributeError: __enter__

didn’t work as well. The error is unfortunately not super descriptive or guiding me how to fix it.

You have to “call” the decorator as given in the docs and examples:

with torch.cuda.amp.autocast():
    ...
2 Likes