senmaonk commited on
Commit
95dc07b
·
verified ·
1 Parent(s): d168242

Upload 2 files

Browse files
basicsr/ops/fused_act/__init__.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ from .fused_act import FusedLeakyReLU, fused_leaky_relu
2
+
3
+ __all__ = ['FusedLeakyReLU', 'fused_leaky_relu']
basicsr/ops/fused_act/fused_act.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # modify from https://github.com/rosinality/stylegan2-pytorch/blob/master/op/fused_act.py # noqa:E501
2
+
3
+ import torch
4
+ from torch import nn
5
+ from torch.autograd import Function
6
+
7
+ try:
8
+ from . import fused_act_ext
9
+ except ImportError:
10
+ import os
11
+ BASICSR_JIT = os.getenv('BASICSR_JIT')
12
+ if BASICSR_JIT == 'True':
13
+ from torch.utils.cpp_extension import load
14
+ module_path = os.path.dirname(__file__)
15
+ fused_act_ext = load(
16
+ 'fused',
17
+ sources=[
18
+ os.path.join(module_path, 'src', 'fused_bias_act.cpp'),
19
+ os.path.join(module_path, 'src', 'fused_bias_act_kernel.cu'),
20
+ ],
21
+ )
22
+
23
+
24
+ class FusedLeakyReLUFunctionBackward(Function):
25
+
26
+ @staticmethod
27
+ def forward(ctx, grad_output, out, negative_slope, scale):
28
+ ctx.save_for_backward(out)
29
+ ctx.negative_slope = negative_slope
30
+ ctx.scale = scale
31
+
32
+ empty = grad_output.new_empty(0)
33
+
34
+ grad_input = fused_act_ext.fused_bias_act(grad_output, empty, out, 3, 1, negative_slope, scale)
35
+
36
+ dim = [0]
37
+
38
+ if grad_input.ndim > 2:
39
+ dim += list(range(2, grad_input.ndim))
40
+
41
+ grad_bias = grad_input.sum(dim).detach()
42
+
43
+ return grad_input, grad_bias
44
+
45
+ @staticmethod
46
+ def backward(ctx, gradgrad_input, gradgrad_bias):
47
+ out, = ctx.saved_tensors
48
+ gradgrad_out = fused_act_ext.fused_bias_act(gradgrad_input, gradgrad_bias, out, 3, 1, ctx.negative_slope,
49
+ ctx.scale)
50
+
51
+ return gradgrad_out, None, None, None
52
+
53
+
54
+ class FusedLeakyReLUFunction(Function):
55
+
56
+ @staticmethod
57
+ def forward(ctx, input, bias, negative_slope, scale):
58
+ empty = input.new_empty(0)
59
+ out = fused_act_ext.fused_bias_act(input, bias, empty, 3, 0, negative_slope, scale)
60
+ ctx.save_for_backward(out)
61
+ ctx.negative_slope = negative_slope
62
+ ctx.scale = scale
63
+
64
+ return out
65
+
66
+ @staticmethod
67
+ def backward(ctx, grad_output):
68
+ out, = ctx.saved_tensors
69
+
70
+ grad_input, grad_bias = FusedLeakyReLUFunctionBackward.apply(grad_output, out, ctx.negative_slope, ctx.scale)
71
+
72
+ return grad_input, grad_bias, None, None
73
+
74
+
75
+ class FusedLeakyReLU(nn.Module):
76
+
77
+ def __init__(self, channel, negative_slope=0.2, scale=2**0.5):
78
+ super().__init__()
79
+
80
+ self.bias = nn.Parameter(torch.zeros(channel))
81
+ self.negative_slope = negative_slope
82
+ self.scale = scale
83
+
84
+ def forward(self, input):
85
+ return fused_leaky_relu(input, self.bias, self.negative_slope, self.scale)
86
+
87
+
88
+ def fused_leaky_relu(input, bias, negative_slope=0.2, scale=2**0.5):
89
+ return FusedLeakyReLUFunction.apply(input, bias, negative_slope, scale)