MCPcopy Create free account
hub / github.com/ML-GSAI/LLaDA-o / Encoder

Class Encoder

modeling/autoencoder.py:122–193  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

120
121
122class Encoder(nn.Module):
123 def __init__(
124 self,
125 resolution: int,
126 in_channels: int,
127 ch: int,
128 ch_mult: list[int],
129 num_res_blocks: int,
130 z_channels: int,
131 ):
132 super().__init__()
133 self.ch = ch
134 self.num_resolutions = len(ch_mult)
135 self.num_res_blocks = num_res_blocks
136 self.resolution = resolution
137 self.in_channels = in_channels
138 # downsampling
139 self.conv_in = nn.Conv2d(in_channels, self.ch, kernel_size=3, stride=1, padding=1)
140
141 curr_res = resolution
142 in_ch_mult = (1,) + tuple(ch_mult)
143 self.in_ch_mult = in_ch_mult
144 self.down = nn.ModuleList()
145 block_in = self.ch
146 for i_level in range(self.num_resolutions):
147 block = nn.ModuleList()
148 attn = nn.ModuleList()
149 block_in = ch * in_ch_mult[i_level]
150 block_out = ch * ch_mult[i_level]
151 for _ in range(self.num_res_blocks):
152 block.append(ResnetBlock(in_channels=block_in, out_channels=block_out))
153 block_in = block_out
154 down = nn.Module()
155 down.block = block
156 down.attn = attn
157 if i_level != self.num_resolutions - 1:
158 down.downsample = Downsample(block_in)
159 curr_res = curr_res // 2
160 self.down.append(down)
161
162 # middle
163 self.mid = nn.Module()
164 self.mid.block_1 = ResnetBlock(in_channels=block_in, out_channels=block_in)
165 self.mid.attn_1 = AttnBlock(block_in)
166 self.mid.block_2 = ResnetBlock(in_channels=block_in, out_channels=block_in)
167
168 # end
169 self.norm_out = nn.GroupNorm(num_groups=32, num_channels=block_in, eps=1e-6, affine=True)
170 self.conv_out = nn.Conv2d(block_in, 2 * z_channels, kernel_size=3, stride=1, padding=1)
171
172 def forward(self, x: Tensor) -> Tensor:
173 # downsampling
174 hs = [self.conv_in(x)]
175 for i_level in range(self.num_resolutions):
176 for i_block in range(self.num_res_blocks):
177 h = self.down[i_level].block[i_block](hs[-1])
178 if len(self.down[i_level].attn) > 0:
179 h = self.down[i_level].attn[i_block](h)

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected