Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Turn Stream into protocol and improve typing in torch/_C/__init__.pyi.in #145239

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

cyyever
Copy link
Collaborator

@cyyever cyyever commented Jan 21, 2025

Fixes #ISSUE_NUMBER

cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel

Copy link

pytorch-bot bot commented Jan 21, 2025

🔗 Helpful Links

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

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

❌ 9 New Failures, 1 Unrelated Failure

As of commit 27b061e with merge base caf6039 (image):

NEW FAILURES - The following jobs have failed:

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

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

@cyyever cyyever requested a review from guangyey January 21, 2025 03:10
@cyyever
Copy link
Collaborator Author

cyyever commented Jan 21, 2025

@pytorchbot label "topic: not user facing"

def __exit__(self, exc_type, exc_val, exc_tb) -> None: ...
def __reduce__(self) -> Tuple[Any, ...]: ... # THPDevice_reduce

# Defined in torch/csrc/Stream.cpp
class Stream:
class Stream(Protocol):
Copy link
Collaborator

Choose a reason for hiding this comment

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

We expect device-specific stream classes (for example, torch.cuda.Stream) to be explicitly derived from this class. What will happen if we remove Protocol here.

Copy link
Collaborator Author

@cyyever cyyever Jan 21, 2025

Choose a reason for hiding this comment

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

Not all streams use this class as base, for example cpu stream in torch/cpu/__init__.py. The Protocol change should not affect the current behaviour of the class hierarchy in principle. However, turning it into a protocol makes it possible to type all kinds of stream implementation as Stream. Otherwise, we have to write ugly type annotation such as cpuStream | cudaStream | xxxStream for device agnostic code.

Copy link
Collaborator

Choose a reason for hiding this comment

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

If we change torch.Stream to Protocol, we must add the same methods defined in torch.Stream to torch.cpu.Stream.
What do you think we change torch.cpu.Stream like this:

class Stream(torch.Stream):
    def __new__(cls, priority=-1):
        return super().__new__(cls, device='cpu', priority=priority)

It seems to look clearer.

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 prefer this if CI passes.

@mikaylagawarecki mikaylagawarecki added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jan 23, 2025
@ezyang ezyang removed their request for review January 24, 2025 04:30
@ezyang
Copy link
Contributor

ezyang commented Jan 24, 2025

This will require some careful review and I won't have time before I go on leave

@cyyever
Copy link
Collaborator Author

cyyever commented Jan 25, 2025

@pytorchbot rebase

@pytorchmergebot
Copy link
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/viable/strict. Check the current status here

@pytorchmergebot
Copy link
Collaborator

Successfully rebased C_init_py onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via git checkout C_init_py && git pull --rebase)

@cyyever cyyever requested a review from albanD January 25, 2025 03:33
@cyyever cyyever added the oncall: jit Add this issue/PR to JIT oncall triage queue label Jan 25, 2025
@cyyever cyyever requested a review from XuehaiPan January 25, 2025 03:34
@cyyever cyyever changed the title Improve typing in torch/_C/__init__.pyi.in Turn Stream into protocol and improve typing in torch/_C/__init__.pyi.in Jan 25, 2025
@cyyever cyyever marked this pull request as draft January 25, 2025 10:42
@overload
def __new__(self, stream_id: _int, device_index: _int, device_type: _int, *, priority: _int = 0) -> Stream: ...
@runtime_checkable
class StreamBase(Protocol):
Copy link
Collaborator

@XuehaiPan XuehaiPan Jan 25, 2025

Choose a reason for hiding this comment

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

Why do we need to split the method into an XxxBase class?

Copy link
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions bot added the Stale label Mar 26, 2025
@github-actions github-actions bot closed this Apr 25, 2025
@cyyever cyyever reopened this Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
oncall: jit Add this issue/PR to JIT oncall triage queue open source Stale 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