MCPcopy Create free account
hub / github.com/FireRedTeam/FireRedTTS2 / ResnetBlock

Class ResnetBlock

fireredtts2/codec/decoder.py:8–63  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6
7
8class ResnetBlock(nn.Module):
9 def __init__(
10 self,
11 in_channels: int,
12 out_channels: int = None,
13 conv_shortcut: bool = False,
14 dropout: float = 0.0,
15 ):
16 super().__init__()
17 self.in_channels = in_channels
18 out_channels = in_channels if out_channels is None else out_channels
19 self.out_channels = out_channels
20 self.use_conv_shortcut = conv_shortcut
21
22 self.block1 = nn.Sequential(
23 nn.GroupNorm(
24 num_groups=32, num_channels=in_channels, eps=1e-6, affine=True
25 ),
26 nn.SiLU(),
27 nn.Conv1d(in_channels, out_channels, kernel_size=3, stride=1, padding=1),
28 )
29
30 self.block2 = nn.Sequential(
31 nn.GroupNorm(
32 num_groups=32, num_channels=out_channels, eps=1e-6, affine=True
33 ),
34 nn.SiLU(),
35 nn.Dropout(dropout),
36 nn.Conv1d(out_channels, out_channels, kernel_size=3, stride=1, padding=1),
37 )
38
39 if self.in_channels != self.out_channels:
40 if self.use_conv_shortcut:
41 self.conv_shortcut = torch.nn.Conv1d(
42 in_channels, out_channels, kernel_size=3, stride=1, padding=1
43 )
44 else:
45 self.nin_shortcut = torch.nn.Conv1d(
46 in_channels, out_channels, kernel_size=1, stride=1, padding=0
47 )
48
49 def forward(self, x: torch.Tensor):
50 """
51 Args:
52 x: shape (b, c, t)
53 """
54 h = x
55 h = self.block1(h)
56 h = self.block2(h)
57
58 if self.in_channels != self.out_channels:
59 if self.use_conv_shortcut:
60 x = self.conv_shortcut(x)
61 else:
62 x = self.nin_shortcut(x)
63 return x + h
64
65

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected