File size: 11,784 Bytes
aceb411 | 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 | import re
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from collections import OrderedDict
from torch.distributions.uniform import Uniform
import math
from typing import Dict, Optional, Tuple, Any
import logging
from utils.hub_mixin import CompatiblePyTorchModelHubMixin
from models.hrdt.model import HRDT
class SigmoidTimestepSampler:
"""
LogitNormal sampler
Sampling: u ~ N(mean, std), t = sigmoid(u)
"""
def __init__(self, timestep_max=1.0, mean=0.0, std=1.0):
self.timestep_max = timestep_max
self.mean = mean # Normal distribution mean
self.std = std # Normal distribution standard deviation
def sample(self, shape):
"""
LogitNormal sampling, which is sigmoid(randn(m,s))
1. u ~ N(mean, std)
2. t = sigmoid(u)
"""
# Generate normal distribution random numbers u ~ N(mean, std)
u = torch.normal(mean=self.mean, std=self.std, size=shape)
# Apply sigmoid transformation to get timesteps in (0,1) range
t = torch.sigmoid(u)
# Scale to [0, timestep_max]
return t * self.timestep_max
def visualize_distribution(self, num_samples=10000):
"""
Visualize sampling distribution
"""
samples = self.sample((num_samples,))
return {
'samples': samples,
'mean': samples.mean().item(),
'std': samples.std().item(),
'min': samples.min().item(),
'max': samples.max().item(),
'config': f'LogitNormal(mean={self.mean}, std={self.std})'
}
class ActionEncoder(nn.Module):
"""Action encoder that combines state and action adaptors"""
def __init__(self, state_dim, action_dim, hidden_size, config):
super().__init__()
self.state_adaptor = self.build_condition_adapter(
config['st_adaptor'],
in_features=state_dim,
out_features=hidden_size
)
self.action_adaptor = self.build_condition_adapter(
config['act_adaptor'],
in_features=action_dim,
out_features=hidden_size
)
def build_condition_adapter(self, projector_type, in_features, out_features):
projector = None
if projector_type == 'linear':
projector = nn.Linear(in_features, out_features)
else:
mlp_silu_match = re.match(r'^mlp(\d+)x_silu$', projector_type)
if mlp_silu_match:
mlp_depth = int(mlp_silu_match.group(1))
modules = [nn.Linear(in_features, out_features)]
for _ in range(1, mlp_depth):
modules.append(nn.SiLU())
modules.append(nn.Linear(out_features, out_features))
projector = nn.Sequential(*modules)
if projector is None:
raise ValueError(f'Unknown projector type: {projector_type}')
return projector
def encode_state(self, state_tokens):
return self.state_adaptor(state_tokens)
def encode_action(self, action_tokens):
return self.action_adaptor(action_tokens)
class HRDTRunner(
nn.Module,
CompatiblePyTorchModelHubMixin,
repo_url="https://huggingface.co/embodiedfoundation/H-RDT"
):
def __init__(self, *, state_dim, action_dim,
pred_horizon, config, act_pos_emb_config=None, img_pos_emb_config=None, lang_pos_emb_config=None,
max_img_len=None, max_lang_len=None,
training_mode='lang',
mode='pretrain',
pretrained_backbone_path=None,
dtype=torch.bfloat16):
super(HRDTRunner, self).__init__()
# Create diffusion model
hidden_size = config['hrdt']['hidden_size']
self.gradient_checkpointing = False
self.hidden_size = hidden_size
self.training_mode = training_mode
self.mode = mode # 'pretrain' or 'finetune'
# Validate mode
if mode not in ['pretrain', 'finetune']:
raise ValueError(f"mode must be 'pretrain' or 'finetune', got {mode}")
# Create H-RDT model
self.model = HRDT(
horizon=pred_horizon,
config=config['hrdt'],
x_pos_emb_config=act_pos_emb_config,
img_pos_emb_config=img_pos_emb_config,
lang_pos_emb_config=lang_pos_emb_config,
max_img_len=max_img_len,
max_lang_len=max_lang_len,
training_mode=training_mode,
dtype=dtype,
)
# Image features adapter - use dimensions from config
self.img_adapter = self.build_condition_adapter(
config.get('img_adapter', 'mlp2x_silu'),
in_features=config.get('vision', {}).get('feature_dim', 2048), # Default to ResNet50 dim
out_features=hidden_size
)
# Action encoder (state and action adaptors)
self.action_encoder = ActionEncoder(
state_dim=state_dim,
action_dim=action_dim,
hidden_size=hidden_size,
config=config
)
# Language features adapter - use dimensions from config
self.lang_adapter = self.build_condition_adapter(
config.get('lang_adapter', 'mlp2x_silu'),
in_features=config.get('text', {}).get('feature_dim', 768), # Default to DistilBERT dim
out_features=hidden_size
)
# Create noise scheduler
noise_scheduler_config = config['noise_scheduler']
self.num_inference_timesteps = noise_scheduler_config['num_inference_timesteps']
self.timestep_max = noise_scheduler_config['timestep_max']
sampler_type = noise_scheduler_config.get('sampler_type', 'sigmoid')
if sampler_type == 'uniform':
self.timestep_sampler = Uniform(0, self.timestep_max)
elif sampler_type == 'sigmoid':
mean = noise_scheduler_config.get('sigmoid_mean', 0.0)
std = noise_scheduler_config.get('sigmoid_std', 1.0)
self.timestep_sampler = SigmoidTimestepSampler(self.timestep_max, mean, std)
else:
raise ValueError(f"Unknown sampler type: {sampler_type}")
self.pred_horizon = pred_horizon
self.action_dim = action_dim
# TimeNoise config
self.time_noise_a = config["time_noise"]["a"]
self.time_noise_beta_m = config["time_noise"]["beta_m"]
self.img_pos_emb_config = img_pos_emb_config
# Print model size
print("Model params: %e" % sum(p.numel() for p in self.parameters()))
@classmethod
def from_pretrained_for_finetune(cls, pretrained_path, state_dim, action_dim, pred_horizon, config, **kwargs):
"""Create model in finetune mode with pretrained backbone"""
return cls(
state_dim=state_dim,
action_dim=action_dim,
pred_horizon=pred_horizon,
config=config,
mode='finetune',
pretrained_backbone_path=pretrained_path,
**kwargs
)
def build_condition_adapter(
self, projector_type, in_features, out_features):
projector = None
if projector_type == 'linear':
projector = nn.Linear(in_features, out_features)
else:
mlp_silu_match = re.match(r'^mlp(\d+)x_silu$', projector_type)
if mlp_silu_match:
mlp_depth = int(mlp_silu_match.group(1))
modules = [nn.Linear(in_features, out_features)]
for _ in range(1, mlp_depth):
modules.append(nn.SiLU())
modules.append(nn.Linear(out_features, out_features))
projector = nn.Sequential(*modules)
if projector is None:
raise ValueError(f'Unknown projector type: {projector_type}')
return projector
def gradient_checkpointing_enable(self, value=True):
"""Enable gradient checkpointing for memory efficiency"""
self.gradient_checkpointing = value
if hasattr(self.model, "gradient_checkpointing_enable"):
self.model.gradient_checkpointing_enable(value)
def compute_loss(self, state_tokens=None, action_gt=None, image_tokens=None, lang_tokens=None, lang_attn_mask=None):
"""
img_tokens: (batch_size, img_len, img_token_dim)
state_tokens: (batch_size, chunk_size, action_dim),
action_gt: (batch_size, chunk_size, action_dim), ground-truth actions for supervision
lang_tokens: (batch_size, L, hidden_size), language features (unpooled)
lang_attn_mask: (batch_size, L), attention mask for language tokens
"""
batch_size = image_tokens.shape[0]
device = image_tokens.device
dtype = image_tokens.dtype
noise = torch.randn(action_gt.shape, dtype=dtype, device=device)
timesteps = self.timestep_sampler.sample((batch_size,)).to(device)
broadcasted = timesteps.view(-1, 1, 1)
noisy_action = (action_gt * broadcasted + noise * (1 - broadcasted)).to(dtype=dtype)
img_c = self.img_adapter(image_tokens)
# Process language features - handle None case
lang_c = None
if lang_tokens is not None:
lang_c = self.lang_adapter(lang_tokens) # [B, L, D] - keep unpooled for cross attention
# state/action using action encoder
state_traj = self.action_encoder.encode_state(state_tokens)
action_traj = self.action_encoder.encode_action(noisy_action)
state_action_traj = torch.cat([state_traj, action_traj], dim=1)
pred = self.model(state_action_traj, timesteps, img_c=img_c, lang_c=lang_c, lang_attn_mask=lang_attn_mask)
target = action_gt - noise
diff_loss = F.mse_loss(pred, target)
return {"diff_loss": diff_loss, "loss": diff_loss}
@torch.no_grad()
def predict_action(self, state_tokens=None, image_tokens=None, lang_tokens=None, lang_attn_mask=None):
'''
state_tokens: (batch_size, chunk_size, action_dim)
image_tokens: (batch_size, img_len, in_feat_dim)
lang_tokens (torch.Tensor): language features [B, L, hidden_size] (unpooled)
lang_attn_mask: (batch_size, L), attention mask for language tokens
return: (batch_size, chunk_size, action_dim), predicted action sequence
'''
batch_size = image_tokens.shape[0]
device = image_tokens.device
dtype = image_tokens.dtype
img_c = self.img_adapter(image_tokens)
# Process language features - handle None case
lang_c = None
if lang_tokens is not None:
lang_c = self.lang_adapter(lang_tokens) # [B, L, D] - keep unpooled for cross attention
state_traj = self.action_encoder.encode_state(state_tokens)
noisy_action = torch.randn((batch_size, self.pred_horizon, self.action_dim), dtype=dtype, device=device)
timestep = torch.tensor([0.0], dtype=dtype, device=device)
step_size = 1.0 / self.num_inference_timesteps
for _ in range(self.num_inference_timesteps):
action_traj = self.action_encoder.encode_action(noisy_action)
state_action_traj = torch.cat([state_traj, action_traj], dim=1)
pred = self.model(state_action_traj, timestep, img_c=img_c, lang_c=lang_c, lang_attn_mask=lang_attn_mask)
noisy_action = pred * step_size + noisy_action
timestep = timestep + step_size
return noisy_action
def forward(self, *args, **kwargs) -> torch.Tensor:
return self.compute_loss(*args, **kwargs) |