8000 [torchgen] Refactor `torchgen.utils.FileManager` to accept `pathlib.Path` by XuehaiPan · Pull Request #150726 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[torchgen] Refactor torchgen.utils.FileManager to accept pathlib.Path #150726

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

Closed
wants to merge 17 commits into from

Conversation

XuehaiPan
Copy link
Collaborator
@XuehaiPan XuehaiPan commented Apr 5, 2025

This PR allows FileManager to accept pathlib.Path as arguments while keeping the original str path support.

This allows us to simplify the code such as:

  1. os.path.join(..., ...) with Path.__floordiv__(..., ...).

template_path = os.path.join(self.template_dir, template_fn)

filename = f"{self.install_dir}/{filename}"

  1. os.path.basename(...) with Path(...).name.

    comment += f" from {os.path.basename(template_path)}"

  2. Manual file extension split with Path(...).with_stem(new_stem)

pytorch/torchgen/utils.py

Lines 241 to 256 in 95a5958

8000
dot_pos = filename.rfind(".")
if dot_pos == -1:
dot_pos = len(filename)
base_filename = filename[:dot_pos]
extension = filename[dot_pos:]
for shard in all_shards:
shard_id = shard["shard_id"]
self.write_with_template(
f"{base_filename}{shard_id}{extension}", filename, lambda: shard
)
# filenames is used to track compiled files, but FooEverything.cpp isn't meant to be compiled
self.filenames.discard(
f"{self.install_dir}/{base_filename}Everything{extension}"
)


Stack from ghstack (oldest at bottom):

[ghstack-poisoned]
Copy link
pytorch-bot bot commented Apr 5, 2025

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 1 New Failure

As of commit 0a437b7 with merge base e2ce17c (image):

NEW FAILURE - The following job has failed:

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

Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

torchgen/utils.py:180

  • [nitpick] The error message does not properly interpolate the file variable; consider using an f-string (e.g. f"duplicate file write {file}") to enhance debugging clarity.
assert file not in self.files, "duplicate file write {file}"

XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Apr 5, 2025
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Apr 5, 2025
[ghstack-poisoned]
@pytorchmergebot
Copy link
Collaborator

Rebased gh/XuehaiPan/269/orig onto refs/remotes/origin/viable/strict because #150732 was rebased, please pull locally before adding more changes (for example, via ghstack checkout https://github.com/pytorch/pytorch/pull/150726)

XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Apr 7, 2025
@albanD albanD removed their request for review April 7, 2025 19:22
[ghstack-poisoned]
@pytorchmergebot
Copy link
Collaborator

Rebased gh/XuehaiPan/269/orig onto refs/remotes/origin/viable/strict because #150732 was rebased, please pull locally before adding more changes (for example, via ghstack checkout https://github.com/pytorch/pytorch/pull/150726)

@pytorchmergebot
Copy link
Collaborator

Starting merge as part of PR stack under #150732

XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request Apr 9, 2025
@XuehaiPan XuehaiPan requested a review from Skylion007 April 9, 2025 14:27
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 2, 2025
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 2, 2025
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 2, 2025
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 10, 2025
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 11, 2025
[ghstack-poisoned]
XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 14, 2025
Copy link
Contributor
@aorenste aorenste left a comment

Choose a reason for hiding this comment

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

Looks pretty good w/ a few changes needed:

You should probably mention the change to assert_never in the summary.
For BC we probably need to keep assert_never and mark it w/ deprecated.

self.filenames = set()
def __init__(
self,
install_dir: str | Path,
Copy link
Contributor

Choose a reason for hiding this comment

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

This syntax is python 3.10+ and we still need to support 3.9. Please change these (here and below) to Union[str, Path]

Copy link
Collaborator Author
@XuehaiPan XuehaiPan May 14, 2025

Choose a reason for hiding this comment

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

There is a future import from __future__ import annotations at the top of the file.

[ghstack-poisoned]
@XuehaiPan XuehaiPan requested a review from aorenste May 14, 2025 15:25
@@ -97,6 +97,15 @@ def context(msg_fn: Callable[[], str]) -> Iterator[None]:
raise


if TYPE_CHECKING:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if the BC checker is "TYPE_CHECKING" or not... but if this passes the BC checker then I'm okay with it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

typing_extensions.assert_never provides the exact same function.

XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 14, 2025
@XuehaiPan
Copy link
Collaborator Author

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

XuehaiPan added a commit to XuehaiPan/pytorch that referenced this pull request May 14, 2025
@XuehaiPan
Copy link
Collaborator Author

@pytorchbot merge -i

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged while ignoring the following 1 checks: pull / linux-focal-py3.13-clang10 / test (dynamo_wrapped, 2, 3, linux.2xlarge)

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

pytorchmergebot pushed a commit that referenced this pull request May 15, 2025
…s (PEP 585) and Union Type (PEP 604) (#150727)

#129001 (comment) is the motivation for the whole stack of PRs. In `torch/__init__.py`, `torch._C.Type` shadows `from typing import Type`, and there is no type stub for `torch._C.Type` in `torch/_C/__init__.pyi`. So we need to use `from typing import Type as _Type`. After enabling [Generic TypeAlias (PEP 585)](https://peps.python.org/pep-0585) in the `.pyi` type stub files, we can use `type` instead of `typing.Type` or `from typing import Type as _Type`.

------

- [Generic TypeAlias (PEP 585)](https://peps.python.org/pep-0585): e.g. `typing.List[T] -> list[T]`, `typing.Dict[KT, VT] -> dict[KT, VT]`, `typing.Type[T] -> type[T]`.
- [Union Type (PEP 604)](https://peps.python.org/pep-0604): e.g. `Union[X, Y] -> X | Y`, `Optional[X] -> X | None`, `Optional[Union[X, Y]] -> X | Y | None`.

Note that in `.pyi` stub files, we do not need `from __future__ import annotations`. So this PR does not violate issue #117449:

- #117449

------

Pull Request resolved: #150727
Approved by: https://github.com/aorenste
ghstack dependencies: #150726
pytorchmergebot pushed a commit that referenced this pull request May 15, 2025
…nd Union Type (PEP 604) (#150728)

#129001 (comment) is the motivation for the whole stack of PRs. In `torch/__init__.py`, `torch._C.Type` shadows `from typing import Type`, and there is no type stub for `torch._C.Type` in `torch/_C/__init__.pyi`. So we need to use `from typing import Type as _Type`. After enabling [Generic TypeAlias (PEP 585)](https://peps.python.org/pep-0585) in the `.pyi` type stub files, we can use `type` instead of `typing.Type` or `from typing import Type as _Type`.

------

- [Generic TypeAlias (PEP 585)](https://peps.python.org/pep-0585): e.g. `typing.List[T] -> list[T]`, `typing.Dict[KT, VT] -> dict[KT, VT]`, `typing.Type[T] -> type[T]`.
- [Union Type (PEP 604)](https://peps.python.org/pep-0604): e.g. `Union[X, Y] -> X | Y`, `Optional[X] -> X | None`, `Optional[Union[X, Y]] -> X | Y | None`.

Note that in `.pyi` stub files, we do not need `from __future__ import annotations`. So this PR does not violate issue #117449:

- #117449

------

Pull Request resolved: #150728
Approved by: https://github.com/cyyever, https://github.com/aorenste
ghstack dependencies: #150726, #150727
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
better-engineering Relatively self-contained tasks for better engineering contributors ci-no-td Do not run TD on this PR ciflow/trunk Trigger trunk jobs on your pull request Merged open source Reverted suppress-bc-linter Suppresses the failures of API backward-compatibility linter (Lint/bc_linter) topic: not user facing topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0