File size: 1,626 Bytes
b26bdfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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)