返回 JoyAI-Echo
res_block.py
根目录 / ltx-core / src / ltx_core / model / upsampler / res_block.py
1 from typing import Optional
2
3 import torch
4
5
6 class ResBlock(torch.nn.Module):
7 """
8 Residual block with two convolutional layers, group normalization, and SiLU activation.
9 Args:
10 channels (int): Number of input and output channels.
11 mid_channels (Optional[int]): Number of channels in the intermediate convolution layer. Defaults to `channels`
12 if not specified.
13 dims (int): Dimensionality of the convolution (2 for Conv2d, 3 for Conv3d). Defaults to 3.
14 """
15
16 def __init__(self, channels: int, mid_channels: Optional[int] = None, dims: int = 3):
17 super().__init__()
18 if mid_channels is None:
19 mid_channels = channels
20
21 conv = torch.nn.Conv2d if dims == 2 else torch.nn.Conv3d
22
23 self.conv1 = conv(channels, mid_channels, kernel_size=3, padding=1)
24 self.norm1 = torch.nn.GroupNorm(32, mid_channels)
25 self.conv2 = conv(mid_channels, channels, kernel_size=3, padding=1)
26 self.norm2 = torch.nn.GroupNorm(32, channels)
27 self.activation = torch.nn.SiLU()
28
29 def forward(self, x: torch.Tensor) -> torch.Tensor:
30 residual = x
31 x = self.conv1(x)
32 x = self.norm1(x)
33 x = self.activation(x)
34 x = self.conv2(x)
35 x = self.norm2(x)
36 x = self.activation(x + residual)
37 return x
38
38 lines PYTHON