Skip to content
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

Add --device argument to run examples on a specific device #1288

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

shink
Copy link

@shink shink commented Sep 23, 2024

All examples can only be run on CUDA or MPS using --cuda or --mps.
This change adds an argument --device to make our examples run on a specific device, e.g., --device cpu. This will benefit all device manufacturers.

cc: @Yikun @FFFrog @hipudding @jgong5 @EikanWang

Copy link

netlify bot commented Sep 23, 2024

Deploy Preview for pytorch-examples-preview canceled.

Name Link
🔨 Latest commit 2846161
🔍 Latest deploy log https://app.netlify.com/sites/pytorch-examples-preview/deploys/66f11872584dd60008bf36a1

@shink shink marked this pull request as draft September 23, 2024 03:35
@shink
Copy link
Author

shink commented Sep 23, 2024

Could someone please review this change? If it makes sense to you, I will proceed.

@shink shink marked this pull request as ready for review September 23, 2024 06:40
@msaroufim
Copy link
Member

This change would be easier to merge if we started testing M1 in CI

Do you have any experience in doing this kind of stuff?

Basically you can copy this https://github.com/pytorch/examples/blob/main/.github/workflows/main_python.yml change this line to support M1 https://github.com/pytorch/examples/blob/main/.github/workflows/main_python.yml#L16 macos-latest and then make sure you have a test file where you always pass in the M1 device

@shink
Copy link
Author

shink commented Sep 25, 2024

@msaroufim Thanks for your review! The following code snippet is the key of this change. args.device is cpu by default so this change is compatible. As you can see, --device has lower priority than --cuda and --mps.

I tested this change on my out-of-tree backend device and the result said OK.

if args.cuda:
    device = torch.device("cuda")
elif args.mps:
    device = torch.device("mps")
else:
-   device = torch.device("cpu")
+   device = torch.device(args.device)

This change would be easier to merge if we started testing M1 in CI

ah yes! So should I test this change on MPS and add a workflow in this PR?

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