pytorch / pytorch/tutorials

module 'torch' has no attribute 'accelerator'

Open
#3,263 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug core intro
Dominant language
Python
Stars
9.3k
Forks
4.4k
Avg merge
1d 21h
Merged PRs (30d)
4

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open the linked quickstart tutorial at the “Creating Models” section and inspect the device-selection example. Reproduce it in Google Colab with PyTorch 2.5.1+cu121, then update the tutorial so the example runs without the reported AttributeError and still prints the selected device.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.