8000 [dynamo][not ready] polyfill infra for classes by anijain2305 · Pull Request #146678 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[dynamo][not ready] polyfill infra for classes #146678

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

Conversation

Copy link
pytorch-bot bot commented Feb 7, 2025

🔗 Helpful Links

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

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

❌ 71 New Failures, 11 Unrelated Failures

As of commit 7bd428d with merge base 2a55311 (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

UNSTABLE - The following jobs failed but were likely due to flakiness present on trunk and has been marked as unstable:

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

anijain2305 added a commit that referenced this pull request Feb 7, 2025
ghstack-source-id: 8957f6c
Pull Request resolved: #146678
Copy link
Contributor
github-actions bot commented Feb 7, 2025

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang amjames

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Feb 8, 2025
ghstack-source-id: bb50efc
Pull Request resolved: #146678
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang amjames

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Feb 8, 2025
ghstack-source-id: ff918a5
Pull Request resolved: #146678
Comment on lines +50 to +51
@substitute_class(functools.partial, supports_reconstruction=True)
class partial:
Copy link
Collaborator
@XuehaiPan XuehaiPan Feb 8, 2025

Choose a reason for hiding this comment

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

Can't we use closure to implement class-like objects?

@substitute_in_graph(functools.partial, is_embedded_type=True)
def partial(func, /, *args, **keywords):
    def newfunc(*fargs, **fkeywords):
        newkeywords = {**keywords, **fkeywords}
        return func(*args, *fargs, **newkeywords)

    newfunc.func = func
    newfunc.args = args
    newfunc.keywords = keywords
    return newfunc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

8000

In the case of partial, it could be ok. But, I am thinking in terms of more complicated C classes that have many methods. We can still use closure and add method names as attributes to the returned function object. But, I think its more intuitive for the user to have one-to-one mapping for their C++ class to Python class.

Another discussion point (which is not a class vs closure discussion) is how to reconstruct the value. For example, in your above example, if we want to reconstruct the partial object, it will be of type function (and not functools.partial). This causes silent correctness issues because the following code could depend on the returned obj being of type functools.partial. This is not a class vs closure issue though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The C++ pytree functions can return a C++ object PyTreeSpec. The pytree polyfills return a Python object with analog methods while setting can_constant_fold_through=False.

See also:

@dataclass(frozen=True)
class PyTreeSpec:
"""Analog for :class:`optree.PyTreeSpec` in Python."""

def _is_pytreespec_instance(obj: Any, /) -> TypeIs[PyTreeSpec]:
return isinstance(obj, PyTreeSpec)
@substitute_in_graph( # type: ignore[arg-type]
cxx_pytree.tree_flatten,
# We need to disable constant folding here because we want the function to reference the
# PyTreeSpec class defined above, not the one in the C++ module.
can_constant_fold_through=False,
)
def tree_flatten(
tree: PyTree,
is_leaf: Callable[[PyTree], bool] | None = None,
) -> tuple[list[Any], PyTreeSpec]:

Copy link
Collaborator
@XuehaiPan XuehaiPan Feb 9, 2025

Choose a reason for hiding this comment

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

8000

For functools.partial:

class partial:  # copy the pure-Python version from stdlib
    ...


@substitute_in_graph(
    functools.partial.__new__, 
    # We need to disable constant folding here because we want the function to reference the 
    # `partial` class defined above, not the one in the C++ module. 
    can_constant_fold_through=False, 
) 
def partial_new(*args, **kwargs):
    return partial(*args, **kwargs)

def is_partial_instance(obj):
    return instance(obj, partial)  # the `partial` class defined above

One drawback for polyfills is the isinstance(...) and issubclass(...) checks for polyfilled instances. Neither function-polyfill-with-closure or class-polyfill can resolve this issue elegantly. We also need to provide polyfilled is_partial_instance functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are really good pointers. I think it makes sense to just polyfill functools.partial.__new__ and provide a helper to convert the already created functools.partial objects to polyfill'd partial objects.

Let me think more about the reconstruction part.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang amjames

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Feb 9, 2025
ghstack-source-id: 3f459a6
Pull Request resolved: #146678
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang amjames

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Feb 9, 2025
ghstack-source-id: 17a0e0e
Pull Request resolved: #146678
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang amjames

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Feb 9, 2025
ghstack-source-id: d9eef66
Pull Request resolved: #146678
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang amjames

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Feb 9, 2025
ghstack-source-id: 15e5296
Pull Request resolved: #146678
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang amjames

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Feb 9, 2025
ghstack-source-id: 682fc95
Pull Request resolved: #146678
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang amjames

[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Feb 9, 2025
ghstack-source-id: 338c40e
Pull Request resolved: #146678
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang amjames

[ghstack-poisoned]
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 Apr 11, 2025
@github-actions github-actions bot closed this May 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0