(self, x, t=None, context=None)
| 405 | padding=1) |
| 406 | |
| 407 | def forward(self, x, t=None, context=None): |
| 408 | #assert x.shape[2] == x.shape[3] == self.resolution |
| 409 | if context is not None: |
| 410 | # assume aligned context, cat along channel axis |
| 411 | x = torch.cat((x, context), dim=1) |
| 412 | if self.use_timestep: |
| 413 | # timestep embedding |
| 414 | assert t is not None |
| 415 | temb = get_timestep_embedding(t, self.ch) |
| 416 | temb = self.temb.dense[0](temb) |
| 417 | temb = nonlinearity(temb) |
| 418 | temb = self.temb.dense[1](temb) |
| 419 | else: |
| 420 | temb = None |
| 421 | |
| 422 | # downsampling |
| 423 | hs = [self.conv_in(x)] |
| 424 | for i_level in range(self.num_resolutions): |
| 425 | for i_block in range(self.num_res_blocks): |
| 426 | h = self.down[i_level].block[i_block](hs[-1], temb) |
| 427 | if len(self.down[i_level].attn) > 0: |
| 428 | h = self.down[i_level].attn[i_block](h) |
| 429 | hs.append(h) |
| 430 | if i_level != self.num_resolutions-1: |
| 431 | hs.append(self.down[i_level].downsample(hs[-1])) |
| 432 | |
| 433 | # middle |
| 434 | h = hs[-1] |
| 435 | h = self.mid.block_1(h, temb) |
| 436 | h = self.mid.attn_1(h) |
| 437 | h = self.mid.block_2(h, temb) |
| 438 | |
| 439 | # upsampling |
| 440 | for i_level in reversed(range(self.num_resolutions)): |
| 441 | for i_block in range(self.num_res_blocks+1): |
| 442 | h = self.up[i_level].block[i_block]( |
| 443 | torch.cat([h, hs.pop()], dim=1), temb) |
| 444 | if len(self.up[i_level].attn) > 0: |
| 445 | h = self.up[i_level].attn[i_block](h) |
| 446 | if i_level != 0: |
| 447 | h = self.up[i_level].upsample(h) |
| 448 | |
| 449 | # end |
| 450 | h = self.norm_out(h) |
| 451 | h = nonlinearity(h) |
| 452 | h = self.conv_out(h) |
| 453 | return h |
| 454 | |
| 455 | def get_last_layer(self): |
| 456 | return self.conv_out.weight |
no test coverage detected