Arnaudding001 commited on
Commit
956e60b
1 Parent(s): cf81973

Create stylegan_op_fused_act.py

Browse files
Files changed (1) hide show
  1. stylegan_op_fused_act.py +34 -0
stylegan_op_fused_act.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch import nn
3
+ from torch.nn import functional as F
4
+
5
+
6
+ class FusedLeakyReLU(nn.Module):
7
+ def __init__(self, channel, bias=True, negative_slope=0.2, scale=2 ** 0.5):
8
+ super().__init__()
9
+
10
+ if bias:
11
+ self.bias = nn.Parameter(torch.zeros(channel))
12
+
13
+ else:
14
+ self.bias = None
15
+
16
+ self.negative_slope = negative_slope
17
+ self.scale = scale
18
+
19
+ def forward(self, inputs):
20
+ return fused_leaky_relu(inputs, self.bias, self.negative_slope, self.scale)
21
+
22
+
23
+ def fused_leaky_relu(inputs, bias=None, negative_slope=0.2, scale=2 ** 0.5):
24
+ if bias is not None:
25
+ rest_dim = [1] * (inputs.ndim - bias.ndim - 1)
26
+ return (
27
+ F.leaky_relu(
28
+ inputs + bias.view(1, bias.shape[0], *rest_dim), negative_slope=negative_slope
29
+ )
30
+ * scale
31
+ )
32
+
33
+ else:
34
+ return F.leaky_relu(inputs, negative_slope=negative_slope) * scale