Spaces:
Build error
Build error
File size: 23,538 Bytes
b6af722 |
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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
A general implementation of adaln-modulated VIT-like~(DiT) transformer for video processing.
It allows us easy to switch building blocks used and their order. Its instantiation includes
* transformer on fully flattened tokens
* factored spatial and temporal attention
* factored non-overlap spatial and temporal attention
* mixing of above attention types
Limitations:
* In favor of simplicity and cleanness, many ops are not fused and we can do better
* such as combining mutiple adaln MLPs into one inside one transformer block.
* we use reshape heavily, which may be not efficient when its occurs unnecessary CUDA memory copy
Purpose:
* A prototype for testing different attention types and their combinations
* Idealy, we want to know where we should allocate our resources / FLOPS / memory via extensive empirical studies
"""
from collections.abc import Container
from typing import List, Optional, Tuple
import torch
from einops import rearrange
from megatron.core import parallel_state
from torch import nn
from cosmos_predict1.diffusion.module.timm import Mlp
from cosmos_predict1.diffusion.training.conditioner import DataType
from cosmos_predict1.diffusion.training.context_parallel import split_inputs_cp
from cosmos_predict1.diffusion.training.networks.general_dit import GeneralDIT
from cosmos_predict1.diffusion.training.tensor_parallel import scatter_along_first_dim
from cosmos_predict1.utils import log
class ActionConditionalGeneralDIT(GeneralDIT):
"""
ActionConditionalGeneralDIT is a subclass of GeneralDIT that take `action` as condition.
Action embedding is would be added to timestep embedding.
"""
def forward_before_blocks(
self,
x: torch.Tensor,
timesteps: torch.Tensor,
crossattn_emb: torch.Tensor,
action: Optional[torch.Tensor] = None,
crossattn_mask: Optional[torch.Tensor] = None,
fps: Optional[torch.Tensor] = None,
image_size: Optional[torch.Tensor] = None,
padding_mask: Optional[torch.Tensor] = None,
scalar_feature: Optional[torch.Tensor] = None,
data_type: Optional[DataType] = DataType.VIDEO,
latent_condition: Optional[torch.Tensor] = None,
latent_condition_sigma: Optional[torch.Tensor] = None,
**kwargs,
) -> torch.Tensor:
"""
Args:
x: (B, C, T, H, W) tensor of spatial-temp inputs
timesteps: (B, ) tensor of timesteps
crossattn_emb: (B, N, D) tensor of cross-attention embeddings
crossattn_mask: (B, N) tensor of cross-attention masks
"""
del kwargs
assert isinstance(
data_type, DataType
), f"Expected DataType, got {type(data_type)}. We need discuss this flag later."
original_shape = x.shape
x_B_T_H_W_D, rope_emb_L_1_1_D, extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D = self.prepare_embedded_sequence(
x,
fps=fps,
padding_mask=padding_mask,
latent_condition=latent_condition,
latent_condition_sigma=latent_condition_sigma,
)
# logging affline scale information
affline_scale_log_info = {}
timesteps_B_D, adaln_lora_B_3D = self.t_embedder(timesteps.flatten())
affline_emb_B_D = timesteps_B_D
affline_scale_log_info["timesteps_B_D"] = timesteps_B_D.detach()
if scalar_feature is not None:
raise NotImplementedError("Scalar feature is not implemented yet.")
timesteps_B_D = timesteps_B_D + scalar_feature.mean(dim=1)
if self.additional_timestamp_channels:
additional_cond_B_D = self.prepare_additional_timestamp_embedder(
bs=x.shape[0],
fps=fps,
h=image_size[:, 0],
w=image_size[:, 1],
org_h=image_size[:, 2],
org_w=image_size[:, 3],
)
affline_emb_B_D += additional_cond_B_D
affline_scale_log_info["additional_cond_B_D"] = additional_cond_B_D.detach()
affline_scale_log_info["affline_emb_B_D"] = affline_emb_B_D.detach()
affline_emb_B_D = self.affline_norm(affline_emb_B_D)
# for logging purpose
self.affline_scale_log_info = affline_scale_log_info
self.affline_emb = affline_emb_B_D
self.crossattn_emb = crossattn_emb
self.crossattn_mask = crossattn_mask
if self.use_cross_attn_mask:
crossattn_mask = crossattn_mask[:, None, None, :].to(dtype=torch.bool) # [B, 1, 1, length]
else:
crossattn_mask = None
if self.blocks["block0"].x_format == "THWBD":
x = rearrange(x_B_T_H_W_D, "B T H W D -> T H W B D")
if extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D is not None:
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D = rearrange(
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D, "B T H W D -> T H W B D"
)
crossattn_emb = rearrange(crossattn_emb, "B M D -> M B D")
if crossattn_mask:
crossattn_mask = rearrange(crossattn_mask, "B M -> M B")
if self.sequence_parallel:
tp_group = parallel_state.get_tensor_model_parallel_group()
# Sequence parallel requires the input tensor to be scattered along the first dimension.
assert self.block_config == "FA-CA-MLP" # Only support this block config for now
T, H, W, B, D = x.shape
# variable name x_T_H_W_B_D is no longer valid. x is reshaped to THW*1*1*b*D and will be reshaped back in FinalLayer
x = x.view(T * H * W, 1, 1, B, D)
assert x.shape[0] % parallel_state.get_tensor_model_parallel_world_size() == 0
x = scatter_along_first_dim(x, tp_group)
if extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D is not None:
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D = extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D.view(
T * H * W, 1, 1, B, D
)
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D = scatter_along_first_dim(
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D, tp_group
)
elif self.blocks["block0"].x_format == "BTHWD":
x = x_B_T_H_W_D
else:
raise ValueError(f"Unknown x_format {self.blocks[0].x_format}")
output = {
"x": x,
"affline_emb_B_D": affline_emb_B_D,
"crossattn_emb": crossattn_emb,
"crossattn_mask": crossattn_mask,
"rope_emb_L_1_1_D": rope_emb_L_1_1_D,
"adaln_lora_B_3D": adaln_lora_B_3D,
"original_shape": original_shape,
"extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D": extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D,
}
return output
def forward(
self,
x: torch.Tensor,
timesteps: torch.Tensor,
crossattn_emb: torch.Tensor,
action: Optional[torch.Tensor] = None,
crossattn_mask: Optional[torch.Tensor] = None,
fps: Optional[torch.Tensor] = None,
image_size: Optional[torch.Tensor] = None,
padding_mask: Optional[torch.Tensor] = None,
scalar_feature: Optional[torch.Tensor] = None,
data_type: Optional[DataType] = DataType.VIDEO,
x_ctrl: Optional[dict] = None,
latent_condition: Optional[torch.Tensor] = None,
latent_condition_sigma: Optional[torch.Tensor] = None,
feature_indices: Optional[Container[int]] = None,
return_features_early: bool = False,
condition_video_augment_sigma: Optional[torch.Tensor] = None,
**kwargs,
) -> torch.Tensor | List[torch.Tensor] | Tuple[torch.Tensor, List[torch.Tensor]]:
"""
Args:
x: (B, C, T, H, W) tensor of spatial-temp inputs
timesteps: (B, ) tensor of timesteps
crossattn_emb: (B, N, D) tensor of cross-attention embeddings
crossattn_mask: (B, N) tensor of cross-attention masks
feature_indices: A set of feature indices (a set of integers) decides which blocks
to extract features from. If the set is non-empty, then features will be returned.
By default, feature_indices=None means extract no features.
return_features_early: If true, the forward pass returns the features once the set is complete.
This means the forward pass will not finish completely and no final output is returned.
condition_video_augment_sigma: (B,) used in lvg(long video generation), we add noise with this sigma to augment condition input, the lvg model will condition on the condition_video_augment_sigma value;
we need forward_before_blocks pass to the forward_before_blocks function.
"""
if feature_indices is None:
feature_indices = {}
if return_features_early and len(feature_indices) == 0:
# Exit immediately if user requested this.
return []
inputs = self.forward_before_blocks(
x=x,
timesteps=timesteps,
crossattn_emb=crossattn_emb,
action=action,
crossattn_mask=crossattn_mask,
fps=fps,
image_size=image_size,
padding_mask=padding_mask,
scalar_feature=scalar_feature,
data_type=data_type,
latent_condition=latent_condition,
latent_condition_sigma=latent_condition_sigma,
condition_video_augment_sigma=condition_video_augment_sigma,
**kwargs,
)
x, affline_emb_B_D, crossattn_emb, crossattn_mask, rope_emb_L_1_1_D, adaln_lora_B_3D, original_shape = (
inputs["x"],
inputs["affline_emb_B_D"],
inputs["crossattn_emb"],
inputs["crossattn_mask"],
inputs["rope_emb_L_1_1_D"],
inputs["adaln_lora_B_3D"],
inputs["original_shape"],
)
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D = inputs["extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D"]
if extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D is not None:
assert (
x.shape == extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D.shape
), f"{x.shape} != {extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D.shape} {original_shape}"
if self.use_memory_save:
return self.forward_blocks_memory_save(
x,
affline_emb_B_D,
crossattn_emb,
crossattn_mask,
rope_emb_L_1_1_D,
adaln_lora_B_3D,
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D,
feature_indices,
original_shape,
x_ctrl,
return_features_early,
)
return self.forward_blocks_regular(
x,
affline_emb_B_D,
crossattn_emb,
crossattn_mask,
rope_emb_L_1_1_D,
adaln_lora_B_3D,
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D,
feature_indices,
original_shape,
x_ctrl,
return_features_early,
)
class ActionConditionalVideoExtendGeneralDIT(ActionConditionalGeneralDIT):
"""
ActionConditionalVideoExtendGeneralDIT is a subclass of ActionConditionalGeneralDIT that take `action` as condition.
Action embedding is would be added to timestep embedding.
"""
def __init__(self, *args, in_channels=16 + 1, add_augment_sigma_embedding=False, **kwargs):
self.add_augment_sigma_embedding = add_augment_sigma_embedding
# extra channel for video condition mask
super().__init__(*args, in_channels=in_channels, **kwargs)
log.info(f"VideoExtendGeneralDIT in_channels: {in_channels}")
assert hasattr(self, "model_channels"), "model_channels attribute is missing"
self.action_embedder_B_D = Mlp(
in_features=7,
hidden_features=self.model_channels * 4,
out_features=self.model_channels,
act_layer=lambda: nn.GELU(approximate="tanh"),
drop=0,
)
self.action_embedder_B_3D = Mlp(
in_features=7,
hidden_features=self.model_channels * 4,
out_features=self.model_channels * 3,
act_layer=lambda: nn.GELU(approximate="tanh"),
drop=0,
)
def forward(
self,
x: torch.Tensor,
timesteps: torch.Tensor,
crossattn_emb: torch.Tensor,
action: Optional[torch.Tensor] = None,
crossattn_mask: Optional[torch.Tensor] = None,
fps: Optional[torch.Tensor] = None,
image_size: Optional[torch.Tensor] = None,
padding_mask: Optional[torch.Tensor] = None,
scalar_feature: Optional[torch.Tensor] = None,
data_type: Optional[DataType] = DataType.VIDEO,
video_cond_bool: Optional[torch.Tensor] = None,
condition_video_indicator: Optional[torch.Tensor] = None,
condition_video_input_mask: Optional[torch.Tensor] = None,
condition_video_augment_sigma: Optional[torch.Tensor] = None,
condition_video_pose: Optional[torch.Tensor] = None,
**kwargs,
) -> torch.Tensor:
"""Args:
condition_video_augment_sigma: (B) tensor of sigma value for the conditional input augmentation
condition_video_pose: (B, 1, T, H, W) tensor of pose condition
"""
B, C, T, H, W = x.shape
if data_type == DataType.VIDEO:
assert (
condition_video_input_mask is not None
), "condition_video_input_mask is required for video data type; check if your model_obj is extend_model.FSDPDiffusionModel or the base DiffusionModel"
if self.cp_group is not None:
condition_video_input_mask = split_inputs_cp(
condition_video_input_mask, seq_dim=2, cp_group=self.cp_group
)
condition_video_indicator = split_inputs_cp(
condition_video_indicator, seq_dim=2, cp_group=self.cp_group
)
if condition_video_pose is not None:
condition_video_pose = split_inputs_cp(condition_video_pose, seq_dim=2, cp_group=self.cp_group)
# log.critical(f"hit video case, video_cond_bool: {video_cond_bool}, condition_video_indicator: {condition_video_indicator.flatten()}, condition_video_input_mask: {condition_video_input_mask.shape}, {condition_video_input_mask[:,:,:,0,0]}", rank0_only=False)
input_list = [x, condition_video_input_mask]
if condition_video_pose is not None:
if condition_video_pose.shape[2] > T:
log.warning(
f"condition_video_pose has more frames than the input video: {condition_video_pose.shape} > {x.shape}"
)
condition_video_pose = condition_video_pose[:, :, :T, :, :].contiguous()
input_list.append(condition_video_pose)
x = torch.cat(
input_list,
dim=1,
)
if data_type == DataType.IMAGE:
# For image, we dont have condition_video_input_mask, or condition_video_pose
# We need to add the extra channel for video condition mask
padding_channels = self.in_channels - x.shape[1]
x = torch.cat([x, torch.zeros((B, padding_channels, T, H, W), dtype=x.dtype, device=x.device)], dim=1)
else:
assert x.shape[1] == self.in_channels, f"Expected {self.in_channels} channels, got {x.shape[1]}"
return super().forward(
x=x,
timesteps=timesteps,
crossattn_emb=crossattn_emb,
action=action,
crossattn_mask=crossattn_mask,
fps=fps,
image_size=image_size,
padding_mask=padding_mask,
scalar_feature=scalar_feature,
data_type=data_type,
condition_video_augment_sigma=condition_video_augment_sigma,
**kwargs,
)
def forward_before_blocks(
self,
x: torch.Tensor,
timesteps: torch.Tensor,
crossattn_emb: torch.Tensor,
action: Optional[torch.Tensor] = None,
crossattn_mask: Optional[torch.Tensor] = None,
fps: Optional[torch.Tensor] = None,
image_size: Optional[torch.Tensor] = None,
padding_mask: Optional[torch.Tensor] = None,
scalar_feature: Optional[torch.Tensor] = None,
data_type: Optional[DataType] = DataType.VIDEO,
latent_condition: Optional[torch.Tensor] = None,
latent_condition_sigma: Optional[torch.Tensor] = None,
condition_video_augment_sigma: Optional[torch.Tensor] = None,
**kwargs,
) -> torch.Tensor:
"""
Args:
x: (B, C, T, H, W) tensor of spatial-temp inputs
timesteps: (B, ) tensor of timesteps
crossattn_emb: (B, N, D) tensor of cross-attention embeddings
crossattn_mask: (B, N) tensor of cross-attention masks
condition_video_augment_sigma: (B, T) tensor of sigma value for the conditional input augmentation
"""
del kwargs
assert isinstance(
data_type, DataType
), f"Expected DataType, got {type(data_type)}. We need discuss this flag later."
original_shape = x.shape
x_B_T_H_W_D, rope_emb_L_1_1_D, extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D = self.prepare_embedded_sequence(
x,
fps=fps,
padding_mask=padding_mask,
latent_condition=latent_condition,
latent_condition_sigma=latent_condition_sigma,
)
# logging affline scale information
affline_scale_log_info = {}
timesteps_B_D, adaln_lora_B_3D = self.t_embedder(timesteps.flatten())
affline_emb_B_D = timesteps_B_D
affline_scale_log_info["timesteps_B_D"] = timesteps_B_D.detach()
# Add action conditioning
assert action is not None, "Action is required for action-conditional training"
if action is not None:
action = action[:, 0, :] # Since we are now training on 1 frame, we only need the first frame action.
action_embedding_B_D = self.action_embedder_B_D(action)
action_embedding_B_3D = self.action_embedder_B_3D(action)
timesteps_B_D = timesteps_B_D + action_embedding_B_D
adaln_lora_B_3D = adaln_lora_B_3D + action_embedding_B_3D
if scalar_feature is not None:
raise NotImplementedError("Scalar feature is not implemented yet.")
timesteps_B_D = timesteps_B_D + scalar_feature.mean(dim=1)
if self.additional_timestamp_channels:
additional_cond_B_D = self.prepare_additional_timestamp_embedder(
bs=x.shape[0],
fps=fps,
h=image_size[:, 0],
w=image_size[:, 1],
org_h=image_size[:, 2],
org_w=image_size[:, 3],
)
affline_emb_B_D += additional_cond_B_D
affline_scale_log_info["additional_cond_B_D"] = additional_cond_B_D.detach()
if self.add_augment_sigma_embedding:
if condition_video_augment_sigma is None:
# Handling image case
# Note: for video case, when there is not condition frames, we also set it as zero, see extend_model augment_conditional_latent_frames function
assert data_type == DataType.IMAGE, "condition_video_augment_sigma is required for video data type"
condition_video_augment_sigma = torch.zeros_like(timesteps.flatten())
affline_augment_sigma_emb_B_D, adaln_lora_sigma_emb_B_3D = self.augment_sigma_embedder(
condition_video_augment_sigma.flatten()
)
affline_emb_B_D = affline_emb_B_D + affline_augment_sigma_emb_B_D
affline_scale_log_info["affline_emb_B_D"] = affline_emb_B_D.detach()
affline_emb_B_D = self.affline_norm(affline_emb_B_D)
# for logging purpose
self.affline_scale_log_info = affline_scale_log_info
self.affline_emb = affline_emb_B_D
self.crossattn_emb = crossattn_emb
self.crossattn_mask = crossattn_mask
if self.use_cross_attn_mask:
crossattn_mask = crossattn_mask[:, None, None, :].to(dtype=torch.bool) # [B, 1, 1, length]
else:
crossattn_mask = None
if self.blocks["block0"].x_format == "THWBD":
x = rearrange(x_B_T_H_W_D, "B T H W D -> T H W B D")
if extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D is not None:
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D = rearrange(
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D, "B T H W D -> T H W B D"
)
crossattn_emb = rearrange(crossattn_emb, "B M D -> M B D")
if crossattn_mask:
crossattn_mask = rearrange(crossattn_mask, "B M -> M B")
if self.sequence_parallel:
tp_group = parallel_state.get_tensor_model_parallel_group()
# Sequence parallel requires the input tensor to be scattered along the first dimension.
assert self.block_config == "FA-CA-MLP" # Only support this block config for now
T, H, W, B, D = x.shape
# variable name x_T_H_W_B_D is no longer valid. x is reshaped to THW*1*1*b*D and will be reshaped back in FinalLayer
x = x.view(T * H * W, 1, 1, B, D)
assert x.shape[0] % parallel_state.get_tensor_model_parallel_world_size() == 0
x = scatter_along_first_dim(x, tp_group)
if extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D is not None:
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D = extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D.view(
T * H * W, 1, 1, B, D
)
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D = scatter_along_first_dim(
extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D, tp_group
)
elif self.blocks["block0"].x_format == "BTHWD":
x = x_B_T_H_W_D
else:
raise ValueError(f"Unknown x_format {self.blocks[0].x_format}")
output = {
"x": x,
"affline_emb_B_D": affline_emb_B_D,
"crossattn_emb": crossattn_emb,
"crossattn_mask": crossattn_mask,
"rope_emb_L_1_1_D": rope_emb_L_1_1_D,
"adaln_lora_B_3D": adaln_lora_B_3D,
"original_shape": original_shape,
"extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D": extra_pos_emb_B_T_H_W_D_or_T_H_W_B_D,
}
return output
|