File size: 1,374 Bytes
09ccad2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""导出 train.py checkpoint -> ComfyUI 适配器 .safetensors(纯 state_dict)。

用法:
    python export_adapter.py <adapter_stage2_step2000.pt> [--out adapter_stage2.safetensors]
"""
from __future__ import annotations

import argparse
import os
import sys

import torch

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)

from adapter.model import H3Adapter  # noqa: E402


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("ckpt", help="train.py checkpoint (.pt)")
    ap.add_argument("--out", default="adapter_stage2.safetensors")
    args = ap.parse_args()

    ckpt = torch.load(args.ckpt, map_location="cpu", weights_only=False)
    state = ckpt.get("model", ckpt)  # 兼容 {"model":..., "config":...} 与纯 state_dict
    if any(k.startswith("model.") for k in state):
        state = {k[len("model."):]: v for k, v in state.items()}

    model = H3Adapter()
    missing, unexpected = model.load_state_dict(state, strict=False)
    assert not missing, f"missing: {missing[:10]}"
    print(f"adapter keys: {len(state)} | missing={len(missing)} unexpected={len(unexpected)}")

    from safetensors.torch import save_file
    save_file({k: v.contiguous().to(torch.bfloat16) for k, v in state.items()}, args.out)
    print(f"saved -> {args.out}")


if __name__ == "__main__":
    main()