Skip to content

Use torch.accelerator API in Fast Neural Style example #1327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025

Conversation

dvrogozh
Copy link
Contributor

@dvrogozh dvrogozh commented Apr 18, 2025

torch.accelerator API allows to abstract some of the accelerator specifics in the user scripts. This commit modifies Fast Neural Style example with the mentioned API.

Things to note:

  • Commit modifies example command line by replacing accelerator specific flags (--cuda, --mps, --xpu) with the --accel.
  • torch.accelerator API appeared in pytorch 2.6, so commit binds example to this torch version
  • Commit introduces USE_ACCEL flag for the test *.sh script. At the moment this flag is just an alias for USE_CUDA. In the future it should replace USE_CUDA once all examples will be converted to use torch.accelerator API.

CC: @malfet, @atalman

Copy link

netlify bot commented Apr 18, 2025

Deploy Preview for pytorch-examples-preview canceled.

Name Link
🔨 Latest commit afbdd24
🔍 Latest deploy log https://app.netlify.com/sites/pytorch-examples-preview/deploys/680fd8ceb40ab8000895e151

@msaroufim
Copy link
Member

Cool! Although looks like you're not using the right pytorch version quite yet, you can be explicit see how we use requirements.txt in the repo within given folders

@dvrogozh
Copy link
Contributor Author

I think I might need help with the requirements part of the story. Here is a snapshot of what was executed by ci (https://github.com/pytorch/examples/actions/runs/14539355819/job/40795679639?pr=1327):

2025-04-18T18:32:00.6714511Z Collecting torch
2025-04-18T18:32:00.7041753Z   Downloading torch-2.6.0-cp310-cp310-manylinux1_x86_64.whl.metadata (28 kB)
...
2025-04-18T18:33:28.7956634Z installing requirements
...
2025-04-18T18:33:39.0976123Z Collecting torch>=2 (from torchdata==0.9.0->-r /dev/stdin (line 16))
2025-04-18T18:33:39.1017813Z   Downloading torch-2.5.0-cp310-cp310-manylinux1_x86_64.whl.metadata (28 kB)
...
2025-04-18T18:34:04.9822716Z   Attempting uninstall: torch
2025-04-18T18:34:04.9832302Z     Found existing installation: torch 2.6.0
2025-04-18T18:34:05.3568406Z     Uninstalling torch-2.6.0:
2025-04-18T18:34:05.4241262Z       Successfully uninstalled torch-2.6.0
...
2025-04-18T18:34:37.8267387Z AttributeError: module 'torch' has no attribute 'accelerator'

I think that CI attempted to test the latest nightly, at least this implies that:

pip install --pre torch torchvision -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html

Then, test shell script downgraded already installed nightly torch to version 2.5.0. My guess is due to pinned version of torchvision in some of the requirements.txt files:

torchvision==0.20.0

Also note:

@dvrogozh
Copy link
Contributor Author

I think I might need help with the requirements part of the story.

Filed #1329 to discuss further.

`torch.accelerator` API allows to abstract some of the accelerator specifics
in the user scripts. This commit modifies Fast Neural Style example with the mentioned API.

Things to note:
* Commit modifies example command line by replacing accelerator specific
  flags (`--cuda`, `--mps`, `--xpu`) with the --accel
* `torch.accelerator` API appeared in pytorch 2.6, so commit binds example to this torch version
* Commit introduces `USE_ACCEL` flag for the test `*.sh` script. At the moment this
  flag is just an alias for `USE_CUDA`. In the future it should replace `USE_CUDA` once all
  examples will be converted to use `torch.accelerator` API.

Signed-off-by: Dmitry Rogozhkin <[email protected]>
@dvrogozh
Copy link
Contributor Author

@msaroufim : I've rebased this PR on top of Friday's changes. Note that I suggest to change USE_CUDA to USE_ACCEL in the future once all examples will be converted to the new API. This PR does a step towards that by just adding USE_ACCEL as alias for USE_CUDA.

@msaroufim msaroufim merged commit 65afde6 into pytorch:main Apr 28, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants