/
weird.py
154 lines (117 loc) · 4.03 KB
/
weird.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import torch
import math
from typing import *
class WeirdMin1(torch.autograd.Function):
"""
max, but gradient is always progragated through the first argument
"""
@staticmethod
def forward(ctx, a,b):
return torch.min(a,b)
@staticmethod
def backward(ctx, grad_output):
return grad_output, torch.zeros_like(grad_output)
weirdMin1 : Callable[[torch.Tensor, torch.Tensor], torch.Tensor]
weirdMin1 = WeirdMin1.apply #type: ignore
class WeirdMax(torch.autograd.Function):
"""
max, but gradient is always progragated through both arguments
"""
@staticmethod
def forward(ctx, a,b):
return torch.max(a,b)
@staticmethod
def backward(ctx, grad_output):
return grad_output / 2, grad_output / 2
class WeirdMax2(torch.autograd.Function):
"""
max, but gradient is proportional to input
"""
@staticmethod
def forward(ctx, a,b):
s = a + b
s = torch.max(s, torch.as_tensor(1e-9, device=a.device))
ctx.sa = a / s
ctx.sb = b / s
return torch.max(a,b)
@staticmethod
def backward(ctx, grad_output):
return grad_output * ctx.sa, grad_output * ctx.sb
class WeirdMin(torch.autograd.Function):
@staticmethod
def forward(ctx, a,b):
return torch.min(a,b)
@staticmethod
def backward(ctx, grad_output):
return grad_output, grad_output
class WeirdMinDim(torch.autograd.Function):
@staticmethod
def forward(ctx, a, dim):
ctx.dim = dim
ctx.size = a.shape[dim]
return a.min(dim=dim).values
@staticmethod
def backward(ctx, grad_output):
size = len(grad_output.shape)+1
rep = [ctx.size if d in {ctx.dim, ctx.dim + size} else 1 for d in range(0, len(grad_output.shape)+1)]
return grad_output.unsqueeze(ctx.dim).repeat(rep), None
class WeirdMaxDim(torch.autograd.Function):
@staticmethod
def forward(ctx, a, dim):
ctx.dim = dim
ctx.size = a.shape[dim]
return a.max(dim=dim).values
@staticmethod
def backward(ctx, grad_output):
size = len(grad_output.shape)+1
rep = [ctx.size if d in {ctx.dim, ctx.dim + size} else 1 for d in range(0, len(grad_output.shape)+1)]
return grad_output.unsqueeze(ctx.dim).repeat(rep), None
class WeirdMax2Dim(torch.autograd.Function):
eps = 1e-9
@staticmethod
def forward(ctx, a, dim):
ctx.dim = dim
ctx.scale = a / torch.max(a.sum(dim=dim,keepdim=True), torch.as_tensor(1e-9, device=a.device))
return a.max(dim).values
@staticmethod
def backward(ctx, grad_output):
x = (grad_output.unsqueeze(ctx.dim) * ctx.scale)
return x, None
class WeirdMax2BDim(torch.autograd.Function):
@staticmethod
def forward(ctx, a, dim):
ctx.dim = dim
am = a.max(dim=dim,keepdim=True).values
#x = a / am
x = a
x = x / x.sum(dim=dim,keepdim=True)
ctx.scale = x
return am.squeeze(dim)
@staticmethod
def backward(ctx, grad_output):
x = (grad_output.unsqueeze(ctx.dim) * ctx.scale)
return x, None
class WeirdNorm(torch.autograd.Function):
@staticmethod
def forward(ctx, a, dims, scale):
ctx.dims = dims #sorted(dims, reverse=True)
ctx.scale = scale
return a
@staticmethod
def backward(ctx, grad_output):
mean = grad_output.mean(ctx.dims, keepdim=True)
var = grad_output.var(ctx.dims, keepdim=True)
eps = 1e-9
return (grad_output - mean) / (ctx.scale * (var + eps)), None, None
class GaussMax(torch.autograd.Function):
@staticmethod
def forward(ctx, a, b, sigma):
ctx.x = phi(a-b, sigma=sigma)
return torch.max(a,b)
@staticmethod
def backward(ctx, grad_output):
return grad_output * ctx.x, grad_output * (1-ctx.x), None
def phi(x, sigma=1.0):
return (1+torch.special.erf(x * 1/math.sqrt(2)/sigma))/2
def gauss_max(sigma=2.0) -> Callable[[torch.Tensor, torch.Tensor], torch.Tensor]:
return lambda a, b: GaussMax.apply(a, b, sigma)