mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
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. qihqi Pull Request resolved: https://github.com/pytorch/pytorch/pull/73066 Reviewed By: mrshenli Differential Revision: D34398107 Pulled By: qihqi fbshipit-source-id: f5a792555c88f3631f97837a96687e4890660a32 (cherry picked from commit ea7f077dc49a4ee75ca0d1409aedd85228952881)
110 lines
3.4 KiB
Python
110 lines
3.4 KiB
Python
# Owner(s): ["oncall: jit"]
|
|
|
|
from dataclasses import dataclass, field, InitVar
|
|
from hypothesis import given, settings, strategies as st
|
|
from torch.testing._internal.jit_utils import JitTestCase
|
|
from typing import List, Optional
|
|
import sys
|
|
import torch
|
|
import unittest
|
|
|
|
if __name__ == '__main__':
|
|
raise RuntimeError("This test file is not meant to be run directly, use:\n\n"
|
|
"\tpython test/test_jit.py TESTNAME\n\n"
|
|
"instead.")
|
|
|
|
# Example jittable dataclass
|
|
@dataclass(order=True)
|
|
class Point:
|
|
x: float
|
|
y: float
|
|
norm: Optional[torch.Tensor] = None
|
|
|
|
def __post_init__(self):
|
|
self.norm = (torch.tensor(self.x) ** 2 + torch.tensor(self.y) ** 2) ** 0.5
|
|
|
|
|
|
# Make sure the Meta internal tooling doesn't raise an overflow error
|
|
NonHugeFloats = st.floats(min_value=-1e4, max_value=1e4, allow_nan=False)
|
|
|
|
class TestDataclasses(JitTestCase):
|
|
# We only support InitVar in JIT dataclasses for Python 3.8+ because it would be very hard
|
|
# to support without the `type` attribute on InitVar (see comment in _dataclass_impls.py).
|
|
@unittest.skipIf(sys.version_info < (3, 8), "InitVar not supported in Python < 3.8")
|
|
def test_init_vars(self):
|
|
@dataclass(order=True)
|
|
class Point2:
|
|
x: float
|
|
y: float
|
|
norm_p: InitVar[int] = 2
|
|
norm: Optional[torch.Tensor] = None
|
|
|
|
def __post_init__(self, norm_p: int):
|
|
self.norm = (torch.tensor(self.x) ** norm_p + torch.tensor(self.y) ** norm_p) ** (1 / norm_p)
|
|
|
|
def fn(x: float, y: float, p: int):
|
|
pt = Point2(x, y, p)
|
|
return pt.norm
|
|
|
|
self.checkScript(fn, (1.0, 2.0, 3))
|
|
|
|
# Sort of tests both __post_init__ and optional fields
|
|
@settings(deadline=None)
|
|
@given(NonHugeFloats, NonHugeFloats)
|
|
def test__post_init__(self, x, y):
|
|
def fn(x: float, y: float):
|
|
pt = Point(x, y)
|
|
return pt.norm
|
|
|
|
self.checkScript(fn, [x, y])
|
|
|
|
@settings(deadline=None)
|
|
@given(st.tuples(NonHugeFloats, NonHugeFloats), st.tuples(NonHugeFloats, NonHugeFloats))
|
|
def test_comparators(self, pt1, pt2):
|
|
x1, y1 = pt1
|
|
x2, y2 = pt2
|
|
|
|
def compare(x1: float, y1: float, x2: float, y2: float):
|
|
pt1 = Point(x1, y1)
|
|
pt2 = Point(x2, y2)
|
|
return (
|
|
pt1 == pt2,
|
|
# pt1 != pt2, # TODO: Modify interpreter to auto-resolve (a != b) to not (a == b) when there's no __ne__
|
|
pt1 < pt2,
|
|
pt1 <= pt2,
|
|
pt1 > pt2,
|
|
pt1 >= pt2,
|
|
)
|
|
|
|
self.checkScript(compare, [x1, y1, x2, y2])
|
|
|
|
def test_default_factories(self):
|
|
@dataclass
|
|
class Foo(object):
|
|
x: List[int] = field(default_factory=list)
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
def fn():
|
|
foo = Foo()
|
|
return foo.x
|
|
|
|
torch.jit.script(fn)()
|
|
|
|
# The user should be able to write their own __eq__ implementation
|
|
# without us overriding it.
|
|
def test_custom__eq__(self):
|
|
@dataclass
|
|
class CustomEq:
|
|
a: int
|
|
b: int
|
|
|
|
def __eq__(self, other: 'CustomEq') -> bool:
|
|
return self.a == other.a # ignore the b field
|
|
|
|
def fn(a: int, b1: int, b2: int):
|
|
pt1 = CustomEq(a, b1)
|
|
pt2 = CustomEq(a, b2)
|
|
return pt1 == pt2
|
|
|
|
self.checkScript(fn, [1, 2, 3])
|