-
Notifications
You must be signed in to change notification settings - Fork 24.2k
[pytree] support PyStructSequence types for Python pytree #113258
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
XuehaiPan
wants to merge
143
commits into
gh/XuehaiPan/14/base
Choose a base branch
from
gh/XuehaiPan/14/head
base: gh/XuehaiPan/14/base
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
143 commits
Select commit
Hold shift + click to select a range
2fc3da2
[pytree] support PyStructSequence types for Python pytree
XuehaiPan 62f0b8d
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan 6a294a8
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan 8f546f7
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan 43a2325
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan ebc1b27
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan cf1ae5e
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan 8475b97
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan d89d657
Update on "[pytree] support PyStructSequence types for Python pytree&quo
10000
t;
XuehaiPan 2eb6fff
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan c5fa703
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan 9a940b5
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan 380cfd0
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan e0a01e0
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan aa066d2
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan d9d16a7
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan 14297e9
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan ebc844c
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan 26faa30
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan 845f037
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan 05e7983
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan bc3ca2f
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan f0baf18
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan a61ff5a
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan 6f2be9b
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan ec64d86
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan 5e31000
Update on "[pytree] support PyStructSequence types for Python pytree"
XuehaiPan ed91b71
Update
XuehaiPan c5431ae
Update
XuehaiPan 28605bd
Update
XuehaiPan 484f024
Update
XuehaiPan 28d82e5
Update
XuehaiPan e4224e7
Update
XuehaiPan 78adeb4
Update
XuehaiPan 27050fe
Update
XuehaiPan b0c0291
Update
XuehaiPan 2b0c0d3
Update
XuehaiPan 59ee2c8
Update
XuehaiPan 0bb2611
Update
XuehaiPan 3a8a3c4
Update
XuehaiPan 3bc59c4
Update
XuehaiPan d103d6f
Update
XuehaiPan baf9639
Update
XuehaiPan 2705eb0
Update
XuehaiPan ab26fb5
Update
XuehaiPan 6392274
Update
XuehaiPan 9963e2b
Update
XuehaiPan a0a7275
Update
XuehaiPan f692809
Update
XuehaiPan 312b879
Update
XuehaiPan 6a3a30b
Update
XuehaiPan 0bde528
Update
XuehaiPan e2fbde8
Update
XuehaiPan 0ccaa9b
Update
XuehaiPan 61762c2
Update
XuehaiPan d622085
Update
XuehaiPan 02e5a90
Update
XuehaiPan a6ae9ba
Update
XuehaiPan 9d6d32d
Update
XuehaiPan d5cb3bb
Update
XuehaiPan 7531a9f
Update
XuehaiPan 81bab69
Update
XuehaiPan d5df9f5
Update
XuehaiPan 0f2580c
Update
XuehaiPan f2429c0
Update
XuehaiPan 35f3b8a
Update
XuehaiPan 5987236
Update
XuehaiPan 83d8183
Update
XuehaiPan 607cc30
Update
XuehaiPan 73f5fb3
Update
XuehaiPan 2af386d
Update
XuehaiPan 15d65d9
Update
XuehaiPan 4fd7c75
Update
XuehaiPan 40dadf3
Update
XuehaiPan a4a45b3
Update
XuehaiPan b2c5515
Update
XuehaiPan c8450bf
Update
XuehaiPan 65c0297
Update
XuehaiPan 92946c4
Update
XuehaiPan 0f4b8fc
Update
XuehaiPan ca5f6df
Update
XuehaiPan 735ac30
Update
XuehaiPan ae21d50
Update
XuehaiPan f5591b0
Update
XuehaiPan d7dd93e
Update
XuehaiPan d3b1ad0
Update
XuehaiPan ff1052b
Update
XuehaiPan baa1b22
Update
XuehaiPan 6c73653
Update
XuehaiPan 188d3ca
Update
XuehaiPan fe8181e
Update
XuehaiPan 6c93d67
Update
XuehaiPan 6cceebd
Update
XuehaiPan 90f2365
Update
XuehaiPan 7faa200
Update
8000
XuehaiPan f251e7c
Update
XuehaiPan ff5523d
Update
XuehaiPan 98a7dab
Update
XuehaiPan 74c7d88
Update
XuehaiPan 97a7ef3
Update
XuehaiPan 23807f0
Update
XuehaiPan 8573d0d
Update
XuehaiPan ee4586e
Update
XuehaiPan d1848b5
Update
XuehaiPan b930161
Update
XuehaiPan bf63d7a
Update
XuehaiPan 7bcc529
Update
XuehaiPan c6dbbee
Update
XuehaiPan c8f6b44
Update
XuehaiPan 2d7f03a
Update
XuehaiPan e9741e4
Update
XuehaiPan a11008d
Update
XuehaiPan 61d14ef
Update
XuehaiPan ecf82cc
Update
XuehaiPan db4ff87
Update
XuehaiPan 0573e00
Update
XuehaiPan 4e9fe91
Update
XuehaiPan 8b03f5c
Update
XuehaiPan 4dd5394
Update
XuehaiPan c2ae349
Update
XuehaiPan 8e2ca8d
Update
XuehaiPan b23377e
Update
XuehaiPan cd7154e
Update
XuehaiPan acff96a
Update
XuehaiPan 58d6cf7
Update
XuehaiPan 9451abd
Update
XuehaiPan 8976c0a
Update
XuehaiPan 67a7b31
Update
XuehaiPan ea3874a
Update
XuehaiPan 84e9321
Update
XuehaiPan c3c1119
Update
XuehaiPan a4d0a3c
Update
XuehaiPan 9f70896
Update
XuehaiPan 4fe3104
Update
XuehaiPan cfc602b
Update
XuehaiPan f009887
Update
XuehaiPan ce0fd35
Update
XuehaiPan 4523c06
Update
XuehaiPan e4184c7
Update
8000
XuehaiPan 7fb72b0
Update
XuehaiPan 056a129
Update
XuehaiPan d7f944e
Update
XuehaiPan 797bb0f
Update
XuehaiPan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,53 @@ | ||
import inspect | ||
import warnings 9E88 span> | ||
from typing_extensions import deprecated | ||
|
||
import torch | ||
from torch.utils._pytree import register_pytree_node, SequenceKey | ||
from torch._C import _return_types as return_types | ||
|
||
|
||
__all__ = ["pytree_register_structseq", "all_return_types"] | ||
|
||
all_return_types = [] | ||
|
||
# error: Module has no attribute "_return_types" | ||
return_types = torch._C._return_types # type: ignore[attr-defined] | ||
all_return_types = [] | ||
|
||
|
||
@deprecated( | ||
"torch.return_types.pytree_register_structseq is now a no-op " | ||
"and will be removed in a future release.", | ||
category=FutureWarning, | ||
) | ||
def pytree_register_structseq(cls): | ||
def structseq_flatten(structseq): | ||
return list(structseq), None | ||
|
||
def structseq_flatten_with_keys(structseq): | ||
values, context = structseq_flatten(structseq) | ||
return [(SequenceKey(i), v) for i, v in enumerate(values)], context | ||
from torch.utils._pytree import is_structseq_class | ||
|
||
def structseq_unflatten(values, context): | ||
return cls(values) | ||
if is_structseq_class(cls): | ||
return | ||
|
||
register_pytree_node( | ||
cls, | ||
structseq_flatten, | ||
structseq_unflatten, | ||
flatten_with_keys_fn=structseq_flatten_with_keys, | ||
) | ||
raise TypeError(f"Class {cls!r} is not a PyStructSequence class.") | ||
|
||
|
||
for name in dir(return_types): | ||
if name.startswith("__"): | ||
_name, _attr = "", None | ||
for _name in dir(return_types): | ||
if _name.startswith("__"): | ||
continue | ||
|
||
_attr = getattr(return_types, name) | ||
globals()[name] = _attr | ||
_attr = getattr(return_types, _name) | ||
globals()[_name] = _attr | ||
|
||
if not name.startswith("_"): | ||
__all__.append(name) | ||
if not _name.startswith("_"): | ||
__all__.append(_name) | ||
all_return_types.append(_attr) | ||
|
||
with warnings.catch_warnings(): | ||
warnings.filterwarnings( | ||
"ignore", | ||
category=FutureWarning, | ||
module=__name__, | ||
append=False, | ||
) | ||
# Today everything in torch.return_types is a structseq, aka a "namedtuple"-like | ||
# thing defined by the Python C-API. We're going to need to modify this when that | ||
# is no longer the case. | ||
# NB: I don't know how to check that something is a "structseq" so we do a fuzzy | ||
# check for tuple | ||
if inspect.isclass(_attr) and issubclass(_attr, tuple): | ||
pytree_register_structseq(_attr) | ||
for _attr in all_return_types: | ||
if isinstance(_attr, type) and issubclass(_attr, tuple): | ||
pytree_register_structseq(_attr) | ||
|
||
del _name, _attr, warnings, deprecated |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need this function ("pytree_register_structseq")? Are you keeping it here just for BC?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, just for BC.