-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbug_zoo.py
54 lines (42 loc) · 1.8 KB
/
bug_zoo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import unittest
import torch
from base_tensor import BaseTensor
from trivial_tensors import TrivialTensorViaInheritance
from torch.testing._internal.common_utils import run_tests, TestCase
class BugZoo(TestCase):
@unittest.expectedFailure
def test_binary_ops_swallow_errors(self):
class BuggyTensor(BaseTensor):
@classmethod
def __torch_dispatch__(cls, func, types, args=(), kwargs=None):
raise TypeError("foobar")
x = BuggyTensor(torch.tensor(1.0))
self.assertRaisesRegex(TypeError, "foobar", lambda: x + x)
@unittest.skip
def test_super_dispatch_segfault(self):
class SuperDispatchSegfaultTensor(BaseTensor):
@classmethod
def __torch_dispatch__(cls, func, types, args=(), kwargs=None):
return super().__torch_dispatch__(func, types, list(args), kwargs)
SuperDispatchSegfaultTensor(torch.tensor(1.0)).neg()
# Fixed!
def test_trivial_inplace(self):
x = TrivialTensorViaInheritance(torch.tensor(1.0))
y = x * torch.tensor(1.0, requires_grad=True)
y.relu_()
# Fixed!
def test_grad_fn(self):
class TestTensor(BaseTensor):
@classmethod
def __torch_dispatch__(cls, func, types, args=(), kwargs=None):
if func is torch.ops.aten.add.Tensor and "alpha" in kwargs:
# decompose it
r = torch.add(args[0], args[1] * kwargs["alpha"])
self.assertIsNone(r.grad_fn)
return r
return super().__torch_dispatch__(func, types, args, kwargs)
x = TestTensor(torch.tensor(1.0)).requires_grad_()
y = TestTensor(torch.tensor(2.0)).requires_grad_()
torch.add(x, y, alpha=2)
if __name__ == "__main__":
run_tests()