-
Notifications
You must be signed in to change notification settings - Fork 26.3k
[JIT] Add default arguments for class types #45098
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
Conversation
**Summary** This commit adds support for default arguments in methods of class types. Similar to how default arguments are supported for regular script functions and methods on scripted modules, default values are retrieved from the definition of a TorchScript class in Python as Python objects, converted to IValues, and then attached to the schemas of already compiled class methods. **Test Plan** This commit adds a set of new tests to TestClassType to test default arguments. **Fixes** This commit fixes #42562. [ghstack-poisoned]
**Summary** This commit adds support for default arguments in methods of class types. Similar to how default arguments are supported for regular script functions and methods on scripted modules, default values are retrieved from the definition of a TorchScript class in Python as Python objects, converted to IValues, and then attached to the schemas of already compiled class methods. **Test Plan** This commit adds a set of new tests to TestClassType to test default arguments. **Fixes** This commit fixes #42562. ghstack-source-id: 9987789 Pull Request resolved: #45098
💊 CI failures summary and remediationsAs of commit 20ba7f7 (more details on the Dr. CI page):
Extra GitHub checks: 1 failed
This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group. This comment has been revised 12 times. |
eellison
left a comment
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.
🚢
torch/jit/frontend.py
Outdated
|
|
||
| # Get method defaults. Property defaults do not need to be considered | ||
| # because setters cannot be invoked without a value. | ||
| defaults = {method[0]: get_default_args(method[1]) for method in methods} |
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.
nit maybe: defaults = {method_name: get_default_args(method_impl)) for method_name, method_impl in methods} to be a little more readable
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.
Woah, I didn't know you could unpack tuples like that inside a comprehension but it makes sense. Cool!
**Summary** This commit adds support for default arguments in methods of class types. Similar to how default arguments are supported for regular script functions and methods on scripted modules, default values are retrieved from the definition of a TorchScript class in Python as Python objects, converted to IValues, and then attached to the schemas of already compiled class methods. **Test Plan** This commit adds a set of new tests to TestClassType to test default arguments. **Fixes** This commit fixes #42562. [ghstack-poisoned]
| auto def_name = (*defs_it).name().name(); | ||
| // If the method is not in the defaults map, assume there are | ||
| // no default arguments for it. | ||
| if (defaults.find(def_name) == defaults.end()) { |
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.
nit:
save results of defaults.find(def_name) and reuse on line 1364 to look up only once
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.
Nice catch.
| A Dict[str, Dict[str, Any]] which maps each method name to a Dict[str, Any] | ||
| that maps each argument name to its default value. | ||
| """ | ||
| # Get methods (except static methods because those are compiled separately as |
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.
OOC, are default arg values for static methods already supported?
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.
Yeah, static methods of classes are actually compiled as if they were standalone methods (since there is no self argument or associated instance).
| cls, | ||
| predicate=lambda m: (inspect.ismethod(m) or inspect.isfunction(m)) | ||
| and not is_static_fn(cls, m.__name__) | ||
| and m.__name__ in cls.__dict__ |
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.
Could you shed some light on why needing to check m.__name__ in cls.__dict__? in case user doesn't want to expose this method?
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.
To be honest I copied this from get_jit_class_def.
**Summary** This commit adds support for default arguments in methods of class types. Similar to how default arguments are supported for regular script functions and methods on scripted modules, default values are retrieved from the definition of a TorchScript class in Python as Python objects, converted to IValues, and then attached to the schemas of already compiled class methods. **Test Plan** This commit adds a set of new tests to TestClassType to test default arguments. **Fixes** This commit fixes #42562. Differential Revision: [D23844769](https://our.internmc.facebook.com/intern/diff/D23844769) [ghstack-poisoned]
**Summary** This commit adds support for default arguments in methods of class types. Similar to how default arguments are supported for regular script functions and methods on scripted modules, default values are retrieved from the definition of a TorchScript class in Python as Python objects, converted to IValues, and then attached to the schemas of already compiled class methods. **Test Plan** This commit adds a set of new tests to TestClassType to test default arguments. **Fixes** This commit fixes #42562. ghstack-source-id: 90b2eeb Pull Request resolved: #45098
Codecov Report
@@ Coverage Diff @@
## gh/splitinfinity/47/base #45098 +/- ##
=========================================================
Coverage 67.87% 67.88%
=========================================================
Files 384 384
Lines 50095 50100 +5
=========================================================
+ Hits 34004 34009 +5
Misses 16091 16091
Continue to review full report at Codecov.
|
|
@SplitInfinity merged this pull request in e045119. |
|
@SplitInfinity merged this pull request in e045119. |
Stack from ghstack:
Summary
This commit adds support for default arguments in methods of class
types. Similar to how default arguments are supported for regular
script functions and methods on scripted modules, default values are
retrieved from the definition of a TorchScript class in Python as Python
objects, converted to IValues, and then attached to the schemas of
already compiled class methods.
Test Plan
This commit adds a set of new tests to TestClassType to test default
arguments.
Fixes
This commit fixes #42562.
Differential Revision: D23844769