TRACE-VLA code snapshot 2026-11-22 11:47 +07: 456 canonical files + SHA256 manifest + sync report
b4396f0 verified Download code/sera_v4/tools/apply_frontend_patches.py from phammminhhieu/trace-vla-code: direct link, hf CLI and curl.
- Browser
- Download file 5.1 kB
-
https://huggingface.co/phammminhhieu/trace-vla-code/resolve/main/code/sera_v4/tools/apply_frontend_patches.py
- Command line
-
hf download hf://phammminhhieu/trace-vla-code/code/sera_v4/tools/apply_frontend_patches.py
-
curl -L -o apply_frontend_patches.py https://huggingface.co/phammminhhieu/trace-vla-code/resolve/main/code/sera_v4/tools/apply_frontend_patches.py
5.1 kB
| #!/usr/bin/env python3 | |
| """Apply SERA-v4 FRONTEND probe patches (F2 alignment loss) to the | |
| sera_vla_v3 package. Same discipline as apply_v4_patches.py: fails loudly | |
| if an anchor is missing or already patched. F0/F1 are unaffected | |
| (v4_align_lambda defaults to 0.0 = off). Spec: frontend_probe/SPEC.md.""" | |
| 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_sera_vla_v3.py: v4_align_lambda field + validation --- | |
| patch( | |
| "configuration_sera_vla_v3.py", | |
| ' v4_moe_mode: str = "control"\n', | |
| ' v4_moe_mode: str = "control"\n v4_align_lambda: float = 0.0\n', | |
| ) | |
| patch( | |
| "configuration_sera_vla_v3.py", | |
| ''' if self.v4_moe_mode not in {"control", "nobal", "bias", "skillroute"}: | |
| raise ValueError(f"Invalid v4_moe_mode: {self.v4_moe_mode}")''', | |
| ''' if self.v4_moe_mode not in {"control", "nobal", "bias", "skillroute"}: | |
| raise ValueError(f"Invalid v4_moe_mode: {self.v4_moe_mode}") | |
| if self.v4_align_lambda < 0: | |
| raise ValueError(f"Invalid v4_align_lambda: {self.v4_align_lambda}")''', | |
| ) | |
| # --- 2. modeling_sera_vla_v3.py: stash vision tokens + pooled text --- | |
| patch( | |
| "modeling_sera_vla_v3.py", | |
| """ self.last_router_probabilities: list[Tensor] = [] | |
| self.last_router_indices: list[Tensor] = [] | |
| """, | |
| """ self.last_router_probabilities: list[Tensor] = [] | |
| self.last_router_indices: list[Tensor] = [] | |
| self.last_vision_tokens: Tensor | None = None | |
| self.last_text_pooled: Tensor | None = None | |
| """, | |
| ) | |
| patch( | |
| "modeling_sera_vla_v3.py", | |
| """ ids, padding = self._tokenize(instructions, images.device) | |
| text = self.text_norm(self.text_projection(self._encode_text_tokens(ids, padding))) | |
| """, | |
| """ ids, padding = self._tokenize(instructions, images.device) | |
| text = self.text_norm(self.text_projection(self._encode_text_tokens(ids, padding))) | |
| if getattr(self.config, "v4_align_lambda", 0.0) > 0: | |
| align_mask = (~padding).to(text.dtype) | |
| self.last_text_pooled = (text * align_mask[..., None]).sum(1) / align_mask.sum( | |
| 1, keepdim=True | |
| ).clamp_min(1.0) | |
| self.last_vision_tokens = vision | |
| """, | |
| ) | |
| # --- 3. modeling_base.py: MIL-InfoNCE loss + forward hook --- | |
| patch( | |
| "modeling_base.py", | |
| """ @staticmethod | |
| def _balance_loss(probabilities: list[Tensor], indices: list[Tensor]) -> Tensor:""", | |
| """ @staticmethod | |
| def _patch_text_align_loss(vision: Tensor, text_pooled: Tensor) -> Tensor: | |
| # MIL-InfoNCE (frontend probe F2; spec frontend_probe/SPEC.md): | |
| # pair score = tau_pool * logsumexp over patches of cosine / tau_pool, | |
| # symmetric cross-entropy over the micro-batch with tau_ce = 0.07. | |
| v = F.normalize(vision.float(), dim=-1) | |
| t = F.normalize(text_pooled.float(), dim=-1) | |
| cos = torch.einsum("bpd,qd->bqp", v, t) | |
| scores = 0.1 * torch.logsumexp(cos / 0.1, dim=-1) | |
| targets = torch.arange(scores.shape[0], device=scores.device) | |
| return 0.5 * ( | |
| F.cross_entropy(scores / 0.07, targets) | |
| + F.cross_entropy(scores.t() / 0.07, targets) | |
| ) | |
| @staticmethod | |
| def _balance_loss(probabilities: list[Tensor], indices: list[Tensor]) -> Tensor:""", | |
| ) | |
| patch( | |
| "modeling_base.py", | |
| """ v4_mode = getattr(self.config, "v4_moe_mode", "control") | |
| if v4_mode == "nobal": | |
| total = fm | |
| elif v4_mode == "bias": | |
| total = fm + 0.0002 * specialization | |
| else: | |
| total = fm + 0.01 * balance + 0.0002 * specialization | |
| return total, { | |
| "flow_matching_loss": fm.item(), | |
| "balance_loss": balance.item(), | |
| "specialization_loss": specialization.item(), | |
| } | |
| """, | |
| """ v4_mode = getattr(self.config, "v4_moe_mode", "control") | |
| if v4_mode == "nobal": | |
| total = fm | |
| elif v4_mode == "bias": | |
| total = fm + 0.0002 * specialization | |
| else: | |
| total = fm + 0.01 * balance + 0.0002 * specialization | |
| metrics = { | |
| "flow_matching_loss": fm.item(), | |
| "balance_loss": balance.item(), | |
| "specialization_loss": specialization.item(), | |
| } | |
| align_lambda = getattr(self.config, "v4_align_lambda", 0.0) | |
| if align_lambda > 0: | |
| align = self._patch_text_align_loss( | |
| self.model.last_vision_tokens, self.model.last_text_pooled | |
| ) | |
| total = total + align_lambda * align | |
| metrics["align_loss"] = align.item() | |
| return total, metrics | |
| """, | |
| ) | |
| print("ALL FRONTEND PATCHES DONE") | |