SleepMastger's picture
Restore sanitized repository files
b6f2e18 verified
Raw History Blame Contribute Delete
15.3 kB
import torch
import torch.nn as nn
import logging
import json
from transformers import AutoModel, AutoConfig
from .vla_model_fm import VLAModel, calc_flow_matching_loss
logger = logging.getLogger(__name__)
class ModelFactory:
"""Initializes the DINOv3 vision encoder and the action prediction model"""
@staticmethod
def create_vision_encoder(checkpoint_path, dtype=torch.bfloat16, device="cuda"):
"""
Load a frozen DINOv3 ViT from a local path (offline).
Returns:
model: DINOv3 model (eval mode, frozen)
hidden_size: int, model hidden dimension
num_register_tokens: int, number of register tokens (usually 4)
patch_size: int, ViT patch size (usually 16)
"""
logger.info(f"Loading frozen DINOv3 vision encoder from {checkpoint_path}...")
# Read config first for metadata
config = AutoConfig.from_pretrained(checkpoint_path, local_files_only=True)
model = AutoModel.from_pretrained(
checkpoint_path,
torch_dtype=dtype,
local_files_only=True,
attn_implementation="sdpa",
).to(device)
# Freeze
model.eval()
for p in model.parameters():
p.requires_grad = False
hidden_size = getattr(config, "hidden_size", None)
num_register_tokens = getattr(config, "num_register_tokens", 4)
patch_size = getattr(config, "patch_size", 16)
if hidden_size is None:
# fallback: infer with a dummy forward
with torch.no_grad():
dummy = torch.zeros(1, 3, 224, 224, device=device, dtype=dtype)
out = model(pixel_values=dummy)
hidden_size = out.last_hidden_state.shape[-1]
logger.info(
f"DINOv3 loaded: hidden_size={hidden_size}, "
f"num_register_tokens={num_register_tokens}, patch_size={patch_size}"
)
return model, hidden_size, num_register_tokens, patch_size
@staticmethod
def create_action_model(config, dino_hidden_size, num_dino_layers, task_cond_dim=None,
patch_size=16):
"""create VLAModel"""
logger.info("Initializing VLAModel...")
model_cfg = config.model
ae_cfg = model_cfg.action_expert
ve_cfg = model_cfg.vision_encoder
# All extracted layers share the same feat_dim (DINOv3 hidden_size is uniform across layers)
dino_feat_dims = tuple([dino_hidden_size] * num_dino_layers)
# ---- Multi-layer feature fusion mode ----
fusion_mode = ve_cfg.get('fusion_mode', 'per_layer')
concat_cfg = ve_cfg.get('concat', {})
concat_proj_type = concat_cfg.get('proj_type', 'linear') if concat_cfg else 'linear'
concat_pre_norm = concat_cfg.get('pre_norm', True) if concat_cfg else True
concat_out_dim = concat_cfg.get('out_dim', None) if concat_cfg else None
if concat_out_dim is None:
concat_out_dim = dino_hidden_size # project down to DINO hidden_size by default
# ---- Future feature prediction ----
ff_cfg = model_cfg['future_feat']
use_future_feat = ff_cfg['enabled']
future_feat_depth = ff_cfg['num_decoder_layers']
future_feat_heads = ff_cfg['num_heads']
# Future-frame patch token count (number of queries for dense prediction); image_size=(W,H)
img_w, img_h = tuple(config.dataset.image_size)
num_patches = (img_h // patch_size) * (img_w // patch_size)
# Views are concatenated along the token axis before the adapter, so K multiplies
# the encoder cost but leaves the adapter output (and the DiT) unchanged.
num_cameras = len(config.dataset.camera_names)
logger.info(f"Fusion mode: {fusion_mode} "
f"(concat_out_dim={concat_out_dim}, proj={concat_proj_type}, pre_norm={concat_pre_norm})")
logger.info(f"Cameras: {num_cameras} {list(config.dataset.camera_names)} "
f"-> {num_cameras * num_patches} patch tokens into the adapter")
if use_future_feat:
logger.info(f"Future-Feat Prediction ENABLED: "
f"num_queries(num_patches)={num_patches}, out_dim={dino_hidden_size}")
model = VLAModel(
action_dim=config.common.action_dim,
proprio_dim=config.common.state_dim,
hidden_dim=ae_cfg.hidden_size,
action_len=config.common.action_chunk_size,
proprio_len=config.common.proprio_len,
depth=ae_cfg.depth,
num_heads=ae_cfg.num_heads,
dino_feat_dims=dino_feat_dims,
num_cameras=num_cameras,
vlm_num_queries=ae_cfg.vlm_adapter_num_queries,
adapter_depth=ae_cfg.adapter_depth,
num_registers=model_cfg.num_registers,
state_inject_start=0,
task_cond_dim=task_cond_dim,
fusion_mode=fusion_mode,
concat_proj_type=concat_proj_type,
concat_pre_norm=concat_pre_norm,
concat_out_dim=concat_out_dim,
use_future_feat=use_future_feat,
future_feat_num_queries=num_patches,
future_feat_out_dim=dino_hidden_size,
future_feat_depth=future_feat_depth,
future_feat_heads=future_feat_heads,
)
return model
class VLAWrapper(nn.Module):
"""
VLA wrapper (DINOv3 version):
1. Run DINOv3 on input images with output_hidden_states=True
2. Extract multi-layer hidden states according to feat_layers, one adapter per layer
3. Call action_model + flow matching loss
"""
def __init__(self,
vision_encoder,
action_model,
time_sampler,
feat_layers,
include_cls_register,
num_register_tokens,
device,
dtype,
norm_stats_path,
train_config=None,
future_feat_target_layer=-1,
):
super().__init__()
self.vision_encoder = vision_encoder
self.action_model = action_model
self.time_sampler = time_sampler
self.feat_layers = list(feat_layers)
self.include_cls_register = include_cls_register
self.num_register_tokens = num_register_tokens
self.future_feat_target_layer = future_feat_target_layer
self.device = device
self.dtype = dtype
# Training-related parameters (only used during training)
self.train_config = train_config
if self.train_config is not None:
self.time_mu = train_config['time_mu']
self.time_sigma = train_config['time_sigma']
self.use_vel_weight = train_config['use_vel_weight']
self.vel_weight_alpha = train_config['vel_weight_alpha']
self.vel_weight_sigma = train_config['vel_weight_sigma']
self.use_future_feat = train_config.get('use_future_feat', False)
self.lambda_future_feat = train_config.get('lambda_future_feat', 0.0)
else:
# Inference-mode defaults
self.time_mu = 0.0
self.time_sigma = 1.0
self.use_vel_weight = False
self.vel_weight_alpha = 0.2
self.vel_weight_sigma = 0.01
self.use_future_feat = False
self.lambda_future_feat = 0.0
logger.info(f"VLAWrapper initialized. feat_layers={self.feat_layers}, "
f"include_cls_register={self.include_cls_register}")
# Load normalization stats
self.load_norm_stats(norm_stats_path)
def load_norm_stats(self, path):
"""Read JSON and load action / state min/max for normalization"""
logger.info(f"Loading normalization stats from {path}...")
with open(path, 'r') as f:
data = json.load(f)
# 'robotwin2' is the key used by the shipped stat-*.json files; stats produced by
# utils/calc_stat_stack.py use a dataset-specific key instead.
if 'robotwin2' in data:
stats = data['robotwin2']
elif len(data) == 1:
stats = next(iter(data.values()))
else:
raise KeyError(f"Cannot pick normalization stats from {path}: "
f"expected a 'robotwin2' key or a single top-level key, "
f"got {sorted(data.keys())}")
action_stats = stats['action']
state_stats = stats['state']
act_min = torch.tensor(action_stats['min'], dtype=torch.float32)
act_max = torch.tensor(action_stats['max'], dtype=torch.float32)
self.register_buffer('action_min', act_min)
self.register_buffer('action_max', act_max)
logger.info(f"Loaded Action stats - Dim: {len(action_stats['min'])}")
state_min = torch.tensor(state_stats['min'], dtype=torch.float32)
state_max = torch.tensor(state_stats['max'], dtype=torch.float32)
self.register_buffer('state_min', state_min)
self.register_buffer('state_max', state_max)
logger.info(f"Loaded State stats - Dim: {len(state_stats['min'])}")
@torch.no_grad()
def get_vision_features(self, pixel_values):
"""
Extract DINOv3 multi-layer hidden states.
Args:
pixel_values: (B, 3, H, W), or (B, K, 3, H, W) for K cameras. ImageNet normalized.
Returns:
List[Tensor(B, K*N, hidden_size)] with length = len(feat_layers), ordered as
feat_layers. If include_cls_register=True, N = 1 + num_register_tokens +
num_patches, otherwise N = num_patches.
Multiple cameras run as one encoder pass over B*K images and are then
concatenated camera-major along the token axis, so the adapter's queries
cross-attend over every view at once. The adapter output stays at
vlm_adapter_num_queries tokens, i.e. the DiT cost does not change with K.
"""
pixel_values = pixel_values.to(self.device, self.dtype, non_blocking=True)
num_cams, bsz = 1, pixel_values.shape[0]
if pixel_values.dim() == 5:
num_cams = pixel_values.shape[1]
pixel_values = pixel_values.flatten(0, 1)
outputs = self.vision_encoder(
pixel_values=pixel_values,
output_hidden_states=True,
return_dict=True,
)
# hidden_states is a tuple of length num_hidden_layers + 1:
# [0] is the embedding output, [1..num_layers] are the transformer block outputs
# feat_layer = -1 -> last layer
hidden_states = outputs.hidden_states
feats_list = []
for layer_idx in self.feat_layers:
h = hidden_states[layer_idx] # (B*K, 1+R+P, D)
if not self.include_cls_register:
skip = 1 + self.num_register_tokens
h = h[:, skip:, :]
if num_cams > 1:
h = h.reshape(bsz, num_cams * h.shape[1], h.shape[2])
feats_list.append(h)
return feats_list
@torch.no_grad()
def get_future_target_features(self, future_pixel_values):
"""
Extract future-frame DINO patch token features as the supervision target
for future-feature prediction (frozen, no gradient).
Args:
future_pixel_values: (B, 3, H, W), ImageNet normalized. Always the primary
camera only -- foresight is supervised on one view, so the decoder's query
count stays tied to a single frame's patch grid regardless of how many
cameras condition the policy.
Returns:
Tensor(B, P, hidden_size): patch tokens only (1 CLS + R registers dropped),
taken from layer future_feat_target_layer
"""
future_pixel_values = future_pixel_values.to(self.device, self.dtype, non_blocking=True)
outputs = self.vision_encoder(
pixel_values=future_pixel_values,
output_hidden_states=True,
return_dict=True,
)
h = outputs.hidden_states[self.future_feat_target_layer] # (B, 1+R+P, D)
skip = 1 + self.num_register_tokens
h = h[:, skip:, :] # (B, P, D) patch tokens only
return h
def _normalize_tensor(self, x, min_val, max_val):
min_v = min_val.to(device=x.device, dtype=x.dtype)
max_v = max_val.to(device=x.device, dtype=x.dtype)
denominator = max_v - min_v
denominator[denominator < 1e-6] = 1.0
norm_x = 2 * (x - min_v) / denominator - 1
return norm_x
def normalize_action(self, action):
return self._normalize_tensor(action, self.action_min, self.action_max)
def normalize_state(self, state):
return self._normalize_tensor(state, self.state_min, self.state_max)
def denormalize_action(self, norm_action):
action_min = self.action_min.to(device=norm_action.device, dtype=norm_action.dtype)
action_max = self.action_max.to(device=norm_action.device, dtype=norm_action.dtype)
denominator = action_max - action_min
denominator[denominator < 1e-6] = 1.0
action = (norm_action + 1) / 2 * denominator + action_min
return action
def forward(self, batch):
"""Forward pass and loss computation"""
# 1. Vision features (multi-layer DINO)
pixel_values = batch['pixel_values'] # (B, 3, H, W) or (B, K, 3, H, W)
dino_features_list = self.get_vision_features(pixel_values)
# 2. Action / State preparation
x1_raw = batch['action_sequence'].to(self.device, self.dtype)
qpos_raw = batch['state'].to(self.device, self.dtype)
if qpos_raw.dim() == 2:
qpos_raw = qpos_raw.unsqueeze(1)
# 3. Normalize
x1 = self.normalize_action(x1_raw)
qpos = self.normalize_state(qpos_raw)
# Align state sequence with proprio_len (history_len=1 when state_indices=[0], qpos_history=qpos)
qpos_history = qpos
# 4. Task condition vector
task_cond = None
if batch.get('task_cond') is not None:
task_cond = batch['task_cond'].to(self.device, self.dtype)
# 4b. Future feature target (future-frame patch features from the frozen DINO)
future_feat_target = None
if self.use_future_feat and batch.get('future_pixel_values') is not None:
future_feat_target = self.get_future_target_features(batch['future_pixel_values'])
# 5. Flow Matching Loss
loss, info_dic = calc_flow_matching_loss(
self.action_model,
x1=x1,
dino_features_list=dino_features_list,
qpos_history=qpos_history,
task_cond=task_cond,
time_sampler=self.time_sampler,
time_mu=self.time_mu,
time_sigma=self.time_sigma,
use_velocity_weighting=self.use_vel_weight,
alpha=self.vel_weight_alpha,
sigma=self.vel_weight_sigma,
future_feat_target=future_feat_target,
use_future_feat=self.use_future_feat,
lambda_future_feat=self.lambda_future_feat,
)
return loss, info_dic