Repository navigation
Expand file tree
/
Copy pathpredict.py
More file actions
122 lines (95 loc) · 3.7 KB
/
Copy pathpredict.py
File metadata and controls
122 lines (95 loc) · 3.7 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import sys
import json
import torch
from pathlib import Path
if sys.stdout.encoding != 'utf-8':
try:
sys.stdout.reconfigure(encoding='utf-8')
except Exception:
pass
sys.path.insert(0, str(Path(__file__).parent.resolve()))
from solver_model import CalculusSolverModel
from tokenizer.slang_serializer import serialize_slang_math
def flatten_vocab(raw_vocab):
"""
Identical to train.py's flatten_vocab — must stay identical, or token IDs
at inference time will drift from the IDs the checkpoint was trained on.
"""
flat = {}
for key, value in raw_vocab.items():
if key.startswith("_"):
continue
if isinstance(value, dict):
flat.update(value)
return flat
with open("tokenizer/vocab.json", "r", encoding="utf-8") as f:
_raw_vocab = json.load(f)
vocab_mapping = flatten_vocab(_raw_vocab)
REAL_VOCAB_SIZE = max(vocab_mapping.values()) + 1
_rule_items = sorted(_raw_vocab.get("rule_tokens", {}).items(), key=lambda kv: kv[1])
RULE_LABELS = [name.split("RULE:", 1)[1] for name, _ in _rule_items]
with open("config.json", "r") as cfg_file:
config = json.load(cfg_file)
MAX_LEN = config.get("max_len", 32)
def encode(tokens):
pad_idx = vocab_mapping["[PAD]"]
ids = []
for t in tokens:
if t not in vocab_mapping:
raise KeyError(f"Token '{t}' missing from vocab.json!")
ids.append(vocab_mapping[t])
pad_len = MAX_LEN - len(ids)
if pad_len > 0:
ids += [pad_idx] * pad_len
return ids[:MAX_LEN]
def evaluate_cli_input():
if len(sys.argv) < 2:
print("💡 Usage: python predict.py '{\"op\": \"diff\", \"var\": \"x\", \"expr\": {\"coeff\": 3, \"var\": {\"x\": 2}}}'")
return
try:
user_envelope = json.loads(sys.argv[1])
except Exception:
print("❌ Error: Command line argument must be valid JSON.")
return
try:
src_tokens = serialize_slang_math(user_envelope)
except Exception as e:
print(f"❌ Error: Input is not a valid SLaNg envelope: {e}")
return
try:
src_ids = encode(src_tokens)
except KeyError as e:
print(f"❌ Error: {e}")
return
src_tensor = torch.tensor([src_ids], dtype=torch.long)
bos_id = vocab_mapping["[BOS]"]
tgt_tensor = torch.full((1, MAX_LEN), vocab_mapping["[PAD]"], dtype=torch.long)
tgt_tensor[0, 0] = bos_id
model = CalculusSolverModel(
vocab_size=REAL_VOCAB_SIZE,
num_rules=len(RULE_LABELS),
hidden_dim=config["hidden_dim"],
rule_labels=RULE_LABELS,
)
checkpoint_path = Path("checkpoints/final/best.pt")
if not checkpoint_path.exists():
checkpoint_path = Path("checkpoints/checkpoint_epoch_1.pt")
state_dict = torch.load(str(checkpoint_path), map_location="cpu")
model.load_state_dict(state_dict) # let mismatches raise loudly, never swallow them
model.eval()
with torch.no_grad():
decoder_logits, rule_logits, verifier_logits = model(
src_tensor,
tgt_tensor,
)
pred_rule_idx = torch.argmax(rule_logits, dim=-1).item()
pred_rule_name = RULE_LABELS[pred_rule_idx] if pred_rule_idx < len(RULE_LABELS) else str(pred_rule_idx)
pred_token_ids = torch.argmax(decoder_logits, dim=-1)[0].tolist()
confidence = torch.sigmoid(verifier_logits).mean().item()
id_to_token = {v: k for k, v in vocab_mapping.items()}
pred_tokens = [id_to_token.get(i, f"<id:{i}>") for i in pred_token_ids]
print(f"\n🎯 Predicted rule: {pred_rule_name}")
print(f" Verification confidence: {confidence * 100:.2f}%")
print(f" Predicted output tokens: {pred_tokens}")
if __name__ == "__main__":
evaluate_cli_input()