File size: 3,541 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
#!/usr/bin/env python3
"""E1 fix 2: teacher-forcing codes via a model-side table lookup.

Finding (probe_batch_keys.py, 2026-11-18): the training
preprocessor drops unknown per-sample keys, so batch["skill_codes"]
never reaches forward; but episode_index / frame_index DO reach
forward as [B] tensors. The policy therefore looks the codes up
from the precomputed table by (episode_index, frame_index).
"""
from pathlib import Path

PKG = Path.home() / "lerobot/src/lerobot/policies/sera_vla_v3"

# --- modeling_base.py: forward reads codes via the model table ---
p = PKG / "modeling_base.py"
text = p.read_text()
old = (
    "        quest_on = getattr(self.config, \"v4_quest_enabled\", False)\n"
    "        quest_codes = None\n"
    "        if quest_on:\n"
    "            if \"skill_codes\" not in batch:\n"
    "                raise KeyError(\"v4_quest_enabled requires batch['skill_codes']\")\n"
    "            quest_codes = batch[\"skill_codes\"].to(images.device).long()\n"
    "            self.model.set_skill_from_codes(quest_codes)\n"
)
new = (
    "        quest_on = getattr(self.config, \"v4_quest_enabled\", False)\n"
    "        quest_codes = None\n"
    "        if quest_on:\n"
    "            quest_codes = self.model.quest_codes_for(batch)\n"
    "            self.model.set_skill_from_codes(quest_codes)\n"
)
assert text.count(old) == 1, f"base anchor count {text.count(old)}"
p.write_text(text.replace(old, new))
print("patched modeling_base.py (fix2)")

# --- modeling_sera_vla_v3.py: table attribute + lookup method ---
p = PKG / "modeling_sera_vla_v3.py"
text = p.read_text()
old = (
    "        self.last_skill_summary: Tensor | None = None\n"
    "        self.last_skill_codes: Tensor | None = None\n"
)
new = (
    "        self.last_skill_summary: Tensor | None = None\n"
    "        self.last_skill_codes: Tensor | None = None\n"
    "        self._quest_table: Tensor | None = None\n"
)
assert text.count(old) == 1, f"v3 anchor count {text.count(old)}"
text = text.replace(old, new)
old2 = "    def skill_summary(self, codes: Tensor) -> Tensor:"
new2 = (
    "    def quest_codes_for(self, batch: dict) -> Tensor:\n"
    "        \"\"\"Teacher-forcing codes for the batch anchors, looked up\n"
    "        from the precomputed frozen-tokenizer table by\n"
    "        (episode_index, frame_index). The training preprocessor\n"
    "        drops unknown per-sample keys, so the codes cannot ride\n"
    "        in the batch; episode_index/frame_index do reach the\n"
    "        policy forward. Table path overridable via the\n"
    "        SERA_QUEST_CODES env var.\"\"\"\n"
    "        if self._quest_table is None:\n"
    "            import os\n"
    "\n"
    "            import numpy as np\n"
    "\n"
    "            path = os.environ.get(\n"
    "                \"SERA_QUEST_CODES\",\n"
    "                os.path.expanduser(\n"
    "                    \"~/sera_v4_experiments/quest_probe/codes_all.npy\"),\n"
    "            )\n"
    "            self._quest_table = torch.from_numpy(\n"
    "                np.load(path).astype(np.int64))\n"
    "        ep = batch[\"episode_index\"]\n"
    "        fr = batch[\"frame_index\"]\n"
    "        out = self._quest_table[ep.long().cpu(), fr.long().cpu()]\n"
    "        return out.to(ep.device)\n"
    "\n"
    "    def skill_summary(self, codes: Tensor) -> Tensor:"
)
assert text.count(old2) == 1, f"v3 anchor2 count {text.count(old2)}"
p.write_text(text.replace(old2, new2))
print("patched modeling_sera_vla_v3.py (fix2)")