#!/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("