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
Support dataclasses in TorchScript #72901
Comments
Hi Nora, This is a good feature to have. Thanks for proposing and offering to contribute PR. I think the second approach is preferable if feasible to implement, given that it does not add extra APIs to torch.jit namespace and is more flexible. I guess implementation-wise it would be: 1. detect is a dataclass; 2. strip @DataClass generated methods that are not torchscript compatible; 3. add new ones that is torchscript compatible? Is that what you have in mind? |
Maybe related proposal: #61686 - remove the need of official Module's super call and make initialization of module's attributes lazy in getattr |
@qihqi On reflection I think you're right, the second approach would probably be better. I've started reading through the relevant parts of the PyTorch source as well as the CPython implementation of @vadimkantorov That's also a nice proposal, but I don't think it's directly related to this one. We could implement dataclasses in TorchScript without removing the need to call I do think it would also be nice to support subclassing/inheritance in TorchScript, and if it looks like it wouldn't be that hard to implement, I might go ahead and make a PR for that too. One thing at a time though. |
If I understand, dataclasses auto-implement Yep, my comments mainly concern regular eager PyTorch, not even TorchScript |
@norabelrose Feel free to start and please add me to PR review when ready. @vadimkantorov Q: Why do you want a dataclass that is subclass of |
After looking at the source code a little bit, it seems like the tricky part of implementing this feature is that the Python frontend assumes and enforces the invariant that all JIT-able code has to have a definite source file, presumably for the purpose of generating error messages. If we synthesize the magic methods for dataclasses then these methods will (probably?) not have a source file, so it looks like we need to get rid of that assumption. As a nice side effect, this should allow for JIT compiling classes inside of IPython notebooks, which currently fails. I'll work on this tonight and over the weekend. EDIT: Never mind about the IPython notebook thing, it seems that there are other reasons why that still wouldn't work with this fix. |
Btw we can also generate source from torchscript IR once we have them. This codepath is used when saving a module to .pt files. |
This is frequent for modules that are composed of other modules, so their constructor is trivially setting the passed arguments into the fields. Another instance when mandatory init call is hurting is when we want to change some existing base class to derive from Module (e.g. Optimizer) without forcing init call to be introduced in all derived modules. Another instance is making Transform into Modules post-factum: #31009 |
I do agree with @vadimkantorov that supporting nn.Module subclasses as dataclass would be useful. It's mainly to cut down on repetitive/boilerplate |
@qihqi I think my PR is ready for you to take a look at. I've implemented all the basic features and added some unit tests. |
Summary: Fixes #72901. Since we can't get access to the source code for synthesized magic methods on dataclasses, we have to synthesize our own versions. `torch/jit/_dataclass_impls.py` has the code that does this. What's supported - Synthesized `__init__`, `__eq__`, and the comparison magic methods when `order=True` is set on the dataclass decorator - Default values for fields - `__post_init__`, including using `InitVar` fields inside of `__post_init__`, on Python 3.8+ - Overriding `__eq__` or any of the comparison magic methods to provide your own implementation What's not supported - Default factory initializers for fields - Frozen dataclasses - `InitVar` on Python 3.7 - `__repr__` and `__hash__` (these are actually implemented, but the TorchScript interpreter won't call them) - Using the `!=` operator on dataclasses inside TorchScript; this is because TorchScript requires that you implement `__ne__` to use this operator, whereas in regular Python the `!=` operator will resolve to the negation of whatever is returned by `__eq__` if there's no `__ne__`. Dataclasses don't actually synthesize an `__ne__` method for this reason. I've been toying with different ways to fix this but `!=` is not working in this PR at the moment. qihqi Pull Request resolved: #73066 Reviewed By: mrshenli Differential Revision: D34398107 Pulled By: qihqi fbshipit-source-id: f5a792555c88f3631f97837a96687e4890660a32
Summary: Fixes pytorch/pytorch#72901. Since we can't get access to the source code for synthesized magic methods on dataclasses, we have to synthesize our own versions. `torch/jit/_dataclass_impls.py` has the code that does this. What's supported - Synthesized `__init__`, `__eq__`, and the comparison magic methods when `order=True` is set on the dataclass decorator - Default values for fields - `__post_init__`, including using `InitVar` fields inside of `__post_init__`, on Python 3.8+ - Overriding `__eq__` or any of the comparison magic methods to provide your own implementation What's not supported - Default factory initializers for fields - Frozen dataclasses - `InitVar` on Python 3.7 - `__repr__` and `__hash__` (these are actually implemented, but the TorchScript interpreter won't call them) - Using the `!=` operator on dataclasses inside TorchScript; this is because TorchScript requires that you implement `__ne__` to use this operator, whereas in regular Python the `!=` operator will resolve to the negation of whatever is returned by `__eq__` if there's no `__ne__`. Dataclasses don't actually synthesize an `__ne__` method for this reason. I've been toying with different ways to fix this but `!=` is not working in this PR at the moment. qihqi Pull Request resolved: pytorch/pytorch#73066 Reviewed By: mrshenli Differential Revision: D34398107 Pulled By: qihqi fbshipit-source-id: f5a792555c88f3631f97837a96687e4890660a32 (cherry picked from commit ea7f077dc49a4ee75ca0d1409aedd85228952881)
Summary: Fixes pytorch/pytorch#72901. Since we can't get access to the source code for synthesized magic methods on dataclasses, we have to synthesize our own versions. `torch/jit/_dataclass_impls.py` has the code that does this. What's supported - Synthesized `__init__`, `__eq__`, and the comparison magic methods when `order=True` is set on the dataclass decorator - Default values for fields - `__post_init__`, including using `InitVar` fields inside of `__post_init__`, on Python 3.8+ - Overriding `__eq__` or any of the comparison magic methods to provide your own implementation What's not supported - Default factory initializers for fields - Frozen dataclasses - `InitVar` on Python 3.7 - `__repr__` and `__hash__` (these are actually implemented, but the TorchScript interpreter won't call them) - Using the `!=` operator on dataclasses inside TorchScript; this is because TorchScript requires that you implement `__ne__` to use this operator, whereas in regular Python the `!=` operator will resolve to the negation of whatever is returned by `__eq__` if there's no `__ne__`. Dataclasses don't actually synthesize an `__ne__` method for this reason. I've been toying with different ways to fix this but `!=` is not working in this PR at the moment. qihqi Pull Request resolved: pytorch/pytorch#73066 Reviewed By: mrshenli Differential Revision: D34398107 Pulled By: qihqi fbshipit-source-id: f5a792555c88f3631f97837a96687e4890660a32 (cherry picked from commit ea7f077dc49a4ee75ca0d1409aedd85228952881)
My PR fixing this issue got merged, but then reverted a few days later with a commit stating that it "broke fluent2 tests." I don't know what fluent2 is- I'm assuming some internal Meta thing- but I'm honestly a bit upset that this was reverted without any explanation as to how I could fix the issue. If @qihqi or anyone else could help me get this re-landed I'd greatly appreciate it. I don't want my work to have gone to waste. |
Hi @norabelrose; Yes sorry about that. Yes I'll try to get it relanded.
Totally understand... I am also in the process of figuring out what tickled it (it was a segfault in their end, which is super wierd given this is a pure python change). Anyways I'll keep you posted. Apologies again. I'll keep this issue open to track progress. |
@qihqi Thank you for this, I really appreciate it. |
Update so far:
I am in process of isolating a test case. My hunch is that either the fact that an Enum is involved or that B has no declared members has something to do with it. |
@qihqi Oh if I had to guess the problem is that |
hmmm weird, so removing
made the test pass. It's like they explicitly want the OSError somehow... |
Hi @norabelrose I think this works: #74353 please take a look. If you prefer to patch it submit as your PR it's OK too. Long story short on the findings:
Just patching the bug in Current solution works because, if user don't explicitly call |
@qihqi Thanks for your work on this, this seems like a fine solution. I'm happy to let your version of the PR be merged. |
This time it's a github CI: https://github.com/pytorch/pytorch/runs/5763579312?check_suite_focus=true It has to do with gpu tests, I am still in process of figuring out what happened (didn't manage to repro locally yet). |
…orch#74353) (pytorch#76771) Summary: Pull Request resolved: pytorch#76771 Pull Request resolved: pytorch#74353 Repatched `d00de0d43598522b8f6ab2de553b6aaf6768faa5` by Nora Belrose (norabelrose). With following changes: * Register fake source of generated methods in linecache so that inspect.get_source will succeed. * this patching is only triggered if the given dataclass passed to torch.jit.script previously. Effectively we make this feature opt-in. ## Original Summary: Fixes pytorch#72901. Since we can't get access to the source code for synthesized magic methods on dataclasses, we have to synthesize our own versions. torch/jit/_dataclass_impls.py has the code that does this. What's supported Synthesized __init__, __eq__, and the comparison magic methods when order=True is set on the dataclass decorator Default values for fields __post_init__, including using InitVar fields inside of __post_init__, on Python 3.8+ Overriding __eq__ or any of the comparison magic methods to provide your own implementation What's not supported Default factory initializers for fields Frozen dataclasses InitVar on Python 3.7 __repr__ and __hash__ (these are actually implemented, but the TorchScript interpreter won't call them) Using the != operator on dataclasses inside TorchScript; this is because TorchScript requires that you implement __ne__ to use this operator, whereas in regular Python the != operator will resolve to the negation of whatever is returned by __eq__ if there's no __ne__. Dataclasses don't actually synthesize an __ne__ method for this reason. I've been toying with different ways to fix this but != is not working in this PR at the moment. Pull Request resolved: pytorch#74889 Test Plan: unittest Also run previously failed test: ``` buck test mode/dev-nosan //fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests -- --exact 'fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests - test_mixmatch_multiclass (fblearner.flow.projects.fluent2.definition.transformers.contrib.faim.test.faim_mixmatch_test.TestFaimTransformerMixMatch)' ``` passes Reviewed By: zhxchen17 Differential Revision: D35206262 Pulled By: qihqi fbshipit-source-id: ed96735d11b1c9a996dc68c2cfc5bdff7797822b
…orch#74353) (pytorch#76771) Summary: Pull Request resolved: pytorch#76771 Pull Request resolved: pytorch#74353 Repatched `d00de0d43598522b8f6ab2de553b6aaf6768faa5` by Nora Belrose (norabelrose). With following changes: * Register fake source of generated methods in linecache so that inspect.get_source will succeed. * this patching is only triggered if the given dataclass passed to torch.jit.script previously. Effectively we make this feature opt-in. ## Original Summary: Fixes pytorch#72901. Since we can't get access to the source code for synthesized magic methods on dataclasses, we have to synthesize our own versions. torch/jit/_dataclass_impls.py has the code that does this. What's supported Synthesized __init__, __eq__, and the comparison magic methods when order=True is set on the dataclass decorator Default values for fields __post_init__, including using InitVar fields inside of __post_init__, on Python 3.8+ Overriding __eq__ or any of the comparison magic methods to provide your own implementation What's not supported Default factory initializers for fields Frozen dataclasses InitVar on Python 3.7 __repr__ and __hash__ (these are actually implemented, but the TorchScript interpreter won't call them) Using the != operator on dataclasses inside TorchScript; this is because TorchScript requires that you implement __ne__ to use this operator, whereas in regular Python the != operator will resolve to the negation of whatever is returned by __eq__ if there's no __ne__. Dataclasses don't actually synthesize an __ne__ method for this reason. I've been toying with different ways to fix this but != is not working in this PR at the moment. Pull Request resolved: pytorch#74889 Test Plan: unittest Also run previously failed test: ``` buck test mode/dev-nosan //fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests -- --exact 'fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests - test_mixmatch_multiclass (fblearner.flow.projects.fluent2.definition.transformers.contrib.faim.test.faim_mixmatch_test.TestFaimTransformerMixMatch)' ``` passes Reviewed By: zhxchen17 Differential Revision: D35206262 Pulled By: qihqi fbshipit-source-id: f25380d72306205cfaf41cd99b2d6c98830e9bed
…orch#74353) (pytorch#76771) Summary: Pull Request resolved: pytorch#76771 Pull Request resolved: pytorch#74353 Repatched `d00de0d43598522b8f6ab2de553b6aaf6768faa5` by Nora Belrose (norabelrose). With following changes: * Register fake source of generated methods in linecache so that inspect.get_source will succeed. * this patching is only triggered if the given dataclass passed to torch.jit.script previously. Effectively we make this feature opt-in. ## Original Summary: Fixes pytorch#72901. Since we can't get access to the source code for synthesized magic methods on dataclasses, we have to synthesize our own versions. torch/jit/_dataclass_impls.py has the code that does this. What's supported Synthesized __init__, __eq__, and the comparison magic methods when order=True is set on the dataclass decorator Default values for fields __post_init__, including using InitVar fields inside of __post_init__, on Python 3.8+ Overriding __eq__ or any of the comparison magic methods to provide your own implementation What's not supported Default factory initializers for fields Frozen dataclasses InitVar on Python 3.7 __repr__ and __hash__ (these are actually implemented, but the TorchScript interpreter won't call them) Using the != operator on dataclasses inside TorchScript; this is because TorchScript requires that you implement __ne__ to use this operator, whereas in regular Python the != operator will resolve to the negation of whatever is returned by __eq__ if there's no __ne__. Dataclasses don't actually synthesize an __ne__ method for this reason. I've been toying with different ways to fix this but != is not working in this PR at the moment. Pull Request resolved: pytorch#74889 Test Plan: unittest Also run previously failed test: ``` buck test mode/dev-nosan //fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests -- --exact 'fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests - test_mixmatch_multiclass (fblearner.flow.projects.fluent2.definition.transformers.contrib.faim.test.faim_mixmatch_test.TestFaimTransformerMixMatch)' ``` passes Reviewed By: zhxchen17 Differential Revision: D35206262 Pulled By: qihqi fbshipit-source-id: 3b72e24907f792592a74f3ac922684d4c6db73a2
Hi, are there any updates on this issue or any workaround? Torchscript isn't working on my NN which has a dataclass. |
…orch#74353) (pytorch#76771) Summary: Pull Request resolved: pytorch#76771 Pull Request resolved: pytorch#74353 Repatched `d00de0d43598522b8f6ab2de553b6aaf6768faa5` by Nora Belrose (norabelrose). With following changes: * Register fake source of generated methods in linecache so that inspect.get_source will succeed. * this patching is only triggered if the given dataclass passed to torch.jit.script previously. Effectively we make this feature opt-in. Fixes pytorch#72901. Since we can't get access to the source code for synthesized magic methods on dataclasses, we have to synthesize our own versions. torch/jit/_dataclass_impls.py has the code that does this. What's supported Synthesized __init__, __eq__, and the comparison magic methods when order=True is set on the dataclass decorator Default values for fields __post_init__, including using InitVar fields inside of __post_init__, on Python 3.8+ Overriding __eq__ or any of the comparison magic methods to provide your own implementation What's not supported Default factory initializers for fields Frozen dataclasses InitVar on Python 3.7 __repr__ and __hash__ (these are actually implemented, but the TorchScript interpreter won't call them) Using the != operator on dataclasses inside TorchScript; this is because TorchScript requires that you implement __ne__ to use this operator, whereas in regular Python the != operator will resolve to the negation of whatever is returned by __eq__ if there's no __ne__. Dataclasses don't actually synthesize an __ne__ method for this reason. I've been toying with different ways to fix this but != is not working in this PR at the moment. Pull Request resolved: pytorch#74889 Test Plan: unittest Also run previously failed test: ``` buck test mode/dev-nosan //fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests -- --exact 'fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests - test_mixmatch_multiclass (fblearner.flow.projects.fluent2.definition.transformers.contrib.faim.test.faim_mixmatch_test.TestFaimTransformerMixMatch)' ``` passes Reviewed By: zhxchen17 Differential Revision: D35206262 Pulled By: qihqi fbshipit-source-id: 3b72e24907f792592a74f3ac922684d4c6db73a2
@leg0m4n @norabelrose @mostafarohani
Running test_jit_fuser_te.py locally passes (with some skipped test). History
What's next?I will make posts in internal groups / pytorch forums to incite help, as I feel it is falling out of my area of expertise. I want this feature, what can I do to helpFew things can be helpful:
|
@qihqi For local testing, are you running with I was able to repro a double free with
|
…orch#74353) (pytorch#76771) Summary: Pull Request resolved: pytorch#76771 Pull Request resolved: pytorch#74353 Repatched `d00de0d43598522b8f6ab2de553b6aaf6768faa5` by Nora Belrose (norabelrose). With following changes: * Register fake source of generated methods in linecache so that inspect.get_source will succeed. * this patching is only triggered if the given dataclass passed to torch.jit.script previously. Effectively we make this feature opt-in. Fixes pytorch#72901. Since we can't get access to the source code for synthesized magic methods on dataclasses, we have to synthesize our own versions. torch/jit/_dataclass_impls.py has the code that does this. What's supported Synthesized __init__, __eq__, and the comparison magic methods when order=True is set on the dataclass decorator Default values for fields __post_init__, including using InitVar fields inside of __post_init__, on Python 3.8+ Overriding __eq__ or any of the comparison magic methods to provide your own implementation What's not supported Default factory initializers for fields Frozen dataclasses InitVar on Python 3.7 __repr__ and __hash__ (these are actually implemented, but the TorchScript interpreter won't call them) Using the != operator on dataclasses inside TorchScript; this is because TorchScript requires that you implement __ne__ to use this operator, whereas in regular Python the != operator will resolve to the negation of whatever is returned by __eq__ if there's no __ne__. Dataclasses don't actually synthesize an __ne__ method for this reason. I've been toying with different ways to fix this but != is not working in this PR at the moment. Pull Request resolved: pytorch#74889 Test Plan: unittest Also run previously failed test: ``` buck test mode/dev-nosan //fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests -- --exact 'fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests - test_mixmatch_multiclass (fblearner.flow.projects.fluent2.definition.transformers.contrib.faim.test.faim_mixmatch_test.TestFaimTransformerMixMatch)' ``` passes Reviewed By: zhxchen17 Differential Revision: D35206262 Pulled By: qihqi fbshipit-source-id: 3b72e24907f792592a74f3ac922684d4c6db73a2
…orch#74353) (pytorch#76771) Summary: Pull Request resolved: pytorch#76771 Pull Request resolved: pytorch#74353 Repatched `d00de0d43598522b8f6ab2de553b6aaf6768faa5` by Nora Belrose (norabelrose). With following changes: * Register fake source of generated methods in linecache so that inspect.get_source will succeed. * this patching is only triggered if the given dataclass passed to torch.jit.script previously. Effectively we make this feature opt-in. Fixes pytorch#72901. Since we can't get access to the source code for synthesized magic methods on dataclasses, we have to synthesize our own versions. torch/jit/_dataclass_impls.py has the code that does this. What's supported Synthesized __init__, __eq__, and the comparison magic methods when order=True is set on the dataclass decorator Default values for fields __post_init__, including using InitVar fields inside of __post_init__, on Python 3.8+ Overriding __eq__ or any of the comparison magic methods to provide your own implementation What's not supported Default factory initializers for fields Frozen dataclasses InitVar on Python 3.7 __repr__ and __hash__ (these are actually implemented, but the TorchScript interpreter won't call them) Using the != operator on dataclasses inside TorchScript; this is because TorchScript requires that you implement __ne__ to use this operator, whereas in regular Python the != operator will resolve to the negation of whatever is returned by __eq__ if there's no __ne__. Dataclasses don't actually synthesize an __ne__ method for this reason. I've been toying with different ways to fix this but != is not working in this PR at the moment. Pull Request resolved: pytorch#74889 Test Plan: unittest Also run previously failed test: ``` buck test mode/dev-nosan //fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests -- --exact 'fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests - test_mixmatch_multiclass (fblearner.flow.projects.fluent2.definition.transformers.contrib.faim.test.faim_mixmatch_test.TestFaimTransformerMixMatch)' ``` passes Reviewed By: zhxchen17 Differential Revision: D35206262 Pulled By: qihqi fbshipit-source-id: 3b72e24907f792592a74f3ac922684d4c6db73a2
…orch#74353) (pytorch#76771) Summary: Pull Request resolved: pytorch#76771 Pull Request resolved: pytorch#74353 Repatched `d00de0d43598522b8f6ab2de553b6aaf6768faa5` by Nora Belrose (norabelrose). With following changes: * Register fake source of generated methods in linecache so that inspect.get_source will succeed. * this patching is only triggered if the given dataclass passed to torch.jit.script previously. Effectively we make this feature opt-in. Fixes pytorch#72901. Since we can't get access to the source code for synthesized magic methods on dataclasses, we have to synthesize our own versions. torch/jit/_dataclass_impls.py has the code that does this. What's supported Synthesized __init__, __eq__, and the comparison magic methods when order=True is set on the dataclass decorator Default values for fields __post_init__, including using InitVar fields inside of __post_init__, on Python 3.8+ Overriding __eq__ or any of the comparison magic methods to provide your own implementation What's not supported Default factory initializers for fields Frozen dataclasses InitVar on Python 3.7 __repr__ and __hash__ (these are actually implemented, but the TorchScript interpreter won't call them) Using the != operator on dataclasses inside TorchScript; this is because TorchScript requires that you implement __ne__ to use this operator, whereas in regular Python the != operator will resolve to the negation of whatever is returned by __eq__ if there's no __ne__. Dataclasses don't actually synthesize an __ne__ method for this reason. I've been toying with different ways to fix this but != is not working in this PR at the moment. Pull Request resolved: pytorch#74889 Test Plan: unittest Also run previously failed test: ``` buck test mode/dev-nosan //fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests -- --exact 'fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests - test_mixmatch_multiclass (fblearner.flow.projects.fluent2.definition.transformers.contrib.faim.test.faim_mixmatch_test.TestFaimTransformerMixMatch)' ``` passes Reviewed By: zhxchen17 Differential Revision: D35206262 Pulled By: qihqi fbshipit-source-id: 3b72e24907f792592a74f3ac922684d4c6db73a2
…orch#74353) (pytorch#76771) Summary: Pull Request resolved: pytorch#76771 Pull Request resolved: pytorch#74353 Repatched `d00de0d43598522b8f6ab2de553b6aaf6768faa5` by Nora Belrose (norabelrose). With following changes: * Register fake source of generated methods in linecache so that inspect.get_source will succeed. * this patching is only triggered if the given dataclass passed to torch.jit.script previously. Effectively we make this feature opt-in. Fixes pytorch#72901. Since we can't get access to the source code for synthesized magic methods on dataclasses, we have to synthesize our own versions. torch/jit/_dataclass_impls.py has the code that does this. What's supported Synthesized __init__, __eq__, and the comparison magic methods when order=True is set on the dataclass decorator Default values for fields __post_init__, including using InitVar fields inside of __post_init__, on Python 3.8+ Overriding __eq__ or any of the comparison magic methods to provide your own implementation What's not supported Default factory initializers for fields Frozen dataclasses InitVar on Python 3.7 __repr__ and __hash__ (these are actually implemented, but the TorchScript interpreter won't call them) Using the != operator on dataclasses inside TorchScript; this is because TorchScript requires that you implement __ne__ to use this operator, whereas in regular Python the != operator will resolve to the negation of whatever is returned by __eq__ if there's no __ne__. Dataclasses don't actually synthesize an __ne__ method for this reason. I've been toying with different ways to fix this but != is not working in this PR at the moment. Pull Request resolved: pytorch#74889 Test Plan: unittest Also run previously failed test: ``` buck test mode/dev-nosan //fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests -- --exact 'fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests - test_mixmatch_multiclass (fblearner.flow.projects.fluent2.definition.transformers.contrib.faim.test.faim_mixmatch_test.TestFaimTransformerMixMatch)' ``` passes Reviewed By: zhxchen17 Differential Revision: D35206262 Pulled By: qihqi fbshipit-source-id: 3b72e24907f792592a74f3ac922684d4c6db73a2
Thanks @davidberard98 with this hint I managed to fix it. I'll try get it landed soon. |
…#76771) (#74353) Summary: Pull Request resolved: #74353 Repatched `d00de0d43598522b8f6ab2de553b6aaf6768faa5` by Nora Belrose (norabelrose). With following changes: * Register fake source of generated methods in linecache so that inspect.get_source will succeed. * this patching is only triggered if the given dataclass passed to torch.jit.script previously. Effectively we make this feature opt-in. ## Original Summary: Fixes #72901. Since we can't get access to the source code for synthesized magic methods on dataclasses, we have to synthesize our own versions. torch/jit/_dataclass_impls.py has the code that does this. What's supported Synthesized __init__, __eq__, and the comparison magic methods when order=True is set on the dataclass decorator Default values for fields __post_init__, including using InitVar fields inside of __post_init__, on Python 3.8+ Overriding __eq__ or any of the comparison magic methods to provide your own implementation What's not supported Default factory initializers for fields Frozen dataclasses InitVar on Python 3.7 __repr__ and __hash__ (these are actually implemented, but the TorchScript interpreter won't call them) Using the != operator on dataclasses inside TorchScript; this is because TorchScript requires that you implement __ne__ to use this operator, whereas in regular Python the != operator will resolve to the negation of whatever is returned by __eq__ if there's no __ne__. Dataclasses don't actually synthesize an __ne__ method for this reason. I've been toying with different ways to fix this but != is not working in this PR at the moment. #74889 #76771 Approved by: https://github.com/seemethere Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/13dff3b2c2f2420f6458804dcc80ded5598636ab Test plan from GitHub: unittest Also run previously failed test: ``` buck test mode/dev-nosan //fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests -- --exact 'fblearner/flow/projects/fluent2/definition/transformers/contrib/faim/test:tests - test_mixmatch_multiclass (fblearner.flow.projects.fluent2.definition.transformers.contrib.faim.test.faim_mixmatch_test.TestFaimTransformerMixMatch)' ``` passes Reviewed By: seemethere, osalpekar, zhxchen17 Differential Revision: D35206262 Pulled By: qihqi fbshipit-source-id: 4b43477930f99ca2dd26073c0ec60f19bb4c8372
+1 |
🚀 The feature, motivation and pitch
Currently, TorchScript supports namedtuples as well as custom Python classes that meet certain requirements. Unfortunately, it's not possible to subclass a namedtuple to add extra methods (since inheritance is not supported), nor is it possible to use dataclasses (since the @DataClass decorator adds implementations of
__init__
and__eq__
whose source code is not accessible withinspect.getsource
). This means that anyone wanting to use custom data structures in TorchScript has to write them "the old-fashioned way," manually implementing all the magic methods they'd like to use. In this respect, TorchScript is less user friendly than JAX + Flax, which has aflax.struct.dataclass
decorator that allows you to use (immutable) dataclass-like objects in jitted JAX code.It seems like there are two possible ways to address this problem. First, we could add a new dataclass-like decorator, perhaps
torch.jit.dataclass
, which would transform a class just like the usual@dataclass
decorator does but in a way that is compatible with TorchScript:The other approach would be to add a special case into the TorchScript frontend that recognizes dataclasses, resulting in a double-decorator syntax like this:
I think I'm partial to the first approach, since it is slightly more compact and might be a bit easier to implement, and it's also more similar to the Flax implementation, but I'm open to the latter approach as well. I'm also interested in contributing a PR to implement this feature if it seems that there's a consensus that this is a good idea.
Alternatives
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: