tutorials icon indicating copy to clipboard operation
tutorials copied to clipboard

module 'torch' has no attribute 'accelerator'

Open shandatascience opened this issue 10 months ago • 4 comments

Add Link

Link to the tutorial on the website - https://pytorch.org/tutorials/beginner/basics/quickstart_tutorial.html#creating-models

Describe the bug

Below piece of code is returning device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else "cpu" print(f"Using {device} device")

When running using google colab throws the below error `--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in <cell line: 0>() ----> 1 device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else "cpu" 2 print(f"Using {device} device") 3 4 # Define model 5 class NeuralNetwork(nn.Module):

/usr/local/lib/python3.11/dist-packages/torch/init.py in getattr(name) 2560 return importlib.import_module(f".{name}", name) 2561 -> 2562 raise AttributeError(f"module '{name}' has no attribute '{name}'") 2563 2564

AttributeError: module 'torch' has no attribute 'accelerator'`

Correct code: `if torch.cuda.is_available(): device = torch.device("cuda") device_name = torch.cuda.get_device_name(0)

elif torch.backends.mps.is_available(): device = torch.device("mps")

else: device = torch.device("cpu")

print(f"Using {device} device")`

Describe your environment

Platform: Google Colab Cuda: Yes Pytorch Version: 2.5.1+cu121

cc @subramen @albanD @jbschlosser

shandatascience avatar Jan 25 '25 15:01 shandatascience

Hey! This is expected as this tutorial is for the 2.6 release that will come out a bit later this week ! @svekars I think it's ok to keep this as is?

albanD avatar Jan 27 '25 16:01 albanD

Sounds good to me!

svekars avatar Jan 27 '25 17:01 svekars

Is torch.accelerator deprecated? Or new? IOW, when encountering this error, how do you fix it?

Seems like it should work, e.g., https://pytorch.org/docs/stable/accelerator.html doesn't have any obvious deprecation notes.

Also seen with https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html

Platform: Jupyter Cuda: mps Pytorch Version: 2.5.1

ed-norris avatar Feb 16 '25 18:02 ed-norris

@ed-norris - I just encountered the same problem, it should work if you install the latest version of pytorch in colab, follow the instructions in https://pytorch.org/tutorials/beginner/colab#pytorch-version-in-google-colab

stonepd avatar Mar 12 '25 14:03 stonepd

Hi, Any update on this?

ishansmishra avatar Apr 30 '25 10:04 ishansmishra

@ishansmishra I noticed that if you run pip install torch torchvision it will install torch 2.4.0. To get around this I explicitly install torch 2.7.0 pip install torch==2.7.0

Can you check your torch version?

python -c 'import torch; print(torch.version.__version__)'

The root cause is likely due to torchvision requiring an older version of torch and will require fixing the dependency tree (or possibly upgrades need to be made to the torchvision library itself)

paulserraino avatar May 13 '25 17:05 paulserraino

@ishansmishra I will suggest using the commands on here: https://pytorch.org/get-started/locally/.

I had the same error, I had to uninstall it and reinstalled again and it worked.

Iphykay avatar May 25 '25 04:05 Iphykay

A straight forward workaround: Comment the original line and add one of the following:

device = "cpu"
device = "cuda"
device = "cuda" if torch.cuda.is_available() else "cpu"

However note that the accelerator method should be preferred in general, as those lines are only for devices without any (used) GPU or with a Nvida GPU, but not some other accelerator.

jokobus avatar Jun 04 '25 17:06 jokobus

I'm having the same problem after installing locally, but the workarounds aren't working for me.

mjcormier avatar Jul 25 '25 16:07 mjcormier