File size: 2,504 Bytes
24918f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""Head files: the weights one training run changes over the frozen backbone.

A head file is a safetensors file holding the head, scorer, type embedding,
and (optionally) action head state of a Laya `DecisionModel`, with header
metadata naming the backbone it was trained over. `pw-decide` refuses a head
whose `backbone` is not the loaded checkpoint's encoder.

    headfile.py show head.safetensors
"""

import argparse
import json
import struct
import sys
from pathlib import Path

HEAD_MODULES = ("head", "scorer", "type_emb", "act_head")
FORMAT = "pw-decide-head/1"


def encoder_id(model_id):
    """The backbone id a checkpoint records, without loading its weights."""
    import laya  # noqa: PLC0415

    model_dir = model_id
    if not Path(model_dir).exists():
        from huggingface_hub import snapshot_download  # noqa: PLC0415

        model_dir = snapshot_download(model_id, allow_patterns=["rl_agent_config.json"])
    with open(Path(model_dir) / "rl_agent_config.json", encoding="utf-8") as fh:
        return json.load(fh)["encoder"]


def save(path, model, label, model_id, extra=None):
    """Write the head modules of `model` as a head file."""
    from safetensors.torch import save_file  # noqa: PLC0415

    tensors = {}
    for name in HEAD_MODULES:
        module = getattr(model, name, None)
        if module is None:
            continue
        for key, value in module.state_dict().items():
            tensors["%s.%s" % (name, key)] = value.detach().contiguous().cpu()
    meta = {"format": FORMAT, "label": label, "model": model_id, "backbone": encoder_id(model_id)}
    for key, value in (extra or {}).items():
        meta[key] = value if isinstance(value, str) else json.dumps(value)
    Path(path).parent.mkdir(parents=True, exist_ok=True)
    save_file(tensors, str(path), metadata=meta)


def read_metadata(path):
    with open(path, "rb") as fh:
        n = struct.unpack("<Q", fh.read(8))[0]
        header = json.loads(fh.read(n))
    meta = header.pop("__metadata__", {})
    return meta, header


def main():
    ap = argparse.ArgumentParser()
    sub = ap.add_subparsers(dest="cmd", required=True)
    s = sub.add_parser("show")
    s.add_argument("path")
    args = ap.parse_args()
    meta, tensors = read_metadata(args.path)
    print(json.dumps(meta, indent=2))
    print("%d tensors, %d bytes" % (len(tensors), sum(t["data_offsets"][1] - t["data_offsets"][0] for t in tensors.values())))


if __name__ == "__main__":
    main()