File size: 1,691 Bytes
a93e9b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
35
36
37
38
"""Private, deterministic tensor export. Run only against the pinned checkpoint."""
import argparse
import json
from pathlib import Path
import sys

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from kinejing_core.engine import CHECKPOINT_SHA256, ENCODER_SHA256, sha256


def main():
    import numpy as np
    import torch
    parser = argparse.ArgumentParser()
    parser.add_argument("checkpoint", type=Path)
    parser.add_argument("output", type=Path)
    args = parser.parse_args()
    if sha256(args.checkpoint) != CHECKPOINT_SHA256:
        raise ValueError("Checkpoint is not the reviewed trained model")
    checkpoint = torch.load(args.checkpoint, map_location="cpu", weights_only=True)
    if checkpoint["encoder_provenance"]["weights_sha256"] != ENCODER_SHA256:
        raise ValueError("Unexpected trained feature space")
    if checkpoint["config"] != {"feature_dim": 768, "action_dim": 14, "views": 3, "hidden": 256}:
        raise ValueError("Unexpected trained architecture")
    tensors = {name: value.detach().cpu().numpy() for name, value in checkpoint["state_dict"].items()}
    tensors.update({name: checkpoint[name].detach().cpu().numpy()
                    for name in ("feature_mean", "feature_std", "action_mean", "action_std")})
    if args.output.exists():
        raise FileExistsError("Refusing to overwrite an existing model export")
    args.output.parent.mkdir(parents=True, exist_ok=True)
    np.savez_compressed(args.output, **tensors)
    print(json.dumps({"checkpoint_sha256": CHECKPOINT_SHA256,
                      "inference_asset_sha256": sha256(args.output), "tensor_count": len(tensors)}))


if __name__ == "__main__":
    main()