oddadmix commited on
Commit
edccc2c
·
verified ·
1 Parent(s): 132969b

add eval_router_only.py

Browse files
Files changed (1) hide show
  1. eval_router_only.py +34 -0
eval_router_only.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """© KAND CA 2026 - evaluate a router checkpoint without retraining.
2
+
3
+ Reuses train_router_head.py's own loaders and report() so a published model and
4
+ a new one are scored by identical code in one session.
5
+
6
+ A checkpoint saved by train_router_head.py is a directory with the backbone plus
7
+ router_model.pt (the span scorer). The published Nawah-Router-v3 has the same
8
+ layout, so both load through the same path.
9
+ """
10
+ import os, sys
11
+ os.environ.setdefault("DATA_DIR", "ds/data")
12
+ import torch
13
+ from transformers import AutoTokenizer
14
+ import train_router_head as T
15
+
16
+ def load_model(mid):
17
+ m = T.RouterModel(mid)
18
+ w = os.path.join(mid, "router_model.pt") if os.path.isdir(mid) else None
19
+ if w is None:
20
+ from huggingface_hub import hf_hub_download
21
+ w = hf_hub_download(mid, "router_model.pt")
22
+ if os.path.exists(w):
23
+ m.load_state_dict(torch.load(w, map_location="cpu", weights_only=True))
24
+ return m.cuda().eval()
25
+
26
+ for mid in sys.argv[1:]:
27
+ tok = AutoTokenizer.from_pretrained(mid)
28
+ m = load_model(mid)
29
+ n = sum(p.numel() for p in m.parameters())
30
+ print(f"\n{'='*66}\n{mid} params={n:,}")
31
+ for name in ("eval_unseen_lanes", "eval_unseen_domain", "eval_unseen_axis", "eval_hard"):
32
+ rows = T.load(name)
33
+ T.report(m, tok, rows, name.replace("eval_", ""))
34
+ del m; torch.cuda.empty_cache()