trace-vla-code / code /sera_v4 /tools /apply_demo_patch2.py
phammminhhieu's picture
TRACE-VLA code snapshot 2026-11-22 11:47 +07: 456 canonical files + SHA256 manifest + sync report
b4396f0 verified
Raw History Blame Contribute Delete
4.04 kB
#!/usr/bin/env python3
"""FEW-SHOT PHASE 1 patch #2: E3 ablation support. Every change
is defaults-preserving (verified by re-running check_demo.py):
- config gains v4_flow_steps (default 4); predict_action_chunk
reads it (tau = step/steps, dt = 1/steps — at 4 this is the
exact current arithmetic);
- compute_demo_slots uses config v4_demo_slots queries: the
first k learned queries (k <= 24), or the query set tiled when
k > 24 (the 32-slot E3 cell; duplicated queries produce
duplicated slots — recorded as the cell's caveat in WORKLOG);
- compute_demo_slots honours model.demo_content_mode
("full" default; "video" zeroes the action/state tokens,
"action" zeroes the vision tokens, sequence length and
positions unchanged) for the E3 content ablation.
"""
from pathlib import Path
PKG = Path.home() / "lerobot/src/lerobot/policies/sera_vla_v3"
def patch(path: Path, pairs: list[tuple[str, str]]) -> None:
text = path.read_text()
for old, new in pairs:
n = text.count(old)
assert n == 1, f"{path.name}: anchor found {n}x: {old[:70]!r}"
text = text.replace(old, new)
path.write_text(text)
print(f"patched {path.name}: {len(pairs)} replacements")
patch(PKG / "configuration_sera_vla_v3.py", [
(
" v4_demo_frames: int = 64\n",
" v4_demo_frames: int = 64\n"
" v4_flow_steps: int = 4\n",
),
(
" if self.v4_demo_frames <= 0:\n"
" raise ValueError(f\"Invalid v4_demo_frames: {self.v4_demo_frames}\")\n",
" if self.v4_demo_frames <= 0:\n"
" raise ValueError(f\"Invalid v4_demo_frames: {self.v4_demo_frames}\")\n"
" if self.v4_flow_steps <= 0:\n"
" raise ValueError(f\"Invalid v4_flow_steps: {self.v4_flow_steps}\")\n",
),
])
patch(PKG / "modeling_sera_vla_v3.py", [
(
" self._demo_store: dict | None = None\n",
" self._demo_store: dict | None = None\n"
" self.demo_content_mode = \"full\"\n",
),
(
" seq = torch.cat([vtok, atok], dim=0) # [13F, 512]\n",
" mode = getattr(self, \"demo_content_mode\", \"full\")\n"
" if mode == \"video\":\n"
" atok = torch.zeros_like(atok)\n"
" elif mode == \"action\":\n"
" vtok = torch.zeros_like(vtok)\n"
" seq = torch.cat([vtok, atok], dim=0) # [13F, 512]\n",
),
(
" q = self.demo_queries.unsqueeze(0).expand(len(flat), -1, -1)\n",
" if n_slots <= self.demo_queries.shape[0]:\n"
" queries = self.demo_queries[:n_slots]\n"
" else:\n"
" reps = -(-n_slots // self.demo_queries.shape[0])\n"
" queries = self.demo_queries.repeat(reps, 1)[:n_slots]\n"
" q = queries.unsqueeze(0).expand(len(flat), -1, -1)\n",
),
])
patch(PKG / "modeling_base.py", [
(
" x = torch.randn(images.shape[0], 16, self.config.action_dim, device=images.device, dtype=state.dtype)\n"
" for step in range(4):\n"
" tau = torch.full((images.shape[0],), step / 4, device=images.device, dtype=state.dtype)\n"
" velocity, _ = self.model.velocity_prediction(x, tau, context, state, indices, weights)\n"
" x = x + 0.25 * velocity\n",
" x = torch.randn(images.shape[0], 16, self.config.action_dim, device=images.device, dtype=state.dtype)\n"
" n_steps = int(getattr(self.config, \"v4_flow_steps\", 4))\n"
" for step in range(n_steps):\n"
" tau = torch.full((images.shape[0],), step / n_steps, device=images.device, dtype=state.dtype)\n"
" velocity, _ = self.model.velocity_prediction(x, tau, context, state, indices, weights)\n"
" x = x + (1.0 / n_steps) * velocity\n",
),
])
print("demo patch #2 applied")