Spaces:
Runtime error
Runtime error
Update Time_TravelRephotography/op/fused_act.py
Browse files
Time_TravelRephotography/op/fused_act.py
CHANGED
@@ -1,87 +1,108 @@
|
|
1 |
import os
|
2 |
-
|
|
|
3 |
import torch
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|