Add Cosmos3 Edge and Distilled checkpoints support (#5001)
Signed-off-by: Bartosz Stefaniak <bstefaniak@nvidia.com>
This commit is contained in:
1 parent
0a9a5c231e
commit
85f7a5b374
12 files changed
+1789
-216
No files matched your search
@@ -7,10 +7,12 @@ from .pipeline_cosmos3 import (
|
||||
get_cosmos3_pre_process_func,
|
||||
)
|
||||
from .transformer_cosmos3 import Cosmos3VFMTransformer
|
||||
from .transformer_cosmos3_edge import Cosmos3EdgeVFMTransformer
|
||||
|
||||
__all__ = [
|
||||
"Cosmos3OmniDiffusersPipeline",
|
||||
"get_cosmos3_post_process_func",
|
||||
"get_cosmos3_pre_process_func",
|
||||
"Cosmos3VFMTransformer",
|
||||
"Cosmos3EdgeVFMTransformer",
|
||||
]
|
||||
@@ -29,6 +29,7 @@ is produced from sound latents rather than from ``multi_modal_data["audio"]``.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
import time
|
||||
@@ -39,7 +40,7 @@ from typing import Any, ClassVar
|
||||
import numpy as np
|
||||
import PIL.Image
|
||||
import torch
|
||||
from diffusers import UniPCMultistepScheduler
|
||||
from diffusers import FlowMatchEulerDiscreteScheduler
|
||||
from diffusers.utils.torch_utils import randn_tensor
|
||||
from diffusers.video_processor import VideoProcessor
|
||||
from torch import nn
|
||||
@@ -61,6 +62,9 @@ from vllm_omni.diffusion.models.interface import (
|
||||
SupportsComponentDiscovery,
|
||||
)
|
||||
from vllm_omni.diffusion.models.progress_bar import ProgressBarMixin, _is_rank_zero
|
||||
from vllm_omni.diffusion.models.schedulers.scheduling_flow_unipc_multistep import (
|
||||
FlowUniPCMultistepScheduler,
|
||||
)
|
||||
from vllm_omni.diffusion.profiler.diffusion_pipeline_profiler import DiffusionPipelineProfilerMixin
|
||||
from vllm_omni.diffusion.request import OmniDiffusionRequest
|
||||
from vllm_omni.diffusion.worker.request_batch import DiffusionRequestBatch
|
||||
@@ -94,7 +98,8 @@ from .transfer import (
|
||||
transfer_max_frames_from_extra_args,
|
||||
uint8_cthw_to_normalized_5d,
|
||||
)
|
||||
from .transformer_cosmos3 import Cosmos3VFMTransformer, resolve_sound_gen
|
||||
from .transformer_cosmos3 import Cosmos3VFMTransformer, _tf_config_get, resolve_sound_gen
|
||||
from .transformer_cosmos3_edge import COSMOS3_EDGE_BACKBONE_TYPE, Cosmos3EdgeVFMTransformer
|
||||
from .utils import (
|
||||
COSMOS3_DEFAULT_CONDITION_FRAME_INDEXES_VISION,
|
||||
COSMOS3_VAE_TEMPORAL_COMPRESSION,
|
||||
@@ -154,6 +159,7 @@ COSMOS3_T2V_DEFAULT_WIDTH = 1280
|
||||
COSMOS3_T2V_DEFAULT_NUM_FRAMES = 189
|
||||
COSMOS3_T2V_DEFAULT_NUM_INFERENCE_STEPS = 35
|
||||
COSMOS3_T2V_DEFAULT_GUIDANCE_SCALE = 6.0
|
||||
COSMOS3_VIDEO_DEFAULT_FLOW_SHIFT = 10.0
|
||||
|
||||
COSMOS3_T2I_DEFAULT_HEIGHT = 1024
|
||||
COSMOS3_T2I_DEFAULT_WIDTH = 1024
|
||||
@@ -162,12 +168,73 @@ COSMOS3_T2I_DEFAULT_GUIDANCE_SCALE = 7.0
|
||||
COSMOS3_T2I_DEFAULT_FLOW_SHIFT = 3.0
|
||||
COSMOS3_T2I_DEFAULT_GUIDANCE_INTERVAL: tuple[float, float] = (400.0, 1000.0)
|
||||
|
||||
COSMOS3_EDGE_T2V_DEFAULT_HEIGHT = 480
|
||||
COSMOS3_EDGE_T2V_DEFAULT_WIDTH = 832
|
||||
COSMOS3_EDGE_T2V_DEFAULT_GUIDANCE_SCALE = 5.0
|
||||
COSMOS3_EDGE_VIDEO_DEFAULT_FLOW_SHIFT = 3.0
|
||||
COSMOS3_EDGE_T2I_DEFAULT_HEIGHT = 640
|
||||
COSMOS3_EDGE_T2I_DEFAULT_WIDTH = 640
|
||||
|
||||
COSMOS3_DISTILLED_CHECKPOINT_SCHEDULER_CLASS = "FlowMatchEulerDiscreteScheduler"
|
||||
|
||||
# Truncation cap on the prompt token count (shared by T2I and T2V). Prompts
|
||||
# are tokenized to their natural length (no padding); this only bounds the
|
||||
# UND pathway / GEN cross-attention cost for pathologically long prompts.
|
||||
COSMOS3_DEFAULT_MAX_SEQUENCE_LENGTH = 4096
|
||||
|
||||
|
||||
def _ceil_video_num_frames(num_frames: int, temporal_compression_factor: int) -> int:
|
||||
"""Round a video length up to the causal VAE's ``factor * k + 1`` grid."""
|
||||
if temporal_compression_factor <= 0:
|
||||
raise ValueError(f"Cosmos3 temporal_compression_factor must be positive, got {temporal_compression_factor}.")
|
||||
if num_frames <= 1:
|
||||
return num_frames
|
||||
latent_frames = math.ceil((num_frames - 1) / temporal_compression_factor)
|
||||
return latent_frames * temporal_compression_factor + 1
|
||||
|
||||
|
||||
def _format_json_object_prompt(
|
||||
prompt: str,
|
||||
*,
|
||||
num_frames: int,
|
||||
frame_rate: float,
|
||||
height: int,
|
||||
width: int,
|
||||
aspect_ratio: str | None,
|
||||
) -> str | None:
|
||||
"""Match Imaginaire4 metadata injection for JSON-object prompts."""
|
||||
try:
|
||||
prompt_obj = json.loads(prompt)
|
||||
except (json.JSONDecodeError, TypeError, ValueError):
|
||||
return None
|
||||
if not isinstance(prompt_obj, dict):
|
||||
return None
|
||||
|
||||
metadata: dict[str, Any] = {}
|
||||
if num_frames > 1:
|
||||
duration_seconds = int(num_frames / frame_rate) if frame_rate > 0 else 0
|
||||
metadata.update({"duration": f"{duration_seconds}s", "fps": float(frame_rate)})
|
||||
else:
|
||||
prompt_obj.pop("duration", None)
|
||||
prompt_obj.pop("fps", None)
|
||||
metadata["resolution"] = {"H": int(height), "W": int(width)}
|
||||
if aspect_ratio is not None:
|
||||
metadata["aspect_ratio"] = aspect_ratio
|
||||
|
||||
prompt_obj.update(metadata)
|
||||
return json.dumps(prompt_obj)
|
||||
|
||||
|
||||
def resolve_cosmos3_transformer_cls(model_config: Any) -> type[Cosmos3VFMTransformer]:
|
||||
"""Select the Cosmos3 transformer implementation from transformer/config.json."""
|
||||
backbone_type = _tf_config_get(model_config, "backbone_type", None)
|
||||
if backbone_type is None:
|
||||
return Cosmos3VFMTransformer
|
||||
if backbone_type == COSMOS3_EDGE_BACKBONE_TYPE:
|
||||
return Cosmos3EdgeVFMTransformer
|
||||
raise ValueError(f"Unsupported Cosmos3 transformer backbone_type={backbone_type!r}.")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Post-process function (registered in registry.py)
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -190,6 +257,9 @@ def get_cosmos3_pre_process_func(od_config: OmniDiffusionConfig):
|
||||
from .guardrails import check_text_safety, ensure_initialized, is_guardrails_enabled
|
||||
|
||||
video_processor = VideoProcessor(vae_scale_factor=16)
|
||||
is_edge_model = (
|
||||
_tf_config_get(getattr(od_config, "tf_model_config", None), "backbone_type", None) == COSMOS3_EDGE_BACKBONE_TYPE
|
||||
)
|
||||
# Eager-load guardrail models at pipeline build time when the server-level
|
||||
# gate is on. Per-request overrides only decide whether the loaded models
|
||||
# are *invoked* — they cannot turn checks on without a server-side preload.
|
||||
@@ -410,12 +480,17 @@ def get_cosmos3_pre_process_func(od_config: OmniDiffusionConfig):
|
||||
extra = _extra_args(request)
|
||||
transfer_requested = action_mode is None and has_transfer_hints(extra)
|
||||
|
||||
# Auto-calculate H/W from aspect ratio (720p max area)
|
||||
# Resolve missing H/W.
|
||||
if transfer_requested:
|
||||
_set_transfer_size_from_image(request, image)
|
||||
elif request.sampling_params.height is None or request.sampling_params.width is None:
|
||||
if action_mode is not None:
|
||||
_set_action_size_from_image(request, image)
|
||||
elif is_edge_model:
|
||||
if request.sampling_params.height is None:
|
||||
request.sampling_params.height = COSMOS3_EDGE_T2V_DEFAULT_HEIGHT
|
||||
if request.sampling_params.width is None:
|
||||
request.sampling_params.width = COSMOS3_EDGE_T2V_DEFAULT_WIDTH
|
||||
else:
|
||||
max_area = 720 * 1280
|
||||
aspect_ratio = image.height / image.width
|
||||
@@ -823,27 +898,52 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
sound_latent_fps = self._sound_tokenizer.latent_fps
|
||||
|
||||
# --- Transformer (weights loaded later via weights_sources) ---
|
||||
self.transformer = Cosmos3VFMTransformer(
|
||||
transformer_cls = resolve_cosmos3_transformer_cls(od_config.tf_model_config)
|
||||
self.transformer = transformer_cls(
|
||||
od_config=od_config,
|
||||
temporal_compression_factor=self.vae_scale_factor_temporal,
|
||||
sound_gen=sound_gen,
|
||||
sound_dim=sound_dim,
|
||||
sound_latent_fps=sound_latent_fps,
|
||||
)
|
||||
self.is_edge_model = transformer_cls is Cosmos3EdgeVFMTransformer
|
||||
|
||||
# --- Scheduler ---
|
||||
# Load from checkpoint to preserve solver_order, timestep_spacing,
|
||||
# beta_schedule, sigma bounds, flow_shift, etc. Only override
|
||||
# flow_shift when explicitly requested by the user.
|
||||
self.scheduler = UniPCMultistepScheduler.from_pretrained(
|
||||
# Distilled model differs from regular one only by scheduler,
|
||||
# distilled one uses FlowMatchEulerDiscreteScheduler, while
|
||||
# regular should use FlowUniPCMultistepScheduler
|
||||
|
||||
scheduler_config = FlowUniPCMultistepScheduler.load_config(
|
||||
model_path,
|
||||
subfolder="scheduler",
|
||||
local_files_only=local_files_only,
|
||||
)
|
||||
if od_config.flow_shift is not None:
|
||||
self.scheduler = UniPCMultistepScheduler.from_config(
|
||||
self.scheduler.config,
|
||||
flow_shift=od_config.flow_shift,
|
||||
|
||||
scheduler_class_name = scheduler_config.get("_class_name")
|
||||
|
||||
self.is_distilled_model = False
|
||||
if scheduler_class_name == COSMOS3_DISTILLED_CHECKPOINT_SCHEDULER_CLASS:
|
||||
fixed_step_config = scheduler_config.get("fixed_step_sampler_config")
|
||||
if not isinstance(fixed_step_config, dict) or fixed_step_config.get("sample_type") != "sde":
|
||||
raise ValueError("Cosmos3 distilled scheduler requires fixed_step_sampler_config.sample_type=sde.")
|
||||
t_list = fixed_step_config.get("t_list")
|
||||
if not isinstance(t_list, list) or not t_list:
|
||||
raise ValueError("Cosmos3 distilled scheduler requires a non-empty fixed_step_sampler_config.t_list.")
|
||||
self.scheduler = FlowMatchEulerDiscreteScheduler.from_config(
|
||||
scheduler_config,
|
||||
stochastic_sampling=True,
|
||||
)
|
||||
self._scheduler_init_t_list = list(t_list)
|
||||
self.is_distilled_model = True
|
||||
else:
|
||||
# Preserve compatible solver settings from the checkpoint, but keep
|
||||
# the base shift neutral. The concrete request shift is applied when
|
||||
# FlowUniPC builds its timesteps.
|
||||
self.scheduler = FlowUniPCMultistepScheduler.from_config(
|
||||
scheduler_config,
|
||||
shift=1.0,
|
||||
use_dynamic_shifting=False,
|
||||
prediction_type="flow_prediction",
|
||||
)
|
||||
self._cpu_scheduler_state()
|
||||
|
||||
@@ -862,14 +962,16 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
),
|
||||
]
|
||||
|
||||
# Snapshot the loaded scheduler config so we can rebuild the
|
||||
# scheduler at request time when a per-request flow_shift override
|
||||
# is supplied (T2I uses shift=3.0; T2V/I2V use the engine default).
|
||||
self._base_scheduler_config = self.scheduler.config
|
||||
self._engine_init_flow_shift = float(getattr(self.scheduler.config, "flow_shift", 1.0) or 1.0)
|
||||
# An engine-level override becomes the video default; otherwise use
|
||||
# the matching regular/Edge default. Per-request values still take
|
||||
# precedence.
|
||||
default_video_flow_shift = (
|
||||
COSMOS3_EDGE_VIDEO_DEFAULT_FLOW_SHIFT if self.is_edge_model else COSMOS3_VIDEO_DEFAULT_FLOW_SHIFT
|
||||
)
|
||||
self._engine_init_flow_shift = float(
|
||||
od_config.flow_shift if od_config.flow_shift is not None else default_video_flow_shift
|
||||
)
|
||||
self._current_flow_shift = self._engine_init_flow_shift
|
||||
self._base_scheduler_use_karras_sigmas = self._scheduler_use_karras_sigmas(self.scheduler.config)
|
||||
self._current_scheduler_use_karras_sigmas = self._base_scheduler_use_karras_sigmas
|
||||
|
||||
self._guidance_scale = None
|
||||
self._num_timesteps = None
|
||||
@@ -989,6 +1091,7 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
"self_attn.to_out.": f"{und_lp}.self_attn.to_out.",
|
||||
"self_attn.norm_q.": f"{und_lp}.self_attn.norm_q.",
|
||||
"self_attn.norm_k.": f"{und_lp}.self_attn.norm_k.",
|
||||
"self_attn.k_norm_und_for_gen.": f"{und_lp}.self_attn.k_norm_und_for_gen.",
|
||||
# GEN attention
|
||||
"self_attn.add_q_proj.": f"{gen_lp}.cross_attention.to_q.",
|
||||
"self_attn.add_k_proj.": f"{gen_lp}.cross_attention.to_k.",
|
||||
@@ -1054,6 +1157,7 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
loaded = loader.load_weights(_remapped_weights())
|
||||
self.transformer.post_load_weights()
|
||||
self.transformer.eval()
|
||||
self.transformer.validate_loaded_weights(loaded)
|
||||
if getattr(self.transformer, "sound_gen", False):
|
||||
sound_markers = ("audio_proj_in.", "audio_proj_out.", "audio_modality_embed")
|
||||
missing = [marker.rstrip(".") for marker in sound_markers if not any(marker in name for name in loaded)]
|
||||
@@ -1359,6 +1463,8 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
inputs: RoboLabPolicyInputs,
|
||||
pipeline_start: float,
|
||||
) -> DiffusionOutput:
|
||||
if self.is_distilled_model:
|
||||
raise self._distilled_unsupported_error("RoboLab/action policy requests are unsupported.")
|
||||
if not getattr(self.transformer, "action_gen", False):
|
||||
raise ValueError(
|
||||
"Cosmos3 RoboLab policy serving was requested, but the transformer "
|
||||
@@ -1469,6 +1575,7 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
guidance_interval=None,
|
||||
raw_action_dim=raw_action_dim,
|
||||
scheduler=scheduler,
|
||||
generator=generator,
|
||||
)
|
||||
|
||||
if _is_rank_zero():
|
||||
@@ -1560,38 +1667,34 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
raise ValueError(f"Incorrect modality value in {modalities}, expected one of {accepted_modalities}.")
|
||||
return "image" in modalities
|
||||
|
||||
def _set_timesteps(self, num_inference_steps: int, device: str | torch.device, shift: float) -> None:
|
||||
if self.is_distilled_model:
|
||||
self.scheduler.set_timesteps(sigmas=self._scheduler_init_t_list, device=device)
|
||||
else:
|
||||
self.scheduler.set_timesteps(
|
||||
num_inference_steps,
|
||||
device=device,
|
||||
shift=shift,
|
||||
)
|
||||
|
||||
def _set_flow_shift(self, target_shift: float) -> None:
|
||||
"""Select the shift applied by FlowUniPC when timesteps are built."""
|
||||
self._current_flow_shift = float(target_shift)
|
||||
|
||||
@staticmethod
|
||||
def _scheduler_use_karras_sigmas(config: Any) -> bool | None:
|
||||
value = getattr(config, "use_karras_sigmas", None)
|
||||
return None if value is None else bool(value)
|
||||
def _resolve_seed(sp: OmniDiffusionSamplingParams, generator: torch.Generator | None, default: int = 42) -> int:
|
||||
if sp.seed is not None:
|
||||
return int(sp.seed)
|
||||
if isinstance(generator, torch.Generator):
|
||||
return int(generator.initial_seed())
|
||||
return int(default)
|
||||
|
||||
def _set_flow_shift(self, target_shift: float, *, use_karras_sigmas: bool | None = None) -> None:
|
||||
"""Set UniPC scheduler mode for a concrete request.
|
||||
|
||||
The scheduler is rebuilt from the saved base config if
|
||||
the target differs from the current shift or Karras-sigma mode.
|
||||
Tracking explicit scheduler state is required because the previous
|
||||
mode may have rebuilt the scheduler - we cannot rely on
|
||||
``self.scheduler.config`` reflecting the last requested target if a
|
||||
rebuild was skipped via the equality check.
|
||||
"""
|
||||
target = float(target_shift)
|
||||
target_use_karras_sigmas = (
|
||||
self._base_scheduler_use_karras_sigmas if use_karras_sigmas is None else bool(use_karras_sigmas)
|
||||
)
|
||||
if (
|
||||
target == float(self._current_flow_shift)
|
||||
and target_use_karras_sigmas == self._current_scheduler_use_karras_sigmas
|
||||
):
|
||||
return
|
||||
|
||||
scheduler_kwargs: dict[str, Any] = {"flow_shift": target}
|
||||
if use_karras_sigmas is not None:
|
||||
scheduler_kwargs["use_karras_sigmas"] = bool(use_karras_sigmas)
|
||||
self.scheduler = UniPCMultistepScheduler.from_config(self._base_scheduler_config, **scheduler_kwargs)
|
||||
self._cpu_scheduler_state()
|
||||
self._current_flow_shift = target
|
||||
self._current_scheduler_use_karras_sigmas = self._scheduler_use_karras_sigmas(self.scheduler.config)
|
||||
def _resolve_guidance_scale(self, sp: OmniDiffusionSamplingParams, default: float) -> float:
|
||||
if self.is_distilled_model:
|
||||
return 1.0
|
||||
if sp.guidance_scale_provided:
|
||||
return float(sp.guidance_scale)
|
||||
return float(default)
|
||||
|
||||
def _cpu_scheduler_state(self) -> None:
|
||||
# We need to move scheduler tensors to CPU, as unipc from diffusers assumes they are on CPU.
|
||||
@@ -1612,6 +1715,53 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
def num_timesteps(self):
|
||||
return self._num_timesteps
|
||||
|
||||
@staticmethod
|
||||
def _distilled_unsupported_error(detail: str) -> ValueError:
|
||||
return ValueError(
|
||||
f"Cosmos3 distilled checkpoints support only text-to-image and image-to-video generation; {detail}"
|
||||
)
|
||||
|
||||
def _validate_distilled_generation_mode(
|
||||
self,
|
||||
*,
|
||||
is_t2i: bool,
|
||||
image_tensor: torch.Tensor | None,
|
||||
action_enabled: bool,
|
||||
transfer_config: Cosmos3TransferConfig | None,
|
||||
is_v2v: bool,
|
||||
sound_enabled: bool,
|
||||
) -> None:
|
||||
if not self.is_distilled_model:
|
||||
return
|
||||
if action_enabled:
|
||||
raise self._distilled_unsupported_error("action requests are unsupported.")
|
||||
if transfer_config is not None:
|
||||
raise self._distilled_unsupported_error("transfer requests are unsupported.")
|
||||
if is_v2v:
|
||||
raise self._distilled_unsupported_error("video-to-video requests are unsupported.")
|
||||
if sound_enabled:
|
||||
raise self._distilled_unsupported_error("sound generation is unsupported.")
|
||||
if not is_t2i and image_tensor is None:
|
||||
raise self._distilled_unsupported_error("text-to-video requests are unsupported.")
|
||||
if is_t2i and image_tensor is not None:
|
||||
raise self._distilled_unsupported_error("image-conditioned image generation is unsupported.")
|
||||
|
||||
def _validate_edge_generation_mode(
|
||||
self,
|
||||
*,
|
||||
transfer_config: Cosmos3TransferConfig | None,
|
||||
is_v2v: bool,
|
||||
sound_enabled: bool,
|
||||
) -> None:
|
||||
if not self.is_edge_model:
|
||||
return
|
||||
if sound_enabled:
|
||||
raise ValueError("Cosmos3 Edge checkpoints do not support sound generation.")
|
||||
if transfer_config is not None:
|
||||
raise ValueError("Cosmos3 Edge checkpoints do not support transfer inference.")
|
||||
if is_v2v:
|
||||
raise ValueError("Cosmos3 Edge checkpoints do not support video-to-video generation.")
|
||||
|
||||
# -- Prompt formatting -----------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
@@ -1628,6 +1778,10 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
"""
|
||||
Append duration and resolution metadata to a prompt.
|
||||
"""
|
||||
prompt = prompt.strip()
|
||||
if duration_template is None and resolution_template is None:
|
||||
return prompt
|
||||
|
||||
parts: list[str] = []
|
||||
head = prompt.rstrip(".").strip()
|
||||
if head:
|
||||
@@ -1903,15 +2057,26 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
res_tmpl = COSMOS3_IMAGE_RESOLUTION_TEMPLATE if is_t2i else COSMOS3_RESOLUTION_TEMPLATE
|
||||
else:
|
||||
res_tmpl = None
|
||||
prompt = self._apply_metadata_templates(
|
||||
json_prompt = _format_json_object_prompt(
|
||||
prompt,
|
||||
num_frames,
|
||||
frame_rate,
|
||||
height,
|
||||
width,
|
||||
duration_template=dur_tmpl,
|
||||
resolution_template=res_tmpl,
|
||||
num_frames=num_frames,
|
||||
frame_rate=frame_rate,
|
||||
height=height,
|
||||
width=width,
|
||||
aspect_ratio=self._get_sp_param(sp, "aspect_ratio", None),
|
||||
)
|
||||
if json_prompt is not None:
|
||||
prompt = json_prompt
|
||||
else:
|
||||
prompt = self._apply_metadata_templates(
|
||||
prompt,
|
||||
num_frames,
|
||||
frame_rate,
|
||||
height,
|
||||
width,
|
||||
duration_template=dur_tmpl,
|
||||
resolution_template=res_tmpl,
|
||||
)
|
||||
if _is_rank_zero():
|
||||
logger.info("Final prompt: '%s'", prompt)
|
||||
|
||||
@@ -2272,6 +2437,7 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
guidance_interval: tuple[float, float] | None = None,
|
||||
raw_action_dim: int | None = None,
|
||||
scheduler: Any | None = None,
|
||||
generator: torch.Generator | None = None,
|
||||
) -> torch.Tensor | tuple[torch.Tensor, ...]:
|
||||
"""Denoising loop with 3-mode CFG support (parallel, sequential, none).
|
||||
|
||||
@@ -2383,11 +2549,23 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
if raw_action_dim is not None and 0 < raw_action_dim < action_pred.shape[-1]:
|
||||
action_pred[..., raw_action_dim:] = 0
|
||||
if action_latents is None and sound_latents is None:
|
||||
latents = step_scheduler.step(video_pred, t, latents, return_dict=False)[0]
|
||||
latents = step_scheduler.step(
|
||||
video_pred,
|
||||
t,
|
||||
latents,
|
||||
generator=generator,
|
||||
return_dict=False,
|
||||
)[0]
|
||||
else:
|
||||
packed_noise, shapes, numels = _pack_joint(video_pred, action_pred, sound_pred)
|
||||
packed_latents, _, _ = _pack_joint(latents, action_latents, sound_latents)
|
||||
packed_next = step_scheduler.step(packed_noise, t, packed_latents, return_dict=False)[0]
|
||||
packed_next = step_scheduler.step(
|
||||
packed_noise,
|
||||
t,
|
||||
packed_latents,
|
||||
generator=generator,
|
||||
return_dict=False,
|
||||
)[0]
|
||||
unpacked = _unpack_joint(packed_next, shapes, numels)
|
||||
latents = unpacked[0]
|
||||
idx = 1
|
||||
@@ -2613,6 +2791,7 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
velocity_mask: torch.Tensor,
|
||||
condition_latents: torch.Tensor,
|
||||
guidance_interval: tuple[float, float] | None = None,
|
||||
generator: torch.Generator | None = None,
|
||||
) -> torch.Tensor:
|
||||
def _active_at(t: torch.Tensor, interval: tuple[float, float] | None) -> bool:
|
||||
if interval is None:
|
||||
@@ -2724,7 +2903,13 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
if isinstance(noise_pred, tuple):
|
||||
raise ValueError("Cosmos3 transfer diffusion expects video-only tensor predictions.")
|
||||
noise_pred = noise_pred * velocity_mask
|
||||
latents = self.scheduler.step(noise_pred, t, latents, return_dict=False)[0]
|
||||
latents = self.scheduler.step(
|
||||
noise_pred,
|
||||
t,
|
||||
latents,
|
||||
generator=generator,
|
||||
return_dict=False,
|
||||
)[0]
|
||||
latents = velocity_mask * latents + (1.0 - velocity_mask) * condition_latents
|
||||
finally:
|
||||
self._cosmos3_branch_caches = None
|
||||
@@ -2741,6 +2926,8 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
transfer_video_tensor: torch.Tensor | None,
|
||||
transfer_input_fps: float | None,
|
||||
) -> DiffusionOutput:
|
||||
if self.is_distilled_model:
|
||||
raise self._distilled_unsupported_error("transfer requests are unsupported.")
|
||||
input_frames = None
|
||||
if transfer_video_tensor is not None:
|
||||
input_frames = normalized_video_to_uint8_cthw(transfer_video_tensor)
|
||||
@@ -2806,7 +2993,7 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
guidance_scale = (
|
||||
float(transfer_config.guidance_scale)
|
||||
if transfer_config.guidance_scale is not None
|
||||
else float(sp.guidance_scale or COSMOS3_T2V_DEFAULT_GUIDANCE_SCALE)
|
||||
else self._resolve_guidance_scale(sp, COSMOS3_T2V_DEFAULT_GUIDANCE_SCALE)
|
||||
)
|
||||
flow_shift_target = float(
|
||||
transfer_config.flow_shift
|
||||
@@ -2821,11 +3008,11 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
|
||||
self._guidance_scale = guidance_scale
|
||||
self._num_timesteps = num_inference_steps
|
||||
self._set_flow_shift(flow_shift_target, use_karras_sigmas=False)
|
||||
self._set_flow_shift(flow_shift_target)
|
||||
|
||||
generator = sp.generator
|
||||
seed = self._resolve_seed(sp, generator)
|
||||
if generator is None:
|
||||
seed = sp.seed if sp.seed is not None else 42
|
||||
generator = torch.Generator(device=self.device).manual_seed(seed)
|
||||
|
||||
cond_ids, cond_mask, uncond_ids, uncond_mask = self._format_and_tokenize_prompts(
|
||||
@@ -2915,7 +3102,11 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
transfer_share_vision_temporal_positions=transfer_config.share_vision_temporal_positions,
|
||||
)
|
||||
|
||||
self.scheduler.set_timesteps(num_inference_steps, device=self.device)
|
||||
self._set_timesteps(
|
||||
num_inference_steps,
|
||||
device=self.device,
|
||||
shift=self._current_flow_shift,
|
||||
)
|
||||
latents = self.diffuse_transfer(
|
||||
latents=latents,
|
||||
timesteps=self.scheduler.timesteps,
|
||||
@@ -2930,6 +3121,7 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
shared_kwargs=shared_kwargs,
|
||||
velocity_mask=velocity_mask,
|
||||
condition_latents=condition_latents,
|
||||
generator=generator,
|
||||
)
|
||||
output_video = self._decode_latents(latents).clamp(-1, 1)
|
||||
previous_output = output_video
|
||||
@@ -3009,6 +3201,20 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
action_enabled = action_mode is not None
|
||||
transfer_config = resolve_transfer_config(sp, prompt_data)
|
||||
action_video_tensor = video_tensor if action_enabled else None
|
||||
is_v2v = video_tensor is not None and not is_t2i and not action_enabled
|
||||
self._validate_distilled_generation_mode(
|
||||
is_t2i=is_t2i,
|
||||
image_tensor=image_tensor,
|
||||
action_enabled=action_enabled,
|
||||
transfer_config=transfer_config,
|
||||
is_v2v=is_v2v,
|
||||
sound_enabled=sound_enabled,
|
||||
)
|
||||
self._validate_edge_generation_mode(
|
||||
transfer_config=transfer_config,
|
||||
is_v2v=is_v2v,
|
||||
sound_enabled=sound_enabled,
|
||||
)
|
||||
if transfer_config is not None:
|
||||
if is_t2i:
|
||||
raise ValueError("Cosmos3 transfer inference is supported only for video outputs.")
|
||||
@@ -3056,31 +3262,36 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
raise ValueError("Cosmos3 non-action generation accepts either image or video input, not both.")
|
||||
if video_tensor is not None and is_t2i:
|
||||
raise ValueError("Cosmos3 video-to-video generation is supported only for video outputs.")
|
||||
is_v2v = video_tensor is not None and not is_t2i and not action_enabled
|
||||
|
||||
# T2I and T2V share the same model + forward path; only defaults
|
||||
# differ:
|
||||
# T2I: 1024x1024, 50 steps, shift=3.0, guidance_interval=[400, 1000]
|
||||
# T2V: 720x1280, 35 steps, shift=engine-init, no interval
|
||||
# T2I and T2V share the same model and forward path; their defaults are:
|
||||
# T2I: regular 1024x1024, Edge 640x640; 50 steps, shift=3.0,
|
||||
# guidance_interval=[400, 1000]
|
||||
# T2V: 189 frames and 35 steps; regular guidance=6, shift=10;
|
||||
# Edge guidance=5, shift=3;
|
||||
# no guidance interval
|
||||
if is_t2i:
|
||||
height = sp.height or COSMOS3_T2I_DEFAULT_HEIGHT
|
||||
width = sp.width or COSMOS3_T2I_DEFAULT_WIDTH
|
||||
height = sp.height or (
|
||||
COSMOS3_EDGE_T2I_DEFAULT_HEIGHT if self.is_edge_model else COSMOS3_T2I_DEFAULT_HEIGHT
|
||||
)
|
||||
width = sp.width or (COSMOS3_EDGE_T2I_DEFAULT_WIDTH if self.is_edge_model else COSMOS3_T2I_DEFAULT_WIDTH)
|
||||
num_frames = 1
|
||||
num_inference_steps = sp.num_inference_steps or COSMOS3_T2I_DEFAULT_NUM_INFERENCE_STEPS
|
||||
guidance_scale = sp.guidance_scale if sp.guidance_scale else COSMOS3_T2I_DEFAULT_GUIDANCE_SCALE
|
||||
guidance_scale = self._resolve_guidance_scale(sp, COSMOS3_T2I_DEFAULT_GUIDANCE_SCALE)
|
||||
default_flow_shift = COSMOS3_T2I_DEFAULT_FLOW_SHIFT
|
||||
default_guidance_interval: tuple[float, float] | None = COSMOS3_T2I_DEFAULT_GUIDANCE_INTERVAL
|
||||
batch_size = max(1, int(sp.num_outputs_per_prompt or 1))
|
||||
else:
|
||||
height = sp.height or COSMOS3_T2V_DEFAULT_HEIGHT
|
||||
width = sp.width or COSMOS3_T2V_DEFAULT_WIDTH
|
||||
height = sp.height or (
|
||||
COSMOS3_EDGE_T2V_DEFAULT_HEIGHT if self.is_edge_model else COSMOS3_T2V_DEFAULT_HEIGHT
|
||||
)
|
||||
width = sp.width or (COSMOS3_EDGE_T2V_DEFAULT_WIDTH if self.is_edge_model else COSMOS3_T2V_DEFAULT_WIDTH)
|
||||
default_guidance_scale = (
|
||||
COSMOS3_EDGE_T2V_DEFAULT_GUIDANCE_SCALE if self.is_edge_model else COSMOS3_T2V_DEFAULT_GUIDANCE_SCALE
|
||||
)
|
||||
num_frames = sp.num_frames or COSMOS3_T2V_DEFAULT_NUM_FRAMES
|
||||
num_inference_steps = sp.num_inference_steps or COSMOS3_T2V_DEFAULT_NUM_INFERENCE_STEPS
|
||||
guidance_scale = sp.guidance_scale if sp.guidance_scale else COSMOS3_T2V_DEFAULT_GUIDANCE_SCALE
|
||||
# Fall back to the engine-init shift, NOT None: passing None
|
||||
# to ``_set_flow_shift`` would leak a prior T2I rebuild
|
||||
# (shift=3.0) into a subsequent video request.
|
||||
default_flow_shift = COSMOS3_V2V_DEFAULT_FLOW_SHIFT if is_v2v else self._engine_init_flow_shift
|
||||
guidance_scale = self._resolve_guidance_scale(sp, default_guidance_scale)
|
||||
default_flow_shift = self._engine_init_flow_shift
|
||||
default_guidance_interval = None
|
||||
batch_size = 1 # Existing video pipeline assumes B=1.
|
||||
|
||||
@@ -3103,9 +3314,23 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
f"or action_chunk_size + 1; got num_frames={num_frames}, action_chunk_size={action_chunk_size}."
|
||||
)
|
||||
num_inference_steps = sp.num_inference_steps or 30
|
||||
guidance_scale = sp.guidance_scale if sp.guidance_scale is not None else 1.0
|
||||
guidance_scale = self._resolve_guidance_scale(sp, 1.0)
|
||||
default_flow_shift = 5.0
|
||||
|
||||
if not is_t2i and not action_enabled:
|
||||
requested_num_frames = int(num_frames)
|
||||
num_frames = _ceil_video_num_frames(
|
||||
requested_num_frames,
|
||||
self.vae_scale_factor_temporal,
|
||||
)
|
||||
if num_frames != requested_num_frames and _is_rank_zero():
|
||||
logger.info(
|
||||
"Rounded Cosmos3 num_frames from %d to %d for temporal compression factor %d.",
|
||||
requested_num_frames,
|
||||
num_frames,
|
||||
self.vae_scale_factor_temporal,
|
||||
)
|
||||
|
||||
domain_id = None
|
||||
if action_enabled:
|
||||
domain_id = resolve_domain_id(
|
||||
@@ -3146,13 +3371,12 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
self._guidance_scale = guidance_scale
|
||||
self._num_timesteps = num_inference_steps
|
||||
|
||||
# Always resolve to a concrete target shift for this request, then
|
||||
# update the shared Diffusers scheduler.
|
||||
self._set_flow_shift(flow_shift_target, use_karras_sigmas=False if is_v2v else None)
|
||||
# Always resolve to a concrete target shift for this request.
|
||||
self._set_flow_shift(flow_shift_target)
|
||||
|
||||
generator = sp.generator
|
||||
seed = self._resolve_seed(sp, generator)
|
||||
if generator is None:
|
||||
seed = sp.seed if sp.seed is not None else 42
|
||||
generator = torch.Generator(device=self.device).manual_seed(seed)
|
||||
|
||||
# --- Format prompts & tokenize (B=1; reused across loop iterations
|
||||
@@ -3293,7 +3517,11 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
)
|
||||
|
||||
def _run_diffusion(start_latents):
|
||||
self.scheduler.set_timesteps(num_inference_steps, device=self.device)
|
||||
self._set_timesteps(
|
||||
num_inference_steps,
|
||||
device=self.device,
|
||||
shift=self._current_flow_shift,
|
||||
)
|
||||
scheduler = self.scheduler
|
||||
return self.diffuse(
|
||||
latents=start_latents,
|
||||
@@ -3314,6 +3542,7 @@ class Cosmos3OmniDiffusersPipeline(
|
||||
guidance_interval=guidance_interval,
|
||||
raw_action_dim=raw_action_dim,
|
||||
scheduler=scheduler,
|
||||
generator=generator,
|
||||
)
|
||||
|
||||
if is_t2i and batch_size > 1:
|
||||
|
||||
@@ -396,7 +396,6 @@ class TimestepEmbedder(nn.Module):
|
||||
hidden_size: int,
|
||||
frequency_embedding_size: int = 256,
|
||||
max_period: int = 10000,
|
||||
target_dtype: torch.dtype = torch.bfloat16,
|
||||
) -> None:
|
||||
super().__init__()
|
||||
# Following diffusers naming pattern here for checkpoint compatibility.
|
||||
@@ -407,7 +406,7 @@ class TimestepEmbedder(nn.Module):
|
||||
self.hidden_size = hidden_size
|
||||
|
||||
half = frequency_embedding_size // 2
|
||||
freqs = torch.exp(-math.log(max_period) * torch.arange(start=0, end=half, dtype=target_dtype) / half)
|
||||
freqs = torch.exp(-math.log(max_period) * torch.arange(start=0, end=half, dtype=torch.float32) / half)
|
||||
self.register_buffer("freqs", freqs, persistent=False)
|
||||
|
||||
def forward(self, t: torch.Tensor) -> torch.Tensor:
|
||||
@@ -587,6 +586,7 @@ class Cosmos3CrossAttention(nn.Module):
|
||||
head_dim: int,
|
||||
rms_norm_eps: float,
|
||||
quant_config: QuantizationConfig | None = None,
|
||||
qk_norm: bool = True,
|
||||
prefix: str = "",
|
||||
) -> None:
|
||||
super().__init__()
|
||||
@@ -636,8 +636,10 @@ class Cosmos3CrossAttention(nn.Module):
|
||||
prefix=f"{prefix}.to_out",
|
||||
)
|
||||
|
||||
self.norm_q = RMSNorm(self.head_dim, eps=rms_norm_eps)
|
||||
self.norm_k = RMSNorm(self.head_dim, eps=rms_norm_eps)
|
||||
self.qk_norm = qk_norm
|
||||
if self.qk_norm:
|
||||
self.norm_q = RMSNorm(self.head_dim, eps=rms_norm_eps)
|
||||
self.norm_k = RMSNorm(self.head_dim, eps=rms_norm_eps)
|
||||
|
||||
self.attn = FrameworkAttention(
|
||||
num_heads=self.num_heads,
|
||||
@@ -721,8 +723,9 @@ class Cosmos3CrossAttention(nn.Module):
|
||||
v = self.to_v(hidden_states).view(B, S_gen, self.num_kv_heads_local, self.head_dim)
|
||||
|
||||
# Per-head QK norm
|
||||
q = F.rms_norm(q, (self.head_dim,), self.norm_q.weight, eps=self.norm_q.variance_epsilon)
|
||||
k = F.rms_norm(k, (self.head_dim,), self.norm_k.weight, eps=self.norm_k.variance_epsilon)
|
||||
if self.qk_norm:
|
||||
q = F.rms_norm(q, (self.head_dim,), self.norm_q.weight, eps=self.norm_q.variance_epsilon)
|
||||
k = F.rms_norm(k, (self.head_dim,), self.norm_k.weight, eps=self.norm_k.variance_epsilon)
|
||||
|
||||
# Qwen3-style RoPE
|
||||
q, k = _apply_rotary_pos_emb(q, k, freqs_cos, freqs_sin)
|
||||
@@ -806,6 +809,8 @@ class Cosmos3GenDecoderLayer(nn.Module):
|
||||
head_dim: int,
|
||||
rms_norm_eps: float,
|
||||
quant_config: QuantizationConfig | None = None,
|
||||
mlp_cls: type[nn.Module] = Cosmos3GatedMLP,
|
||||
qk_norm: bool = True,
|
||||
prefix: str = "",
|
||||
) -> None:
|
||||
super().__init__()
|
||||
@@ -817,11 +822,12 @@ class Cosmos3GenDecoderLayer(nn.Module):
|
||||
head_dim=head_dim,
|
||||
rms_norm_eps=rms_norm_eps,
|
||||
quant_config=quant_config,
|
||||
qk_norm=qk_norm,
|
||||
prefix=f"{prefix}.cross_attention",
|
||||
)
|
||||
self.input_layernorm = RMSNorm(hidden_size, eps=rms_norm_eps)
|
||||
self.post_attention_layernorm = RMSNorm(hidden_size, eps=rms_norm_eps)
|
||||
self.mlp = Cosmos3GatedMLP(
|
||||
self.mlp = mlp_cls(
|
||||
hidden_size=hidden_size,
|
||||
intermediate_size=intermediate_size,
|
||||
quant_config=quant_config,
|
||||
@@ -996,6 +1002,9 @@ class Cosmos3VFMTransformer(nn.Module):
|
||||
|
||||
packed_modules_mapping = {}
|
||||
|
||||
_language_model_cls = Cosmos3LanguageModel
|
||||
_gen_mlp_cls = Cosmos3GatedMLP
|
||||
|
||||
@staticmethod
|
||||
def _is_transformer_block(name: str, module) -> bool:
|
||||
return ("gen_layers" in name or "language_model.layers" in name) and name.split(".")[-1].isdigit()
|
||||
@@ -1032,6 +1041,25 @@ class Cosmos3VFMTransformer(nn.Module):
|
||||
if actual != expected:
|
||||
raise ValueError(f"Unsupported Cosmos3 transformer config: {key}={actual!r}; expected {expected!r}.")
|
||||
|
||||
@classmethod
|
||||
def _resolve_rms_norm_eps(cls, model_config: Any) -> float:
|
||||
return float(_tf_config_get(model_config, "rms_norm_eps", 1e-6))
|
||||
|
||||
@classmethod
|
||||
def _resolve_rope_theta(cls, model_config: Any) -> float:
|
||||
return float(_tf_config_get(model_config, "rope_theta", 5_000_000))
|
||||
|
||||
@classmethod
|
||||
def _resolve_mrope_section(cls, model_config: Any) -> list[int]:
|
||||
rope_scaling = _tf_config_get(model_config, "rope_scaling", {}) or {}
|
||||
return list(rope_scaling.get("mrope_section", [24, 20, 20]))
|
||||
|
||||
def _language_model_kwargs(self) -> dict[str, Any]:
|
||||
return {}
|
||||
|
||||
def validate_loaded_weights(self, loaded: set[str]) -> None:
|
||||
del loaded
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
od_config: OmniDiffusionConfig,
|
||||
@@ -1044,7 +1072,6 @@ class Cosmos3VFMTransformer(nn.Module):
|
||||
super().__init__()
|
||||
model_config = od_config.tf_model_config
|
||||
self._validate_supported_config(model_config)
|
||||
rope_scaling = _tf_config_get(model_config, "rope_scaling", {}) or {}
|
||||
|
||||
self.hidden_size = int(_tf_config_get(model_config, "hidden_size", 4096))
|
||||
self.num_hidden_layers = int(_tf_config_get(model_config, "num_hidden_layers", 36))
|
||||
@@ -1053,9 +1080,10 @@ class Cosmos3VFMTransformer(nn.Module):
|
||||
self.head_dim = int(_tf_config_get(model_config, "head_dim", 128))
|
||||
self.intermediate_size = int(_tf_config_get(model_config, "intermediate_size", 12288))
|
||||
self.vocab_size = int(_tf_config_get(model_config, "vocab_size", 151936))
|
||||
self.rms_norm_eps = float(_tf_config_get(model_config, "rms_norm_eps", 1e-6))
|
||||
self.rope_theta = float(_tf_config_get(model_config, "rope_theta", 5_000_000))
|
||||
self.mrope_section = list(rope_scaling.get("mrope_section", [24, 20, 20]))
|
||||
self.rms_norm_eps = self._resolve_rms_norm_eps(model_config)
|
||||
self.rope_theta = self._resolve_rope_theta(model_config)
|
||||
self.mrope_section = self._resolve_mrope_section(model_config)
|
||||
self.qk_norm_for_diffusion = bool(_tf_config_get(model_config, "qk_norm_for_diffusion", True))
|
||||
self.latent_patch_size = int(_tf_config_get(model_config, "latent_patch_size", 2))
|
||||
self.latent_channel_size = int(_tf_config_get(model_config, "latent_channel", 48))
|
||||
self.timestep_scale = float(_tf_config_get(model_config, "timestep_scale", 0.001))
|
||||
@@ -1095,10 +1123,12 @@ class Cosmos3VFMTransformer(nn.Module):
|
||||
)
|
||||
self.patch_latent_dim = (self.latent_patch_size**2) * self.latent_channel_size
|
||||
|
||||
self.use_k_norm_und_for_gen = _tf_config_get(model_config, "use_k_norm_und_for_gen", None)
|
||||
|
||||
dtype = od_config.dtype
|
||||
quant_config = getattr(od_config, "quantization_config", None) if od_config else None
|
||||
|
||||
self.language_model = Cosmos3LanguageModel(
|
||||
self.language_model = self._language_model_cls(
|
||||
hidden_size=self.hidden_size,
|
||||
intermediate_size=self.intermediate_size,
|
||||
num_hidden_layers=self.num_hidden_layers,
|
||||
@@ -1111,12 +1141,13 @@ class Cosmos3VFMTransformer(nn.Module):
|
||||
mrope_section=self.mrope_section,
|
||||
quant_config=quant_config,
|
||||
prefix="language_model",
|
||||
**self._language_model_kwargs(),
|
||||
)
|
||||
|
||||
# Video projection layers are small; not worth quantizing.
|
||||
self.proj_in = nn.Linear(self.patch_latent_dim, self.hidden_size)
|
||||
self.proj_out = nn.Linear(self.hidden_size, self.patch_latent_dim)
|
||||
self.time_embedder = TimestepEmbedder(self.hidden_size, target_dtype=dtype)
|
||||
self.time_embedder = TimestepEmbedder(self.hidden_size)
|
||||
if self.action_gen:
|
||||
self.action_proj_in = DomainAwareLinear(
|
||||
self.action_dim,
|
||||
@@ -1147,6 +1178,8 @@ class Cosmos3VFMTransformer(nn.Module):
|
||||
head_dim=self.head_dim,
|
||||
rms_norm_eps=self.rms_norm_eps,
|
||||
quant_config=quant_config,
|
||||
mlp_cls=self._gen_mlp_cls,
|
||||
qk_norm=self.qk_norm_for_diffusion,
|
||||
prefix=f"gen_layers.{i}",
|
||||
)
|
||||
for i in range(self.num_hidden_layers)
|
||||
|
||||
@@ -0,0 +1,396 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Cosmos3 Edge transformer variant with a Nemotron dense UND backbone."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
from vllm.distributed import get_tensor_model_parallel_world_size
|
||||
from vllm.model_executor.layers.linear import ColumnParallelLinear, RowParallelLinear
|
||||
from vllm.model_executor.layers.quantization.base_config import QuantizationConfig
|
||||
|
||||
from vllm_omni.diffusion.attention.layer import Attention as FrameworkAttention
|
||||
|
||||
from .transformer_cosmos3 import (
|
||||
Cosmos3VFMTransformer,
|
||||
Qwen3VLTextRotaryEmbedding,
|
||||
RMSNorm,
|
||||
_apply_rotary_pos_emb,
|
||||
_nested_get,
|
||||
_tf_config_get,
|
||||
)
|
||||
|
||||
COSMOS3_EDGE_BACKBONE_TYPE = "cosmos3_edge_nemotron_dense"
|
||||
|
||||
|
||||
def _tf_config_nested_get(config: Any, key: str, default: Any = None) -> Any:
|
||||
if config is None:
|
||||
return default
|
||||
if isinstance(config, dict):
|
||||
value = _nested_get(config, key)
|
||||
elif hasattr(config, "to_dict"):
|
||||
value = _nested_get(config.to_dict(), key)
|
||||
elif hasattr(config, "params"):
|
||||
value = _nested_get(config.params, key)
|
||||
else:
|
||||
value = getattr(config, key, None)
|
||||
return default if value is None else value
|
||||
|
||||
|
||||
class Cosmos3Relu2MLP(nn.Module):
|
||||
"""Nemotron dense MLP: down_proj(relu(up_proj(x)) ** 2)."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hidden_size: int,
|
||||
intermediate_size: int,
|
||||
quant_config: QuantizationConfig | None = None,
|
||||
prefix: str = "",
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self.up_proj = ColumnParallelLinear(
|
||||
hidden_size,
|
||||
intermediate_size,
|
||||
bias=False,
|
||||
gather_output=False,
|
||||
return_bias=False,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.up_proj",
|
||||
)
|
||||
self.down_proj = RowParallelLinear(
|
||||
intermediate_size,
|
||||
hidden_size,
|
||||
bias=False,
|
||||
input_is_parallel=True,
|
||||
return_bias=False,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.down_proj",
|
||||
)
|
||||
|
||||
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
||||
hidden = F.relu(self.up_proj(x))
|
||||
return self.down_proj(hidden * hidden)
|
||||
|
||||
|
||||
class Cosmos3EdgeCausalAttention(nn.Module):
|
||||
"""Edge UND causal attention.
|
||||
|
||||
Reasoner self-attention uses raw UND Q/K with RoPE because
|
||||
``qk_norm_for_text=false``. The GEN-facing UND key cache is a separate
|
||||
normalized-then-RoPE'd view of the raw K tensor when GEN QK norm is enabled;
|
||||
no-GEN-QK-norm checkpoints reuse the raw RoPE'd UND K.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
hidden_size: int,
|
||||
num_attention_heads: int,
|
||||
num_key_value_heads: int,
|
||||
head_dim: int,
|
||||
rms_norm_eps: float,
|
||||
use_k_norm_und_for_gen: bool,
|
||||
quant_config: QuantizationConfig | None = None,
|
||||
prefix: str = "",
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self.hidden_size = hidden_size
|
||||
self.num_heads = num_attention_heads
|
||||
self.num_kv_heads = num_key_value_heads
|
||||
self.head_dim = head_dim
|
||||
|
||||
tp_size = get_tensor_model_parallel_world_size()
|
||||
self.num_heads_local = self.num_heads // tp_size
|
||||
self.num_kv_heads_local = self.num_kv_heads // tp_size
|
||||
|
||||
self.to_q = ColumnParallelLinear(
|
||||
hidden_size,
|
||||
self.num_heads * self.head_dim,
|
||||
bias=False,
|
||||
gather_output=False,
|
||||
return_bias=False,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.to_q",
|
||||
)
|
||||
self.to_k = ColumnParallelLinear(
|
||||
hidden_size,
|
||||
self.num_kv_heads * self.head_dim,
|
||||
bias=False,
|
||||
gather_output=False,
|
||||
return_bias=False,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.to_k",
|
||||
)
|
||||
self.to_v = ColumnParallelLinear(
|
||||
hidden_size,
|
||||
self.num_kv_heads * self.head_dim,
|
||||
bias=False,
|
||||
gather_output=False,
|
||||
return_bias=False,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.to_v",
|
||||
)
|
||||
self.to_out = RowParallelLinear(
|
||||
self.num_heads * self.head_dim,
|
||||
hidden_size,
|
||||
bias=False,
|
||||
input_is_parallel=True,
|
||||
return_bias=False,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.to_out",
|
||||
)
|
||||
|
||||
self.k_norm_und_for_gen = RMSNorm(self.head_dim, eps=rms_norm_eps) if use_k_norm_und_for_gen else None
|
||||
self.attn = FrameworkAttention(
|
||||
num_heads=self.num_heads,
|
||||
head_size=self.head_dim,
|
||||
causal=True,
|
||||
softmax_scale=1.0 / (self.head_dim**0.5),
|
||||
num_kv_heads=self.num_kv_heads,
|
||||
skip_sequence_parallel=True,
|
||||
)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
freqs_cos: torch.Tensor,
|
||||
freqs_sin: torch.Tensor,
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
||||
B, S, _ = hidden_states.shape
|
||||
|
||||
q = self.to_q(hidden_states).view(B, S, self.num_heads_local, self.head_dim)
|
||||
k = self.to_k(hidden_states).view(B, S, self.num_kv_heads_local, self.head_dim)
|
||||
v = self.to_v(hidden_states).view(B, S, self.num_kv_heads_local, self.head_dim)
|
||||
|
||||
q_reasoner, k_reasoner = _apply_rotary_pos_emb(q, k, freqs_cos, freqs_sin)
|
||||
|
||||
if self.k_norm_und_for_gen is not None:
|
||||
k_gen = F.rms_norm(
|
||||
k,
|
||||
(self.head_dim,),
|
||||
self.k_norm_und_for_gen.weight,
|
||||
eps=self.k_norm_und_for_gen.variance_epsilon,
|
||||
)
|
||||
_, k_gen = _apply_rotary_pos_emb(q, k_gen, freqs_cos, freqs_sin)
|
||||
else:
|
||||
k_gen = k_reasoner
|
||||
|
||||
out = self.attn(q_reasoner, k_reasoner, v).reshape(B, S, -1)
|
||||
return self.to_out(out), k_gen, v
|
||||
|
||||
|
||||
class Cosmos3EdgeUndDecoderLayer(nn.Module):
|
||||
"""Edge UND decoder layer: causal self-attention + ReLU2 MLP."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
hidden_size: int,
|
||||
intermediate_size: int,
|
||||
num_attention_heads: int,
|
||||
num_key_value_heads: int,
|
||||
head_dim: int,
|
||||
rms_norm_eps: float,
|
||||
use_k_norm_und_for_gen: bool,
|
||||
quant_config: QuantizationConfig | None = None,
|
||||
prefix: str = "",
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self.self_attn = Cosmos3EdgeCausalAttention(
|
||||
hidden_size=hidden_size,
|
||||
num_attention_heads=num_attention_heads,
|
||||
num_key_value_heads=num_key_value_heads,
|
||||
head_dim=head_dim,
|
||||
rms_norm_eps=rms_norm_eps,
|
||||
use_k_norm_und_for_gen=use_k_norm_und_for_gen,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.self_attn",
|
||||
)
|
||||
self.input_layernorm = RMSNorm(hidden_size, eps=rms_norm_eps)
|
||||
self.post_attention_layernorm = RMSNorm(hidden_size, eps=rms_norm_eps)
|
||||
self.mlp = Cosmos3Relu2MLP(
|
||||
hidden_size=hidden_size,
|
||||
intermediate_size=intermediate_size,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.mlp",
|
||||
)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
hidden_states: torch.Tensor,
|
||||
freqs: tuple[torch.Tensor, torch.Tensor],
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
||||
residual = hidden_states
|
||||
hidden_states = self.input_layernorm(hidden_states)
|
||||
|
||||
cos, sin = freqs
|
||||
attn_out, k, v = self.self_attn(hidden_states, cos, sin)
|
||||
hidden_states = residual + attn_out
|
||||
|
||||
residual = hidden_states
|
||||
hidden_states = self.post_attention_layernorm(hidden_states)
|
||||
hidden_states = residual + self.mlp(hidden_states)
|
||||
|
||||
return hidden_states, k, v
|
||||
|
||||
|
||||
class Cosmos3EdgeLanguageModel(nn.Module):
|
||||
"""Nemotron dense UND tower that returns GEN-facing K plus raw V per layer."""
|
||||
|
||||
_layerwise_offload_blocks_attrs = ["layers"]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
hidden_size: int,
|
||||
intermediate_size: int,
|
||||
num_hidden_layers: int,
|
||||
num_attention_heads: int,
|
||||
num_key_value_heads: int,
|
||||
head_dim: int,
|
||||
vocab_size: int,
|
||||
rms_norm_eps: float,
|
||||
rope_theta: float,
|
||||
mrope_section: list[int],
|
||||
use_k_norm_und_for_gen: bool,
|
||||
quant_config: QuantizationConfig | None = None,
|
||||
prefix: str = "",
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self.embed_tokens = nn.Embedding(vocab_size, hidden_size)
|
||||
self.rotary_emb = Qwen3VLTextRotaryEmbedding(
|
||||
head_dim=head_dim,
|
||||
rope_theta=rope_theta,
|
||||
mrope_section=mrope_section,
|
||||
)
|
||||
self.layers = nn.ModuleList(
|
||||
[
|
||||
Cosmos3EdgeUndDecoderLayer(
|
||||
hidden_size=hidden_size,
|
||||
intermediate_size=intermediate_size,
|
||||
num_attention_heads=num_attention_heads,
|
||||
num_key_value_heads=num_key_value_heads,
|
||||
head_dim=head_dim,
|
||||
rms_norm_eps=rms_norm_eps,
|
||||
use_k_norm_und_for_gen=use_k_norm_und_for_gen,
|
||||
quant_config=quant_config,
|
||||
prefix=f"{prefix}.layers.{i}",
|
||||
)
|
||||
for i in range(num_hidden_layers)
|
||||
]
|
||||
)
|
||||
self.norm = RMSNorm(hidden_size, eps=rms_norm_eps)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
text_ids: torch.Tensor,
|
||||
freqs: tuple[torch.Tensor, torch.Tensor],
|
||||
) -> list[tuple[torch.Tensor, torch.Tensor]]:
|
||||
hidden = self.embed_tokens(text_ids)
|
||||
|
||||
cached_kv: list[tuple[torch.Tensor, torch.Tensor]] = []
|
||||
for layer in self.layers:
|
||||
hidden, k, v = layer(hidden, freqs)
|
||||
cached_kv.append((k, v))
|
||||
|
||||
return cached_kv
|
||||
|
||||
|
||||
class Cosmos3EdgeVFMTransformer(Cosmos3VFMTransformer):
|
||||
"""Cosmos3 Edge variant with Nemotron dense UND and shared GEN diffusion."""
|
||||
|
||||
_language_model_cls = Cosmos3EdgeLanguageModel
|
||||
_gen_mlp_cls = Cosmos3Relu2MLP
|
||||
|
||||
@staticmethod
|
||||
def _validate_supported_config(model_config: Any) -> None:
|
||||
expected_values = {
|
||||
"qk_norm_for_text": False,
|
||||
"position_embedding_type": "unified_3d_mrope",
|
||||
"unified_3d_mrope_reset_spatial_ids": True,
|
||||
"joint_attn_implementation": "two_way",
|
||||
}
|
||||
for key, expected in expected_values.items():
|
||||
actual = _tf_config_get(model_config, key, expected)
|
||||
if actual != expected:
|
||||
raise ValueError(
|
||||
f"Unsupported Cosmos3 Edge transformer config: {key}={actual!r}; expected {expected!r}."
|
||||
)
|
||||
|
||||
backbone_type = _tf_config_get(model_config, "backbone_type", None)
|
||||
if backbone_type is None:
|
||||
raise ValueError(
|
||||
f"Cosmos3 Edge transformer config must declare backbone_type={COSMOS3_EDGE_BACKBONE_TYPE!r}."
|
||||
)
|
||||
if backbone_type != COSMOS3_EDGE_BACKBONE_TYPE:
|
||||
raise ValueError(
|
||||
"Unsupported Cosmos3 Edge transformer config: "
|
||||
f"backbone_type={backbone_type!r}; expected {COSMOS3_EDGE_BACKBONE_TYPE!r}."
|
||||
)
|
||||
|
||||
required_values = {
|
||||
"latent_channel": 48,
|
||||
"latent_patch_size": 2,
|
||||
"temporal_compression_factor": 4,
|
||||
}
|
||||
for key, expected in required_values.items():
|
||||
actual = _tf_config_get(model_config, key, None)
|
||||
if actual is None:
|
||||
raise ValueError(f"Cosmos3 Edge transformer config must declare {key}={expected}.")
|
||||
if int(actual) != expected:
|
||||
raise ValueError(
|
||||
f"Unsupported Cosmos3 Edge transformer config: {key}={actual!r}; expected {expected!r}."
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _resolve_rms_norm_eps(cls, model_config: Any) -> float:
|
||||
return float(
|
||||
_tf_config_get(
|
||||
model_config,
|
||||
"rms_norm_eps",
|
||||
_tf_config_get(model_config, "layer_norm_epsilon", 1e-5),
|
||||
)
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def _resolve_rope_theta(cls, model_config: Any) -> float:
|
||||
return float(_tf_config_get(model_config, "rope_theta", 100_000_000))
|
||||
|
||||
@classmethod
|
||||
def _resolve_mrope_section(cls, model_config: Any) -> list[int]:
|
||||
mrope_section = _tf_config_get(model_config, "mrope_section", None)
|
||||
if mrope_section is None:
|
||||
mrope_section = _tf_config_nested_get(model_config, "mrope_section", [24, 20, 20])
|
||||
return list(mrope_section)
|
||||
|
||||
def _language_model_kwargs(self) -> dict[str, Any]:
|
||||
return {"use_k_norm_und_for_gen": self.use_k_norm_und_for_gen}
|
||||
|
||||
def validate_loaded_weights(self, loaded: set[str]) -> None:
|
||||
missing: list[str] = []
|
||||
for layer_idx in range(self.num_hidden_layers):
|
||||
required_markers = (
|
||||
f"language_model.layers.{layer_idx}.mlp.up_proj.",
|
||||
f"language_model.layers.{layer_idx}.mlp.down_proj.",
|
||||
f"gen_layers.{layer_idx}.mlp.up_proj.",
|
||||
f"gen_layers.{layer_idx}.mlp.down_proj.",
|
||||
)
|
||||
if self.use_k_norm_und_for_gen:
|
||||
required_markers = (
|
||||
*required_markers,
|
||||
f"language_model.layers.{layer_idx}.self_attn.k_norm_und_for_gen.",
|
||||
)
|
||||
missing.extend(
|
||||
marker.rstrip(".") for marker in required_markers if not any(marker in name for name in loaded)
|
||||
)
|
||||
if missing:
|
||||
preview = ", ".join(missing[:10])
|
||||
suffix = " ..." if len(missing) > 10 else ""
|
||||
raise ValueError(
|
||||
"Cosmos3 Edge transformer checkpoint is missing required weights for "
|
||||
f"{preview}{suffix}. Use a converted Edge checkpoint with ReLU2 UND/GEN MLP weights."
|
||||
)
|
||||
Reference in new issue
Block a user