File size: 2,137 Bytes
5b2e6c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
import argparse
import json
import torch


def tensor_norm(x):
    x = x.detach().float()
    return {
        "shape": list(x.shape),
        "l2": float(x.norm().cpu()),
        "mean_abs": float(x.abs().mean().cpu()),
        "max_abs": float(x.abs().max().cpu()),
    }


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--ckpt", required=True)
    ap.add_argument("--out", required=True)
    args = ap.parse_args()

    ckpt = torch.load(args.ckpt, map_location="cpu", weights_only=False)
    sd = ckpt.get("state_dict", ckpt)

    groups = {
        "unsigned_node_z_node_adapter": ["z_node_adapter"],
        "signed_node_disp_node_adapter": ["disp_node_adapter"],
        "unsigned_edge_z_edge_adapter": ["z_edge_adapter"],
        "signed_edge_disp_edge_adapter": ["disp_edge_adapter"],
        "unsigned_film_mlp": ["film_mlp"],
    }

    report = {}
    for group, tokens in groups.items():
        items = {}
        for k, v in sd.items():
            if not torch.is_tensor(v):
                continue
            if all(t in k for t in tokens):
                items[k] = tensor_norm(v)
        report[group] = items

    summary = {}
    for group, items in report.items():
        total_l2_sq = 0.0
        last_layer_l2_sq = 0.0
        n = 0
        for k, stats in items.items():
            total_l2_sq += stats["l2"] ** 2
            n += 1
            if any(s in k for s in ["2.weight", "2.bias", "film_mlp.2.weight", "film_mlp.2.bias"]):
                last_layer_l2_sq += stats["l2"] ** 2
        summary[group] = {
            "num_tensors": n,
            "total_l2": total_l2_sq ** 0.5,
            "last_layer_l2": last_layer_l2_sq ** 0.5,
        }

    out = Path(args.out)
    out.parent.mkdir(parents=True, exist_ok=True)
    out.write_text(json.dumps({"summary": summary, "details": report}, indent=2), encoding="utf-8")

    print("===== condition branch weight summary =====")
    print(json.dumps(summary, indent=2))
    print("\nSaved:", out)


if __name__ == "__main__":
    main()