File size: 5,534 Bytes
ae1ed83 | 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 | """TRACE-VLA v3 — PDF core assembly (TRACE-ARCH-002), arm R5.
Keeps the v1 model intact and replaces the v2 conditioning path:
- RegionBindingV3 (binding_v3.py): soft region relevance over the
demo, explicit binding record {role, time_span, confidence}.
- CorrespondenceV3 (correspondence_v3.py): position-free identity
features; per-camera heatmap pi = sum_i p_i * mask_i; per-camera
target features and map centre mu.
- ObjectTokenAdapter (PDF 8.1/8.3): builds, PER CAMERA, one
identity token and one position token (from mu, the pi peak and
the match confidence). The four tokens are appended to the demo
memory windows, so they enter the SYNC input set and are read by
SYNC's cross-attention like every other memory token. Nothing
is added to the context after SYNC (the v2 adapter did that;
the PDF forbids it).
No optional modules: no temporal alignment, no SDE, no ECHO v2,
no adaptive horizon, no precision correction (those are R6).
Token output layers and camera embeddings are zero-initialized,
so at initialization the object tokens are exactly zero.
"""
import torch
import torch.nn as nn
from .binding_v3 import RegionBindingV3
from .correspondence_v3 import CorrespondenceV3
from .model import TraceVLA
class ObjectTokenAdapter(nn.Module):
def __init__(self, d_model: int = 512, hidden: int = 256,
n_cameras: int = 2):
super().__init__()
self.n_cameras = n_cameras
self.id_proj = nn.Linear(hidden, d_model)
self.pos_mlp = nn.Sequential(
nn.Linear(4, 128), nn.SiLU(), nn.Linear(128, d_model))
self.cam_emb = nn.Parameter(torch.zeros(n_cameras, d_model))
nn.init.zeros_(self.id_proj.weight)
nn.init.zeros_(self.id_proj.bias)
nn.init.zeros_(self.pos_mlp[-1].weight)
nn.init.zeros_(self.pos_mlp[-1].bias)
def forward(self, corr_out: dict) -> torch.Tensor:
"""Returns object tokens [B, 4, 512]: per camera an
identity token then a position token."""
f_cam = corr_out["target_features_cam"] # [B,C,256]
mu = corr_out["mu"] # [B,C,2]
pi = corr_out["pi"] # [B,C,64]
conf = corr_out["confidence"] # [B,1]
b, c = f_cam.shape[0], f_cam.shape[1]
peak = pi.max(dim=-1).values # [B,C]
pos_in = torch.cat([mu, peak.unsqueeze(-1),
conf.expand(-1, c).unsqueeze(-1)], dim=-1)
id_tok = self.id_proj(f_cam) + self.cam_emb[None]
pos_tok = self.pos_mlp(pos_in) + self.cam_emb[None]
tokens = torch.stack([id_tok, pos_tok], dim=2) # [B,C,2,512]
return tokens.reshape(b, c * 2, -1)
class TraceVLA3(nn.Module):
def __init__(self, cfg, load_anchor: bool = True):
super().__init__()
self.cfg = cfg
self.v1 = TraceVLA(cfg, load_anchor=load_anchor)
self.binding = RegionBindingV3(vision_width=cfg.vision_width)
self.correspondence = CorrespondenceV3(
vision_width=cfg.vision_width, d_model=cfg.d_model,
n_cameras=cfg.n_live_cameras, pooled_grid=cfg.pooled_grid)
self.adapter = ObjectTokenAdapter(
d_model=cfg.d_model, n_cameras=cfg.n_live_cameras)
def object_condition(self, demo_pooled, frame_mask, pooled_live,
cand_mask, cand_valid=None):
e_demo = self.binding(demo_pooled, frame_mask)
corr = self.correspondence(e_demo["target_embedding"],
pooled_live, cand_mask, cand_valid)
tokens = self.adapter(corr)
return tokens, e_demo, corr
def compute_context_v3(self, pooled_live, text_emb, state_norm,
history, demo_pooled=None, frame_mask=None,
frame_times=None, cand_mask=None,
cand_valid=None, extra_windows=None):
"""v1 SYNC context with the object tokens (and, for R6,
any extra token windows) inside the SYNC memory set."""
demo_memory = window_mask = None
if demo_pooled is not None:
demo_memory, window_mask, _ = self.v1.encode_demo(
demo_pooled, frame_mask, frame_times)
aux = None
windows = []
if demo_pooled is not None and cand_mask is not None:
tokens, e_demo, corr = self.object_condition(
demo_pooled, frame_mask, pooled_live, cand_mask,
cand_valid)
windows.append(tokens.unsqueeze(1)) # [B,1,4,512]
aux = {"binding": e_demo, "correspondence": corr,
"object_tokens": tokens}
if extra_windows is not None:
windows.extend(extra_windows)
if windows and demo_memory is not None:
demo_memory = torch.cat([demo_memory] + windows, dim=1)
ones = torch.ones(window_mask.shape[0], len(windows),
dtype=torch.bool,
device=window_mask.device)
window_mask = torch.cat([window_mask, ones], dim=1)
context = self.v1.compute_context(
pooled_live, text_emb, state_norm, history,
demo_memory=demo_memory, window_mask=window_mask)
return context, aux
def sample_actions(self, context, batch_size, device, generator=None):
return self.v1.sample_actions(context, batch_size, device,
generator)
|