| 569 | |
| 570 | |
| 571 | class DownSample3D(nn.Module): |
| 572 | def __init__(self, in_channels, with_conv, compress_time=False, out_channels=None): |
| 573 | super().__init__() |
| 574 | self.with_conv = with_conv |
| 575 | if out_channels is None: |
| 576 | out_channels = in_channels |
| 577 | if self.with_conv: |
| 578 | # no asymmetric padding in torch conv, must do it ourselves |
| 579 | self.conv = torch.nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=2, padding=0) |
| 580 | self.compress_time = compress_time |
| 581 | |
| 582 | def forward(self, x): |
| 583 | if self.compress_time and x.shape[2] > 1: |
| 584 | h, w = x.shape[-2:] |
| 585 | x = rearrange(x, "b c t h w -> (b h w) c t") |
| 586 | |
| 587 | if x.shape[-1] % 2 == 1: |
| 588 | # split first frame |
| 589 | x_first, x_rest = x[..., 0], x[..., 1:] |
| 590 | |
| 591 | if x_rest.shape[-1] > 0: |
| 592 | x_rest = torch.nn.functional.avg_pool1d(x_rest, kernel_size=2, stride=2) |
| 593 | x = torch.cat([x_first[..., None], x_rest], dim=-1) |
| 594 | x = rearrange(x, "(b h w) c t -> b c t h w", h=h, w=w) |
| 595 | else: |
| 596 | x = torch.nn.functional.avg_pool1d(x, kernel_size=2, stride=2) |
| 597 | x = rearrange(x, "(b h w) c t -> b c t h w", h=h, w=w) |
| 598 | |
| 599 | if self.with_conv: |
| 600 | pad = (0, 1, 0, 1) |
| 601 | x = torch.nn.functional.pad(x, pad, mode="constant", value=0) |
| 602 | t = x.shape[2] |
| 603 | x = rearrange(x, "b c t h w -> (b t) c h w") |
| 604 | x = self.conv(x) |
| 605 | x = rearrange(x, "(b t) c h w -> b c t h w", t=t) |
| 606 | else: |
| 607 | t = x.shape[2] |
| 608 | x = rearrange(x, "b c t h w -> (b t) c h w") |
| 609 | x = torch.nn.functional.avg_pool2d(x, kernel_size=2, stride=2) |
| 610 | x = rearrange(x, "(b t) c h w -> b c t h w", t=t) |
| 611 | return x |
| 612 | |
| 613 | |
| 614 | class ContextParallelResnetBlock3D(nn.Module): |