| 92 | |
| 93 | |
| 94 | class ResnetBlock(torch.nn.Module): |
| 95 | def __init__(self, in_channels, out_channels, temb_channels=None, groups=32, eps=1e-5): |
| 96 | super().__init__() |
| 97 | self.norm1 = torch.nn.GroupNorm(num_groups=groups, num_channels=in_channels, eps=eps, affine=True) |
| 98 | self.conv1 = torch.nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1) |
| 99 | if temb_channels is not None: |
| 100 | self.time_emb_proj = torch.nn.Linear(temb_channels, out_channels) |
| 101 | self.norm2 = torch.nn.GroupNorm(num_groups=groups, num_channels=out_channels, eps=eps, affine=True) |
| 102 | self.conv2 = torch.nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1) |
| 103 | self.nonlinearity = torch.nn.SiLU() |
| 104 | self.conv_shortcut = None |
| 105 | if in_channels != out_channels: |
| 106 | self.conv_shortcut = torch.nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=True) |
| 107 | |
| 108 | def forward(self, hidden_states, time_emb, text_emb, res_stack, **kwargs): |
| 109 | x = hidden_states |
| 110 | x = self.norm1(x) |
| 111 | x = self.nonlinearity(x) |
| 112 | x = self.conv1(x) |
| 113 | if time_emb is not None: |
| 114 | emb = self.nonlinearity(time_emb) |
| 115 | emb = self.time_emb_proj(emb)[:, :, None, None] |
| 116 | x = x + emb |
| 117 | x = self.norm2(x) |
| 118 | x = self.nonlinearity(x) |
| 119 | x = self.conv2(x) |
| 120 | if self.conv_shortcut is not None: |
| 121 | hidden_states = self.conv_shortcut(hidden_states) |
| 122 | hidden_states = hidden_states + x |
| 123 | return hidden_states, time_emb, text_emb, res_stack |
| 124 | |
| 125 | |
| 126 | class AttentionBlock(torch.nn.Module): |