Download code/experiments/lerobot/check_geco.py from herooooooooo/repro-generative-control-as-optimization: direct link, hf CLI and curl.
- Browser
- Download file 3.24 kB
-
https://huggingface.co/spaces/herooooooooo/repro-generative-control-as-optimization/resolve/main/code/experiments/lerobot/check_geco.py
- Command line
-
hf download hf://spaces/herooooooooo/repro-generative-control-as-optimization/code/experiments/lerobot/check_geco.py
-
curl -L -o check_geco.py https://huggingface.co/spaces/herooooooooo/repro-generative-control-as-optimization/resolve/main/code/experiments/lerobot/check_geco.py
3.24 kB
| #!/usr/bin/env python3 | |
| """CPU checks for the GeCO reproduction patches (Part G) on the real smolvla_base weights. | |
| 1. patch_fm_score: the patched sampler returns exactly SmolVLA's stock Euler chunk (same noise), plus scores. | |
| 2. patch_geco training: γ = 1 - t mapping, target (ε - a) c(γ), loss finite, gradients reach the action expert. | |
| At t = 0 (γ = 1) the target is exactly zero, so the loss is ‖f‖² of the model output. | |
| 3. patch_geco inference: runs at most k_max steps, records one score per planning call and episode. | |
| Run: CUDA_VISIBLE_DEVICES="" envs/lerobot/.venv/bin/python experiments/lerobot/check_geco.py""" | |
| import importlib.util | |
| import sys | |
| from pathlib import Path | |
| import torch | |
| spec = importlib.util.spec_from_file_location("variants", Path(__file__).with_name("variants.py")) | |
| V = importlib.util.module_from_spec(spec); spec.loader.exec_module(V) | |
| from lerobot.policies.smolvla import modeling_smolvla as S # noqa: E402 | |
| torch.manual_seed(0) | |
| pol = S.SmolVLAPolicy.from_pretrained("lerobot/smolvla_base").eval().float() | |
| m, cfg = pol.model, pol.config | |
| B = 2 | |
| images = [torch.rand(B, 3, 512, 512) * 2 - 1] | |
| img_masks = [torch.ones(B, dtype=torch.bool)] | |
| lang = torch.randint(0, 1000, (B, 12)); lmask = torch.ones(B, 12, dtype=torch.bool) | |
| state = torch.randn(B, cfg.max_state_dim) | |
| noise = torch.randn(B, cfg.chunk_size, cfg.max_action_dim) | |
| ok = True | |
| with torch.no_grad(): | |
| ref = S.VLAFlowMatching.sample_actions(m, images, img_masks, lang, lmask, state, noise=noise.clone()) | |
| fst = V.patch_fm_score() | |
| new = S.VLAFlowMatching.sample_actions(m, images, img_masks, lang, lmask, state, noise=noise.clone()) | |
| err = (ref - new).abs().max().item() | |
| print(f"[1] fm_score sampler vs stock: max abs diff {err:.2e}; scores {fst['calls']}") | |
| ok &= err < 1e-3 and len(fst["calls"]) == B and all(c[1] > 0 for c in fst["calls"]) | |
| gst = V.patch_geco(k_max=10) | |
| actions = torch.randn(B, cfg.chunk_size, cfg.max_action_dim) | |
| loss = S.VLAFlowMatching.forward(m, images, img_masks, lang, lmask, state, actions, noise=noise, | |
| time=torch.tensor([0.3, 0.8])) | |
| loss.mean().backward() | |
| g = sum(p.grad.abs().sum().item() for n, p in m.named_parameters() if p.grad is not None and "lm_expert" in n) | |
| print(f"[2] GeCO loss {loss.mean().item():.4f}, expert grad L1 {g:.3e}") | |
| ok &= torch.isfinite(loss).all().item() and g > 0 | |
| with torch.no_grad(): | |
| l0 = S.VLAFlowMatching.forward(m, images, img_masks, lang, lmask, state, actions, noise=noise, | |
| time=torch.zeros(B)) # γ = 1: target exactly 0 | |
| pp, pkv = V._prefix_cache(m, S, images, img_masks, lang, lmask, state) | |
| f = m.denoise_step(pp, pkv, actions, torch.zeros(B)) | |
| d = (l0 - f ** 2).abs().max().item() | |
| print(f"[2b] at γ=1 loss == ‖f(a)‖² elementwise: max abs diff {d:.2e}") | |
| ok &= d < 1e-3 | |
| with torch.no_grad(): | |
| pol.reset() | |
| x = S.VLAFlowMatching.sample_actions(m, images, img_masks, lang, lmask, state) | |
| print(f"[3] GeCO sample: shape {tuple(x.shape)}, calls {gst['calls']}") | |
| ok &= tuple(x.shape) == (B, cfg.chunk_size, cfg.max_action_dim) and len(gst["calls"]) == B | |
| ok &= all(0 <= c[2] <= 10 for c in gst["calls"]) and gst["calls"][0][0] == 0 | |
| print("PASS" if ok else "FAIL") | |
| sys.exit(0 if ok else 1) | |