File size: 4,041 Bytes
b4396f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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")