Skip to content

Conversation

@SplitInfinity
Copy link

@SplitInfinity SplitInfinity commented Sep 22, 2020

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

**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]
SplitInfinity pushed a commit that referenced this pull request Sep 22, 2020
**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
@facebook-github-bot facebook-github-bot added the oncall: jit Add this issue/PR to JIT oncall triage queue label Sep 22, 2020
@SplitInfinity
Copy link
Author

This is a different approach to #42562 than #42988 that requires less changes but uses the same approach that we use for methods and functions (take Python objects representing the default values, convert them to IValues, and attach them to the schemas after compilation).

@dr-ci
Copy link

dr-ci bot commented Sep 22, 2020

💊 CI failures summary and remediations

As of commit 20ba7f7 (more details on the Dr. CI page):


  • 1/1 failures possibly* introduced in this PR
    • 1/1 non-CircleCI failure(s)

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.

See how this bot performed.

This comment has been revised 12 times.

Copy link
Contributor

@eellison eellison left a comment

Choose a reason for hiding this comment

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

🚢


# 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}
Copy link
Contributor

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

Copy link
Author

@SplitInfinity SplitInfinity Sep 22, 2020

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()) {
Copy link
Contributor

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

Copy link
Author

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
Copy link
Contributor

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?

Copy link
Author

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__
Copy link
Contributor

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?

Copy link
Author

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]
SplitInfinity pushed a commit that referenced this pull request Sep 22, 2020
**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
Copy link

codecov bot commented Sep 22, 2020

Codecov Report

Merging #45098 into gh/splitinfinity/47/base will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@                    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           
Impacted Files Coverage Δ
torch/jit/_script.py 91.35% <100.00%> (+0.02%) ⬆️
torch/jit/frontend.py 90.98% <100.00%> (+0.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c253b10...20ba7f7. Read the comment docs.

@facebook-github-bot
Copy link
Contributor

@SplitInfinity merged this pull request in e045119.

@facebook-github-bot
Copy link
Contributor

@SplitInfinity merged this pull request in e045119.

@facebook-github-bot facebook-github-bot deleted the gh/splitinfinity/47/head branch September 26, 2020 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants