8000 [Accelerator] Fix Python typing in accelerator by cyyever · Pull Request #152394 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[Accelerator] Fix Python typing in accelerator #152394

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

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

Conversation

cyyever
Copy link
Collaborator
@cyyever cyyever commented Apr 29, 2025

There are some changes:

  1. Use keywords for arguments if possible.
  2. __exit__ of device_index is changed to return None.

Copy link
pytorch-bot bot commented Apr 29, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/152394

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit daac118 with merge base 7243c69 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@cyyever cyyever changed the title Fix Python typing in accelerator [Accelerator] Fix Python typing in accelerator Apr 29, 2025
@cyyever
Copy link
Collaborator Author
cyyever commented Apr 29, 2025

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot bot added the topic: not user facing topic category label Apr 29, 2025
@cyyever
Copy link
Collaborator Author
cyyever commented Apr 29, 2025

@pytorchbot label "topic: not user facing"

@cyyever cyyever requested review from guangyey and XuehaiPan and removed request for guangyey April 30, 2025 02:26
@cyyever
Copy link
Collaborator Author
cyyever commented Apr 30, 2025

@pytorchmergebot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 30, 2025
@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: Approvers from one of the following sets are needed:

  • superuser (pytorch/metamates)
  • Core Reviewers (mruberry, lezcano, Skylion007, ngimel, peterbell10, ...)
  • Core Maintainers (soumith, gchanan, ezyang, dzhulgakov, malfet, ...)
Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@cyyever cyyever requested a review from Skylion007 May 1, 2025 14:33
Copy link
Collaborator
@guangyey guangyey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your update. Overall LGTM. +@albanD for final stamp.

@guangyey guangyey requested a review from albanD May 6, 2025 02:57
albanD
albanD previously requested changes May 6, 2025
Copy link
Collaborator
@albanD albanD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Device becoming optional sounds pretty wrong. Do we have any test for our typing?

torch/types.py Outdated
# None means use the default device (typically CPU)
Device: TypeAlias = Union[_device, str, int, None]
OptionalDevice: TypeAlias = Union[ExplicitDevice, None]
Device: TypeAlias = OptionalDevice
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Device is not optional usually !

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't think of a better type name.. Any advice?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can deprecate Device and add a new DeviceLike type.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the current "Device" type used heavily today? I would not expect we can change it !

Copy link
Collaborator Author
@cyyever cyyever May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DeviceLike makes less sense because str and int are treated as device in device APIs for many years. Is there any API where Device is allowed but str and int aren't?

torch/types.py Outdated
@@ -70,8 +70,10 @@

# Meta-type for "device-like" things. Not to be confused with 'device' (a
# literal device object). This nomenclature is consistent with PythonArgParser.
ExplicitDevice: TypeAlias = Union[_device, str, int]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the benefit of having this "ExplicitDevice" and "Device" if they're the same thing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Device can be None, which is cpu. But for accelerator APIs, their devices are accelerator cards (CUDA,XPU,...).
ExplicitDevice means that users can't pass None (CPU) to these APIs, that is meaningless..

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Device can be None, which is cpu.

I don't understand what you mean by that? Where did you read that?
Generally, when passing an optional device around, empty means the current default device not None.
In current_accelerator() API, we use None when no accelerator is available. But that's very unique to only that one function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, misunderstanding..

Copy link
Collaborator Author
@cyyever cyyever May 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExplicitDevice is used in only one place set_device_index, here the device shouldn't be None. Otherwise, it becomes "set device to the current device".

@cyyever
Copy link
Collaborator Author
cyyever commented May 6, 2025

@albanD OptionalDevice removed

@cyyever cyyever requested a review from albanD May 6, 2025 14:32
@janeyx99 janeyx99 added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 7, 2025
cyyever and others added 7 commits May 13, 2025 09:40
Co-authored-by: Xuehai Pan <XuehaiPan@outlook.com>
Co-authored-by: Xuehai Pan <XuehaiPan@outlook.com>
Co-authored-by: Yu, Guangye <106960996+guangyey@users.noreply.github.com>
@cyyever
Copy link
Collaborator Author
cyyever commented May 13, 2025

@pytorchbot merge

Copy link
pytorch-bot bot commented May 13, 2025

This PR has pending changes requested. Please address the comments and update the PR before merging.

@cyyever cyyever dismissed albanD’s stale review May 13, 2025 01:46

The new device type has been reverted because there is no suitable name for it.

@cyyever
Copy link
Collaborator Author
cyyever commented May 13, 2025

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: Approvers from one of the following sets are needed:

  • superuser (pytorch/metamates)
  • Core Reviewers (mruberry, lezcano, Skylion007, ngimel, peterbell10, ...)
  • Core Maintainers (soumith, gchanan, ezyang, dzhulgakov, malfet, ...)
Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/trunk Trigger trunk jobs on your pull request open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
0