Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/tokenizer sampling #106

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 83 additions & 82 deletions cosmos1/models/tokenizer/modules/layers3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@

class CausalConv3d(nn.Module):
def __init__(
self,
chan_in: int = 1,
chan_out: int = 1,
kernel_size: Union[int, Tuple[int, int, int]] = 3,
pad_mode: str = "constant",
**kwargs,
self,
chan_in: int = 1,
chan_out: int = 1,
kernel_size: Union[int, Tuple[int, int, int]] = 3,
pad_mode: str = "constant",
**kwargs,
):
super().__init__()
kernel_size = cast_tuple(kernel_size, 3)
Expand Down Expand Up @@ -112,7 +112,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
# Shoule reverse the order of the following two ops,
# better perf and better temporal smoothness.
x = self.conv(x)
return x[..., int(time_factor - 1) :, :, :]
return x[..., int(time_factor - 1):, :, :]


class CausalDownsample3d(nn.Module):
Expand All @@ -137,11 +137,11 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:

class CausalHybridUpsample3d(nn.Module):
def __init__(
self,
in_channels: int,
spatial_up: bool = True,
temporal_up: bool = True,
**kwargs,
self,
in_channels: int,
spatial_up: bool = True,
temporal_up: bool = True,
**kwargs,
) -> None:
super().__init__()
self.conv1 = CausalConv3d(
Expand Down Expand Up @@ -181,7 +181,7 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
if isinstance(time_factor, torch.Tensor):
time_factor = time_factor.item()
x = x.repeat_interleave(int(time_factor), dim=2)
x = x[..., int(time_factor - 1) :, :, :]
x = x[..., int(time_factor - 1):, :, :]
x = self.conv1(x) + x

# hybrid upsample spatially.
Expand All @@ -196,11 +196,11 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:

class CausalHybridDownsample3d(nn.Module):
def __init__(
self,
in_channels: int,
spatial_down: bool = True,
temporal_down: bool = True,
**kwargs,
self,
in_channels: int,
spatial_down: bool = True,
temporal_down: bool = True,
**kwargs,
) -> None:
super().__init__()
self.conv1 = CausalConv3d(
Expand Down Expand Up @@ -256,12 +256,12 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:

class CausalResnetBlock3d(nn.Module):
def __init__(
self,
*,
in_channels: int,
out_channels: int = None,
dropout: float,
num_groups: int,
self,
*,
in_channels: int,
out_channels: int = None,
dropout: float,
num_groups: int,
) -> None:
super().__init__()
self.in_channels = in_channels
Expand Down Expand Up @@ -295,12 +295,12 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:

class CausalResnetBlockFactorized3d(nn.Module):
def __init__(
self,
*,
in_channels: int,
out_channels: int = None,
dropout: float,
num_groups: int,
self,
*,
in_channels: int,
out_channels: int = None,
dropout: float,
num_groups: int,
) -> None:
super().__init__()
self.in_channels = in_channels
Expand Down Expand Up @@ -449,16 +449,16 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:

class EncoderBase(nn.Module):
def __init__(
self,
in_channels: int,
channels: int,
channels_mult: list[int],
num_res_blocks: int,
attn_resolutions: list[int],
dropout: float,
resolution: int,
z_channels: int,
**ignore_kwargs,
self,
in_channels: int,
channels: int,
channels_mult: list[int],
num_res_blocks: int,
attn_resolutions: list[int],
dropout: float,
resolution: int,
z_channels: int,
**ignore_kwargs,
) -> None:
super().__init__()
self.num_resolutions = len(channels_mult)
Expand Down Expand Up @@ -571,16 +571,16 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:

class DecoderBase(nn.Module):
def __init__(
self,
out_channels: int,
channels: int,
channels_mult: list[int],
num_res_blocks: int,
attn_resolutions: list[int],
dropout: float,
resolution: int,
z_channels: int,
**ignore_kwargs,
self,
out_channels: int,
channels: int,
channels_mult: list[int],
num_res_blocks: int,
attn_resolutions: list[int],
dropout: float,
resolution: int,
z_channels: int,
**ignore_kwargs,
):
super().__init__()
self.num_resolutions = len(channels_mult)
Expand Down Expand Up @@ -677,7 +677,7 @@ def forward(self, z):
if isinstance(time_factor, torch.Tensor):
time_factor = time_factor.item()
h = h.repeat_interleave(int(time_factor), dim=2)
h = h[..., int(time_factor - 1) :, :, :]
h = h[..., int(time_factor - 1):, :, :]

h = self.norm_out(h)
h = nonlinearity(h)
Expand All @@ -688,18 +688,18 @@ def forward(self, z):

class EncoderFactorized(nn.Module):
def __init__(
self,
in_channels: int,
channels: int,
channels_mult: list[int],
num_res_blocks: int,
attn_resolutions: list[int],
dropout: float,
resolution: int,
z_channels: int,
spatial_compression: int = 16,
temporal_compression: int = 8,
**ignore_kwargs,
self,
in_channels: int,
channels: int,
channels_mult: list[int],
num_res_blocks: int,
attn_resolutions: list[int],
dropout: float,
resolution: int,
z_channels: int,
spatial_compression: int = 16,
temporal_compression: int = 8,
**ignore_kwargs,
) -> None:
super().__init__()
self.num_resolutions = len(channels_mult)
Expand All @@ -713,13 +713,13 @@ def __init__(
# calculate the number of downsample operations
self.num_spatial_downs = int(math.log2(spatial_compression)) - int(math.log2(patch_size))
assert (
self.num_spatial_downs <= self.num_resolutions
), f"Spatially downsample {self.num_resolutions} times at most"
self.num_spatial_downs <= (self.num_resolutions - 1)
), f"Spatially downsample {self.num_resolutions - 1} times at most"

self.num_temporal_downs = int(math.log2(temporal_compression)) - int(math.log2(patch_size))
assert (
self.num_temporal_downs <= self.num_resolutions
), f"Temporally downsample {self.num_resolutions} times at most"
self.num_temporal_downs <= (self.num_resolutions - 1)
), f"Temporally downsample {self.num_resolutions - 1} times at most"

# downsampling
self.conv_in = nn.Sequential(
Expand Down Expand Up @@ -770,7 +770,8 @@ def __init__(
spatial_down=spatial_down,
temporal_down=temporal_down,
)
curr_res = curr_res // 2
if spatial_down:
curr_res = curr_res // 2
self.down.append(down)

# middle
Expand Down Expand Up @@ -834,18 +835,18 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:

class DecoderFactorized(nn.Module):
def __init__(
self,
out_channels: int,
channels: int,
channels_mult: list[int],
num_res_blocks: int,
attn_resolutions: list[int],
dropout: float,
resolution: int,
z_channels: int,
spatial_compression: int = 16,
temporal_compression: int = 8,
**ignore_kwargs,
self,
out_channels: int,
channels: int,
channels_mult: list[int],
num_res_blocks: int,
attn_resolutions: list[int],
dropout: float,
resolution: int,
z_channels: int,
spatial_compression: int = 16,
temporal_compression: int = 8,
**ignore_kwargs,
):
super().__init__()
self.num_resolutions = len(channels_mult)
Expand All @@ -861,7 +862,7 @@ def __init__(
assert self.num_spatial_ups <= self.num_resolutions, f"Spatially upsample {self.num_resolutions} times at most"
self.num_temporal_ups = int(math.log2(temporal_compression)) - int(math.log2(patch_size))
assert (
self.num_temporal_ups <= self.num_resolutions
self.num_temporal_ups <= self.num_resolutions
), f"Temporally upsample {self.num_resolutions} times at most"

block_in = channels * channels_mult[self.num_resolutions - 1]
Expand Down Expand Up @@ -932,7 +933,7 @@ def __init__(
else:
temporal_up = 0 < i_level_reverse < self.num_temporal_ups + 1
spatial_up = temporal_up or (
i_level_reverse < self.num_spatial_ups and self.num_spatial_ups > self.num_temporal_ups
i_level_reverse < self.num_spatial_ups and self.num_spatial_ups > self.num_temporal_ups
)
up.upsample = CausalHybridUpsample3d(block_in, spatial_up=spatial_up, temporal_up=temporal_up)
curr_res = curr_res * 2
Expand Down
Loading