Skip to content

Conversation

@eellison
Copy link
Contributor

@eellison eellison commented Aug 13, 2019

Stack from ghstack:

Follow up to #23886, add the same overload api specified in PEP 484 to module methods to reduce the friction of adding method overloads that was brought up in #23266.

The usage is:

@torch.jit.overload
def add(self, y: int) -> int: ...
@torch.jit.overload
def add(self, y: float) -> float: ...
def add():
   ... 

Differential Revision: D16921304

@pytorchbot pytorchbot added oncall: jit Add this issue/PR to JIT oncall triage queue module: pybind Related to our Python bindings / interactions with other Python libraries labels Aug 13, 2019
eellison pushed a commit that referenced this pull request Aug 13, 2019
ghstack-source-id: 344a8a8
Pull Request resolved: #24259
@eellison eellison requested review from driazati and suo and removed request for suo August 13, 2019 18:47
@eellison eellison changed the title extend torch.jit._overload to modules extend torch.jit._overload to module methods Aug 13, 2019
Follow up to #23886, add the same overload api specified in PEP 484 to module methods to reduce the friction of adding method overloads that was brought up in #23266.

The usage is:
```
@torch.jit.overload
def add(self, y: int) -> int: ...
@torch.jit.overload
def add(self, y: float) -> float: ...
def add():
   ... 
```
Follow up to #23886, add the same overload api specified in PEP 484 to module methods to reduce the friction of adding method overloads that was brought up in #23266.

The usage is:
```
@torch.jit.overload
def add(self, y: int) -> int: ...
@torch.jit.overload
def add(self, y: float) -> float: ...
def add():
   ... 
```
eellison pushed a commit that referenced this pull request Aug 15, 2019
ghstack-source-id: 622771c
Pull Request resolved: #24259
z-a-f pushed a commit that referenced this pull request Aug 15, 2019
Note: This should be landed ONLY after #24259
z-a-f pushed a commit that referenced this pull request Aug 15, 2019
Note: This should be landed ONLY after #24259

ghstack-source-id: d6686c6
Pull Request resolved: #24447
Follow up to #23886, add the same overload api specified in PEP 484 to module methods to reduce the friction of adding method overloads that was brought up in #23266.

The usage is:
```
@torch.jit.overload
def add(self, y: int) -> int: ...
@torch.jit.overload
def add(self, y: float) -> float: ...
def add():
   ... 
```
Follow up to #23886, add the same overload api specified in PEP 484 to module methods to reduce the friction of adding method overloads that was brought up in #23266.

The usage is:
```
@torch.jit.overload
def add(self, y: int) -> int: ...
@torch.jit.overload
def add(self, y: float) -> float: ...
def add():
   ... 
```
eellison pushed a commit that referenced this pull request Aug 16, 2019
ghstack-source-id: af601e7
Pull Request resolved: #24259
return self.hello("hi"), self.hello(.5)

w = CompileOverloadError()
with self.assertRaisesRegex(Exception, "but instead found type \'str\'"):
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't a str work here since there's an overload for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an error here because you're trying to do x + 1 with a str

class_name_map = {}
_overloaded_methods[qual_name] = class_name_map

class_name = get_class_name(func)
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't seem like a safe way to globally identify classes since it will break for nested classes with the same class name and same function names since the name is not fully qualified

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, i can add a check that the line number is the same. This is still internal-only tho.


methods = methods + tuple(exported)

overload_name_mappings = dict(getattr(mod, "__overloads__", {}))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you delete the __overloads__ and replace it with these new decorators instead of adding both together? (or do that in a stacked PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea i was going to do that as a follow up, since that likely breaks the ScriptModules in quantization


for orig_fn, overload_fns in overloads:
orig_ast = torch.jit.get_jit_def(orig_fn, self_name="ScriptModule")
names = list(map(lambda i: orig_ast.name().name + "__" + str(i), range(len(overload_fns))))
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the overloaded methods aren't directly callable why do they need to be added as normal methods with this mangled name to the ScriptModule? The class could hold its schemas for each overload and check that in OverloadedMethodValue instead of what it's doing now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The normal name isn't being added, only the mangled ones are being added.

Defining the mangled ones lazily would be an improvement but I don't think it's necessary for this PR. It would also complicate export logic / possibly other things, and would require removing the other __overload__ mechanism first.

z-a-f pushed a commit that referenced this pull request Aug 16, 2019
z-a-f pushed a commit that referenced this pull request Aug 16, 2019
z-a-f pushed a commit that referenced this pull request Aug 16, 2019
Note: This should be landed ONLY after #24259

ghstack-source-id: f03afcf
Pull Request resolved: #24447
Follow up to #23886, add the same overload api specified in PEP 484 to module methods to reduce the friction of adding method overloads that was brought up in #23266.

The usage is:
```
@torch.jit.overload
def add(self, y: int) -> int: ...
@torch.jit.overload
def add(self, y: float) -> float: ...
def add():
   ... 
```
Follow up to #23886, add the same overload api specified in PEP 484 to module methods to reduce the friction of adding method overloads that was brought up in #23266.

The usage is:
```
@torch.jit.overload
def add(self, y: int) -> int: ...
@torch.jit.overload
def add(self, y: float) -> float: ...
def add():
   ... 
```
eellison pushed a commit that referenced this pull request Aug 16, 2019
ghstack-source-id: c4fbf4b
Pull Request resolved: #24259
z-a-f pushed a commit that referenced this pull request Aug 19, 2019
z-a-f pushed a commit that referenced this pull request Aug 19, 2019
z-a-f pushed a commit that referenced this pull request Aug 19, 2019
Note: This should be landed ONLY after #24259

ghstack-source-id: 3435595
Pull Request resolved: #24447
Follow up to #23886, add the same overload api specified in PEP 484 to module methods to reduce the friction of adding method overloads that was brought up in #23266.

The usage is:
```
@torch.jit.overload
def add(self, y: int) -> int: ...
@torch.jit.overload
def add(self, y: float) -> float: ...
def add():
   ... 
```

Differential Revision: [D16921304](https://our.internmc.facebook.com/intern/diff/D16921304)
Follow up to #23886, add the same overload api specified in PEP 484 to module methods to reduce the friction of adding method overloads that was brought up in #23266.

The usage is:
```
@torch.jit.overload
def add(self, y: int) -> int: ...
@torch.jit.overload
def add(self, y: float) -> float: ...
def add():
   ... 
```

Differential Revision: [D16921304](https://our.internmc.facebook.com/intern/diff/D16921304)
eellison pushed a commit that referenced this pull request Aug 20, 2019
ghstack-source-id: bc26c02
Pull Request resolved: #24259
@zou3519 zou3519 deleted the gh/eellison/10/head branch August 20, 2019 23:47
z-a-f pushed a commit that referenced this pull request Aug 21, 2019
@facebook-github-bot
Copy link
Contributor

@eellison merged this pull request in 8e3c021.

z-a-f pushed a commit that referenced this pull request Aug 21, 2019
z-a-f pushed a commit that referenced this pull request Aug 23, 2019
z-a-f pushed a commit that referenced this pull request Aug 23, 2019
z-a-f pushed a commit that referenced this pull request Aug 25, 2019
z-a-f pushed a commit that referenced this pull request Aug 25, 2019
facebook-github-bot pushed a commit that referenced this pull request Aug 26, 2019
Summary:
Pull Request resolved: #24447

Note: This should be landed ONLY after #24259

Pull Request resolved: #24447

Differential Revision: D16846006

Test Plan: Imported from OSS

Pulled By: zafartahirov

fbshipit-source-id: 458fd65279d98cb177ef206240d24dfcbc8d1c1b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: pybind Related to our Python bindings / interactions with other Python libraries oncall: jit Add this issue/PR to JIT oncall triage queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants