File size: 2,919 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
#!/usr/bin/env python3
"""REG-DROPOUT FIX 1 (2026-11-20): key-compatible FFN dropout.

The first regdrop patch inserted nn.Dropout INTO the nn.Sequential
FFNs of FusionBlock and DemoPerceiverBlock, shifting the second
Linear from index 2 to index 3 and renaming its state-dict keys
(ffn.2.* -> ffn.3.*). check_jedi caught it: init checkpoints no
longer load. This fix restores the original Sequential layout
(Linear, GELU, Linear) and applies dropout through a standalone
`ffn_drop` attribute between the GELU and the second Linear, so
every state-dict key is byte-identical to the pre-patch layout.
(SwiGLUExpert was already key-safe: its `drop` is standalone.)

Backups: <file>.bak-regdropfix1-20261120.
"""
import py_compile
import shutil
import sys
from pathlib import Path

SRC = Path.home() / "lerobot/src/lerobot/policies/sera_vla_v3"
BAK_SUFFIX = ".bak-regdropfix1-20261120"

EDITS = {
    "modeling_base.py": [
        (
            """        self.ffn = nn.Sequential(nn.Linear(512, 2048), nn.GELU(), nn.Dropout(dropout), nn.Linear(2048, 512))
""",
            """        self.ffn = nn.Sequential(nn.Linear(512, 2048), nn.GELU(), nn.Linear(2048, 512))
        self.ffn_drop = nn.Dropout(dropout)
""",
        ),
        (
            """        return q + self.ffn(self.norms[3](q))
""",
            """        h = self.norms[3](q)
        h = self.ffn[2](self.ffn_drop(self.ffn[1](self.ffn[0](h))))
        return q + h
""",
        ),
    ],
    "modeling_sera_vla_v3.py": [
        (
            """        self.ffn = nn.Sequential(nn.Linear(512, 2048), nn.GELU(), nn.Dropout(dropout), nn.Linear(2048, 512))
""",
            """        self.ffn = nn.Sequential(nn.Linear(512, 2048), nn.GELU(), nn.Linear(2048, 512))
        self.ffn_drop = nn.Dropout(dropout)
""",
        ),
        (
            """        q = q + self.ffn(self.norm_f(q))
        return q
""",
            """        h = self.norm_f(q)
        h = self.ffn[2](self.ffn_drop(self.ffn[1](self.ffn[0](h))))
        q = q + h
        return q
""",
        ),
    ],
}


def main() -> int:
    for fname, edits in EDITS.items():
        path = SRC / fname
        text = path.read_text()
        for i, (old, new) in enumerate(edits):
            if new in text and old not in text:
                print(f"{fname} edit {i}: already applied, skipping")
                continue
            count = text.count(old)
            assert count == 1, f"{fname} edit {i}: anchor found {count} times"
            text = text.replace(old, new)
        bak = path.with_name(path.name + BAK_SUFFIX)
        if not bak.exists():
            shutil.copy2(path, bak)
            print(f"backup: {bak.name}")
        path.write_text(text)
        py_compile.compile(str(path), doraise=True)
        print(f"{fname}: {len(edits)} edits applied, compiles OK")
    print("REG-DROPOUT FIX1 DONE")
    return 0


if __name__ == "__main__":
    sys.exit(main())