-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathattacher.py
60 lines (47 loc) · 1.52 KB
/
attacher.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
55
56
57
58
from torch.autograd import Function
# f is any callable object
# attacher to forward
class attach_to_forward_class(Function):
@staticmethod
def forward(ctx, tensor, f, tag):
# print('forward')
# we want that output will have different id from input
ctx.tag = tag
return 1*f(tensor, tag)
@staticmethod
def backward(ctx, grad_output):
return grad_output, None, None
# attacher to backward
class attach_to_backward_class(Function):
@staticmethod
def forward(ctx, tensor, f, tag):
ctx.f = f
ctx.tag = tag
return 1*tensor
@staticmethod
def backward(ctx, grad_output):
# print('backward')
f = ctx.f
return f(grad_output, ctx.tag), None, None
# attacher to backward
class attach_to_forward_backward_class(Function):
@staticmethod
def forward(ctx, tensor, f, b, tag):
# print('forward')
ctx.f = f
ctx.b = b
ctx.tag = tag
return f(tensor, tag)
@staticmethod
def backward(ctx, grad_output):
# print('backward')
return ctx.b(grad_output, ctx.tag), None, None, None
# attacher to forward and backward
def pytorch_attach(tensor, f=None, b=None, tag=''):
if f is not None and b is not None:
tensor = attach_to_forward_backward_class.apply(tensor, f, b, tag)
elif f is not None:
tensor = attach_to_forward_class.apply(tensor, f, tag)
elif b is not None:
tensor = attach_to_backward_class.apply(tensor, b, tag)
return tensor