AlexWortega commited on
Commit
1ee3857
·
verified ·
1 Parent(s): afe36fe

Upload tinyvla2/scripts/rekey_checkpoint.py with huggingface_hub

Browse files
tinyvla2/scripts/rekey_checkpoint.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ """Strip torch.compile's `_orig_mod.` prefix from a saved checkpoint.
3
+
4
+ The physical_ai_ft run was launched with `compile: true` and saved the COMPILED
5
+ module, so 167 of its 489 tensors are named `expert._orig_mod.*` while the model
6
+ class expects `expert.*`.
7
+
8
+ This matters more than a normal key mismatch. LeRobot's `PreTrainedPolicy.
9
+ from_pretrained` loads with `strict=False` and merely LOGS missing keys, and
10
+ `FlowMatchingExpert.__init__` zero-initialises `action_out` — so a model loaded
11
+ from the raw checkpoint predicts velocity 0, and `predict_action_chunk` returns
12
+ its initial Gaussian noise unchanged. That is a plausible-looking bad number, not
13
+ a crash: nothing anywhere in the stack tells you the action expert is random.
14
+
15
+ Usage:
16
+ python scripts/rekey_checkpoint.py <src_ckpt_dir> <dst_ckpt_dir>
17
+ """
18
+
19
+ from __future__ import annotations
20
+
21
+ import shutil
22
+ import sys
23
+ from pathlib import Path
24
+
25
+ from safetensors.torch import load_file, save_file
26
+
27
+ PREFIX = "expert._orig_mod."
28
+
29
+
30
+ def main():
31
+ src = Path(sys.argv[1] if len(sys.argv) > 1
32
+ else "/home/alexw/tinyvla_data/b200/outputs/physical_ai_ft/final")
33
+ dst = Path(sys.argv[2] if len(sys.argv) > 2
34
+ else "/home/alexw/tinyvla/outputs/physical_ai_ft_fixed")
35
+
36
+ dst.mkdir(parents=True, exist_ok=True)
37
+ for f in src.iterdir():
38
+ if f.suffix == ".json" or f.name.startswith("README"):
39
+ shutil.copy(f, dst / f.name)
40
+
41
+ sd = load_file(str(src / "model.safetensors"))
42
+ renamed = {k.replace(PREFIX, "expert."): v for k, v in sd.items()}
43
+ n_hit = sum(1 for k in sd if k.startswith(PREFIX))
44
+ assert len(renamed) == len(sd), "rename collided — two keys mapped to one name"
45
+ print(f"renamed {n_hit}/{len(sd)} tensors ({PREFIX} -> expert.)")
46
+
47
+ save_file(renamed, str(dst / "model.safetensors"), metadata={"format": "pt"})
48
+
49
+ # Hard gate: build the model from the checkpoint's own config and demand a
50
+ # STRICT load. from_pretrained would silently accept a missing expert.
51
+ from tinyvla.modeling_tinyvla import TinyVLAPolicy
52
+
53
+ pol = TinyVLAPolicy.from_pretrained(str(dst))
54
+ fresh = load_file(str(dst / "model.safetensors"))
55
+ missing, unexpected = pol.load_state_dict(fresh, strict=True), None
56
+ n_model = len(pol.state_dict())
57
+ aow = pol.expert.action_out.weight.abs().mean().item()
58
+
59
+ print(f"{len(fresh)}/{n_model} tensors, strict=True OK")
60
+ print(f"expert.action_out |w| = {aow:.4f} (zero at init -> nonzero proves training)")
61
+ assert aow > 1e-3, "action_out is at its zero init — the expert never trained"
62
+ print(f"wrote {dst}")
63
+
64
+
65
+ if __name__ == "__main__":
66
+ main()