-
Notifications
You must be signed in to change notification settings - Fork 26.3k
[JIT] Add property support to TorchScript classes #42389
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 properties to TorchScript classes, specifically for getters and setters. They are implemented essentially as pointers to the methods that the corresponding decorators decorate, which are treated like regular class methods. Deleters for properties are considered to be out of scope (and probably useless for TorchScript anyway). **Test Plan** This commit adds a unit test for a class with a property that has both getter and setter and one that has only a getter. `python test/test_jit.py TestClassType.test_properties` [ghstack-poisoned]
**Summary** This commit adds support for properties to TorchScript classes, specifically for getters and setters. They are implemented essentially as pointers to the methods that the corresponding decorators decorate, which are treated like regular class methods. Deleters for properties are considered to be out of scope (and probably useless for TorchScript anyway). **Test Plan** This commit adds a unit test for a class with a property that has both getter and setter and one that has only a getter. `python test/test_jit.py TestClassType.test_properties` Differential Revision: [D22880232](https://our.internmc.facebook.com/intern/diff/D22880232) [ghstack-poisoned]
💊 CI failures summary and remediationsAs of commit d2b03c9 (more details on the Dr. CI page): 💚 💚 Looks good so far! There are no failures yet. 💚 💚 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 22 times. |
**Summary** This commit adds support for properties to TorchScript classes, specifically for getters and setters. They are implemented essentially as pointers to the methods that the corresponding decorators decorate, which are treated like regular class methods. Deleters for properties are considered to be out of scope (and probably useless for TorchScript anyway). **Test Plan** This commit adds a unit test for a class with a property that has both getter and setter and one that has only a getter. `python test/test_jit.py TestClassType.test_properties` Differential Revision: [D22880232](https://our.internmc.facebook.com/intern/diff/D22880232) [ghstack-poisoned]
**Summary** This commit adds support for properties to TorchScript classes, specifically for getters and setters. They are implemented essentially as pointers to the methods that the corresponding decorators decorate, which are treated like regular class methods. Deleters for properties are considered to be out of scope (and probably useless for TorchScript anyway). **Test Plan** This commit adds a unit test for a class with a property that has both getter and setter and one that has only a getter. `python test/test_jit.py TestClassType.test_properties` Differential Revision: [D22880232](https://our.internmc.facebook.com/intern/diff/D22880232) [ghstack-poisoned]
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.
Cool, this looks great ! I think this is logically good to go, but there are a couple refactorings that would be good to do before landing. Those should be pretty mechanical/easy to do though.
| } | ||
|
|
||
| // Version of define but with support for properties. | ||
| std::vector<Function*> define( |
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.
can we unify this with the constructor below and adjust the callsites or make properties / propResolvers optional ?
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.
Done. Although there are a bunch of callsites now where we compile free functions but still have to add empty {} for properties/property resolvers. I thought of adding them to the end of the argument list so as to avoid modifying all those callsites but it didn't seem as clean.
| if (isTorch(select.value()) && name == "Tensor") { | ||
| return "Tensor"; | ||
| } else if (isTorch(select.value()) && name == "device") { | ||
| return "Device"; |
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.
is there a reason for this change to be included ?
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.
Internal fbcode Detectron2 tests break without it because they use -> torch.device as a return type annotation.
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.
That should work bc of https://github.com/pytorch/pytorch/blob/master/torch/jit/annotations.py#L313. I think what's happening here is the resolver for properties is not being set properly. We should be closing over torch bc it's used in the function, but we're not.
Sometimes with python wrapped functions you have to unpack the wrapped function to do resolution correctly, as in #40468.
Could you add a test where a property getterr function calls into a free-function ?
def foo(x):
return x + 1
...
@property
def attr(self):
return foo(self.a)
that should check whether the resolver is being properly set
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.
short repro:
class Boxes(object):
@property
def device(self) -> torch.device:
return 1 + hi
closure = torch._jit_internal.get_closure(Boxes.device.fget)
# torch is closed over
print(closure)
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.
I added a test for this but I'm not sure if I did it right...
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.
No, the test looks good. We shouldn't need to add the torch.device thing, it means something else has gone wrong... It would be nice to fix but if resolution in general is working, then I think we can land this.
Could you remove this "torch.device" logic here, and then in the D2 test change the property to look like ?
from torch import device
@property
def foo() -> device:
...
This should workaround the problem without added unnecessary stuff to our code base...
**Summary** This commit adds support for properties to TorchScript classes, specifically for getters and setters. They are implemented essentially as pointers to the methods that the corresponding decorators decorate, which are treated like regular class methods. Deleters for properties are considered to be out of scope (and probably useless for TorchScript anyway). **Test Plan** This commit adds a unit test for a class with a property that has both getter and setter and one that has only a getter. `python test/test_jit.py TestClassType.test_properties` Differential Revision: [D22880232](https://our.internmc.facebook.com/intern/diff/D22880232) [ghstack-poisoned]
**Summary** This commit adds support for properties to TorchScript classes, specifically for getters and setters. They are implemented essentially as pointers to the methods that the corresponding decorators decorate, which are treated like regular class methods. Deleters for properties are considered to be out of scope (and probably useless for TorchScript anyway). **Test Plan** This commit adds a unit test for a class with a property that has both getter and setter and one that has only a getter. `python test/test_jit.py TestClassType.test_properties` ghstack-source-id: 678bf1f Pull Request resolved: #42389
|
Hey, I addressed all of the comments but I had to amend my commit because I didn't want |
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.
Sweet, looks great!! One comment about the torch.device thing.
Also, it would be maybe be nice to land this with the property support for TorchScript modules...
| if (isTorch(select.value()) && name == "Tensor") { | ||
| return "Tensor"; | ||
| } else if (isTorch(select.value()) && name == "device") { | ||
| return "Device"; |
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.
No, the test looks good. We shouldn't need to add the torch.device thing, it means something else has gone wrong... It would be nice to fix but if resolution in general is working, then I think we can land this.
Could you remove this "torch.device" logic here, and then in the D2 test change the property to look like ?
from torch import device
@property
def foo() -> device:
...
This should workaround the problem without added unnecessary stuff to our code base...
**Summary** This commit adds support for properties to TorchScript classes, specifically for getters and setters. They are implemented essentially as pointers to the methods that the corresponding decorators decorate, which are treated like regular class methods. Deleters for properties are considered to be out of scope (and probably useless for TorchScript anyway). **Test Plan** This commit adds a unit test for a class with a property that has both getter and setter and one that has only a getter. `python test/test_jit.py TestClassType.test_properties` Differential Revision: [D22880232](https://our.internmc.facebook.com/intern/diff/D22880232) [ghstack-poisoned]
|
Okay, I reverted the |
**Summary** This commit adds support for properties to TorchScript classes, specifically for getters and setters. They are implemented essentially as pointers to the methods that the corresponding decorators decorate, which are treated like regular class methods. Deleters for properties are considered to be out of scope (and probably useless for TorchScript anyway). **Test Plan** This commit adds a unit test for a class with a property that has both getter and setter and one that has only a getter. `python test/test_jit.py TestClassType.test_properties` Differential Revision: [D22880232](https://our.internmc.facebook.com/intern/diff/D22880232) [ghstack-poisoned]
Summary: Pull Request resolved: #42389 **Summary** This commit adds support for properties to TorchScript classes, specifically for getters and setters. They are implemented essentially as pointers to the methods that the corresponding decorators decorate, which are treated like regular class methods. Deleters for properties are considered to be out of scope (and probably useless for TorchScript anyway). **Test Plan** This commit adds a unit test for a class with a property that has both getter and setter and one that has only a getter. `python test/test_jit.py TestClassType.test_properties` Test Plan: Imported from OSS Reviewed By: eellison, ppwwyyxx Differential Revision: D22880232 Pulled By: SplitInfinity fbshipit-source-id: 4828640f4234cb3b0d4f3da4872a75fbf519e5b0
**Summary** This commit adds support for properties to TorchScript classes, specifically for getters and setters. They are implemented essentially as pointers to the methods that the corresponding decorators decorate, which are treated like regular class methods. Deleters for properties are considered to be out of scope (and probably useless for TorchScript anyway). **Test Plan** This commit adds a unit test for a class with a property that has both getter and setter and one that has only a getter. `python test/test_jit.py TestClassType.test_properties` ghstack-source-id: 75e42d4 Pull Request resolved: pytorch/pytorch#42389
Stack from ghstack:
Summary
This commit adds support for properties to TorchScript classes,
specifically for getters and setters. They are implemented essentially
as pointers to the methods that the corresponding decorators decorate,
which are treated like regular class methods. Deleters for properties
are considered to be out of scope (and probably useless for TorchScript
anyway).
Test Plan
This commit adds a unit test for a class with a property that has both
getter and setter and one that has only a getter.
python test/test_jit.py TestClassType.test_propertiesDifferential Revision: D22880232