Download code/starVLA/model/framework/WM4A/CosmoPredict2OFT.py from yuuu94/GraphVLA-StackBowls-V7-10k: direct link, hf CLI and curl.
- Browser
- Download file 9.67 kB
-
https://huggingface.co/yuuu94/GraphVLA-StackBowls-V7-10k/resolve/main/code/starVLA/model/framework/WM4A/CosmoPredict2OFT.py
- Command line
-
hf download hf://yuuu94/GraphVLA-StackBowls-V7-10k/code/starVLA/model/framework/WM4A/CosmoPredict2OFT.py
-
curl -L -o CosmoPredict2OFT.py https://huggingface.co/yuuu94/GraphVLA-StackBowls-V7-10k/resolve/main/code/starVLA/model/framework/WM4A/CosmoPredict2OFT.py
9.67 kB
| # Copyright 2025 starVLA community. All rights reserved. | |
| # Licensed under the MIT License, Version 1.0 (the "License"); | |
| """ | |
| CosmoPredict2-OFT Framework β World Model + MLP Regression for Action Prediction. | |
| Uses Cosmos-Predict2 DiT as the perception backbone with a lightweight MLP | |
| action head (L1 regression), inspired by OpenVLA-OFT. | |
| Architecture: | |
| T5 (text) + VAE (image) β DiT Transformer β hidden_states [B, N, 2048] | |
| β Global avg pool β [B, 2048] | |
| β Linear projection β [B, chunk_len, 2048] | |
| β MLP (L1 regression) β action predictions [B, chunk_len, action_dim] | |
| Key differences from CosmoPredict2GR00T: | |
| - Action head: MLP L1 regression (not flow-matching diffusion) | |
| - No repeated_diffusion_steps (single forward pass) | |
| - Faster training & inference | |
| """ | |
| 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 | |
| import torch.nn as nn | |
| from deployment.model_server.tools.image_tools import to_pil_preserve | |
| from starVLA.training.trainer_utils import initialize_overwatch | |
| logger = initialize_overwatch(__name__) | |
| IGNORE_INDEX = -100 | |
| from starVLA.model.framework.base_framework import baseframework | |
| from starVLA.model.framework.share_tools import add_discretized_state_to_instruction, merge_framework_config | |
| from starVLA.model.modules.action_model.MLP_ActionHeader import 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.trainer_tools import resize_images | |
| class CosmoPredict2OFTDefaultConfig: | |
| """CosmoPredict2-OFT default parameters.""" | |
| name: str = "CosmoPredict2OFT" | |
| # === World Model backbone (Cosmos-Predict2) === | |
| world_model: dict = field( | |
| default_factory=lambda: { | |
| "base_wm": "./playground/Pretrained_models/nvidia/Cosmos-Predict2-2B-Video2World", | |
| "extract_layers": [-1], | |
| } | |
| ) | |
| qwenvl: dict = field( | |
| default_factory=lambda: { | |
| "base_vlm": "./playground/Pretrained_models/nvidia/Cosmos-Predict2-2B-Video2World", | |
| } | |
| ) | |
| # === Action head (MLP L1 regression) === | |
| action_model: dict = field( | |
| default_factory=lambda: { | |
| "action_model_type": "MLP", | |
| "action_dim": 7, | |
| "action_hidden_dim": 2048, | |
| "future_action_window_size": 8, | |
| "past_action_window_size": 0, | |
| } | |
| ) | |
| class CosmoPredict2_OFT(baseframework): | |
| """ | |
| World-Model-for-Action framework using Cosmos-Predict2 + MLP regression. | |
| Components: | |
| - Cosmos-Predict2 DiT (T5 + VAE + Transformer) for spatiotemporal features | |
| - Adaptive pooling + MLP regression head (L1 loss) | |
| """ | |
| def __init__(self, config: Optional[dict] = None, **kwargs) -> None: | |
| super().__init__() | |
| self.config = merge_framework_config(CosmoPredict2OFTDefaultConfig, config) | |
| self.backbone = get_world_model(config=self.config) | |
| wm_hidden = self.backbone.model.config.hidden_size | |
| self.config.framework.action_model.action_hidden_dim = wm_hidden | |
| self.action_model = get_action_model(config=self.config) | |
| # `action_horizon` is the single source of truth for chunk length. | |
| # Legacy aliases (`future_action_window_size`, `past_action_window_size`) | |
| # are normalised upstream by `share_tools.apply_config_compat`, so we | |
| # only ever read `action_horizon` here. | |
| self.action_horizon = int(self.config.framework.action_model.action_horizon) | |
| self.chunk_len = self.action_horizon | |
| self.action_query_proj = nn.Linear(wm_hidden, self.chunk_len * wm_hidden) # Project into a two-layer MLP | |
| self.l1_loss = nn.L1Loss() | |
| def _pool_to_action_queries(self, hidden_states: torch.Tensor) -> torch.Tensor: | |
| B, N, H = hidden_states.shape | |
| pooled = hidden_states.mean(dim=1) | |
| queries = self.action_query_proj(pooled) | |
| action_queries = queries.view(B, self.chunk_len, H) | |
| return action_queries | |
| def forward(self, examples: List[dict] = None, **kwargs) -> Tuple: | |
| batch_images = [example["image"] for example in examples] | |
| instructions = [example["lang"] for example in examples] | |
| actions = [example["action"] for example in examples] | |
| state = [example["state"] for example in examples] if "state" in examples[0] else None | |
| # Optionally prepend discretised proprioceptive state tokens (Οβ.5 style). | |
| instructions = ( | |
| add_discretized_state_to_instruction(instructions, state) if state is not None else instructions | |
| ) | |
| 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] | |
| with torch.autocast("cuda", dtype=torch.float32): | |
| action_queries = self._pool_to_action_queries(last_hidden) # B, chunk_len, hidden_dim | |
| pred_actions = self.action_model.predict_action(action_queries) | |
| actions = torch.tensor(np.array(actions), device=pred_actions.device, dtype=pred_actions.dtype) | |
| actions_target = actions[:, -self.action_horizon :, :] | |
| action_loss = self.l1_loss(pred_actions, actions_target) | |
| return {"action_loss": action_loss} | |
| def predict_action(self, examples: List[dict], **kwargs) -> np.ndarray: | |
| 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 | |
| instructions = ( | |
| add_discretized_state_to_instruction(instructions, state) if state is not None else instructions | |
| ) | |
| train_obs_image_size = getattr(self.config.datasets.vla_data, "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] | |
| with torch.autocast("cuda", dtype=torch.float32): | |
| action_queries = self._pool_to_action_queries(last_hidden) | |
| pred_actions = self.action_model.predict_action(action_queries) | |
| normalized_actions = pred_actions.detach().cpu().numpy() | |
| return {"normalized_actions": normalized_actions} | |
| if __name__ == "__main__": | |
| import argparse | |
| import os | |
| from omegaconf import OmegaConf | |
| from PIL import Image | |
| if os.getenv("DEBUGPY_ENABLE", "0") == "1": | |
| import debugpy | |
| debugpy.listen(("0.0.0.0", 10092)) | |
| print("Rank 0 waiting for debugger attach on port 10092...") | |
| debugpy.wait_for_client() | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument( | |
| "--config_yaml", | |
| type=str, | |
| default="examples/simBenchmarks/LIBERO/train_files/starvla_cotrain_libero.yaml", | |
| help="Path to YAML config", | |
| ) | |
| args, clipargs = parser.parse_known_args() | |
| cfg = OmegaConf.load(args.config_yaml) | |
| cfg.framework.name = "CosmoPredict2OFT" | |
| cfg.framework.world_model = { | |
| "base_wm": "./playground/Pretrained_models/nvidia/Cosmos-Predict2-2B-Video2World", | |
| "extract_layers": [-1], | |
| } | |
| model: CosmoPredict2_OFT = CosmoPredict2_OFT(cfg) | |
| print(model) | |
| image = Image.fromarray(np.random.randint(0, 255, (224, 224, 3), dtype=np.uint8)) | |
| sample = { | |
| "action": np.random.uniform(-1, 1, size=(16, 7)).astype(np.float16), | |
| "image": [image, image], | |
| "lang": "This is a fake instruction for testing.", | |
| "state": np.random.uniform(-1, 1, size=(1, 7)).astype(np.float16), # chunk, state_dim | |
| } | |
| sample2 = sample.copy() | |
| sample2["lang"] = "Another fake instruction for testing." | |
| batch = [sample, sample2] | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| model = model.to(device) | |
| forward_output = model(batch) | |
| action_loss = forward_output["action_loss"] | |
| print(f"[train] Action Loss (with state): {action_loss.item()}") | |
| predict_output = model.predict_action(examples=[sample]) | |
| normalized_actions = predict_output["normalized_actions"] | |
| print(f"[infer] Predicted Action shape: {normalized_actions.shape}") | |
| # Backward-compat: examples without `state` should still work. | |
| sample_no_state = {k: v for k, v in sample.items() if k != "state"} | |
| forward_no_state = model([sample_no_state, sample_no_state]) | |
| print(f"[train] Action Loss (no state): {forward_no_state['action_loss'].item()}") | |
| predict_no_state = model.predict_action(examples=[sample_no_state]) | |
| print(f"[infer] Predicted Action shape (no state): {predict_no_state['normalized_actions'].shape}") | |
| print("Finished") | |