Spaces:
Runtime error
Runtime error
Update Time_TravelRephotography/op/fused_act.py
Browse files
Time_TravelRephotography/op/fused_act.py
CHANGED
@@ -1,108 +1,86 @@
|
|
1 |
import os
|
2 |
-
|
3 |
-
import numpy as np
|
4 |
import torch
|
5 |
-
import
|
6 |
-
import
|
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 |
-
# Add bias.
|
90 |
-
if b is not None:
|
91 |
-
assert isinstance(b, torch.Tensor) and b.ndim == 1
|
92 |
-
assert 0 <= dim < x.ndim
|
93 |
-
assert b.shape[0] == x.shape[dim]
|
94 |
-
x = x + b.reshape([-1 if i == dim else 1 for i in range(x.ndim)])
|
95 |
-
|
96 |
-
# Evaluate activation function.
|
97 |
-
alpha = float(alpha)
|
98 |
-
x = spec.func(x, alpha=alpha)
|
99 |
-
|
100 |
-
# Scale by gain.
|
101 |
-
gain = float(gain)
|
102 |
-
if gain != 1:
|
103 |
-
x = x * gain
|
104 |
-
|
105 |
-
# Clamp.
|
106 |
-
if clamp >= 0:
|
107 |
-
x = x.clamp(-clamp, clamp) # pylint: disable=invalid-unary-operand-type
|
108 |
-
return
|
|
|
1 |
import os
|
2 |
+
|
|
|
3 |
import torch
|
4 |
+
from torch import nn
|
5 |
+
from torch.autograd import Function
|
6 |
+
from torch.utils.cpp_extension import load
|
7 |
+
|
8 |
+
|
9 |
+
module_path = os.path.dirname(__file__)
|
10 |
+
fused = load(
|
11 |
+
'fused',
|
12 |
+
sources=[
|
13 |
+
os.path.join(module_path, 'fused_bias_act.cpp'),
|
14 |
+
os.path.join(module_path, 'fused_bias_act_kernel.cu'),
|
15 |
+
],
|
16 |
+
)
|
17 |
+
|
18 |
+
|
19 |
+
class FusedLeakyReLUFunctionBackward(Function):
|
20 |
+
@staticmethod
|
21 |
+
def forward(ctx, grad_output, out, negative_slope, scale):
|
22 |
+
ctx.save_for_backward(out)
|
23 |
+
ctx.negative_slope = negative_slope
|
24 |
+
ctx.scale = scale
|
25 |
+
|
26 |
+
empty = grad_output.new_empty(0)
|
27 |
+
|
28 |
+
grad_input = fused.fused_bias_act(
|
29 |
+
grad_output, empty, out, 3, 1, negative_slope, scale
|
30 |
+
)
|
31 |
+
|
32 |
+
dim = [0]
|
33 |
+
|
34 |
+
if grad_input.ndim > 2:
|
35 |
+
dim += list(range(2, grad_input.ndim))
|
36 |
+
|
37 |
+
grad_bias = grad_input.sum(dim).detach()
|
38 |
+
|
39 |
+
return grad_input, grad_bias
|
40 |
+
|
41 |
+
@staticmethod
|
42 |
+
def backward(ctx, gradgrad_input, gradgrad_bias):
|
43 |
+
out, = ctx.saved_tensors
|
44 |
+
gradgrad_out = fused.fused_bias_act(
|
45 |
+
gradgrad_input, gradgrad_bias, out, 3, 1, ctx.negative_slope, ctx.scale
|
46 |
+
)
|
47 |
+
|
48 |
+
return gradgrad_out, None, None, None
|
49 |
+
|
50 |
+
|
51 |
+
class FusedLeakyReLUFunction(Function):
|
52 |
+
@staticmethod
|
53 |
+
def forward(ctx, input, bias, negative_slope, scale):
|
54 |
+
empty = input.new_empty(0)
|
55 |
+
out = fused.fused_bias_act(input, bias, empty, 3, 0, negative_slope, scale)
|
56 |
+
ctx.save_for_backward(out)
|
57 |
+
ctx.negative_slope = negative_slope
|
58 |
+
ctx.scale = scale
|
59 |
+
|
60 |
+
return out
|
61 |
+
|
62 |
+
@staticmethod
|
63 |
+
def backward(ctx, grad_output):
|
64 |
+
out, = ctx.saved_tensors
|
65 |
+
|
66 |
+
grad_input, grad_bias = FusedLeakyReLUFunctionBackward.apply(
|
67 |
+
grad_output, out, ctx.negative_slope, ctx.scale
|
68 |
+
)
|
69 |
+
|
70 |
+
return grad_input, grad_bias, None, None
|
71 |
+
|
72 |
+
|
73 |
+
class FusedLeakyReLU(nn.Module):
|
74 |
+
def __init__(self, channel, negative_slope=0.2, scale=2 ** 0.5):
|
75 |
+
super().__init__()
|
76 |
+
|
77 |
+
self.bias = nn.Parameter(torch.zeros(channel))
|
78 |
+
self.negative_slope = negative_slope
|
79 |
+
self.scale = scale
|
80 |
+
|
81 |
+
def forward(self, input):
|
82 |
+
return fused_leaky_relu(input, self.bias, self.negative_slope, self.scale)
|
83 |
+
|
84 |
+
|
85 |
+
def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2 ** 0.5):
|
86 |
+
return FusedLeakyReLUFunction.apply(input, bias, negative_slope, scale)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|