feng2022 commited on
Commit
b089fcc
1 Parent(s): 991a44c

Update Time_TravelRephotography/op/fused_act.py

Browse files
Time_TravelRephotography/op/fused_act.py CHANGED
@@ -1,108 +1,86 @@
1
  import os
2
- import warnings
3
- import numpy as np
4
  import torch
5
- import dnnlib
6
- import traceback
7
-
8
- from .. import custom_ops
9
- from .. import misc
10
-
11
- #----------------------------------------------------------------------------
12
-
13
- activation_funcs = {
14
- 'linear': dnnlib.EasyDict(func=lambda x, **_: x, def_alpha=0, def_gain=1, cuda_idx=1, ref='', has_2nd_grad=False),
15
- 'relu': dnnlib.EasyDict(func=lambda x, **_: torch.nn.functional.relu(x), def_alpha=0, def_gain=np.sqrt(2), cuda_idx=2, ref='y', has_2nd_grad=False),
16
- 'lrelu': dnnlib.EasyDict(func=lambda x, alpha, **_: torch.nn.functional.leaky_relu(x, alpha), def_alpha=0.2, def_gain=np.sqrt(2), cuda_idx=3, ref='y', has_2nd_grad=False),
17
- 'tanh': dnnlib.EasyDict(func=lambda x, **_: torch.tanh(x), def_alpha=0, def_gain=1, cuda_idx=4, ref='y', has_2nd_grad=True),
18
- 'sigmoid': dnnlib.EasyDict(func=lambda x, **_: torch.sigmoid(x), def_alpha=0, def_gain=1, cuda_idx=5, ref='y', has_2nd_grad=True),
19
- 'elu': dnnlib.EasyDict(func=lambda x, **_: torch.nn.functional.elu(x), def_alpha=0, def_gain=1, cuda_idx=6, ref='y', has_2nd_grad=True),
20
- 'selu': dnnlib.EasyDict(func=lambda x, **_: torch.nn.functional.selu(x), def_alpha=0, def_gain=1, cuda_idx=7, ref='y', has_2nd_grad=True),
21
- 'softplus': dnnlib.EasyDict(func=lambda x, **_: torch.nn.functional.softplus(x), def_alpha=0, def_gain=1, cuda_idx=8, ref='y', has_2nd_grad=True),
22
- 'swish': dnnlib.EasyDict(func=lambda x, **_: torch.sigmoid(x) * x, def_alpha=0, def_gain=np.sqrt(2), cuda_idx=9, ref='x', has_2nd_grad=True),
23
- }
24
-
25
- #----------------------------------------------------------------------------
26
-
27
- _inited = False
28
- _plugin = None
29
- _null_tensor = torch.empty([0])
30
-
31
- def _init():
32
- global _inited, _plugin
33
- if not _inited:
34
- _inited = True
35
- sources = ['bias_act.cpp', 'bias_act.cu']
36
- sources = [os.path.join(os.path.dirname(__file__), s) for s in sources]
37
- try:
38
- _plugin = custom_ops.get_plugin('bias_act_plugin', sources=sources, extra_cuda_cflags=['--use_fast_math'])
39
- except:
40
- warnings.warn('Failed to build CUDA kernels for bias_act. Falling back to slow reference implementation. Details:\n\n' + traceback.format_exc())
41
- return _plugin is not None
42
-
43
- #----------------------------------------------------------------------------
44
-
45
- def bias_act(x, b=None, dim=1, act='linear', alpha=None, gain=None, clamp=None, impl='cuda'):
46
- r"""Fused bias and activation function.
47
- Adds bias `b` to activation tensor `x`, evaluates activation function `act`,
48
- and scales the result by `gain`. Each of the steps is optional. In most cases,
49
- the fused op is considerably more efficient than performing the same calculation
50
- using standard PyTorch ops. It supports first and second order gradients,
51
- but not third order gradients.
52
- Args:
53
- x: Input activation tensor. Can be of any shape.
54
- b: Bias vector, or `None` to disable. Must be a 1D tensor of the same type
55
- as `x`. The shape must be known, and it must match the dimension of `x`
56
- corresponding to `dim`.
57
- dim: The dimension in `x` corresponding to the elements of `b`.
58
- The value of `dim` is ignored if `b` is not specified.
59
- act: Name of the activation function to evaluate, or `"linear"` to disable.
60
- Can be e.g. `"relu"`, `"lrelu"`, `"tanh"`, `"sigmoid"`, `"swish"`, etc.
61
- See `activation_funcs` for a full list. `None` is not allowed.
62
- alpha: Shape parameter for the activation function, or `None` to use the default.
63
- gain: Scaling factor for the output tensor, or `None` to use default.
64
- See `activation_funcs` for the default scaling of each activation function.
65
- If unsure, consider specifying 1.
66
- clamp: Clamp the output values to `[-clamp, +clamp]`, or `None` to disable
67
- the clamping (default).
68
- impl: Name of the implementation to use. Can be `"ref"` or `"cuda"` (default).
69
- Returns:
70
- Tensor of the same shape and datatype as `x`.
71
- """
72
- assert isinstance(x, torch.Tensor)
73
- assert impl in ['ref', 'cuda']
74
- return _bias_act_ref(x=x, b=b, dim=dim, act=act, alpha=alpha, gain=gain, clamp=clamp)
75
-
76
- #----------------------------------------------------------------------------
77
-
78
- @misc.profiled_function
79
- def _bias_act_ref(x, b=None, dim=1, act='linear', alpha=None, gain=None, clamp=None):
80
- """Slow reference implementation of `bias_act()` using standard TensorFlow ops.
81
- """
82
- assert isinstance(x, torch.Tensor)
83
- assert clamp is None or clamp >= 0
84
- spec = activation_funcs[act]
85
- alpha = float(alpha if alpha is not None else spec.def_alpha)
86
- gain = float(gain if gain is not None else spec.def_gain)
87
- clamp = float(clamp if clamp is not None else -1)
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)