File size: 17,205 Bytes
dd37dbc | 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 | # ------------------------------------------------------------------------------
# Copyright 2025 2toINF (https://github.com/2toINF)
#
# 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.
# ------------------------------------------------------------------------------
from __future__ import annotations
from typing import Any, Dict, List
import torch
import numpy as np
from PIL import Image
from fastapi import FastAPI
import cv2
from transformers import PreTrainedModel
from .server import ModelServer
from .modeling_florence2 import Florence2ForConditionalGeneration
from .transformer import SoftPromptedTransformer
from .action_hub import build_action_space
from .configuration_xvla import XVLAConfig
class XVLA(PreTrainedModel, ModelServer):
"""
XVLA: HuggingFace-compatible Vision-Language-Action policy.
Components:
• Florence2 encoder-only backbone (vision-language)
• SoftPromptedTransformer (temporal/action head)
• Action space (pre/post-processing + loss)
"""
config_class = XVLAConfig
base_model_prefix = "xvla"
supports_gradient_checkpointing = True
def __init__(self, config: XVLAConfig, *args, **kwargs):
super().__init__(config, *args, **kwargs)
# Core settings
self.num_actions: int = config.num_actions
self.use_proprio: bool = config.use_proprio
self.action_mode: str = config.action_mode.lower()
# Action space (dimensions + hooks)
if config.action_mode.lower() == "auto":
self.action_space = build_action_space(
config.action_mode.lower(),
real_dim=config.real_action_dim,
max_dim=config.max_action_dim,
idx_for_delta=config.idx_for_delta,
idx_for_mask_proprio=config.idx_for_mask_proprio
)
else:
self.action_space = build_action_space(config.action_mode.lower())
dim_action = self.action_space.dim_action
dim_proprio = getattr(self.action_space, "dim_proprio", dim_action)
# Florence2 backbone (encoder only)
self.vlm = Florence2ForConditionalGeneration(config.florence_config).to(torch.float32)
if hasattr(self.vlm, "language_model"):
lm = self.vlm.language_model
if hasattr(lm, "model") and hasattr(lm.model, "decoder"):
del lm.model.decoder
if hasattr(lm, "lm_head"):
del lm.lm_head
projection_dim = getattr(self.vlm.config, "projection_dim", None)
if projection_dim is None:
raise ValueError("Florence2 config must provide `projection_dim` for multimodal fusion.")
# Temporal/action head
self.transformer = SoftPromptedTransformer(
hidden_size=config.hidden_size,
multi_modal_input_size=projection_dim,
depth=config.depth,
num_heads=config.num_heads,
mlp_ratio=config.mlp_ratio,
num_domains=config.num_domains,
dim_action=dim_action,
dim_propio=dim_proprio,
len_soft_prompts=config.len_soft_prompts,
dim_time=config.dim_time,
max_len_seq=config.max_len_seq,
use_hetero_proj=config.use_hetero_proj,
)
# Deferred FastAPI app
self.app: FastAPI | None = None
# ========================== pretrained loading ================================
@classmethod
def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
"""
Load pretrained XVLA, automatically handling action-head dimension
mismatches.
* Shape-compatible parameters are loaded normally.
* Mismatched parameters are logged and explicitly re-initialised
(Xavier-uniform for weight, zeros for bias — matching
``DomainAwareLinear.__init__``).
"""
import os
import json
import logging
from collections import OrderedDict
logger = logging.getLogger(__name__)
config = kwargs.pop("config", None)
torch_dtype = kwargs.pop("torch_dtype", None)
if config is None:
config = cls.config_class.from_pretrained(
pretrained_model_name_or_path, **kwargs
)
model = cls(config, *model_args)
if torch_dtype is not None:
model = model.to(torch_dtype)
pretrained_state = cls._load_pretrained_state_dict(
pretrained_model_name_or_path
)
model_state = model.state_dict()
to_load = OrderedDict()
mismatched = []
for key, param in pretrained_state.items():
if key not in model_state:
continue
if param.shape == model_state[key].shape:
to_load[key] = param
else:
mismatched.append(
(key, tuple(param.shape), tuple(model_state[key].shape))
)
model.load_state_dict(to_load, strict=False)
if mismatched:
logger.warning(
"=== Mismatched pretrained keys (reinitialized) ===\n"
+ "\n".join(
f" {k}: pretrained {ps} -> current {cs}"
for k, ps, cs in mismatched
)
)
for key, _, _ in mismatched:
parts = key.split(".")
module = model
for part in parts[:-1]:
module = getattr(module, part)
param = getattr(module, parts[-1])
with torch.no_grad():
if "bias" in key:
torch.nn.init.zeros_(param)
elif param.dim() >= 2:
torch.nn.init.xavier_uniform_(param)
else:
torch.nn.init.zeros_(param)
logger.warning(
"Above %d parameter(s) have been re-initialised.",
len(mismatched),
)
return model
@staticmethod
def _load_pretrained_state_dict(model_path: str) -> dict:
"""Load state dict from a local checkpoint (file or directory).
Supports single-file, directory, and sharded safetensors / bin.
"""
import os
import json
from collections import OrderedDict
def _load_safetensors(path):
from safetensors.torch import load_file
return load_file(path)
def _load_bin(path):
return torch.load(path, map_location="cpu")
if os.path.isfile(model_path):
if model_path.endswith(".safetensors"):
return _load_safetensors(model_path)
return _load_bin(model_path)
for fname, loader in [
("model.safetensors", _load_safetensors),
("pytorch_model.bin", _load_bin),
]:
fpath = os.path.join(model_path, fname)
if os.path.isfile(fpath):
return loader(fpath)
for index_name, loader in [
("model.safetensors.index.json", _load_safetensors),
("pytorch_model.bin.index.json", _load_bin),
]:
index_path = os.path.join(model_path, index_name)
if os.path.isfile(index_path):
with open(index_path) as f:
weight_map = json.load(f)["weight_map"]
state_dict = OrderedDict()
for shard_file in dict.fromkeys(weight_map.values()):
state_dict.update(
loader(os.path.join(model_path, shard_file))
)
return state_dict
raise FileNotFoundError(
f"No checkpoint found at '{model_path}'. Expected "
f"model.safetensors, pytorch_model.bin, or sharded index files."
)
# ============================= Florence2 encoder =============================
def forward_vlm(
self,
input_ids: torch.LongTensor, # [B, L]
pixel_values: torch.FloatTensor, # [B, V, C, H, W]
image_mask: torch.Tensor, # [B, V] (bool or 0/1)
) -> Dict[str, torch.Tensor]:
"""
Encode text + multi-view images via Florence2 encoder.
Returns:
{ "vlm_features": [B, T_enc, D], "aux_visual_inputs": [B, (V-1)*N, D] }
"""
B, V = pixel_values.shape[:2]
flat_mask = image_mask.view(-1).to(torch.bool) # [B*V]
flat_images = pixel_values.flatten(0, 1) # [B*V, C, H, W]
num_valid = int(flat_mask.sum().item())
if num_valid == 0:
raise ValueError("At least one image view must be valid per batch.")
valid_images = flat_images[flat_mask] # [#valid, C, H, W]
valid_feats = self.vlm._encode_image(valid_images) # [#valid, N, D]
N, D = valid_feats.shape[1:]
image_features = valid_feats.new_zeros((B * V, N, D))
image_features[flat_mask] = valid_feats
image_features = image_features.view(B, V, N, D) # [B, V, N, D]
inputs_embeds = self.vlm.get_input_embeddings()(input_ids) # [B, L, D]
merged_embeds, attention_mask = self.vlm._merge_input_ids_with_image_features(
image_features[:, 0], # first view: [B, N, D]
inputs_embeds, # [B, L, D]
)
enc_out = self.vlm.language_model.model.encoder(
attention_mask=attention_mask,
inputs_embeds=merged_embeds,
)[0] # [B, T_enc, D]
aux_visual_inputs = image_features[:, 1:].reshape(B, -1, D) # remaining views flattened
return {"vlm_features": enc_out, "aux_visual_inputs": aux_visual_inputs}
# ================================= training =================================
def forward(
self,
input_ids: torch.LongTensor,
image_input: torch.FloatTensor,
image_mask: torch.Tensor,
domain_id: torch.LongTensor,
proprio: torch.Tensor,
action: torch.Tensor, # [B, T=num_actions, D=dim_action]
) -> Dict[str, torch.Tensor]:
"""
1) Encode multimodal inputs.
2) Diffusion-style noisy mixture of actions: x_t = t*noise + (1-t)*gt.
3) Space-specific preprocessing, prediction, and supervised loss.
"""
action, proprio = self.action_space.prepare_for_training(action, proprio)
enc = self.forward_vlm(input_ids, image_input, image_mask)
B = input_ids.shape[0]
t = (torch.rand(1, device=input_ids.device)
+ torch.arange(B, device=input_ids.device) / B) % (1 - 1e-5)
action_noisy = torch.randn_like(action) * t.view(-1, 1, 1) + action * (1 - t).view(-1, 1, 1)
proprio_m, action_noisy_m = self.action_space.preprocess(proprio, action_noisy)
pred_action = self.transformer(
domain_id=domain_id,
action_with_noise=action_noisy_m,
t=t,
proprio=proprio_m,
**enc,
)
return self.action_space.compute_loss(pred_action, action)
# ================================= inference =================================
@torch.no_grad()
def generate_actions(
self,
input_ids: torch.LongTensor,
image_input: torch.FloatTensor,
image_mask: torch.Tensor,
domain_id: torch.LongTensor,
proprio: torch.Tensor,
steps: int = 10,
) -> torch.Tensor:
"""
Iterative denoising (linear schedule).
Applies action_space.postprocess at the end (e.g., sigmoid on gripper).
"""
self.eval()
enc = self.forward_vlm(input_ids, image_input, image_mask)
B = input_ids.shape[0]
D = self.action_space.dim_action
x1 = torch.randn(B, self.num_actions, D, device=proprio.device, dtype=proprio.dtype)
action = torch.zeros_like(x1)
steps = max(1, int(steps))
for i in range(steps, 0, -1):
t = torch.full((B,), i / steps, device=proprio.device, dtype=proprio.dtype)
x_t = x1 * t.view(-1, 1, 1) + action * (1 - t).view(-1, 1, 1)
proprio_m, x_t_m = self.action_space.preprocess(proprio, x_t)
action = self.transformer(
domain_id=domain_id,
action_with_noise=x_t_m,
proprio=proprio_m,
t=t,
**enc,
)
return self.action_space.postprocess(action, proprio=proprio)
# =============================== FastAPI service =============================
def inference_api(self, payload: Dict[str, Any] | List[Dict[str, Any]], **kwargs) -> np.ndarray:
"""
XVLA inference supporting:
- Single sample: payload is a dict of scalars/arrays.
- Grouped batch: payload is a list of dicts with same-length fields.
payload contents:
- "language_instruction": str or List[str], optional
- "image0", "image1", ... : np.ndarray (H, W, C) or encoded buffer, required
- "proprio": np.ndarray (D,) or (B, D), required
- "domain_id": int / List[int] if batch > 1, required
- "steps": int, optional, default=10
- "batch_size": int, optional, default=1
Returns:
- (T, D) for single sample
- (B, T, D) for grouped batch
"""
# -------------------------
# 1) Normalize payload -> List[Dict[str, Any]]
# -------------------------
processor = kwargs.get("processor")
if isinstance(payload, dict):
batch_payloads: List[Dict[str, Any]] = [payload]
batch_size = len(batch_payloads)
device = next(self.parameters()).device
dtype = next(self.parameters()).dtype
# -------------------------
# 2) Utilities
# -------------------------
def move_to_device(x: Any) -> torch.Tensor:
"""Convert to tensor and move to model device/dtype."""
tensor = x if isinstance(x, torch.Tensor) else torch.as_tensor(x)
if tensor.is_floating_point():
return tensor.to(device=device, dtype=dtype)
return tensor.to(device=device)
def decode_image_list(sample: Dict[str, Any]) -> List[Image.Image]:
"""Decode image0/image1/... from np.ndarray into PIL Images."""
images: List[Image.Image] = []
idx = 0
while f"image{idx}" in sample:
arr = sample[f"image{idx}"]
if not isinstance(arr, np.ndarray): raise ValueError(f"image{idx} must be np.ndarray, got {type(arr)}")
if arr.ndim == 1: # encoded buffer
arr = cv2.imdecode(arr, cv2.IMREAD_COLOR)
if arr is None: raise ValueError(f"cv2.imdecode failed for image{idx}")
arr = cv2.cvtColor(arr, cv2.COLOR_BGR2RGB)
images.append(Image.fromarray(arr))
idx += 1
if not images:
raise ValueError("Missing images: expected keys image0, image1, ...")
return images
# -------------------------
# 3) Per-sample preprocessing + strict collation (no padding)
# -------------------------
language_batch: List[str] = []
images_batch: List[List[Image.Image]] = []
proprio_batch: List[torch.Tensor] = []
domain_id_list: List[int] = []
denoiseing_steps = batch_payloads[0].get("steps", 10)
for sample in batch_payloads:
images_batch.append(decode_image_list(sample))
language_batch.append(sample.get("language_instruction", ""))
proprio_batch.append(move_to_device(sample["proprio"]))
domain_id_list.append(int(sample.get("domain_id", 0)))
model_inputs = processor(
images=images_batch,
language_instruction=language_batch,
)
model_inputs = {k: move_to_device(v) for k, v in model_inputs.items()}
model_inputs.update(
proprio=torch.stack(proprio_batch, dim=0), # (B, state_dim)
domain_id=torch.tensor(domain_id_list, dtype=torch.long, device=device), # (B,)
steps=denoiseing_steps, # one scalar for whole batch
)
# -------------------------
# 4) Inference
# -------------------------
self.eval()
with torch.inference_mode():
actions = self.generate_actions(**model_inputs) # expected: (B, T, D)
actions_np = actions.float().cpu().numpy()
return actions_np[0] if batch_size == 1 else actions_np |