8000 Make fx.node.map_arg() and .map_aggregate() generic by rec · Pull Request #146248 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

Make fx.node.map_arg() and .map_aggregate() generic #146248

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 27 commits into from

Conversation

rec
Copy link
Collaborator
@rec rec commented Feb 1, 2025

What's the problem?

The popular fx.node.map_arg() and fx.node.map_aggregate() apply operations recursively on dicts, tuples, lists, etc, and return a new collection of the same type.

Unfortunately, their base input type is Argument, which is very unspecific indeed: most type information is just thrown away at the call site of either of these functions, as far as the type checker goes.

As torch moves to a more typed code base, this would force innocent, unsuspecting developers to add logically unnecessary casts or # type: ignore statements.

What's the solution?

Making these two node.map_* functions generic on the first argument and return type means that type information is preserved for the type checker. (The signature of the other parameter, the function that visits the nodes and subnodes, has not changed, nor should it.)

Won't it break everything?

It doesn't break the type checker - one place needed an extra hint.

There have been code breakages, resolved one, at least one new one... we'll see!

Stack from ghstack (oldest at bottom):

cc @H-Huang @awgu @kwen2501 @wanchaol @fegin @fduwjj @wz337 @wconstab @d4l3k @c-p-i-o @ezyang @malfet @xuzhao9 @gramster @SherlockNoMad @EikanWang @jgong5 @wenzhe-nrv @voznesenskym @penguinwu @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @jiayisunx @chenyang78 @kadeng @chauhang @amjames @StrongerXi

< 8000 div class="pr-review-reactions ">
[ghstack-poisoned]
@pytorch-bot pytorch-bot bot added the release notes: fx release notes category label Feb 1, 2025
Copy link
pytorch-bot bot commented Feb 1, 2025

🔗 Helpful Links

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

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

⏳ 5 Pending, 3 Unrelated Failures

As of commit d0d7327 with merge base f95bdf5 (image):

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:

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

rec added a commit that referenced this pull request Feb 1, 2025
ghstack-source-id: 8d173a3
Pull Request resolved: #146248
@rec rec added topic: not user facing topic category module: typing Related to mypy type annotations better-engineering Relatively self-contained tasks for better engineering contributors suppress-bc-linter Suppresses the failures of API backward-compatibility linter (Lint/bc_linter) labels Feb 1, 2025
torch/fx/node.py Outdated
@@ -915,10 +916,10 @@ def map_aggregate(a: Argument, fn: Callable[[Argument], Argument]) -> Argument:
dict.__setitem__(rv, k, map_aggregate(v, fn))
return rv
elif isinstance(a, slice):
return slice(
return slice( # type: ignore[return-value]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will this work, will we need to do typing.overrload to properly handle it?

Copy link
Collaborator
@Skylion007 Skylion007 Feb 1, 2025

Choose a reason for hiding this comment

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

Okay, Slice is now generic, but may require a typing_extensions / typeshed / mypy update: python/typeshed#8647 python/typeshed#11637

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@Skylion007 Well, I'm not entirely sure this will work, but it seems to represent what is actually going on!, gives better typing at point of use, and running some tests locally seemed to work FWIW.

(This actually emerged from tweaking some typing elsewhere...)

I'm seeing some tests fail due to backwards compatibility checks which need human approval to tweak. I'll see how the whole thing shakes out before I actually contact a human and ask for lenience... 😬


As for generic slice - well, it seems very hard to deduce what type is actually being sliced on line 919 there.

Copy link
Collaborator
@Skylion007 Skylion007 Feb 1, 2025

Choose a reason for hiding this comment

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

Actually was referring to the typing of Argument, there was a comment that the slice in that TypeAlias that slice in the Union should be typed, but it wasn't because of a lack of support. That should be fixed now. it's slice[Argument, Argument, Argument] :) Link for that comment is here:

Argument = Optional[

@rec rec added the suppress-api-compatibility-check Suppresses the failures of API backward-compatibility linter (Lint/bc_linter) label Feb 1, 2025
[ghstack-poisoned]
rec added a commit that referenced this pull request Feb 1, 2025
ghstack-source-id: 65d35fb
Pull Request resolved: #146248
rec added a commit to rec/pytorch that referenced this pull request Feb 2, 2025
[ghstack-poisoned]
rec added a commit that referenced this pull request Feb 2, 2025
ghstack-source-id: e0d1cf8
Pull Request resolved: #146248
@pytorch-bot pytorch-bot bot added the oncall: distributed Add this issue/PR to distributed oncall triage queue label Feb 2, 2025
[ghstack-poisoned]
rec added a commit that referenced this pull request Feb 2, 2025
ghstack-source-id: 07fd20f
Pull Request resolved: #146248
rec added a commit to rec/pytorch that referenced this pull request Feb 2, 2025
[ghstack-poisoned]
rec added a commit that referenced this pull request Feb 2, 2025
ghstack-source-id: 6a35d17
Pull Request resolved: #146248
@rec
Copy link
Collaborator Author
rec commented Feb 2, 2025

@Skylion007: of course, that makes sense!

Since I just had to look, the word slice only appears 324 places in torch, as high as 20% in comments or error messages, none of which appear to be type hints except here in this one spot. I'll bet that would change with full hinting.

I changed the pull request to be sleeker and also found a neat fix for the backward compatibility test, if it passes the tests I'll take it out of draft.

@rec rec changed the title Make fx.node.map_arg generic Make fx.node.map_arg() and .map_aggregate() generic Feb 2, 2025
@Skylion007 Skylion007 requested a review from XuehaiPan February 2, 2025 18:45
[ghstack-poisoned]
rec added a commit that referenced this pull request Feb 3, 2025
ghstack-source-id: 3d96a8b
Pull Request resolved: #146248
@rec
Copy link
Collaborator Author
rec commented Feb 13, 2025

@pytorchbot rebase

@pytorchmergebot
Copy link
Collaborator

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

[ghstack-poisoned]
@pytorchmergebot
Copy link
Collaborator

Successfully rebased gh/rec/129/orig onto refs/remotes/origin/viable/strict, please pull locally before adding more changes (for example, via ghstack checkout https://github.com/pytorch/pytorch/pull/146248)

pytorchmergebot pushed a commit that referenced this pull request Feb 13, 2025
ghstack-source-id: 3443065
Pull Request resolved: #146248
@rec
Copy link
Collaborator Author
rec commented Feb 13, 2025

@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

rec added a commit that referenced this pull request Feb 13, 2025
ghstack-source-id: 3443065
Pull Request resolved: #146248
[ghstack-poisoned]
@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: New commits were pushed while merging. Please rerun the merge command.

Details for Dev Infra team Raised by workflow job

9E88
[ghstack-poisoned]
rec added a commit that referenced this pull request Feb 13, 2025
ghstack-source-id: 7467ff5
Pull Request resolved: #146248
[ghstack-poisoned]
rec added a commit that referenced this pull request Feb 14, 2025
ghstack-source-id: bc4b430
Pull Request resolved: #146248
[ghstack-poisoned]
rec added a commit that referenced this pull request Feb 14, 2025
ghstack-source-id: 8e24bd2
Pull Request resolved: #146248
@rec
Copy link
Collaborator Author
rec commented Feb 14, 2025

@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

Raymo111 pushed a commit that referenced this pull request Feb 20, 2025
## What's the problem?

The popular `fx.node.map_arg()` and `fx.node.map_aggregate()` apply operations recursively on `dict`s, `tuples`, `list`s, etc, and return a new collection of the same type.

Unfortunately, their base input type is `Argument`, which is [very unspecific indeed](https://github.com/pytorch/pytorch/blob/5d55a6585d5806c2743e92118e663f5abb261895/torch/fx/node.py#L48-L58): most type information is just thrown away at the call site of either of these functions, as far as the type checker goes.

As `torch` moves to a more typed code base, this would force innocent, unsuspecting developers to add logically unnecessary casts or `# type: ignore` statements.

## What's the solution?

Making these two `node.map_*` functions generic on the first argument and return type means that type information is preserved for the type checker. (The signature of the other parameter, the function that visits the nodes and subnodes, has not changed, nor should it.)

## Won't it break everything?

It doesn't break the type checker - one place needed an extra hint.

There have been code breakages, resolved one, at least one new one... we'll see!

Pull Request resolved: #146248
Approved by: https://github.com/XuehaiPan, https://github.com/Skylion007
pytorch-bot bot pushed a commit that referenced this pull request Feb 24, 2025
## What's the problem?

The popular `fx.node.map_arg()` and `fx.node.map_aggregate()` apply operations recursively on `dict`s, `tuples`, `list`s, etc, and return a new collection of the same type.

Unfortunately, their base input type is `Argument`, which is [very unspecific indeed](https://github.com/pytorch/pytorch/blob/5d55a6585d5806c2743e92118e663f5abb261895/torch/fx/node.py#L48-L58): most type information is just thrown away at the call site of either of these functions, as far as the type checker goes.

As `torch` moves to a more typed code base, this would force innocent, unsuspecting developers to add logically unnecessary casts or `# type: ignore` statements.

## What's the solution?

Making these two `node.map_*` functions generic on the first argument and return type means that type information is preserved for the type checker. (The signature of the other parameter, the function that visits the nodes and subnodes, has not changed, nor should it.)

## Won't it break everything?

It doesn't break the type checker - one place needed an extra hint.

There have been code breakages, resolved one, at least one new one... we'll see!

Pull Request resolved: #146248
Approved by: https://github.com/XuehaiPan, https://github.com/Skylion007
desai0007 pushed a commit to desai0007/test-repo-pytorch that referenced this pull request Feb 26, 2025
majing921201 pushed a commit to majing921201/pytorch that referenced this pull request Mar 4, 2025
## What's the problem?

The popular `fx.node.map_arg()` and `fx.node.map_aggregate()` apply operations recursively on `dict`s, `tuples`, `list`s, etc, and return a new collection of the same type.

Unfortunately, their base input type is `Argument`, which is [very unspecific indeed](https://github.com/pytorch/pytorch/blob/5d55a6585d5806c2743e92118e663f5abb261895/torch/fx/node.py#L48-L58): most type information is just thrown away at the call site of either of these functions, as far as the type checker goes.

As `torch` moves to a more typed code base, this would force innocent, unsuspecting developers to add logically unnecessary casts or `# type: ignore` statements.

## What's the solution?

Making these two `node.map_*` functions generic on the first argument and return type means that type information is preserved for the type checker. (The signature of the other parameter, the function that visits the nodes and subnodes, has not changed, nor should it.)

## Won't it break everything?

It doesn't break the type checker - one place needed an extra hint.

There have been code breakages, resolved one, at least one new one... we'll see!

Pull Request resolved: pytorch#146248
Approved by: https://github.com/XuehaiPan, https://github.com/Skylion007
@github-actions github-actions bot deleted the gh/rec/129/head branch March 25, 2025 02:13
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 ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request fx Merged module: dynamo module: typing Related to mypy type annotations oncall: distributed Add this issue/PR to distributed oncall triage queue open source release notes: fx release notes category suppress-api-compatibility-check Suppresses the failures of API backward-compatibility linter (Lint/bc_linter) 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