Spaces:
Running
on
L40S
Running
on
L40S
File size: 8,188 Bytes
a342aa8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
from dataclasses import dataclass, field
import torch
import torch.nn as nn
from seva.modules.layers import (
Downsample,
GroupNorm32,
ResBlock,
TimestepEmbedSequential,
Upsample,
timestep_embedding,
)
from seva.modules.transformer import MultiviewTransformer
@dataclass
class SevaParams(object):
in_channels: int = 11
model_channels: int = 320
out_channels: int = 4
num_frames: int = 21
num_res_blocks: int = 2
attention_resolutions: list[int] = field(default_factory=lambda: [4, 2, 1])
channel_mult: list[int] = field(default_factory=lambda: [1, 2, 4, 4])
num_head_channels: int = 64
transformer_depth: list[int] = field(default_factory=lambda: [1, 1, 1, 1])
context_dim: int = 1024
dense_in_channels: int = 6
dropout: float = 0.0
unflatten_names: list[str] = field(
default_factory=lambda: ["middle_ds8", "output_ds4", "output_ds2"]
)
def __post_init__(self):
assert len(self.channel_mult) == len(self.transformer_depth)
class Seva(nn.Module):
def __init__(self, params: SevaParams) -> None:
super().__init__()
self.params = params
self.model_channels = params.model_channels
self.out_channels = params.out_channels
self.num_head_channels = params.num_head_channels
time_embed_dim = params.model_channels * 4
self.time_embed = nn.Sequential(
nn.Linear(params.model_channels, time_embed_dim),
nn.SiLU(),
nn.Linear(time_embed_dim, time_embed_dim),
)
self.input_blocks = nn.ModuleList(
[
TimestepEmbedSequential(
nn.Conv2d(params.in_channels, params.model_channels, 3, padding=1)
)
]
)
self._feature_size = params.model_channels
input_block_chans = [params.model_channels]
ch = params.model_channels
ds = 1
for level, mult in enumerate(params.channel_mult):
for _ in range(params.num_res_blocks):
input_layers: list[ResBlock | MultiviewTransformer | Downsample] = [
ResBlock(
channels=ch,
emb_channels=time_embed_dim,
out_channels=mult * params.model_channels,
dense_in_channels=params.dense_in_channels,
dropout=params.dropout,
)
]
ch = mult * params.model_channels
if ds in params.attention_resolutions:
num_heads = ch // params.num_head_channels
dim_head = params.num_head_channels
input_layers.append(
MultiviewTransformer(
ch,
num_heads,
dim_head,
name=f"input_ds{ds}",
depth=params.transformer_depth[level],
context_dim=params.context_dim,
unflatten_names=params.unflatten_names,
)
)
self.input_blocks.append(TimestepEmbedSequential(*input_layers))
self._feature_size += ch
input_block_chans.append(ch)
if level != len(params.channel_mult) - 1:
ds *= 2
out_ch = ch
self.input_blocks.append(
TimestepEmbedSequential(Downsample(ch, out_channels=out_ch))
)
ch = out_ch
input_block_chans.append(ch)
self._feature_size += ch
num_heads = ch // params.num_head_channels
dim_head = params.num_head_channels
self.middle_block = TimestepEmbedSequential(
ResBlock(
channels=ch,
emb_channels=time_embed_dim,
out_channels=None,
dense_in_channels=params.dense_in_channels,
dropout=params.dropout,
),
MultiviewTransformer(
ch,
num_heads,
dim_head,
name=f"middle_ds{ds}",
depth=params.transformer_depth[-1],
context_dim=params.context_dim,
unflatten_names=params.unflatten_names,
),
ResBlock(
channels=ch,
emb_channels=time_embed_dim,
out_channels=None,
dense_in_channels=params.dense_in_channels,
dropout=params.dropout,
),
)
self._feature_size += ch
self.output_blocks = nn.ModuleList([])
for level, mult in list(enumerate(params.channel_mult))[::-1]:
for i in range(params.num_res_blocks + 1):
ich = input_block_chans.pop()
output_layers: list[ResBlock | MultiviewTransformer | Upsample] = [
ResBlock(
channels=ch + ich,
emb_channels=time_embed_dim,
out_channels=params.model_channels * mult,
dense_in_channels=params.dense_in_channels,
dropout=params.dropout,
)
]
ch = params.model_channels * mult
if ds in params.attention_resolutions:
num_heads = ch // params.num_head_channels
dim_head = params.num_head_channels
output_layers.append(
MultiviewTransformer(
ch,
num_heads,
dim_head,
name=f"output_ds{ds}",
depth=params.transformer_depth[level],
context_dim=params.context_dim,
unflatten_names=params.unflatten_names,
)
)
if level and i == params.num_res_blocks:
out_ch = ch
ds //= 2
output_layers.append(Upsample(ch, out_ch))
self.output_blocks.append(TimestepEmbedSequential(*output_layers))
self._feature_size += ch
self.out = nn.Sequential(
GroupNorm32(32, ch),
nn.SiLU(),
nn.Conv2d(self.model_channels, params.out_channels, 3, padding=1),
)
def forward(
self,
x: torch.Tensor,
t: torch.Tensor,
y: torch.Tensor,
dense_y: torch.Tensor,
num_frames: int | None = None,
) -> torch.Tensor:
num_frames = num_frames or self.params.num_frames
t_emb = timestep_embedding(t, self.model_channels)
t_emb = self.time_embed(t_emb)
hs = []
h = x
for module in self.input_blocks:
h = module(
h,
emb=t_emb,
context=y,
dense_emb=dense_y,
num_frames=num_frames,
)
hs.append(h)
h = self.middle_block(
h,
emb=t_emb,
context=y,
dense_emb=dense_y,
num_frames=num_frames,
)
for module in self.output_blocks:
h = torch.cat([h, hs.pop()], dim=1)
h = module(
h,
emb=t_emb,
context=y,
dense_emb=dense_y,
num_frames=num_frames,
)
h = h.type(x.dtype)
return self.out(h)
class SGMWrapper(nn.Module):
def __init__(self, module: Seva):
super().__init__()
self.module = module
def forward(
self, x: torch.Tensor, t: torch.Tensor, c: dict, **kwargs
) -> torch.Tensor:
x = torch.cat((x, c.get("concat", torch.Tensor([]).type_as(x))), dim=1)
return self.module(
x,
t=t,
y=c["crossattn"],
dense_y=c["dense_vector"],
**kwargs,
)
|