File size: 9,638 Bytes
c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 a25598f c5a2786 | 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 | # Copyright 2025 starVLA community. All rights reserved.
# Licensed under the MIT License, Version 1.0 (the "License");
"""
CosmoPredict2-Perceiver Framework.
Uses Cosmos-Predict2 as the world-model backbone and the Perceiver-style
flow-matching action head for continuous action prediction.
"""
import sys
from pathlib import Path
_workspace_root = Path(__file__).parent.parent.parent.parent.parent
if str(_workspace_root) not in sys.path:
sys.path.insert(0, str(_workspace_root))
from dataclasses import dataclass, field
from typing import List, Optional, Tuple
import numpy as np
import torch
from deployment.model_server.tools.image_tools import to_pil_preserve
from starVLA.model.framework.base_framework import baseframework
from starVLA.model.framework.share_tools import merge_framework_config
from starVLA.model.modules.action_model.PerceiverHead import FlowmatchingActionHead, get_action_model
from starVLA.model.modules.world_model import get_world_model
from starVLA.model.tools import FRAMEWORK_REGISTRY
from starVLA.training.trainer_utils import initialize_overwatch
from starVLA.training.trainer_utils.trainer_tools import resize_images
logger = initialize_overwatch(__name__)
@dataclass
class CosmoPredict2PerceiverDefaultConfig:
"""CosmoPredict2-Perceiver default parameters."""
name: str = "CosmoPredict2Perceiver"
world_model: dict = field(default_factory=lambda: {
"base_wm": "./playground/Pretrained_models/nvidia/Cosmos-Predict2-2B-Video2World",
"extract_layers": [-1],
})
# Kept for compatibility with shared utilities and action-head configs.
qwenvl: dict = field(default_factory=lambda: {
"base_vlm": "./playground/Pretrained_models/nvidia/Cosmos-Predict2-2B-Video2World",
"vl_hidden_dim": 2048,
})
action_model: dict = field(default_factory=lambda: {
"action_model_type": "DiT-B",
"action_hidden_dim": 1024,
"hidden_size": 1024,
"add_pos_embed": True,
"max_seq_len": 1024,
"action_dim": 7,
"state_dim": 7,
"future_action_window_size": 7,
"action_horizon": 8,
"past_action_window_size": 0,
"repeated_diffusion_steps": 8,
"noise_beta_alpha": 1.5,
"noise_beta_beta": 1.0,
"noise_s": 0.999,
"num_timestep_buckets": 1000,
"num_inference_timesteps": 4,
"num_target_vision_tokens": 32,
"max_num_embodiments": 32,
"use_category_specific": False,
"diffusion_model_cfg": {
"cross_attention_dim": 2048,
"dropout": 0.2,
"final_dropout": True,
"interleave_self_attention": True,
"norm_type": "ada_norm",
"num_layers": 16,
"output_dim": 1024,
"positional_embeddings": None,
},
})
obs_image_size: Optional[list] = None
@FRAMEWORK_REGISTRY.register("CosmoPredict2Perceiver")
class CosmoPredict2_Perceiver(baseframework):
"""World-model-for-action framework using Cosmos-Predict2 + Perceiver head."""
def __init__(self, config: Optional[dict] = None, **kwargs) -> None:
super().__init__()
self.config = merge_framework_config(CosmoPredict2PerceiverDefaultConfig, config)
self.backbone = get_world_model(config=self.config)
wm_hidden = self.backbone.model.config.hidden_size
self.config.framework.qwenvl.vl_hidden_dim = wm_hidden
self.config.framework.action_model.diffusion_model_cfg.cross_attention_dim = wm_hidden
self.action_model: FlowmatchingActionHead = get_action_model(config=self.config)
self.future_action_window_size = self.config.framework.action_model.future_action_window_size
self.past_action_window_size = self.config.framework.action_model.past_action_window_size
self.chunk_len = self.past_action_window_size + 1 + self.future_action_window_size
# 训练时
# action支路,self.backbone.build_inputs、self.backbone.forward()、self.action_model.forward()
def forward(self, examples: List[dict] = None, **kwargs) -> Tuple:
# 1. CosmosPredict:构建输入
batch_images = [example["image"] for example in examples]
instructions = [example["lang"] for example in examples]
wm_inputs = self.backbone.build_inputs(images=batch_images, instructions=instructions)
# 2. CosmosPredict:前向推理
# 这里的CosmosPredict起到了一个编码器的作用,将输入的当前观测和prompt编码为vl-tokens
with torch.autocast("cuda", dtype=torch.bfloat16):
wm_outputs = self.backbone(
**wm_inputs,
output_hidden_states=True,
return_dict=True,
)
last_hidden = wm_outputs.hidden_states[-1]
# 3. ActionHead:构建输入
with torch.autocast("cuda", dtype=torch.float32):
# 3.1 初始化action
actions = [example["action"] for example in examples]
actions = torch.tensor(np.array(actions), device=last_hidden.device, dtype=last_hidden.dtype)
actions_target = actions[:, -(self.future_action_window_size + 1):, :]
# 3.2 重复降噪
# 将action和vl-tokens复制多次,重复多次降噪
repeated_diffusion_steps = (
self.config.trainer.get("repeated_diffusion_steps", 4)
if self.config and hasattr(self.config, "trainer")
else self.config.framework.action_model.get("repeated_diffusion_steps", 4)
)
actions_target_repeated = actions_target.repeat(repeated_diffusion_steps, 1, 1)
last_hidden_repeated = last_hidden.repeat(repeated_diffusion_steps, 1, 1)
# 3.3 准备state、action_mask和embodiment_tag
state = [example["state"] for example in examples] if "state" in examples[0] else None
if state is not None:
state = torch.tensor(np.array(state), device=last_hidden.device, dtype=last_hidden.dtype)
state_repeated = state.repeat(repeated_diffusion_steps, 1, 1)
else:
state_repeated = None
action_mask = [example["action_mask"] for example in examples] if "action_mask" in examples[0] else None
if action_mask is not None:
action_mask_tensor = torch.tensor(np.array(action_mask), device=last_hidden.device, dtype=last_hidden.dtype)
action_mask_target = action_mask_tensor[:, -(self.future_action_window_size + 1):, :]
action_mask_repeated = action_mask_target.repeat(repeated_diffusion_steps, 1, 1)
else:
action_mask_repeated = None
embodiment_tag = [example["embodiment_tag"] for example in examples] if "embodiment_tag" in examples[0] else None
if embodiment_tag is not None:
embodiment_tag = torch.tensor(np.array(embodiment_tag), device=last_hidden.device, dtype=torch.int64)
embodiment_tag = embodiment_tag.view(-1)
embodiment_tag_repeated = embodiment_tag.repeat(repeated_diffusion_steps)
else:
embodiment_tag_repeated = None
action_loss, target_emb = self.action_model(
last_hidden_repeated,
actions_target_repeated,
state_repeated,
embodiment_tag_repeated,
action_mask=action_mask_repeated,
use_vjepa2ac=True,
repeated_diffusion_steps=repeated_diffusion_steps,
)
return action_loss, target_emb
# @torch.inference_mode()
# def predict_action(self, examples: List[dict], **kwargs) -> dict:
# if type(examples) is not list:
# examples = [examples]
# batch_images = [to_pil_preserve(example["image"]) for example in examples]
# instructions = [example["lang"] for example in examples]
# state = [example["state"] for example in examples] if "state" in examples[0] else None
# embodiment_tag = [example["embodiment_tag"] for example in examples] if "embodiment_tag" in examples[0] else None
# train_obs_image_size = getattr(self.config.framework, "obs_image_size", None)
# if train_obs_image_size:
# batch_images = resize_images(batch_images, target_size=train_obs_image_size)
# wm_inputs = self.backbone.build_inputs(images=batch_images, instructions=instructions)
# with torch.autocast("cuda", dtype=torch.bfloat16):
# wm_outputs = self.backbone(
# **wm_inputs,
# output_hidden_states=True,
# return_dict=True,
# )
# last_hidden = wm_outputs.hidden_states[-1]
# state = (
# torch.from_numpy(np.array(state)).to(last_hidden.device, dtype=last_hidden.dtype)
# if state is not None
# else None
# )
# embodiment_tag = (
# torch.from_numpy(np.array(embodiment_tag)).to(last_hidden.device, dtype=torch.int64)
# if embodiment_tag is not None
# else None
# )
# if embodiment_tag is not None:
# embodiment_tag = embodiment_tag.view(-1)
# with torch.autocast("cuda", dtype=torch.float32):
# pred_actions = self.action_model.predict_action(last_hidden, state, embodiment_tag)
# normalized_actions = pred_actions.detach().cpu().numpy()
# return {"normalized_actions": normalized_actions}
|