trace-vla-code / code /trace_vla /pkg /model_v2.py
phammminhhieu's picture
TRACE-VLA code snapshot 2026-11-22 11:47 +07: 456 canonical files + SHA256 manifest + sync report (part 2)
ae1ed83 verified
Raw History Blame Contribute Delete
5.01 kB
"""TRACE-VLA v2 core assembly (spec v2.0, sections III and VII).
Keeps the v1 model (ANCHOR + Ordered Raw Memory + SYNC + NEXUS +
FLUX) completely intact and adds, alongside it:
- DemoObjectBinding (binding.py): demo -> target object embedding.
- DemoToLiveCorrespondence (correspondence.py): target embedding +
live patches + candidate masks -> matched target features.
- ObjectConditionedAdapter: fuses C_object = {target_embedding,
live_target_features, spatial_heatmap, confidence} into ONE
512-dim conditioning vector that is ADDED to every SYNC context
token before NEXUS.
Zero-condition guarantee: the adapter's final linear layer is
zero-initialized (weights and bias), so at initialization the
conditioning vector is exactly zero and the v2 context is bitwise
identical to the v1 context, whether or not C_object is supplied.
When no demo / no candidates are supplied, the v2 path is skipped
entirely and the call is the pure v1 path. v1 weights are never
modified by this wrapper.
Parameter budget (spec XXII): adapter / goal projection 1-3M.
"""
import torch
import torch.nn as nn
from .binding import DemoObjectBinding
from .correspondence import DemoToLiveCorrespondence
from .model import TraceVLA
class ObjectConditionedAdapter(nn.Module):
def __init__(self, d_model: int = 512, hidden: int = 256,
n_cameras: int = 2, pooled_grid: int = 8):
super().__init__()
heat_n = n_cameras * pooled_grid * pooled_grid
self.heat_proj = nn.Linear(heat_n, 128)
in_dim = hidden + d_model + 128 + 1
self.mlp = nn.Sequential(
nn.Linear(in_dim, 768), nn.SiLU(),
nn.Linear(768, d_model))
# Zero-init the output layer: C_object contributes exactly
# nothing until training moves it (see module docstring).
nn.init.zeros_(self.mlp[-1].weight)
nn.init.zeros_(self.mlp[-1].bias)
def forward(self, target_embedding: torch.Tensor,
target_features_512: torch.Tensor,
heatmap: torch.Tensor,
confidence: torch.Tensor) -> torch.Tensor:
"""All inputs describe the SAME matched target. Returns the
conditioning vector [B, 512] added to the SYNC context."""
heat = self.heat_proj(heatmap.flatten(1)) # [B,128]
x = torch.cat([target_embedding, target_features_512,
heat, confidence], dim=-1)
return self.mlp(x)
class TraceVLA2(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 = DemoObjectBinding(vision_width=cfg.vision_width)
self.correspondence = DemoToLiveCorrespondence(
vision_width=cfg.vision_width, d_model=cfg.d_model,
n_cameras=cfg.n_live_cameras, pooled_grid=cfg.pooled_grid)
self.adapter = ObjectConditionedAdapter(
d_model=cfg.d_model, n_cameras=cfg.n_live_cameras,
pooled_grid=cfg.pooled_grid)
def object_condition(self, demo_pooled, frame_mask, pooled_live,
cand_mask, cand_valid=None):
"""Run Binding + Correspondence. Returns (cond [B, 512],
binding_out, corr_out)."""
e_demo = self.binding(demo_pooled, frame_mask)
corr = self.correspondence(e_demo["target_embedding"],
pooled_live, cand_mask, cand_valid)
cond = self.adapter(e_demo["target_embedding"],
corr["target_features_512"],
corr["heatmap"], corr["confidence"])
return cond, e_demo, corr
def compute_context_v2(self, pooled_live, text_emb, state_norm,
history, demo_pooled=None, frame_mask=None,
frame_times=None, cand_mask=None,
cand_valid=None):
"""v1 SYNC context, plus the object-conditioning residual
when a demo and candidate regions are available."""
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)
context = self.v1.compute_context(
pooled_live, text_emb, state_norm, history,
demo_memory=demo_memory, window_mask=window_mask)
aux = None
if demo_pooled is not None and cand_mask is not None:
cond, e_demo, corr = self.object_condition(
demo_pooled, frame_mask, pooled_live, cand_mask,
cand_valid)
context = context + cond.unsqueeze(1)
aux = {"binding": e_demo, "correspondence": corr,
"cond": cond}
return context, aux
def sample_actions(self, context, batch_size, device, generator=None):
return self.v1.sample_actions(context, batch_size, device,
generator)