trace-vla-code / code /sera_v4 /tools /apply_entropy_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
2.21 kB
#!/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")