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

Class CausalResnetBlock

fireredtts2/codec/decoder.py:105–171  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

103
104# A causal variant of ResnetBlock
105class CausalResnetBlock(nn.Module):
106 def __init__(
107 self,
108 in_channels: int,
109 out_channels: int = None,
110 dropout: float = 0.0,
111 ):
112 super().__init__()
113 self.in_channels = in_channels
114 out_channels = in_channels if out_channels is None else out_channels
115 self.out_channels = out_channels
116
117 self.block1 = nn.Sequential(
118 Transpose(1, 2),
119 nn.LayerNorm(in_channels),
120 Transpose(1, 2),
121 nn.SiLU(),
122 CausalConv1d(in_channels, out_channels, kernel_size=3),
123 )
124
125 self.block2 = nn.Sequential(
126 Transpose(1, 2),
127 nn.LayerNorm(out_channels),
128 Transpose(1, 2),
129 nn.SiLU(),
130 nn.Dropout(dropout),
131 CausalConv1d(out_channels, out_channels, kernel_size=3),
132 )
133 if self.in_channels != self.out_channels:
134 self.nin_shortcut = torch.nn.Conv1d(
135 in_channels, out_channels, kernel_size=1, stride=1, padding=0
136 )
137
138 def forward(self, x: torch.Tensor):
139 """
140 Args:
141 x: shape (b, c, t)
142 """
143 h = x
144 h = self.block1(h)
145 h = self.block2(h)
146 if self.in_channels != self.out_channels:
147 x = self.nin_shortcut(x)
148 return x + h
149
150 def forward_chunk(self, x: torch.Tensor, cache: torch.Tensor = None):
151 """
152 Args:
153 x: shape (b, c, t)
154 cache: shape (b, c_in+c_out, t=2)
155 """
156 cache1, cache2 = (
157 (None, None)
158 if cache is None
159 else cache.split((self.in_channels, self.out_channels), dim=1)
160 )
161 h = x
162 # block1

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected