Download code/experiments/lerobot/check_multisample.py from herooooooooo/repro-generative-control-as-optimization: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/herooooooooo/repro-generative-control-as-optimization/resolve/main/code/experiments/lerobot/check_multisample.py
- Command line
-
hf download hf://spaces/herooooooooo/repro-generative-control-as-optimization/code/experiments/lerobot/check_multisample.py
-
curl -L -o check_multisample.py https://huggingface.co/spaces/herooooooooo/repro-generative-control-as-optimization/resolve/main/code/experiments/lerobot/check_multisample.py
1.63 kB
| #!/usr/bin/env python3 | |
| """Equivalence check for B5: patched forward with K=1 must equal SmolVLA's stock forward (same noise/time). | |
| Run: CUDA_VISIBLE_DEVICES="" envs/lerobot/.venv/bin/python experiments/lerobot/check_multisample.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.modeling_smolvla import SmolVLAPolicy, VLAFlowMatching # noqa: E402 | |
| torch.manual_seed(0) | |
| pol = SmolVLAPolicy.from_pretrained("lerobot/smolvla_base").eval() | |
| print("dtypes:", sorted({str(p.dtype) for p in pol.parameters()})) | |
| pol = pol.float() # bf16 parts would make the two (mathematically equal) paths round differently | |
| m = pol.model | |
| cfg = 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) | |
| actions = torch.randn(B, cfg.chunk_size, cfg.max_action_dim) | |
| noise = torch.randn_like(actions); time = torch.tensor([0.3, 0.8]) | |
| with torch.no_grad(): | |
| ref = m.forward(images, img_masks, lang, lmask, state, actions, noise=noise, time=time) | |
| V.patch_multisample(1) | |
| new = VLAFlowMatching.forward(m, images, img_masks, lang, lmask, state, actions, noise=noise, time=time) | |
| err = (ref - new).abs().max().item() | |
| print(f"max |stock - patched(K=1)| = {err:.2e} (loss {ref.mean():.5f} vs {new.mean():.5f})") | |
| sys.exit(0 if err < 1e-3 else 1) | |