pytorch/torch/jit
Han Qi 13dff3b2c2 Reland "[pytorch][PR] Support dataclasses in TorchScript" take 2 (#74353) (#74353) (#76771)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/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 https://github.com/pytorch/pytorch/issues/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: https://github.com/pytorch/pytorch/pull/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

Pull Request resolved: https://github.com/pytorch/pytorch/pull/76771
Approved by: https://github.com/seemethere
2022-06-07 21:44:55 +00:00
..
_passes Move the apply_tensor_props into its own function for more public use (#67786) 2022-01-10 11:26:03 -08:00
mobile Back out "[pytorch][PR] Add ability for a mobile::Module to save as flatbuffer" (#69796) 2021-12-10 21:29:53 -08:00
__init__.py Python function to extract information on mobile::Module from flatbuffer (#77624) 2022-05-18 00:42:57 +00:00
_async.py
_builtins.py Update min python version to 3.7 in setup.py and mypy configs (#71494) 2022-01-20 00:03:57 +00:00
_check.py Fix comment indentation issue (#70227) 2022-01-06 19:14:39 -08:00
_dataclass_impls.py Reland "[pytorch][PR] Support dataclasses in TorchScript" take 2 (#74353) (#74353) (#76771) 2022-06-07 21:44:55 +00:00
_decomposition_utils.py Reland reland 2022-04-28 20:41:55 +00:00
_decompositions.py [JIT] Make aot autograd decompositions usable in JIT, add script for serializing the decompositions (#73938) 2022-03-29 18:38:52 +00:00
_freeze.py [JIT] optimize_for_inference on methods other than forward (#69367) 2021-12-07 12:36:47 -08:00
_fuser.py Clean up profiling mode and profiling executor strategy (#73875) 2022-03-29 18:38:51 +00:00
_ir_utils.py [JIT] Add more python ir utilities (#69871) 2022-02-25 01:07:05 +00:00
_logging.py
_monkeytype_config.py
_pickle.py
_recursive.py [JIT] Support for ParameterDict getattr 2022-05-13 01:03:25 +00:00
_script.py [JIT] correct torch.jit.Attribute docs 2022-05-11 01:30:15 +00:00
_serialization.py Support saving extra files in pytorch flat buffer format via python (#77870) 2022-05-19 22:15:17 +00:00
_shape_functions.py Adding SSA support for convolution_backward 2022-05-20 18:39:47 +00:00
_state.py
_trace.py tracer compare_outputs should compare in cdouble for complex 2022-05-06 06:05:50 +00:00
annotations.py Reland "[pytorch][PR] Support dataclasses in TorchScript" take 2 (#74353) (#74353) (#76771) 2022-06-07 21:44:55 +00:00
frontend.py Reland "[pytorch][PR] Support dataclasses in TorchScript" take 2 (#74353) (#74353) (#76771) 2022-06-07 21:44:55 +00:00
generate_bytecode.py Move bytecode generation to python (#71681) 2022-01-28 02:33:00 +00:00
quantized.py Deprecate torch.jit.quantized (#72690) 2022-02-11 23:18:28 +00:00
supported_ops.py
unsupported_tensor_ops.py