trace-vla-code / code /sera_v4 /tools /apply_regdrop_patch.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
11.5 kB
#!/usr/bin/env python3
"""REG-DROPOUT patch for the l30k JEDI-lite retrain (2026-11-20).
Adds ONE config flag, `v4_reg_dropout` (default 0.0), wired into the
attention + FFN of the TRAINABLE blocks only:
- modeling_base.py: FusionBlock (attn x3 + FFN), MoEBlock
(attn x2 + SwiGLU FFN), SwiGLUExpert (dropout on the hidden
activations feeding `down`; the returned z stays clean because
the specialization loss consumes z).
- modeling_sera_vla_v3.py: ARMoEBlock, DenseBlock (same shell),
DemoPerceiverBlock (demo reader + JEDI predictor blocks).
- configuration_sera_vla_v3.py: the flag + range validation.
The frozen TIPSv2 backbone is NOT touched. With the default 0.0,
nn.Dropout(0.0) and MultiheadAttention(dropout=0.0) are exact
identities and add no parameters, so every old run/checkpoint is
bit-identical and state-dict compatible.
Backups: <file>.bak-regdrop-20261120 next to each edited file.
Every replacement is an exact-match with a count==1 assert; a file
is written only after all of its replacements succeed in memory.
"""
import py_compile
import shutil
import sys
from pathlib import Path
SRC = Path.home() / "lerobot/src/lerobot/policies/sera_vla_v3"
BAK_SUFFIX = ".bak-regdrop-20261120"
EDITS = {
"configuration_sera_vla_v3.py": [
(
""" v4_jedi_distractor_k: int = 2
v4_flow_steps: int = 4
""",
""" v4_jedi_distractor_k: int = 2
v4_reg_dropout: float = 0.0
v4_flow_steps: int = 4
""",
),
(
""" if not 0.0 <= self.v4_demo_dropout <= 1.0:
raise ValueError(f"Invalid v4_demo_dropout: {self.v4_demo_dropout}")
""",
""" if not 0.0 <= self.v4_demo_dropout <= 1.0:
raise ValueError(f"Invalid v4_demo_dropout: {self.v4_demo_dropout}")
if not 0.0 <= self.v4_reg_dropout < 1.0:
raise ValueError(f"Invalid v4_reg_dropout: {self.v4_reg_dropout}")
""",
),
],
"modeling_base.py": [
(
"""class FusionBlock(nn.Module):
def __init__(self) -> None:
super().__init__()
self.norms = nn.ModuleList([nn.LayerNorm(512, eps=1e-5) for _ in range(4)])
self.text_attn = nn.MultiheadAttention(512, 8, dropout=0.0, bias=True, batch_first=True)
self.vision_attn = nn.MultiheadAttention(512, 8, dropout=0.0, bias=True, batch_first=True)
self.self_attn = nn.MultiheadAttention(512, 8, dropout=0.0, bias=True, batch_first=True)
self.ffn = nn.Sequential(nn.Linear(512, 2048), nn.GELU(), nn.Linear(2048, 512))
""",
"""class FusionBlock(nn.Module):
def __init__(self, dropout: float = 0.0) -> None:
super().__init__()
self.norms = nn.ModuleList([nn.LayerNorm(512, eps=1e-5) for _ in range(4)])
self.text_attn = nn.MultiheadAttention(512, 8, dropout=dropout, bias=True, batch_first=True)
self.vision_attn = nn.MultiheadAttention(512, 8, dropout=dropout, bias=True, batch_first=True)
self.self_attn = nn.MultiheadAttention(512, 8, dropout=dropout, bias=True, batch_first=True)
self.ffn = nn.Sequential(nn.Linear(512, 2048), nn.GELU(), nn.Dropout(dropout), nn.Linear(2048, 512))
""",
),
(
"""class SwiGLUExpert(nn.Module):
def __init__(self, width: int) -> None:
super().__init__()
self.gate = nn.Linear(512, width, bias=False)
self.up = nn.Linear(512, width, bias=False)
self.down = nn.Linear(width, 512, bias=False)
def forward(self, x: Tensor) -> tuple[Tensor, Tensor]:
z = F.silu(self.gate(x)) * self.up(x)
return self.down(z), z
""",
"""class SwiGLUExpert(nn.Module):
def __init__(self, width: int, dropout: float = 0.0) -> None:
super().__init__()
self.gate = nn.Linear(512, width, bias=False)
self.up = nn.Linear(512, width, bias=False)
self.down = nn.Linear(width, 512, bias=False)
self.drop = nn.Dropout(dropout)
def forward(self, x: Tensor) -> tuple[Tensor, Tensor]:
z = F.silu(self.gate(x)) * self.up(x)
return self.down(self.drop(z)), z
""",
),
(
"""class MoEBlock(nn.Module):
def __init__(self) -> None:
super().__init__()
self.norms = nn.ModuleList([nn.LayerNorm(512, eps=1e-5) for _ in range(3)])
self.self_attn = nn.MultiheadAttention(512, 8, dropout=0.0, bias=True, batch_first=True)
self.cross_attn = nn.MultiheadAttention(512, 8, dropout=0.0, bias=True, batch_first=True)
self.shared = SwiGLUExpert(1344)
self.experts = nn.ModuleList(SwiGLUExpert(672) for _ in range(8))
""",
"""class MoEBlock(nn.Module):
def __init__(self, dropout: float = 0.0) -> None:
super().__init__()
self.norms = nn.ModuleList([nn.LayerNorm(512, eps=1e-5) for _ in range(3)])
self.self_attn = nn.MultiheadAttention(512, 8, dropout=dropout, bias=True, batch_first=True)
self.cross_attn = nn.MultiheadAttention(512, 8, dropout=dropout, bias=True, batch_first=True)
self.shared = SwiGLUExpert(1344, dropout)
self.experts = nn.ModuleList(SwiGLUExpert(672, dropout) for _ in range(8))
""",
),
(
""" self.config = config
self.tips = AutoModel.from_pretrained(
""",
""" self.config = config
_reg_drop = float(getattr(config, "v4_reg_dropout", 0.0))
self.tips = AutoModel.from_pretrained(
""",
),
(
""" self.fusion = nn.ModuleList(FusionBlock() for _ in range(3))
""",
""" self.fusion = nn.ModuleList(FusionBlock(dropout=_reg_drop) for _ in range(3))
""",
),
(
""" self.blocks = nn.ModuleList(MoEBlock() for _ in range(4))
""",
""" self.blocks = nn.ModuleList(MoEBlock(dropout=_reg_drop) for _ in range(4))
""",
),
],
"modeling_sera_vla_v3.py": [
(
""" def __init__(self) -> None:
super().__init__()
self.norms = nn.ModuleList([nn.LayerNorm(512, eps=1e-5) for _ in range(3)])
self.self_attn = nn.MultiheadAttention(512, 8, dropout=0.0, bias=True, batch_first=True)
self.cross_attn = nn.MultiheadAttention(512, 8, dropout=0.0, bias=True, batch_first=True)
self.shared = SwiGLUExpert(1344)
self.experts = nn.ModuleList(SwiGLUExpert(672) for _ in range(8))
def forward(
self, x: Tensor, context: Tensor, indices: Tensor, weights: Tensor,
key_padding: Tensor | None,
) -> tuple[Tensor, Tensor | None]:
""",
""" def __init__(self, dropout: float = 0.0) -> None:
super().__init__()
self.norms = nn.ModuleList([nn.LayerNorm(512, eps=1e-5) for _ in range(3)])
self.self_attn = nn.MultiheadAttention(512, 8, dropout=dropout, bias=True, batch_first=True)
self.cross_attn = nn.MultiheadAttention(512, 8, dropout=dropout, bias=True, batch_first=True)
self.shared = SwiGLUExpert(1344, dropout)
self.experts = nn.ModuleList(SwiGLUExpert(672, dropout) for _ in range(8))
def forward(
self, x: Tensor, context: Tensor, indices: Tensor, weights: Tensor,
key_padding: Tensor | None,
) -> tuple[Tensor, Tensor | None]:
""",
),
(
""" def __init__(self) -> None:
super().__init__()
self.norms = nn.ModuleList([nn.LayerNorm(512, eps=1e-5) for _ in range(3)])
self.self_attn = nn.MultiheadAttention(512, 8, dropout=0.0, bias=True, batch_first=True)
self.cross_attn = nn.MultiheadAttention(512, 8, dropout=0.0, bias=True, batch_first=True)
self.shared = SwiGLUExpert(1344)
self.dense = SwiGLUExpert(2688)
""",
""" def __init__(self, dropout: float = 0.0) -> None:
super().__init__()
self.norms = nn.ModuleList([nn.LayerNorm(512, eps=1e-5) for _ in range(3)])
self.self_attn = nn.MultiheadAttention(512, 8, dropout=dropout, bias=True, batch_first=True)
self.cross_attn = nn.MultiheadAttention(512, 8, dropout=dropout, bias=True, batch_first=True)
self.shared = SwiGLUExpert(1344, dropout)
self.dense = SwiGLUExpert(2688, dropout)
""",
),
(
""" def __init__(self) -> None:
super().__init__()
self.norm_q = nn.LayerNorm(512, eps=1e-5)
self.norm_kv = nn.LayerNorm(512, eps=1e-5)
self.cross_attn = nn.MultiheadAttention(512, 8, dropout=0.0, bias=True, batch_first=True)
self.norm_s = nn.LayerNorm(512, eps=1e-5)
self.self_attn = nn.MultiheadAttention(512, 8, dropout=0.0, bias=True, batch_first=True)
self.norm_f = nn.LayerNorm(512, eps=1e-5)
self.ffn = nn.Sequential(nn.Linear(512, 2048), nn.GELU(), nn.Linear(2048, 512))
""",
""" def __init__(self, dropout: float = 0.0) -> None:
super().__init__()
self.norm_q = nn.LayerNorm(512, eps=1e-5)
self.norm_kv = nn.LayerNorm(512, eps=1e-5)
self.cross_attn = nn.MultiheadAttention(512, 8, dropout=dropout, bias=True, batch_first=True)
self.norm_s = nn.LayerNorm(512, eps=1e-5)
self.self_attn = nn.MultiheadAttention(512, 8, dropout=dropout, bias=True, batch_first=True)
self.norm_f = nn.LayerNorm(512, eps=1e-5)
self.ffn = nn.Sequential(nn.Linear(512, 2048), nn.GELU(), nn.Dropout(dropout), nn.Linear(2048, 512))
""",
),
(
""" super().__init__(config)
self.goal = GroundedGoal()
""",
""" super().__init__(config)
_reg_drop = float(getattr(config, "v4_reg_dropout", 0.0))
self.goal = GroundedGoal()
""",
),
(
""" self.ar_blocks = nn.ModuleList(ARMoEBlock() for _ in range(4))
""",
""" self.ar_blocks = nn.ModuleList(ARMoEBlock(dropout=_reg_drop) for _ in range(4))
""",
),
(
""" self.blocks = nn.ModuleList(DenseBlock() for _ in range(4))
""",
""" self.blocks = nn.ModuleList(DenseBlock(dropout=_reg_drop) for _ in range(4))
""",
),
(
""" self.demo_blocks = nn.ModuleList(DemoPerceiverBlock() for _ in range(2))
""",
""" self.demo_blocks = nn.ModuleList(DemoPerceiverBlock(dropout=_reg_drop) for _ in range(2))
""",
),
(
""" self.demo_pred_blocks = nn.ModuleList(
DemoPerceiverBlock() for _ in range(2))
""",
""" self.demo_pred_blocks = nn.ModuleList(
DemoPerceiverBlock(dropout=_reg_drop) for _ in range(2))
""",
),
],
}
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 PATCH DONE")
return 0
if __name__ == "__main__":
sys.exit(main())