thanks to showlab ❤
Browse files- __pycache__/unet_3d_blocks.cpython-310.pyc +0 -0
- __pycache__/unet_3d_condition.cpython-310.pyc +0 -0
- unet_3d_blocks.py +842 -0
- unet_3d_condition.py +500 -0
- zeroscope_v2_576w/.gitattributes +35 -0
- zeroscope_v2_576w/README.md +64 -0
- zeroscope_v2_576w/model_index.json +24 -0
- zeroscope_v2_576w/scheduler/scheduler_config.json +18 -0
- zeroscope_v2_576w/text_encoder/config.json +25 -0
- zeroscope_v2_576w/text_encoder/pytorch_model.bin +3 -0
- zeroscope_v2_576w/tokenizer/merges.txt +0 -0
- zeroscope_v2_576w/tokenizer/special_tokens_map.json +24 -0
- zeroscope_v2_576w/tokenizer/tokenizer_config.json +33 -0
- zeroscope_v2_576w/tokenizer/vocab.json +0 -0
- zeroscope_v2_576w/unet/config.json +34 -0
- zeroscope_v2_576w/unet/diffusion_pytorch_model.bin +3 -0
- zeroscope_v2_576w/vae/config.json +31 -0
- zeroscope_v2_576w/vae/diffusion_pytorch_model.bin +3 -0
__pycache__/unet_3d_blocks.cpython-310.pyc
ADDED
|
Binary file (12.9 kB). View file
|
|
|
__pycache__/unet_3d_condition.cpython-310.pyc
ADDED
|
Binary file (13.8 kB). View file
|
|
|
unet_3d_blocks.py
ADDED
|
@@ -0,0 +1,842 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2023 The HuggingFace Team. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
|
| 15 |
+
import torch
|
| 16 |
+
import torch.utils.checkpoint as checkpoint
|
| 17 |
+
from torch import nn
|
| 18 |
+
from diffusers.models.resnet import Downsample2D, ResnetBlock2D, TemporalConvLayer, Upsample2D
|
| 19 |
+
from diffusers.models.transformer_2d import Transformer2DModel
|
| 20 |
+
from diffusers.models.transformer_temporal import TransformerTemporalModel
|
| 21 |
+
|
| 22 |
+
# Assign gradient checkpoint function to simple variable for readability.
|
| 23 |
+
g_c = checkpoint.checkpoint
|
| 24 |
+
|
| 25 |
+
def use_temporal(module, num_frames, x):
|
| 26 |
+
if num_frames == 1:
|
| 27 |
+
if isinstance(module, TransformerTemporalModel):
|
| 28 |
+
return {"sample": x}
|
| 29 |
+
else:
|
| 30 |
+
return x
|
| 31 |
+
|
| 32 |
+
def custom_checkpoint(module, mode=None):
|
| 33 |
+
if mode == None: raise ValueError('Mode for gradient checkpointing cannot be none.')
|
| 34 |
+
custom_forward = None
|
| 35 |
+
|
| 36 |
+
if mode == 'resnet':
|
| 37 |
+
def custom_forward(hidden_states, temb):
|
| 38 |
+
inputs = module(hidden_states, temb)
|
| 39 |
+
return inputs
|
| 40 |
+
|
| 41 |
+
if mode == 'attn':
|
| 42 |
+
def custom_forward(
|
| 43 |
+
hidden_states,
|
| 44 |
+
encoder_hidden_states=None,
|
| 45 |
+
cross_attention_kwargs=None
|
| 46 |
+
):
|
| 47 |
+
inputs = module(
|
| 48 |
+
hidden_states,
|
| 49 |
+
encoder_hidden_states,
|
| 50 |
+
cross_attention_kwargs
|
| 51 |
+
)
|
| 52 |
+
return inputs
|
| 53 |
+
|
| 54 |
+
if mode == 'temp':
|
| 55 |
+
def custom_forward(hidden_states, num_frames=None):
|
| 56 |
+
inputs = use_temporal(module, num_frames, hidden_states)
|
| 57 |
+
if inputs is None: inputs = module(
|
| 58 |
+
hidden_states,
|
| 59 |
+
num_frames=num_frames
|
| 60 |
+
)
|
| 61 |
+
return inputs
|
| 62 |
+
|
| 63 |
+
return custom_forward
|
| 64 |
+
|
| 65 |
+
def transformer_g_c(transformer, sample, num_frames):
|
| 66 |
+
sample = g_c(custom_checkpoint(transformer, mode='temp'),
|
| 67 |
+
sample, num_frames, use_reentrant=False
|
| 68 |
+
)['sample']
|
| 69 |
+
|
| 70 |
+
return sample
|
| 71 |
+
|
| 72 |
+
def cross_attn_g_c(
|
| 73 |
+
attn,
|
| 74 |
+
temp_attn,
|
| 75 |
+
resnet,
|
| 76 |
+
temp_conv,
|
| 77 |
+
hidden_states,
|
| 78 |
+
encoder_hidden_states,
|
| 79 |
+
cross_attention_kwargs,
|
| 80 |
+
temb,
|
| 81 |
+
num_frames,
|
| 82 |
+
inverse_temp=False
|
| 83 |
+
):
|
| 84 |
+
|
| 85 |
+
def ordered_g_c(idx):
|
| 86 |
+
|
| 87 |
+
# Self and CrossAttention
|
| 88 |
+
if idx == 0: return g_c(custom_checkpoint(attn, mode='attn'),
|
| 89 |
+
hidden_states, encoder_hidden_states,cross_attention_kwargs, use_reentrant=False
|
| 90 |
+
)['sample']
|
| 91 |
+
|
| 92 |
+
# Temporal Self and CrossAttention
|
| 93 |
+
if idx == 1: return g_c(custom_checkpoint(temp_attn, mode='temp'),
|
| 94 |
+
hidden_states, num_frames, use_reentrant=False)['sample']
|
| 95 |
+
|
| 96 |
+
# Resnets
|
| 97 |
+
if idx == 2: return g_c(custom_checkpoint(resnet, mode='resnet'),
|
| 98 |
+
hidden_states, temb, use_reentrant=False)
|
| 99 |
+
|
| 100 |
+
# Temporal Convolutions
|
| 101 |
+
if idx == 3: return g_c(custom_checkpoint(temp_conv, mode='temp'),
|
| 102 |
+
hidden_states, num_frames, use_reentrant=False
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Here we call the function depending on the order in which they are called.
|
| 106 |
+
# For some layers, the orders are different, so we access the appropriate one by index.
|
| 107 |
+
|
| 108 |
+
if not inverse_temp:
|
| 109 |
+
for idx in [0,1,2,3]: hidden_states = ordered_g_c(idx)
|
| 110 |
+
else:
|
| 111 |
+
for idx in [2,3,0,1]: hidden_states = ordered_g_c(idx)
|
| 112 |
+
|
| 113 |
+
return hidden_states
|
| 114 |
+
|
| 115 |
+
def up_down_g_c(resnet, temp_conv, hidden_states, temb, num_frames):
|
| 116 |
+
hidden_states = g_c(custom_checkpoint(resnet, mode='resnet'), hidden_states, temb, use_reentrant=False)
|
| 117 |
+
hidden_states = g_c(custom_checkpoint(temp_conv, mode='temp'),
|
| 118 |
+
hidden_states, num_frames, use_reentrant=False
|
| 119 |
+
)
|
| 120 |
+
return hidden_states
|
| 121 |
+
|
| 122 |
+
def get_down_block(
|
| 123 |
+
down_block_type,
|
| 124 |
+
num_layers,
|
| 125 |
+
in_channels,
|
| 126 |
+
out_channels,
|
| 127 |
+
temb_channels,
|
| 128 |
+
add_downsample,
|
| 129 |
+
resnet_eps,
|
| 130 |
+
resnet_act_fn,
|
| 131 |
+
attn_num_head_channels,
|
| 132 |
+
resnet_groups=None,
|
| 133 |
+
cross_attention_dim=None,
|
| 134 |
+
downsample_padding=None,
|
| 135 |
+
dual_cross_attention=False,
|
| 136 |
+
use_linear_projection=True,
|
| 137 |
+
only_cross_attention=False,
|
| 138 |
+
upcast_attention=False,
|
| 139 |
+
resnet_time_scale_shift="default",
|
| 140 |
+
):
|
| 141 |
+
if down_block_type == "DownBlock3D":
|
| 142 |
+
return DownBlock3D(
|
| 143 |
+
num_layers=num_layers,
|
| 144 |
+
in_channels=in_channels,
|
| 145 |
+
out_channels=out_channels,
|
| 146 |
+
temb_channels=temb_channels,
|
| 147 |
+
add_downsample=add_downsample,
|
| 148 |
+
resnet_eps=resnet_eps,
|
| 149 |
+
resnet_act_fn=resnet_act_fn,
|
| 150 |
+
resnet_groups=resnet_groups,
|
| 151 |
+
downsample_padding=downsample_padding,
|
| 152 |
+
resnet_time_scale_shift=resnet_time_scale_shift,
|
| 153 |
+
)
|
| 154 |
+
elif down_block_type == "CrossAttnDownBlock3D":
|
| 155 |
+
if cross_attention_dim is None:
|
| 156 |
+
raise ValueError("cross_attention_dim must be specified for CrossAttnDownBlock3D")
|
| 157 |
+
return CrossAttnDownBlock3D(
|
| 158 |
+
num_layers=num_layers,
|
| 159 |
+
in_channels=in_channels,
|
| 160 |
+
out_channels=out_channels,
|
| 161 |
+
temb_channels=temb_channels,
|
| 162 |
+
add_downsample=add_downsample,
|
| 163 |
+
resnet_eps=resnet_eps,
|
| 164 |
+
resnet_act_fn=resnet_act_fn,
|
| 165 |
+
resnet_groups=resnet_groups,
|
| 166 |
+
downsample_padding=downsample_padding,
|
| 167 |
+
cross_attention_dim=cross_attention_dim,
|
| 168 |
+
attn_num_head_channels=attn_num_head_channels,
|
| 169 |
+
dual_cross_attention=dual_cross_attention,
|
| 170 |
+
use_linear_projection=use_linear_projection,
|
| 171 |
+
only_cross_attention=only_cross_attention,
|
| 172 |
+
upcast_attention=upcast_attention,
|
| 173 |
+
resnet_time_scale_shift=resnet_time_scale_shift,
|
| 174 |
+
)
|
| 175 |
+
raise ValueError(f"{down_block_type} does not exist.")
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def get_up_block(
|
| 179 |
+
up_block_type,
|
| 180 |
+
num_layers,
|
| 181 |
+
in_channels,
|
| 182 |
+
out_channels,
|
| 183 |
+
prev_output_channel,
|
| 184 |
+
temb_channels,
|
| 185 |
+
add_upsample,
|
| 186 |
+
resnet_eps,
|
| 187 |
+
resnet_act_fn,
|
| 188 |
+
attn_num_head_channels,
|
| 189 |
+
resnet_groups=None,
|
| 190 |
+
cross_attention_dim=None,
|
| 191 |
+
dual_cross_attention=False,
|
| 192 |
+
use_linear_projection=True,
|
| 193 |
+
only_cross_attention=False,
|
| 194 |
+
upcast_attention=False,
|
| 195 |
+
resnet_time_scale_shift="default",
|
| 196 |
+
):
|
| 197 |
+
if up_block_type == "UpBlock3D":
|
| 198 |
+
return UpBlock3D(
|
| 199 |
+
num_layers=num_layers,
|
| 200 |
+
in_channels=in_channels,
|
| 201 |
+
out_channels=out_channels,
|
| 202 |
+
prev_output_channel=prev_output_channel,
|
| 203 |
+
temb_channels=temb_channels,
|
| 204 |
+
add_upsample=add_upsample,
|
| 205 |
+
resnet_eps=resnet_eps,
|
| 206 |
+
resnet_act_fn=resnet_act_fn,
|
| 207 |
+
resnet_groups=resnet_groups,
|
| 208 |
+
resnet_time_scale_shift=resnet_time_scale_shift,
|
| 209 |
+
)
|
| 210 |
+
elif up_block_type == "CrossAttnUpBlock3D":
|
| 211 |
+
if cross_attention_dim is None:
|
| 212 |
+
raise ValueError("cross_attention_dim must be specified for CrossAttnUpBlock3D")
|
| 213 |
+
return CrossAttnUpBlock3D(
|
| 214 |
+
num_layers=num_layers,
|
| 215 |
+
in_channels=in_channels,
|
| 216 |
+
out_channels=out_channels,
|
| 217 |
+
prev_output_channel=prev_output_channel,
|
| 218 |
+
temb_channels=temb_channels,
|
| 219 |
+
add_upsample=add_upsample,
|
| 220 |
+
resnet_eps=resnet_eps,
|
| 221 |
+
resnet_act_fn=resnet_act_fn,
|
| 222 |
+
resnet_groups=resnet_groups,
|
| 223 |
+
cross_attention_dim=cross_attention_dim,
|
| 224 |
+
attn_num_head_channels=attn_num_head_channels,
|
| 225 |
+
dual_cross_attention=dual_cross_attention,
|
| 226 |
+
use_linear_projection=use_linear_projection,
|
| 227 |
+
only_cross_attention=only_cross_attention,
|
| 228 |
+
upcast_attention=upcast_attention,
|
| 229 |
+
resnet_time_scale_shift=resnet_time_scale_shift,
|
| 230 |
+
)
|
| 231 |
+
raise ValueError(f"{up_block_type} does not exist.")
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
class UNetMidBlock3DCrossAttn(nn.Module):
|
| 235 |
+
def __init__(
|
| 236 |
+
self,
|
| 237 |
+
in_channels: int,
|
| 238 |
+
temb_channels: int,
|
| 239 |
+
dropout: float = 0.0,
|
| 240 |
+
num_layers: int = 1,
|
| 241 |
+
resnet_eps: float = 1e-6,
|
| 242 |
+
resnet_time_scale_shift: str = "default",
|
| 243 |
+
resnet_act_fn: str = "swish",
|
| 244 |
+
resnet_groups: int = 32,
|
| 245 |
+
resnet_pre_norm: bool = True,
|
| 246 |
+
attn_num_head_channels=1,
|
| 247 |
+
output_scale_factor=1.0,
|
| 248 |
+
cross_attention_dim=1280,
|
| 249 |
+
dual_cross_attention=False,
|
| 250 |
+
use_linear_projection=True,
|
| 251 |
+
upcast_attention=False,
|
| 252 |
+
):
|
| 253 |
+
super().__init__()
|
| 254 |
+
|
| 255 |
+
self.gradient_checkpointing = False
|
| 256 |
+
self.has_cross_attention = True
|
| 257 |
+
self.attn_num_head_channels = attn_num_head_channels
|
| 258 |
+
resnet_groups = resnet_groups if resnet_groups is not None else min(in_channels // 4, 32)
|
| 259 |
+
|
| 260 |
+
# there is always at least one resnet
|
| 261 |
+
resnets = [
|
| 262 |
+
ResnetBlock2D(
|
| 263 |
+
in_channels=in_channels,
|
| 264 |
+
out_channels=in_channels,
|
| 265 |
+
temb_channels=temb_channels,
|
| 266 |
+
eps=resnet_eps,
|
| 267 |
+
groups=resnet_groups,
|
| 268 |
+
dropout=dropout,
|
| 269 |
+
time_embedding_norm=resnet_time_scale_shift,
|
| 270 |
+
non_linearity=resnet_act_fn,
|
| 271 |
+
output_scale_factor=output_scale_factor,
|
| 272 |
+
pre_norm=resnet_pre_norm,
|
| 273 |
+
)
|
| 274 |
+
]
|
| 275 |
+
temp_convs = [
|
| 276 |
+
TemporalConvLayer(
|
| 277 |
+
in_channels,
|
| 278 |
+
in_channels,
|
| 279 |
+
dropout=0.1
|
| 280 |
+
)
|
| 281 |
+
]
|
| 282 |
+
attentions = []
|
| 283 |
+
temp_attentions = []
|
| 284 |
+
|
| 285 |
+
for _ in range(num_layers):
|
| 286 |
+
attentions.append(
|
| 287 |
+
Transformer2DModel(
|
| 288 |
+
in_channels // attn_num_head_channels,
|
| 289 |
+
attn_num_head_channels,
|
| 290 |
+
in_channels=in_channels,
|
| 291 |
+
num_layers=1,
|
| 292 |
+
cross_attention_dim=cross_attention_dim,
|
| 293 |
+
norm_num_groups=resnet_groups,
|
| 294 |
+
use_linear_projection=use_linear_projection,
|
| 295 |
+
upcast_attention=upcast_attention,
|
| 296 |
+
)
|
| 297 |
+
)
|
| 298 |
+
temp_attentions.append(
|
| 299 |
+
TransformerTemporalModel(
|
| 300 |
+
in_channels // attn_num_head_channels,
|
| 301 |
+
attn_num_head_channels,
|
| 302 |
+
in_channels=in_channels,
|
| 303 |
+
num_layers=1,
|
| 304 |
+
cross_attention_dim=cross_attention_dim,
|
| 305 |
+
norm_num_groups=resnet_groups,
|
| 306 |
+
)
|
| 307 |
+
)
|
| 308 |
+
resnets.append(
|
| 309 |
+
ResnetBlock2D(
|
| 310 |
+
in_channels=in_channels,
|
| 311 |
+
out_channels=in_channels,
|
| 312 |
+
temb_channels=temb_channels,
|
| 313 |
+
eps=resnet_eps,
|
| 314 |
+
groups=resnet_groups,
|
| 315 |
+
dropout=dropout,
|
| 316 |
+
time_embedding_norm=resnet_time_scale_shift,
|
| 317 |
+
non_linearity=resnet_act_fn,
|
| 318 |
+
output_scale_factor=output_scale_factor,
|
| 319 |
+
pre_norm=resnet_pre_norm,
|
| 320 |
+
)
|
| 321 |
+
)
|
| 322 |
+
temp_convs.append(
|
| 323 |
+
TemporalConvLayer(
|
| 324 |
+
in_channels,
|
| 325 |
+
in_channels,
|
| 326 |
+
dropout=0.1
|
| 327 |
+
)
|
| 328 |
+
)
|
| 329 |
+
|
| 330 |
+
self.resnets = nn.ModuleList(resnets)
|
| 331 |
+
self.temp_convs = nn.ModuleList(temp_convs)
|
| 332 |
+
self.attentions = nn.ModuleList(attentions)
|
| 333 |
+
self.temp_attentions = nn.ModuleList(temp_attentions)
|
| 334 |
+
|
| 335 |
+
def forward(
|
| 336 |
+
self,
|
| 337 |
+
hidden_states,
|
| 338 |
+
temb=None,
|
| 339 |
+
encoder_hidden_states=None,
|
| 340 |
+
attention_mask=None,
|
| 341 |
+
num_frames=1,
|
| 342 |
+
cross_attention_kwargs=None,
|
| 343 |
+
):
|
| 344 |
+
if self.gradient_checkpointing:
|
| 345 |
+
hidden_states = up_down_g_c(
|
| 346 |
+
self.resnets[0],
|
| 347 |
+
self.temp_convs[0],
|
| 348 |
+
hidden_states,
|
| 349 |
+
temb,
|
| 350 |
+
num_frames
|
| 351 |
+
)
|
| 352 |
+
else:
|
| 353 |
+
hidden_states = self.resnets[0](hidden_states, temb)
|
| 354 |
+
hidden_states = self.temp_convs[0](hidden_states, num_frames=num_frames)
|
| 355 |
+
|
| 356 |
+
for attn, temp_attn, resnet, temp_conv in zip(
|
| 357 |
+
self.attentions, self.temp_attentions, self.resnets[1:], self.temp_convs[1:]
|
| 358 |
+
):
|
| 359 |
+
if self.gradient_checkpointing:
|
| 360 |
+
hidden_states = cross_attn_g_c(
|
| 361 |
+
attn,
|
| 362 |
+
temp_attn,
|
| 363 |
+
resnet,
|
| 364 |
+
temp_conv,
|
| 365 |
+
hidden_states,
|
| 366 |
+
encoder_hidden_states,
|
| 367 |
+
cross_attention_kwargs,
|
| 368 |
+
temb,
|
| 369 |
+
num_frames
|
| 370 |
+
)
|
| 371 |
+
else:
|
| 372 |
+
hidden_states = attn(
|
| 373 |
+
hidden_states,
|
| 374 |
+
encoder_hidden_states=encoder_hidden_states,
|
| 375 |
+
cross_attention_kwargs=cross_attention_kwargs,
|
| 376 |
+
).sample
|
| 377 |
+
|
| 378 |
+
if num_frames > 1:
|
| 379 |
+
hidden_states = temp_attn(hidden_states, num_frames=num_frames).sample
|
| 380 |
+
|
| 381 |
+
hidden_states = resnet(hidden_states, temb)
|
| 382 |
+
|
| 383 |
+
if num_frames > 1:
|
| 384 |
+
hidden_states = temp_conv(hidden_states, num_frames=num_frames)
|
| 385 |
+
|
| 386 |
+
return hidden_states
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
class CrossAttnDownBlock3D(nn.Module):
|
| 390 |
+
def __init__(
|
| 391 |
+
self,
|
| 392 |
+
in_channels: int,
|
| 393 |
+
out_channels: int,
|
| 394 |
+
temb_channels: int,
|
| 395 |
+
dropout: float = 0.0,
|
| 396 |
+
num_layers: int = 1,
|
| 397 |
+
resnet_eps: float = 1e-6,
|
| 398 |
+
resnet_time_scale_shift: str = "default",
|
| 399 |
+
resnet_act_fn: str = "swish",
|
| 400 |
+
resnet_groups: int = 32,
|
| 401 |
+
resnet_pre_norm: bool = True,
|
| 402 |
+
attn_num_head_channels=1,
|
| 403 |
+
cross_attention_dim=1280,
|
| 404 |
+
output_scale_factor=1.0,
|
| 405 |
+
downsample_padding=1,
|
| 406 |
+
add_downsample=True,
|
| 407 |
+
dual_cross_attention=False,
|
| 408 |
+
use_linear_projection=False,
|
| 409 |
+
only_cross_attention=False,
|
| 410 |
+
upcast_attention=False,
|
| 411 |
+
):
|
| 412 |
+
super().__init__()
|
| 413 |
+
resnets = []
|
| 414 |
+
attentions = []
|
| 415 |
+
temp_attentions = []
|
| 416 |
+
temp_convs = []
|
| 417 |
+
|
| 418 |
+
self.gradient_checkpointing = False
|
| 419 |
+
self.has_cross_attention = True
|
| 420 |
+
self.attn_num_head_channels = attn_num_head_channels
|
| 421 |
+
|
| 422 |
+
for i in range(num_layers):
|
| 423 |
+
in_channels = in_channels if i == 0 else out_channels
|
| 424 |
+
resnets.append(
|
| 425 |
+
ResnetBlock2D(
|
| 426 |
+
in_channels=in_channels,
|
| 427 |
+
out_channels=out_channels,
|
| 428 |
+
temb_channels=temb_channels,
|
| 429 |
+
eps=resnet_eps,
|
| 430 |
+
groups=resnet_groups,
|
| 431 |
+
dropout=dropout,
|
| 432 |
+
time_embedding_norm=resnet_time_scale_shift,
|
| 433 |
+
non_linearity=resnet_act_fn,
|
| 434 |
+
output_scale_factor=output_scale_factor,
|
| 435 |
+
pre_norm=resnet_pre_norm,
|
| 436 |
+
)
|
| 437 |
+
)
|
| 438 |
+
temp_convs.append(
|
| 439 |
+
TemporalConvLayer(
|
| 440 |
+
out_channels,
|
| 441 |
+
out_channels,
|
| 442 |
+
dropout=0.1
|
| 443 |
+
)
|
| 444 |
+
)
|
| 445 |
+
attentions.append(
|
| 446 |
+
Transformer2DModel(
|
| 447 |
+
out_channels // attn_num_head_channels,
|
| 448 |
+
attn_num_head_channels,
|
| 449 |
+
in_channels=out_channels,
|
| 450 |
+
num_layers=1,
|
| 451 |
+
cross_attention_dim=cross_attention_dim,
|
| 452 |
+
norm_num_groups=resnet_groups,
|
| 453 |
+
use_linear_projection=use_linear_projection,
|
| 454 |
+
only_cross_attention=only_cross_attention,
|
| 455 |
+
upcast_attention=upcast_attention,
|
| 456 |
+
)
|
| 457 |
+
)
|
| 458 |
+
temp_attentions.append(
|
| 459 |
+
TransformerTemporalModel(
|
| 460 |
+
out_channels // attn_num_head_channels,
|
| 461 |
+
attn_num_head_channels,
|
| 462 |
+
in_channels=out_channels,
|
| 463 |
+
num_layers=1,
|
| 464 |
+
cross_attention_dim=cross_attention_dim,
|
| 465 |
+
norm_num_groups=resnet_groups,
|
| 466 |
+
)
|
| 467 |
+
)
|
| 468 |
+
self.resnets = nn.ModuleList(resnets)
|
| 469 |
+
self.temp_convs = nn.ModuleList(temp_convs)
|
| 470 |
+
self.attentions = nn.ModuleList(attentions)
|
| 471 |
+
self.temp_attentions = nn.ModuleList(temp_attentions)
|
| 472 |
+
|
| 473 |
+
if add_downsample:
|
| 474 |
+
self.downsamplers = nn.ModuleList(
|
| 475 |
+
[
|
| 476 |
+
Downsample2D(
|
| 477 |
+
out_channels, use_conv=True, out_channels=out_channels, padding=downsample_padding, name="op"
|
| 478 |
+
)
|
| 479 |
+
]
|
| 480 |
+
)
|
| 481 |
+
else:
|
| 482 |
+
self.downsamplers = None
|
| 483 |
+
|
| 484 |
+
def forward(
|
| 485 |
+
self,
|
| 486 |
+
hidden_states,
|
| 487 |
+
temb=None,
|
| 488 |
+
encoder_hidden_states=None,
|
| 489 |
+
attention_mask=None,
|
| 490 |
+
num_frames=1,
|
| 491 |
+
cross_attention_kwargs=None,
|
| 492 |
+
):
|
| 493 |
+
# TODO(Patrick, William) - attention mask is not used
|
| 494 |
+
output_states = ()
|
| 495 |
+
|
| 496 |
+
for resnet, temp_conv, attn, temp_attn in zip(
|
| 497 |
+
self.resnets, self.temp_convs, self.attentions, self.temp_attentions
|
| 498 |
+
):
|
| 499 |
+
|
| 500 |
+
if self.gradient_checkpointing:
|
| 501 |
+
hidden_states = cross_attn_g_c(
|
| 502 |
+
attn,
|
| 503 |
+
temp_attn,
|
| 504 |
+
resnet,
|
| 505 |
+
temp_conv,
|
| 506 |
+
hidden_states,
|
| 507 |
+
encoder_hidden_states,
|
| 508 |
+
cross_attention_kwargs,
|
| 509 |
+
temb,
|
| 510 |
+
num_frames,
|
| 511 |
+
inverse_temp=True
|
| 512 |
+
)
|
| 513 |
+
else:
|
| 514 |
+
hidden_states = resnet(hidden_states, temb)
|
| 515 |
+
|
| 516 |
+
if num_frames > 1:
|
| 517 |
+
hidden_states = temp_conv(hidden_states, num_frames=num_frames)
|
| 518 |
+
|
| 519 |
+
hidden_states = attn(
|
| 520 |
+
hidden_states,
|
| 521 |
+
encoder_hidden_states=encoder_hidden_states,
|
| 522 |
+
cross_attention_kwargs=cross_attention_kwargs,
|
| 523 |
+
).sample
|
| 524 |
+
|
| 525 |
+
if num_frames > 1:
|
| 526 |
+
hidden_states = temp_attn(hidden_states, num_frames=num_frames).sample
|
| 527 |
+
|
| 528 |
+
output_states += (hidden_states,)
|
| 529 |
+
|
| 530 |
+
if self.downsamplers is not None:
|
| 531 |
+
for downsampler in self.downsamplers:
|
| 532 |
+
hidden_states = downsampler(hidden_states)
|
| 533 |
+
|
| 534 |
+
output_states += (hidden_states,)
|
| 535 |
+
|
| 536 |
+
return hidden_states, output_states
|
| 537 |
+
|
| 538 |
+
|
| 539 |
+
class DownBlock3D(nn.Module):
|
| 540 |
+
def __init__(
|
| 541 |
+
self,
|
| 542 |
+
in_channels: int,
|
| 543 |
+
out_channels: int,
|
| 544 |
+
temb_channels: int,
|
| 545 |
+
dropout: float = 0.0,
|
| 546 |
+
num_layers: int = 1,
|
| 547 |
+
resnet_eps: float = 1e-6,
|
| 548 |
+
resnet_time_scale_shift: str = "default",
|
| 549 |
+
resnet_act_fn: str = "swish",
|
| 550 |
+
resnet_groups: int = 32,
|
| 551 |
+
resnet_pre_norm: bool = True,
|
| 552 |
+
output_scale_factor=1.0,
|
| 553 |
+
add_downsample=True,
|
| 554 |
+
downsample_padding=1,
|
| 555 |
+
):
|
| 556 |
+
super().__init__()
|
| 557 |
+
resnets = []
|
| 558 |
+
temp_convs = []
|
| 559 |
+
|
| 560 |
+
self.gradient_checkpointing = False
|
| 561 |
+
for i in range(num_layers):
|
| 562 |
+
in_channels = in_channels if i == 0 else out_channels
|
| 563 |
+
resnets.append(
|
| 564 |
+
ResnetBlock2D(
|
| 565 |
+
in_channels=in_channels,
|
| 566 |
+
out_channels=out_channels,
|
| 567 |
+
temb_channels=temb_channels,
|
| 568 |
+
eps=resnet_eps,
|
| 569 |
+
groups=resnet_groups,
|
| 570 |
+
dropout=dropout,
|
| 571 |
+
time_embedding_norm=resnet_time_scale_shift,
|
| 572 |
+
non_linearity=resnet_act_fn,
|
| 573 |
+
output_scale_factor=output_scale_factor,
|
| 574 |
+
pre_norm=resnet_pre_norm,
|
| 575 |
+
)
|
| 576 |
+
)
|
| 577 |
+
temp_convs.append(
|
| 578 |
+
TemporalConvLayer(
|
| 579 |
+
out_channels,
|
| 580 |
+
out_channels,
|
| 581 |
+
dropout=0.1
|
| 582 |
+
)
|
| 583 |
+
)
|
| 584 |
+
|
| 585 |
+
self.resnets = nn.ModuleList(resnets)
|
| 586 |
+
self.temp_convs = nn.ModuleList(temp_convs)
|
| 587 |
+
|
| 588 |
+
if add_downsample:
|
| 589 |
+
self.downsamplers = nn.ModuleList(
|
| 590 |
+
[
|
| 591 |
+
Downsample2D(
|
| 592 |
+
out_channels, use_conv=True, out_channels=out_channels, padding=downsample_padding, name="op"
|
| 593 |
+
)
|
| 594 |
+
]
|
| 595 |
+
)
|
| 596 |
+
else:
|
| 597 |
+
self.downsamplers = None
|
| 598 |
+
|
| 599 |
+
def forward(self, hidden_states, temb=None, num_frames=1):
|
| 600 |
+
output_states = ()
|
| 601 |
+
|
| 602 |
+
for resnet, temp_conv in zip(self.resnets, self.temp_convs):
|
| 603 |
+
if self.gradient_checkpointing:
|
| 604 |
+
hidden_states = up_down_g_c(resnet, temp_conv, hidden_states, temb, num_frames)
|
| 605 |
+
else:
|
| 606 |
+
hidden_states = resnet(hidden_states, temb)
|
| 607 |
+
|
| 608 |
+
if num_frames > 1:
|
| 609 |
+
hidden_states = temp_conv(hidden_states, num_frames=num_frames)
|
| 610 |
+
|
| 611 |
+
output_states += (hidden_states,)
|
| 612 |
+
|
| 613 |
+
if self.downsamplers is not None:
|
| 614 |
+
for downsampler in self.downsamplers:
|
| 615 |
+
hidden_states = downsampler(hidden_states)
|
| 616 |
+
|
| 617 |
+
output_states += (hidden_states,)
|
| 618 |
+
|
| 619 |
+
return hidden_states, output_states
|
| 620 |
+
|
| 621 |
+
|
| 622 |
+
class CrossAttnUpBlock3D(nn.Module):
|
| 623 |
+
def __init__(
|
| 624 |
+
self,
|
| 625 |
+
in_channels: int,
|
| 626 |
+
out_channels: int,
|
| 627 |
+
prev_output_channel: int,
|
| 628 |
+
temb_channels: int,
|
| 629 |
+
dropout: float = 0.0,
|
| 630 |
+
num_layers: int = 1,
|
| 631 |
+
resnet_eps: float = 1e-6,
|
| 632 |
+
resnet_time_scale_shift: str = "default",
|
| 633 |
+
resnet_act_fn: str = "swish",
|
| 634 |
+
resnet_groups: int = 32,
|
| 635 |
+
resnet_pre_norm: bool = True,
|
| 636 |
+
attn_num_head_channels=1,
|
| 637 |
+
cross_attention_dim=1280,
|
| 638 |
+
output_scale_factor=1.0,
|
| 639 |
+
add_upsample=True,
|
| 640 |
+
dual_cross_attention=False,
|
| 641 |
+
use_linear_projection=False,
|
| 642 |
+
only_cross_attention=False,
|
| 643 |
+
upcast_attention=False,
|
| 644 |
+
):
|
| 645 |
+
super().__init__()
|
| 646 |
+
resnets = []
|
| 647 |
+
temp_convs = []
|
| 648 |
+
attentions = []
|
| 649 |
+
temp_attentions = []
|
| 650 |
+
|
| 651 |
+
self.gradient_checkpointing = False
|
| 652 |
+
self.has_cross_attention = True
|
| 653 |
+
self.attn_num_head_channels = attn_num_head_channels
|
| 654 |
+
|
| 655 |
+
for i in range(num_layers):
|
| 656 |
+
res_skip_channels = in_channels if (i == num_layers - 1) else out_channels
|
| 657 |
+
resnet_in_channels = prev_output_channel if i == 0 else out_channels
|
| 658 |
+
|
| 659 |
+
resnets.append(
|
| 660 |
+
ResnetBlock2D(
|
| 661 |
+
in_channels=resnet_in_channels + res_skip_channels,
|
| 662 |
+
out_channels=out_channels,
|
| 663 |
+
temb_channels=temb_channels,
|
| 664 |
+
eps=resnet_eps,
|
| 665 |
+
groups=resnet_groups,
|
| 666 |
+
dropout=dropout,
|
| 667 |
+
time_embedding_norm=resnet_time_scale_shift,
|
| 668 |
+
non_linearity=resnet_act_fn,
|
| 669 |
+
output_scale_factor=output_scale_factor,
|
| 670 |
+
pre_norm=resnet_pre_norm,
|
| 671 |
+
)
|
| 672 |
+
)
|
| 673 |
+
temp_convs.append(
|
| 674 |
+
TemporalConvLayer(
|
| 675 |
+
out_channels,
|
| 676 |
+
out_channels,
|
| 677 |
+
dropout=0.1
|
| 678 |
+
)
|
| 679 |
+
)
|
| 680 |
+
attentions.append(
|
| 681 |
+
Transformer2DModel(
|
| 682 |
+
out_channels // attn_num_head_channels,
|
| 683 |
+
attn_num_head_channels,
|
| 684 |
+
in_channels=out_channels,
|
| 685 |
+
num_layers=1,
|
| 686 |
+
cross_attention_dim=cross_attention_dim,
|
| 687 |
+
norm_num_groups=resnet_groups,
|
| 688 |
+
use_linear_projection=use_linear_projection,
|
| 689 |
+
only_cross_attention=only_cross_attention,
|
| 690 |
+
upcast_attention=upcast_attention,
|
| 691 |
+
)
|
| 692 |
+
)
|
| 693 |
+
temp_attentions.append(
|
| 694 |
+
TransformerTemporalModel(
|
| 695 |
+
out_channels // attn_num_head_channels,
|
| 696 |
+
attn_num_head_channels,
|
| 697 |
+
in_channels=out_channels,
|
| 698 |
+
num_layers=1,
|
| 699 |
+
cross_attention_dim=cross_attention_dim,
|
| 700 |
+
norm_num_groups=resnet_groups,
|
| 701 |
+
)
|
| 702 |
+
)
|
| 703 |
+
self.resnets = nn.ModuleList(resnets)
|
| 704 |
+
self.temp_convs = nn.ModuleList(temp_convs)
|
| 705 |
+
self.attentions = nn.ModuleList(attentions)
|
| 706 |
+
self.temp_attentions = nn.ModuleList(temp_attentions)
|
| 707 |
+
|
| 708 |
+
if add_upsample:
|
| 709 |
+
self.upsamplers = nn.ModuleList([Upsample2D(out_channels, use_conv=True, out_channels=out_channels)])
|
| 710 |
+
else:
|
| 711 |
+
self.upsamplers = None
|
| 712 |
+
|
| 713 |
+
def forward(
|
| 714 |
+
self,
|
| 715 |
+
hidden_states,
|
| 716 |
+
res_hidden_states_tuple,
|
| 717 |
+
temb=None,
|
| 718 |
+
encoder_hidden_states=None,
|
| 719 |
+
upsample_size=None,
|
| 720 |
+
attention_mask=None,
|
| 721 |
+
num_frames=1,
|
| 722 |
+
cross_attention_kwargs=None,
|
| 723 |
+
):
|
| 724 |
+
# TODO(Patrick, William) - attention mask is not used
|
| 725 |
+
for resnet, temp_conv, attn, temp_attn in zip(
|
| 726 |
+
self.resnets, self.temp_convs, self.attentions, self.temp_attentions
|
| 727 |
+
):
|
| 728 |
+
# pop res hidden states
|
| 729 |
+
res_hidden_states = res_hidden_states_tuple[-1]
|
| 730 |
+
res_hidden_states_tuple = res_hidden_states_tuple[:-1]
|
| 731 |
+
hidden_states = torch.cat([hidden_states, res_hidden_states], dim=1)
|
| 732 |
+
|
| 733 |
+
if self.gradient_checkpointing:
|
| 734 |
+
hidden_states = cross_attn_g_c(
|
| 735 |
+
attn,
|
| 736 |
+
temp_attn,
|
| 737 |
+
resnet,
|
| 738 |
+
temp_conv,
|
| 739 |
+
hidden_states,
|
| 740 |
+
encoder_hidden_states,
|
| 741 |
+
cross_attention_kwargs,
|
| 742 |
+
temb,
|
| 743 |
+
num_frames,
|
| 744 |
+
inverse_temp=True
|
| 745 |
+
)
|
| 746 |
+
else:
|
| 747 |
+
hidden_states = resnet(hidden_states, temb)
|
| 748 |
+
|
| 749 |
+
if num_frames > 1:
|
| 750 |
+
hidden_states = temp_conv(hidden_states, num_frames=num_frames)
|
| 751 |
+
|
| 752 |
+
hidden_states = attn(
|
| 753 |
+
hidden_states,
|
| 754 |
+
encoder_hidden_states=encoder_hidden_states,
|
| 755 |
+
cross_attention_kwargs=cross_attention_kwargs,
|
| 756 |
+
).sample
|
| 757 |
+
|
| 758 |
+
if num_frames > 1:
|
| 759 |
+
hidden_states = temp_attn(hidden_states, num_frames=num_frames).sample
|
| 760 |
+
|
| 761 |
+
if self.upsamplers is not None:
|
| 762 |
+
for upsampler in self.upsamplers:
|
| 763 |
+
hidden_states = upsampler(hidden_states, upsample_size)
|
| 764 |
+
|
| 765 |
+
return hidden_states
|
| 766 |
+
|
| 767 |
+
|
| 768 |
+
class UpBlock3D(nn.Module):
|
| 769 |
+
def __init__(
|
| 770 |
+
self,
|
| 771 |
+
in_channels: int,
|
| 772 |
+
prev_output_channel: int,
|
| 773 |
+
out_channels: int,
|
| 774 |
+
temb_channels: int,
|
| 775 |
+
dropout: float = 0.0,
|
| 776 |
+
num_layers: int = 1,
|
| 777 |
+
resnet_eps: float = 1e-6,
|
| 778 |
+
resnet_time_scale_shift: str = "default",
|
| 779 |
+
resnet_act_fn: str = "swish",
|
| 780 |
+
resnet_groups: int = 32,
|
| 781 |
+
resnet_pre_norm: bool = True,
|
| 782 |
+
output_scale_factor=1.0,
|
| 783 |
+
add_upsample=True,
|
| 784 |
+
):
|
| 785 |
+
super().__init__()
|
| 786 |
+
resnets = []
|
| 787 |
+
temp_convs = []
|
| 788 |
+
self.gradient_checkpointing = False
|
| 789 |
+
for i in range(num_layers):
|
| 790 |
+
res_skip_channels = in_channels if (i == num_layers - 1) else out_channels
|
| 791 |
+
resnet_in_channels = prev_output_channel if i == 0 else out_channels
|
| 792 |
+
|
| 793 |
+
resnets.append(
|
| 794 |
+
ResnetBlock2D(
|
| 795 |
+
in_channels=resnet_in_channels + res_skip_channels,
|
| 796 |
+
out_channels=out_channels,
|
| 797 |
+
temb_channels=temb_channels,
|
| 798 |
+
eps=resnet_eps,
|
| 799 |
+
groups=resnet_groups,
|
| 800 |
+
dropout=dropout,
|
| 801 |
+
time_embedding_norm=resnet_time_scale_shift,
|
| 802 |
+
non_linearity=resnet_act_fn,
|
| 803 |
+
output_scale_factor=output_scale_factor,
|
| 804 |
+
pre_norm=resnet_pre_norm,
|
| 805 |
+
)
|
| 806 |
+
)
|
| 807 |
+
temp_convs.append(
|
| 808 |
+
TemporalConvLayer(
|
| 809 |
+
out_channels,
|
| 810 |
+
out_channels,
|
| 811 |
+
dropout=0.1
|
| 812 |
+
)
|
| 813 |
+
)
|
| 814 |
+
|
| 815 |
+
self.resnets = nn.ModuleList(resnets)
|
| 816 |
+
self.temp_convs = nn.ModuleList(temp_convs)
|
| 817 |
+
|
| 818 |
+
if add_upsample:
|
| 819 |
+
self.upsamplers = nn.ModuleList([Upsample2D(out_channels, use_conv=True, out_channels=out_channels)])
|
| 820 |
+
else:
|
| 821 |
+
self.upsamplers = None
|
| 822 |
+
|
| 823 |
+
def forward(self, hidden_states, res_hidden_states_tuple, temb=None, upsample_size=None, num_frames=1):
|
| 824 |
+
for resnet, temp_conv in zip(self.resnets, self.temp_convs):
|
| 825 |
+
# pop res hidden states
|
| 826 |
+
res_hidden_states = res_hidden_states_tuple[-1]
|
| 827 |
+
res_hidden_states_tuple = res_hidden_states_tuple[:-1]
|
| 828 |
+
hidden_states = torch.cat([hidden_states, res_hidden_states], dim=1)
|
| 829 |
+
|
| 830 |
+
if self.gradient_checkpointing:
|
| 831 |
+
hidden_states = up_down_g_c(resnet, temp_conv, hidden_states, temb, num_frames)
|
| 832 |
+
else:
|
| 833 |
+
hidden_states = resnet(hidden_states, temb)
|
| 834 |
+
|
| 835 |
+
if num_frames > 1:
|
| 836 |
+
hidden_states = temp_conv(hidden_states, num_frames=num_frames)
|
| 837 |
+
|
| 838 |
+
if self.upsamplers is not None:
|
| 839 |
+
for upsampler in self.upsamplers:
|
| 840 |
+
hidden_states = upsampler(hidden_states, upsample_size)
|
| 841 |
+
|
| 842 |
+
return hidden_states
|
unet_3d_condition.py
ADDED
|
@@ -0,0 +1,500 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2023 Alibaba DAMO-VILAB and The HuggingFace Team. All rights reserved.
|
| 2 |
+
# Copyright 2023 The ModelScope Team.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
from dataclasses import dataclass
|
| 16 |
+
from typing import Any, Dict, List, Optional, Tuple, Union
|
| 17 |
+
|
| 18 |
+
import torch
|
| 19 |
+
import torch.nn as nn
|
| 20 |
+
import torch.utils.checkpoint
|
| 21 |
+
|
| 22 |
+
from diffusers.configuration_utils import ConfigMixin, register_to_config
|
| 23 |
+
from diffusers.utils import BaseOutput, logging
|
| 24 |
+
from diffusers.models.embeddings import TimestepEmbedding, Timesteps
|
| 25 |
+
from diffusers.models.modeling_utils import ModelMixin
|
| 26 |
+
from diffusers.models.transformer_temporal import TransformerTemporalModel
|
| 27 |
+
from .unet_3d_blocks import (
|
| 28 |
+
CrossAttnDownBlock3D,
|
| 29 |
+
CrossAttnUpBlock3D,
|
| 30 |
+
DownBlock3D,
|
| 31 |
+
UNetMidBlock3DCrossAttn,
|
| 32 |
+
UpBlock3D,
|
| 33 |
+
get_down_block,
|
| 34 |
+
get_up_block,
|
| 35 |
+
transformer_g_c
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
@dataclass
|
| 43 |
+
class UNet3DConditionOutput(BaseOutput):
|
| 44 |
+
"""
|
| 45 |
+
Args:
|
| 46 |
+
sample (`torch.FloatTensor` of shape `(batch_size, num_frames, num_channels, height, width)`):
|
| 47 |
+
Hidden states conditioned on `encoder_hidden_states` input. Output of last layer of model.
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
sample: torch.FloatTensor
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class UNet3DConditionModel(ModelMixin, ConfigMixin):
|
| 54 |
+
r"""
|
| 55 |
+
UNet3DConditionModel is a conditional 2D UNet model that takes in a noisy sample, conditional state, and a timestep
|
| 56 |
+
and returns sample shaped output.
|
| 57 |
+
|
| 58 |
+
This model inherits from [`ModelMixin`]. Check the superclass documentation for the generic methods the library
|
| 59 |
+
implements for all the models (such as downloading or saving, etc.)
|
| 60 |
+
|
| 61 |
+
Parameters:
|
| 62 |
+
sample_size (`int` or `Tuple[int, int]`, *optional*, defaults to `None`):
|
| 63 |
+
Height and width of input/output sample.
|
| 64 |
+
in_channels (`int`, *optional*, defaults to 4): The number of channels in the input sample.
|
| 65 |
+
out_channels (`int`, *optional*, defaults to 4): The number of channels in the output.
|
| 66 |
+
down_block_types (`Tuple[str]`, *optional*, defaults to `("CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "CrossAttnDownBlock2D", "DownBlock2D")`):
|
| 67 |
+
The tuple of downsample blocks to use.
|
| 68 |
+
up_block_types (`Tuple[str]`, *optional*, defaults to `("UpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D", "CrossAttnUpBlock2D",)`):
|
| 69 |
+
The tuple of upsample blocks to use.
|
| 70 |
+
block_out_channels (`Tuple[int]`, *optional*, defaults to `(320, 640, 1280, 1280)`):
|
| 71 |
+
The tuple of output channels for each block.
|
| 72 |
+
layers_per_block (`int`, *optional*, defaults to 2): The number of layers per block.
|
| 73 |
+
downsample_padding (`int`, *optional*, defaults to 1): The padding to use for the downsampling convolution.
|
| 74 |
+
mid_block_scale_factor (`float`, *optional*, defaults to 1.0): The scale factor to use for the mid block.
|
| 75 |
+
act_fn (`str`, *optional*, defaults to `"silu"`): The activation function to use.
|
| 76 |
+
norm_num_groups (`int`, *optional*, defaults to 32): The number of groups to use for the normalization.
|
| 77 |
+
If `None`, it will skip the normalization and activation layers in post-processing
|
| 78 |
+
norm_eps (`float`, *optional*, defaults to 1e-5): The epsilon to use for the normalization.
|
| 79 |
+
cross_attention_dim (`int`, *optional*, defaults to 1280): The dimension of the cross attention features.
|
| 80 |
+
attention_head_dim (`int`, *optional*, defaults to 8): The dimension of the attention heads.
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
_supports_gradient_checkpointing = True
|
| 84 |
+
|
| 85 |
+
@register_to_config
|
| 86 |
+
def __init__(
|
| 87 |
+
self,
|
| 88 |
+
sample_size: Optional[int] = None,
|
| 89 |
+
in_channels: int = 4,
|
| 90 |
+
out_channels: int = 4,
|
| 91 |
+
down_block_types: Tuple[str] = (
|
| 92 |
+
"CrossAttnDownBlock3D",
|
| 93 |
+
"CrossAttnDownBlock3D",
|
| 94 |
+
"CrossAttnDownBlock3D",
|
| 95 |
+
"DownBlock3D",
|
| 96 |
+
),
|
| 97 |
+
up_block_types: Tuple[str] = ("UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D"),
|
| 98 |
+
block_out_channels: Tuple[int] = (320, 640, 1280, 1280),
|
| 99 |
+
layers_per_block: int = 2,
|
| 100 |
+
downsample_padding: int = 1,
|
| 101 |
+
mid_block_scale_factor: float = 1,
|
| 102 |
+
act_fn: str = "silu",
|
| 103 |
+
norm_num_groups: Optional[int] = 32,
|
| 104 |
+
norm_eps: float = 1e-5,
|
| 105 |
+
cross_attention_dim: int = 1024,
|
| 106 |
+
attention_head_dim: Union[int, Tuple[int]] = 64,
|
| 107 |
+
):
|
| 108 |
+
super().__init__()
|
| 109 |
+
|
| 110 |
+
self.sample_size = sample_size
|
| 111 |
+
self.gradient_checkpointing = False
|
| 112 |
+
# Check inputs
|
| 113 |
+
if len(down_block_types) != len(up_block_types):
|
| 114 |
+
raise ValueError(
|
| 115 |
+
f"Must provide the same number of `down_block_types` as `up_block_types`. `down_block_types`: {down_block_types}. `up_block_types`: {up_block_types}."
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
if len(block_out_channels) != len(down_block_types):
|
| 119 |
+
raise ValueError(
|
| 120 |
+
f"Must provide the same number of `block_out_channels` as `down_block_types`. `block_out_channels`: {block_out_channels}. `down_block_types`: {down_block_types}."
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
if not isinstance(attention_head_dim, int) and len(attention_head_dim) != len(down_block_types):
|
| 124 |
+
raise ValueError(
|
| 125 |
+
f"Must provide the same number of `attention_head_dim` as `down_block_types`. `attention_head_dim`: {attention_head_dim}. `down_block_types`: {down_block_types}."
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# input
|
| 129 |
+
conv_in_kernel = 3
|
| 130 |
+
conv_out_kernel = 3
|
| 131 |
+
conv_in_padding = (conv_in_kernel - 1) // 2
|
| 132 |
+
self.conv_in = nn.Conv2d(
|
| 133 |
+
in_channels, block_out_channels[0], kernel_size=conv_in_kernel, padding=conv_in_padding
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# time
|
| 137 |
+
time_embed_dim = block_out_channels[0] * 4
|
| 138 |
+
self.time_proj = Timesteps(block_out_channels[0], True, 0)
|
| 139 |
+
timestep_input_dim = block_out_channels[0]
|
| 140 |
+
|
| 141 |
+
self.time_embedding = TimestepEmbedding(
|
| 142 |
+
timestep_input_dim,
|
| 143 |
+
time_embed_dim,
|
| 144 |
+
act_fn=act_fn,
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
self.transformer_in = TransformerTemporalModel(
|
| 148 |
+
num_attention_heads=8,
|
| 149 |
+
attention_head_dim=attention_head_dim,
|
| 150 |
+
in_channels=block_out_channels[0],
|
| 151 |
+
num_layers=1,
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# class embedding
|
| 155 |
+
self.down_blocks = nn.ModuleList([])
|
| 156 |
+
self.up_blocks = nn.ModuleList([])
|
| 157 |
+
|
| 158 |
+
if isinstance(attention_head_dim, int):
|
| 159 |
+
attention_head_dim = (attention_head_dim,) * len(down_block_types)
|
| 160 |
+
|
| 161 |
+
# down
|
| 162 |
+
output_channel = block_out_channels[0]
|
| 163 |
+
for i, down_block_type in enumerate(down_block_types):
|
| 164 |
+
input_channel = output_channel
|
| 165 |
+
output_channel = block_out_channels[i]
|
| 166 |
+
is_final_block = i == len(block_out_channels) - 1
|
| 167 |
+
|
| 168 |
+
down_block = get_down_block(
|
| 169 |
+
down_block_type,
|
| 170 |
+
num_layers=layers_per_block,
|
| 171 |
+
in_channels=input_channel,
|
| 172 |
+
out_channels=output_channel,
|
| 173 |
+
temb_channels=time_embed_dim,
|
| 174 |
+
add_downsample=not is_final_block,
|
| 175 |
+
resnet_eps=norm_eps,
|
| 176 |
+
resnet_act_fn=act_fn,
|
| 177 |
+
resnet_groups=norm_num_groups,
|
| 178 |
+
cross_attention_dim=cross_attention_dim,
|
| 179 |
+
attn_num_head_channels=attention_head_dim[i],
|
| 180 |
+
downsample_padding=downsample_padding,
|
| 181 |
+
dual_cross_attention=False,
|
| 182 |
+
)
|
| 183 |
+
self.down_blocks.append(down_block)
|
| 184 |
+
|
| 185 |
+
# mid
|
| 186 |
+
self.mid_block = UNetMidBlock3DCrossAttn(
|
| 187 |
+
in_channels=block_out_channels[-1],
|
| 188 |
+
temb_channels=time_embed_dim,
|
| 189 |
+
resnet_eps=norm_eps,
|
| 190 |
+
resnet_act_fn=act_fn,
|
| 191 |
+
output_scale_factor=mid_block_scale_factor,
|
| 192 |
+
cross_attention_dim=cross_attention_dim,
|
| 193 |
+
attn_num_head_channels=attention_head_dim[-1],
|
| 194 |
+
resnet_groups=norm_num_groups,
|
| 195 |
+
dual_cross_attention=False,
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
# count how many layers upsample the images
|
| 199 |
+
self.num_upsamplers = 0
|
| 200 |
+
|
| 201 |
+
# up
|
| 202 |
+
reversed_block_out_channels = list(reversed(block_out_channels))
|
| 203 |
+
reversed_attention_head_dim = list(reversed(attention_head_dim))
|
| 204 |
+
|
| 205 |
+
output_channel = reversed_block_out_channels[0]
|
| 206 |
+
for i, up_block_type in enumerate(up_block_types):
|
| 207 |
+
is_final_block = i == len(block_out_channels) - 1
|
| 208 |
+
|
| 209 |
+
prev_output_channel = output_channel
|
| 210 |
+
output_channel = reversed_block_out_channels[i]
|
| 211 |
+
input_channel = reversed_block_out_channels[min(i + 1, len(block_out_channels) - 1)]
|
| 212 |
+
|
| 213 |
+
# add upsample block for all BUT final layer
|
| 214 |
+
if not is_final_block:
|
| 215 |
+
add_upsample = True
|
| 216 |
+
self.num_upsamplers += 1
|
| 217 |
+
else:
|
| 218 |
+
add_upsample = False
|
| 219 |
+
|
| 220 |
+
up_block = get_up_block(
|
| 221 |
+
up_block_type,
|
| 222 |
+
num_layers=layers_per_block + 1,
|
| 223 |
+
in_channels=input_channel,
|
| 224 |
+
out_channels=output_channel,
|
| 225 |
+
prev_output_channel=prev_output_channel,
|
| 226 |
+
temb_channels=time_embed_dim,
|
| 227 |
+
add_upsample=add_upsample,
|
| 228 |
+
resnet_eps=norm_eps,
|
| 229 |
+
resnet_act_fn=act_fn,
|
| 230 |
+
resnet_groups=norm_num_groups,
|
| 231 |
+
cross_attention_dim=cross_attention_dim,
|
| 232 |
+
attn_num_head_channels=reversed_attention_head_dim[i],
|
| 233 |
+
dual_cross_attention=False,
|
| 234 |
+
)
|
| 235 |
+
self.up_blocks.append(up_block)
|
| 236 |
+
prev_output_channel = output_channel
|
| 237 |
+
|
| 238 |
+
# out
|
| 239 |
+
if norm_num_groups is not None:
|
| 240 |
+
self.conv_norm_out = nn.GroupNorm(
|
| 241 |
+
num_channels=block_out_channels[0], num_groups=norm_num_groups, eps=norm_eps
|
| 242 |
+
)
|
| 243 |
+
self.conv_act = nn.SiLU()
|
| 244 |
+
else:
|
| 245 |
+
self.conv_norm_out = None
|
| 246 |
+
self.conv_act = None
|
| 247 |
+
|
| 248 |
+
conv_out_padding = (conv_out_kernel - 1) // 2
|
| 249 |
+
self.conv_out = nn.Conv2d(
|
| 250 |
+
block_out_channels[0], out_channels, kernel_size=conv_out_kernel, padding=conv_out_padding
|
| 251 |
+
)
|
| 252 |
+
|
| 253 |
+
def set_attention_slice(self, slice_size):
|
| 254 |
+
r"""
|
| 255 |
+
Enable sliced attention computation.
|
| 256 |
+
|
| 257 |
+
When this option is enabled, the attention module will split the input tensor in slices, to compute attention
|
| 258 |
+
in several steps. This is useful to save some memory in exchange for a small speed decrease.
|
| 259 |
+
|
| 260 |
+
Args:
|
| 261 |
+
slice_size (`str` or `int` or `list(int)`, *optional*, defaults to `"auto"`):
|
| 262 |
+
When `"auto"`, halves the input to the attention heads, so attention will be computed in two steps. If
|
| 263 |
+
`"max"`, maxium amount of memory will be saved by running only one slice at a time. If a number is
|
| 264 |
+
provided, uses as many slices as `attention_head_dim // slice_size`. In this case, `attention_head_dim`
|
| 265 |
+
must be a multiple of `slice_size`.
|
| 266 |
+
"""
|
| 267 |
+
sliceable_head_dims = []
|
| 268 |
+
|
| 269 |
+
def fn_recursive_retrieve_slicable_dims(module: torch.nn.Module):
|
| 270 |
+
if hasattr(module, "set_attention_slice"):
|
| 271 |
+
sliceable_head_dims.append(module.sliceable_head_dim)
|
| 272 |
+
|
| 273 |
+
for child in module.children():
|
| 274 |
+
fn_recursive_retrieve_slicable_dims(child)
|
| 275 |
+
|
| 276 |
+
# retrieve number of attention layers
|
| 277 |
+
for module in self.children():
|
| 278 |
+
fn_recursive_retrieve_slicable_dims(module)
|
| 279 |
+
|
| 280 |
+
num_slicable_layers = len(sliceable_head_dims)
|
| 281 |
+
|
| 282 |
+
if slice_size == "auto":
|
| 283 |
+
# half the attention head size is usually a good trade-off between
|
| 284 |
+
# speed and memory
|
| 285 |
+
slice_size = [dim // 2 for dim in sliceable_head_dims]
|
| 286 |
+
elif slice_size == "max":
|
| 287 |
+
# make smallest slice possible
|
| 288 |
+
slice_size = num_slicable_layers * [1]
|
| 289 |
+
|
| 290 |
+
slice_size = num_slicable_layers * [slice_size] if not isinstance(slice_size, list) else slice_size
|
| 291 |
+
|
| 292 |
+
if len(slice_size) != len(sliceable_head_dims):
|
| 293 |
+
raise ValueError(
|
| 294 |
+
f"You have provided {len(slice_size)}, but {self.config} has {len(sliceable_head_dims)} different"
|
| 295 |
+
f" attention layers. Make sure to match `len(slice_size)` to be {len(sliceable_head_dims)}."
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
for i in range(len(slice_size)):
|
| 299 |
+
size = slice_size[i]
|
| 300 |
+
dim = sliceable_head_dims[i]
|
| 301 |
+
if size is not None and size > dim:
|
| 302 |
+
raise ValueError(f"size {size} has to be smaller or equal to {dim}.")
|
| 303 |
+
|
| 304 |
+
# Recursively walk through all the children.
|
| 305 |
+
# Any children which exposes the set_attention_slice method
|
| 306 |
+
# gets the message
|
| 307 |
+
def fn_recursive_set_attention_slice(module: torch.nn.Module, slice_size: List[int]):
|
| 308 |
+
if hasattr(module, "set_attention_slice"):
|
| 309 |
+
module.set_attention_slice(slice_size.pop())
|
| 310 |
+
|
| 311 |
+
for child in module.children():
|
| 312 |
+
fn_recursive_set_attention_slice(child, slice_size)
|
| 313 |
+
|
| 314 |
+
reversed_slice_size = list(reversed(slice_size))
|
| 315 |
+
for module in self.children():
|
| 316 |
+
fn_recursive_set_attention_slice(module, reversed_slice_size)
|
| 317 |
+
|
| 318 |
+
def _set_gradient_checkpointing(self, value=False):
|
| 319 |
+
self.gradient_checkpointing = value
|
| 320 |
+
self.mid_block.gradient_checkpointing = value
|
| 321 |
+
for module in self.down_blocks + self.up_blocks:
|
| 322 |
+
if isinstance(module, (CrossAttnDownBlock3D, DownBlock3D, CrossAttnUpBlock3D, UpBlock3D)):
|
| 323 |
+
module.gradient_checkpointing = value
|
| 324 |
+
|
| 325 |
+
def forward(
|
| 326 |
+
self,
|
| 327 |
+
sample: torch.FloatTensor,
|
| 328 |
+
timestep: Union[torch.Tensor, float, int],
|
| 329 |
+
encoder_hidden_states: torch.Tensor,
|
| 330 |
+
class_labels: Optional[torch.Tensor] = None,
|
| 331 |
+
timestep_cond: Optional[torch.Tensor] = None,
|
| 332 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 333 |
+
cross_attention_kwargs: Optional[Dict[str, Any]] = None,
|
| 334 |
+
down_block_additional_residuals: Optional[Tuple[torch.Tensor]] = None,
|
| 335 |
+
mid_block_additional_residual: Optional[torch.Tensor] = None,
|
| 336 |
+
return_dict: bool = True,
|
| 337 |
+
) -> Union[UNet3DConditionOutput, Tuple]:
|
| 338 |
+
r"""
|
| 339 |
+
Args:
|
| 340 |
+
sample (`torch.FloatTensor`): (batch, num_frames, channel, height, width) noisy inputs tensor
|
| 341 |
+
timestep (`torch.FloatTensor` or `float` or `int`): (batch) timesteps
|
| 342 |
+
encoder_hidden_states (`torch.FloatTensor`): (batch, sequence_length, feature_dim) encoder hidden states
|
| 343 |
+
return_dict (`bool`, *optional*, defaults to `True`):
|
| 344 |
+
Whether or not to return a [`models.unet_2d_condition.UNet3DConditionOutput`] instead of a plain tuple.
|
| 345 |
+
cross_attention_kwargs (`dict`, *optional*):
|
| 346 |
+
A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
|
| 347 |
+
`self.processor` in
|
| 348 |
+
[diffusers.cross_attention](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/cross_attention.py).
|
| 349 |
+
|
| 350 |
+
Returns:
|
| 351 |
+
[`~models.unet_2d_condition.UNet3DConditionOutput`] or `tuple`:
|
| 352 |
+
[`~models.unet_2d_condition.UNet3DConditionOutput`] if `return_dict` is True, otherwise a `tuple`. When
|
| 353 |
+
returning a tuple, the first element is the sample tensor.
|
| 354 |
+
"""
|
| 355 |
+
# By default samples have to be AT least a multiple of the overall upsampling factor.
|
| 356 |
+
# The overall upsampling factor is equal to 2 ** (# num of upsampling layears).
|
| 357 |
+
# However, the upsampling interpolation output size can be forced to fit any upsampling size
|
| 358 |
+
# on the fly if necessary.
|
| 359 |
+
default_overall_up_factor = 2**self.num_upsamplers
|
| 360 |
+
|
| 361 |
+
# upsample size should be forwarded when sample is not a multiple of `default_overall_up_factor`
|
| 362 |
+
forward_upsample_size = False
|
| 363 |
+
upsample_size = None
|
| 364 |
+
|
| 365 |
+
if any(s % default_overall_up_factor != 0 for s in sample.shape[-2:]):
|
| 366 |
+
logger.info("Forward upsample size to force interpolation output size.")
|
| 367 |
+
forward_upsample_size = True
|
| 368 |
+
|
| 369 |
+
# prepare attention_mask
|
| 370 |
+
if attention_mask is not None:
|
| 371 |
+
attention_mask = (1 - attention_mask.to(sample.dtype)) * -10000.0
|
| 372 |
+
attention_mask = attention_mask.unsqueeze(1)
|
| 373 |
+
|
| 374 |
+
# 1. time
|
| 375 |
+
timesteps = timestep
|
| 376 |
+
if not torch.is_tensor(timesteps):
|
| 377 |
+
# TODO: this requires sync between CPU and GPU. So try to pass timesteps as tensors if you can
|
| 378 |
+
# This would be a good case for the `match` statement (Python 3.10+)
|
| 379 |
+
is_mps = sample.device.type == "mps"
|
| 380 |
+
if isinstance(timestep, float):
|
| 381 |
+
dtype = torch.float32 if is_mps else torch.float64
|
| 382 |
+
else:
|
| 383 |
+
dtype = torch.int32 if is_mps else torch.int64
|
| 384 |
+
timesteps = torch.tensor([timesteps], dtype=dtype, device=sample.device)
|
| 385 |
+
elif len(timesteps.shape) == 0:
|
| 386 |
+
timesteps = timesteps[None].to(sample.device)
|
| 387 |
+
|
| 388 |
+
# broadcast to batch dimension in a way that's compatible with ONNX/Core ML
|
| 389 |
+
num_frames = sample.shape[2]
|
| 390 |
+
timesteps = timesteps.expand(sample.shape[0])
|
| 391 |
+
|
| 392 |
+
t_emb = self.time_proj(timesteps)
|
| 393 |
+
|
| 394 |
+
# timesteps does not contain any weights and will always return f32 tensors
|
| 395 |
+
# but time_embedding might actually be running in fp16. so we need to cast here.
|
| 396 |
+
# there might be better ways to encapsulate this.
|
| 397 |
+
t_emb = t_emb.to(dtype=self.dtype)
|
| 398 |
+
|
| 399 |
+
emb = self.time_embedding(t_emb, timestep_cond)
|
| 400 |
+
emb = emb.repeat_interleave(repeats=num_frames, dim=0)
|
| 401 |
+
encoder_hidden_states = encoder_hidden_states.repeat_interleave(repeats=num_frames, dim=0)
|
| 402 |
+
|
| 403 |
+
# 2. pre-process
|
| 404 |
+
sample = sample.permute(0, 2, 1, 3, 4).reshape((sample.shape[0] * num_frames, -1) + sample.shape[3:])
|
| 405 |
+
sample = self.conv_in(sample)
|
| 406 |
+
|
| 407 |
+
if num_frames > 1:
|
| 408 |
+
if self.gradient_checkpointing:
|
| 409 |
+
sample = transformer_g_c(self.transformer_in, sample, num_frames)
|
| 410 |
+
else:
|
| 411 |
+
sample = self.transformer_in(sample, num_frames=num_frames).sample
|
| 412 |
+
|
| 413 |
+
# 3. down
|
| 414 |
+
down_block_res_samples = (sample,)
|
| 415 |
+
for downsample_block in self.down_blocks:
|
| 416 |
+
if hasattr(downsample_block, "has_cross_attention") and downsample_block.has_cross_attention:
|
| 417 |
+
sample, res_samples = downsample_block(
|
| 418 |
+
hidden_states=sample,
|
| 419 |
+
temb=emb,
|
| 420 |
+
encoder_hidden_states=encoder_hidden_states,
|
| 421 |
+
attention_mask=attention_mask,
|
| 422 |
+
num_frames=num_frames,
|
| 423 |
+
cross_attention_kwargs=cross_attention_kwargs,
|
| 424 |
+
)
|
| 425 |
+
else:
|
| 426 |
+
sample, res_samples = downsample_block(hidden_states=sample, temb=emb, num_frames=num_frames)
|
| 427 |
+
|
| 428 |
+
down_block_res_samples += res_samples
|
| 429 |
+
|
| 430 |
+
if down_block_additional_residuals is not None:
|
| 431 |
+
new_down_block_res_samples = ()
|
| 432 |
+
|
| 433 |
+
for down_block_res_sample, down_block_additional_residual in zip(
|
| 434 |
+
down_block_res_samples, down_block_additional_residuals
|
| 435 |
+
):
|
| 436 |
+
down_block_res_sample = down_block_res_sample + down_block_additional_residual
|
| 437 |
+
new_down_block_res_samples += (down_block_res_sample,)
|
| 438 |
+
|
| 439 |
+
down_block_res_samples = new_down_block_res_samples
|
| 440 |
+
|
| 441 |
+
# 4. mid
|
| 442 |
+
if self.mid_block is not None:
|
| 443 |
+
sample = self.mid_block(
|
| 444 |
+
sample,
|
| 445 |
+
emb,
|
| 446 |
+
encoder_hidden_states=encoder_hidden_states,
|
| 447 |
+
attention_mask=attention_mask,
|
| 448 |
+
num_frames=num_frames,
|
| 449 |
+
cross_attention_kwargs=cross_attention_kwargs,
|
| 450 |
+
)
|
| 451 |
+
|
| 452 |
+
if mid_block_additional_residual is not None:
|
| 453 |
+
sample = sample + mid_block_additional_residual
|
| 454 |
+
|
| 455 |
+
# 5. up
|
| 456 |
+
for i, upsample_block in enumerate(self.up_blocks):
|
| 457 |
+
is_final_block = i == len(self.up_blocks) - 1
|
| 458 |
+
|
| 459 |
+
res_samples = down_block_res_samples[-len(upsample_block.resnets) :]
|
| 460 |
+
down_block_res_samples = down_block_res_samples[: -len(upsample_block.resnets)]
|
| 461 |
+
|
| 462 |
+
# if we have not reached the final block and need to forward the
|
| 463 |
+
# upsample size, we do it here
|
| 464 |
+
if not is_final_block and forward_upsample_size:
|
| 465 |
+
upsample_size = down_block_res_samples[-1].shape[2:]
|
| 466 |
+
|
| 467 |
+
if hasattr(upsample_block, "has_cross_attention") and upsample_block.has_cross_attention:
|
| 468 |
+
sample = upsample_block(
|
| 469 |
+
hidden_states=sample,
|
| 470 |
+
temb=emb,
|
| 471 |
+
res_hidden_states_tuple=res_samples,
|
| 472 |
+
encoder_hidden_states=encoder_hidden_states,
|
| 473 |
+
upsample_size=upsample_size,
|
| 474 |
+
attention_mask=attention_mask,
|
| 475 |
+
num_frames=num_frames,
|
| 476 |
+
cross_attention_kwargs=cross_attention_kwargs,
|
| 477 |
+
)
|
| 478 |
+
else:
|
| 479 |
+
sample = upsample_block(
|
| 480 |
+
hidden_states=sample,
|
| 481 |
+
temb=emb,
|
| 482 |
+
res_hidden_states_tuple=res_samples,
|
| 483 |
+
upsample_size=upsample_size,
|
| 484 |
+
num_frames=num_frames,
|
| 485 |
+
)
|
| 486 |
+
|
| 487 |
+
# 6. post-process
|
| 488 |
+
if self.conv_norm_out:
|
| 489 |
+
sample = self.conv_norm_out(sample)
|
| 490 |
+
sample = self.conv_act(sample)
|
| 491 |
+
|
| 492 |
+
sample = self.conv_out(sample)
|
| 493 |
+
|
| 494 |
+
# reshape to (batch, channel, framerate, width, height)
|
| 495 |
+
sample = sample[None, :].reshape((-1, num_frames) + sample.shape[1:]).permute(0, 2, 1, 3, 4)
|
| 496 |
+
|
| 497 |
+
if not return_dict:
|
| 498 |
+
return (sample,)
|
| 499 |
+
|
| 500 |
+
return UNet3DConditionOutput(sample=sample)
|
zeroscope_v2_576w/.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
zeroscope_v2_576w/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
pipeline_tag: text-to-video
|
| 3 |
+
license: cc-by-nc-4.0
|
| 4 |
+
---
|
| 5 |
+
|
| 6 |
+

|
| 7 |
+
|
| 8 |
+
# zeroscope_v2 576w
|
| 9 |
+
A watermark-free Modelscope-based video model optimized for producing high-quality 16:9 compositions and a smooth video output. This model was trained from the [original weights](https://huggingface.co/damo-vilab/modelscope-damo-text-to-video-synthesis) using 9,923 clips and 29,769 tagged frames at 24 frames, 576x320 resolution.<br />
|
| 10 |
+
zeroscope_v2_567w is specifically designed for upscaling with [zeroscope_v2_XL](https://huggingface.co/cerspense/zeroscope_v2_XL) using vid2vid in the [1111 text2video](https://github.com/kabachuha/sd-webui-text2video) extension by [kabachuha](https://github.com/kabachuha). Leveraging this model as a preliminary step allows for superior overall compositions at higher resolutions in zeroscope_v2_XL, permitting faster exploration in 576x320 before transitioning to a high-resolution render. See some [example outputs](https://www.youtube.com/watch?v=HO3APT_0UA4) that have been upscaled to 1024x576 using zeroscope_v2_XL. (courtesy of [dotsimulate](https://www.instagram.com/dotsimulate/))<br />
|
| 11 |
+
|
| 12 |
+
zeroscope_v2_576w uses 7.9gb of vram when rendering 30 frames at 576x320
|
| 13 |
+
|
| 14 |
+
### Using it with the 1111 text2video extension
|
| 15 |
+
|
| 16 |
+
1. Download files in the zs2_576w folder.
|
| 17 |
+
2. Replace the respective files in the 'stable-diffusion-webui\models\ModelScope\t2v' directory.
|
| 18 |
+
|
| 19 |
+
### Upscaling recommendations
|
| 20 |
+
|
| 21 |
+
For upscaling, it's recommended to use [zeroscope_v2_XL](https://huggingface.co/cerspense/zeroscope_v2_XL) via vid2vid in the 1111 extension. It works best at 1024x576 with a denoise strength between 0.66 and 0.85. Remember to use the same prompt that was used to generate the original clip. <br />
|
| 22 |
+
|
| 23 |
+
### Usage in 🧨 Diffusers
|
| 24 |
+
|
| 25 |
+
Let's first install the libraries required:
|
| 26 |
+
|
| 27 |
+
```bash
|
| 28 |
+
$ pip install diffusers transformers accelerate torch
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
Now, generate a video:
|
| 32 |
+
|
| 33 |
+
```py
|
| 34 |
+
import torch
|
| 35 |
+
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
|
| 36 |
+
from diffusers.utils import export_to_video
|
| 37 |
+
|
| 38 |
+
pipe = DiffusionPipeline.from_pretrained("cerspense/zeroscope_v2_576w", torch_dtype=torch.float16)
|
| 39 |
+
pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
|
| 40 |
+
pipe.enable_model_cpu_offload()
|
| 41 |
+
|
| 42 |
+
prompt = "Darth Vader is surfing on waves"
|
| 43 |
+
video_frames = pipe(prompt, num_inference_steps=40, height=320, width=576, num_frames=24).frames
|
| 44 |
+
video_path = export_to_video(video_frames)
|
| 45 |
+
```
|
| 46 |
+
|
| 47 |
+
Here are some results:
|
| 48 |
+
|
| 49 |
+
<table>
|
| 50 |
+
<tr>
|
| 51 |
+
Darth vader is surfing on waves.
|
| 52 |
+
<br>
|
| 53 |
+
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/darthvader_cerpense.gif"
|
| 54 |
+
alt="Darth vader surfing in waves."
|
| 55 |
+
style="width: 576;" />
|
| 56 |
+
</center></td>
|
| 57 |
+
</tr>
|
| 58 |
+
</table>
|
| 59 |
+
|
| 60 |
+
### Known issues
|
| 61 |
+
|
| 62 |
+
Lower resolutions or fewer frames could lead to suboptimal output. <br />
|
| 63 |
+
|
| 64 |
+
Thanks to [camenduru](https://github.com/camenduru), [kabachuha](https://github.com/kabachuha), [ExponentialML](https://github.com/ExponentialML), [dotsimulate](https://www.instagram.com/dotsimulate/), [VANYA](https://twitter.com/veryVANYA), [polyware](https://twitter.com/polyware_ai), [tin2tin](https://github.com/tin2tin)<br />
|
zeroscope_v2_576w/model_index.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "TextToVideoSDPipeline",
|
| 3 |
+
"_diffusers_version": "0.17.0.dev0",
|
| 4 |
+
"scheduler": [
|
| 5 |
+
"diffusers",
|
| 6 |
+
"DDIMScheduler"
|
| 7 |
+
],
|
| 8 |
+
"text_encoder": [
|
| 9 |
+
"transformers",
|
| 10 |
+
"CLIPTextModel"
|
| 11 |
+
],
|
| 12 |
+
"tokenizer": [
|
| 13 |
+
"transformers",
|
| 14 |
+
"CLIPTokenizer"
|
| 15 |
+
],
|
| 16 |
+
"unet": [
|
| 17 |
+
"diffusers",
|
| 18 |
+
"UNet3DConditionModel"
|
| 19 |
+
],
|
| 20 |
+
"vae": [
|
| 21 |
+
"diffusers",
|
| 22 |
+
"AutoencoderKL"
|
| 23 |
+
]
|
| 24 |
+
}
|
zeroscope_v2_576w/scheduler/scheduler_config.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "DDIMScheduler",
|
| 3 |
+
"_diffusers_version": "0.17.0.dev0",
|
| 4 |
+
"beta_end": 0.012,
|
| 5 |
+
"beta_schedule": "scaled_linear",
|
| 6 |
+
"beta_start": 0.00085,
|
| 7 |
+
"clip_sample": false,
|
| 8 |
+
"clip_sample_range": 1.0,
|
| 9 |
+
"dynamic_thresholding_ratio": 0.995,
|
| 10 |
+
"num_train_timesteps": 1000,
|
| 11 |
+
"prediction_type": "epsilon",
|
| 12 |
+
"sample_max_value": 1.0,
|
| 13 |
+
"set_alpha_to_one": false,
|
| 14 |
+
"skip_prk_steps": true,
|
| 15 |
+
"steps_offset": 1,
|
| 16 |
+
"thresholding": false,
|
| 17 |
+
"trained_betas": null
|
| 18 |
+
}
|
zeroscope_v2_576w/text_encoder/config.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "./models/model_scope_diffusers/",
|
| 3 |
+
"architectures": [
|
| 4 |
+
"CLIPTextModel"
|
| 5 |
+
],
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"bos_token_id": 0,
|
| 8 |
+
"dropout": 0.0,
|
| 9 |
+
"eos_token_id": 2,
|
| 10 |
+
"hidden_act": "gelu",
|
| 11 |
+
"hidden_size": 1024,
|
| 12 |
+
"initializer_factor": 1.0,
|
| 13 |
+
"initializer_range": 0.02,
|
| 14 |
+
"intermediate_size": 4096,
|
| 15 |
+
"layer_norm_eps": 1e-05,
|
| 16 |
+
"max_position_embeddings": 77,
|
| 17 |
+
"model_type": "clip_text_model",
|
| 18 |
+
"num_attention_heads": 16,
|
| 19 |
+
"num_hidden_layers": 23,
|
| 20 |
+
"pad_token_id": 1,
|
| 21 |
+
"projection_dim": 512,
|
| 22 |
+
"torch_dtype": "float16",
|
| 23 |
+
"transformers_version": "4.29.2",
|
| 24 |
+
"vocab_size": 49408
|
| 25 |
+
}
|
zeroscope_v2_576w/text_encoder/pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:76877281ed10a4a71f6c2aa0edd286a9e5e23a852a05d13fb05965b464a305bb
|
| 3 |
+
size 680904225
|
zeroscope_v2_576w/tokenizer/merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
zeroscope_v2_576w/tokenizer/special_tokens_map.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"bos_token": {
|
| 3 |
+
"content": "<|startoftext|>",
|
| 4 |
+
"lstrip": false,
|
| 5 |
+
"normalized": true,
|
| 6 |
+
"rstrip": false,
|
| 7 |
+
"single_word": false
|
| 8 |
+
},
|
| 9 |
+
"eos_token": {
|
| 10 |
+
"content": "<|endoftext|>",
|
| 11 |
+
"lstrip": false,
|
| 12 |
+
"normalized": true,
|
| 13 |
+
"rstrip": false,
|
| 14 |
+
"single_word": false
|
| 15 |
+
},
|
| 16 |
+
"pad_token": "!",
|
| 17 |
+
"unk_token": {
|
| 18 |
+
"content": "<|endoftext|>",
|
| 19 |
+
"lstrip": false,
|
| 20 |
+
"normalized": true,
|
| 21 |
+
"rstrip": false,
|
| 22 |
+
"single_word": false
|
| 23 |
+
}
|
| 24 |
+
}
|
zeroscope_v2_576w/tokenizer/tokenizer_config.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"bos_token": {
|
| 4 |
+
"__type": "AddedToken",
|
| 5 |
+
"content": "<|startoftext|>",
|
| 6 |
+
"lstrip": false,
|
| 7 |
+
"normalized": true,
|
| 8 |
+
"rstrip": false,
|
| 9 |
+
"single_word": false
|
| 10 |
+
},
|
| 11 |
+
"clean_up_tokenization_spaces": true,
|
| 12 |
+
"do_lower_case": true,
|
| 13 |
+
"eos_token": {
|
| 14 |
+
"__type": "AddedToken",
|
| 15 |
+
"content": "<|endoftext|>",
|
| 16 |
+
"lstrip": false,
|
| 17 |
+
"normalized": true,
|
| 18 |
+
"rstrip": false,
|
| 19 |
+
"single_word": false
|
| 20 |
+
},
|
| 21 |
+
"errors": "replace",
|
| 22 |
+
"model_max_length": 77,
|
| 23 |
+
"pad_token": "<|endoftext|>",
|
| 24 |
+
"tokenizer_class": "CLIPTokenizer",
|
| 25 |
+
"unk_token": {
|
| 26 |
+
"__type": "AddedToken",
|
| 27 |
+
"content": "<|endoftext|>",
|
| 28 |
+
"lstrip": false,
|
| 29 |
+
"normalized": true,
|
| 30 |
+
"rstrip": false,
|
| 31 |
+
"single_word": false
|
| 32 |
+
}
|
| 33 |
+
}
|
zeroscope_v2_576w/tokenizer/vocab.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
zeroscope_v2_576w/unet/config.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "UNet3DConditionModel",
|
| 3 |
+
"_diffusers_version": "0.17.0.dev0",
|
| 4 |
+
"_name_or_path": "./models/model_scope_diffusers/",
|
| 5 |
+
"act_fn": "silu",
|
| 6 |
+
"attention_head_dim": 64,
|
| 7 |
+
"block_out_channels": [
|
| 8 |
+
320,
|
| 9 |
+
640,
|
| 10 |
+
1280,
|
| 11 |
+
1280
|
| 12 |
+
],
|
| 13 |
+
"cross_attention_dim": 1024,
|
| 14 |
+
"down_block_types": [
|
| 15 |
+
"CrossAttnDownBlock3D",
|
| 16 |
+
"CrossAttnDownBlock3D",
|
| 17 |
+
"CrossAttnDownBlock3D",
|
| 18 |
+
"DownBlock3D"
|
| 19 |
+
],
|
| 20 |
+
"downsample_padding": 1,
|
| 21 |
+
"in_channels": 4,
|
| 22 |
+
"layers_per_block": 2,
|
| 23 |
+
"mid_block_scale_factor": 1,
|
| 24 |
+
"norm_eps": 1e-05,
|
| 25 |
+
"norm_num_groups": 32,
|
| 26 |
+
"out_channels": 4,
|
| 27 |
+
"sample_size": 32,
|
| 28 |
+
"up_block_types": [
|
| 29 |
+
"UpBlock3D",
|
| 30 |
+
"CrossAttnUpBlock3D",
|
| 31 |
+
"CrossAttnUpBlock3D",
|
| 32 |
+
"CrossAttnUpBlock3D"
|
| 33 |
+
]
|
| 34 |
+
}
|
zeroscope_v2_576w/unet/diffusion_pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:42b51ea486109b7479366e01aae7e267a81bb750c13eb5c6b765a3d5bc7987c9
|
| 3 |
+
size 2823110385
|
zeroscope_v2_576w/vae/config.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_class_name": "AutoencoderKL",
|
| 3 |
+
"_diffusers_version": "0.17.0.dev0",
|
| 4 |
+
"_name_or_path": "./models/model_scope_diffusers/",
|
| 5 |
+
"act_fn": "silu",
|
| 6 |
+
"block_out_channels": [
|
| 7 |
+
128,
|
| 8 |
+
256,
|
| 9 |
+
512,
|
| 10 |
+
512
|
| 11 |
+
],
|
| 12 |
+
"down_block_types": [
|
| 13 |
+
"DownEncoderBlock2D",
|
| 14 |
+
"DownEncoderBlock2D",
|
| 15 |
+
"DownEncoderBlock2D",
|
| 16 |
+
"DownEncoderBlock2D"
|
| 17 |
+
],
|
| 18 |
+
"in_channels": 3,
|
| 19 |
+
"latent_channels": 4,
|
| 20 |
+
"layers_per_block": 2,
|
| 21 |
+
"norm_num_groups": 32,
|
| 22 |
+
"out_channels": 3,
|
| 23 |
+
"sample_size": 512,
|
| 24 |
+
"scaling_factor": 0.18215,
|
| 25 |
+
"up_block_types": [
|
| 26 |
+
"UpDecoderBlock2D",
|
| 27 |
+
"UpDecoderBlock2D",
|
| 28 |
+
"UpDecoderBlock2D",
|
| 29 |
+
"UpDecoderBlock2D"
|
| 30 |
+
]
|
| 31 |
+
}
|
zeroscope_v2_576w/vae/diffusion_pytorch_model.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8b0d11ff25d00ceaa02f602831d9cfe650509fdc850c0a1bcb2acdfa03bd5d56
|
| 3 |
+
size 167407857
|