File size: 2,214 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 | #!/usr/bin/env python3
"""Apply the ARM E patch (MoE-DP-style router entropy loss) to the
sera_vla_v3 package. Same discipline as the earlier patch scripts: fails
loudly if an anchor is missing or already patched. Guarded by
v4_entropy_lambda = 0.0 default -> all existing arms unaffected.
Design fixed in WORKLOG (2026-11-18, D@20k verdict entry)."""
import pathlib, sys
ROOT = pathlib.Path.home() / "lerobot/src/lerobot/policies/sera_vla_v3"
def patch(fname, old, new, count=1):
p = ROOT / fname
text = p.read_text()
if new in text:
print(f"SKIP {fname}: already patched")
return
if text.count(old) != count:
print(f"FAIL {fname}: anchor found {text.count(old)} times (expected {count})")
sys.exit(1)
p.write_text(text.replace(old, new))
print(f"OK {fname}")
# --- 1. configuration: v4_entropy_lambda field + validation ---
patch(
"configuration_sera_vla_v3.py",
" v4_align_lambda: float = 0.0\n",
" v4_align_lambda: float = 0.0\n v4_entropy_lambda: float = 0.0\n",
)
patch(
"configuration_sera_vla_v3.py",
''' if self.v4_align_lambda < 0:
raise ValueError(f"Invalid v4_align_lambda: {self.v4_align_lambda}")''',
''' if self.v4_align_lambda < 0:
raise ValueError(f"Invalid v4_align_lambda: {self.v4_align_lambda}")
if self.v4_entropy_lambda < 0:
raise ValueError(f"Invalid v4_entropy_lambda: {self.v4_entropy_lambda}")''',
)
# --- 2. modeling_base.py: entropy term in the policy forward ---
patch(
"modeling_base.py",
''' metrics["align_loss"] = align.item()
return total, metrics
''',
''' metrics["align_loss"] = align.item()
entropy_lambda = getattr(self.config, "v4_entropy_lambda", 0.0)
if entropy_lambda > 0:
ent = torch.stack(
[
-(p.float() * p.float().clamp_min(1e-8).log()).sum(-1).mean()
for p in probabilities
]
).mean()
total = total + entropy_lambda * ent
metrics["router_entropy_mean"] = ent.item()
return total, metrics
''',
)
print("ALL ENTROPY PATCHES DONE")
|