-
Notifications
You must be signed in to change notification settings - Fork 15
/
common.py
183 lines (154 loc) · 4.75 KB
/
common.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
Some utility functions.
"""
import re
import torch
import random
import unicodedata
from ete3 import Tree, TreeNode
from transformers import get_cosine_schedule_with_warmup
from typing import *
Example = Dict[str, Any]
Batch = Dict[str, Any]
def normalize(text: str) -> str:
"""
Deal with unicode-related artifacts.
"""
return unicodedata.normalize("NFD", text)
def normalize_sentence(text: str) -> str:
"""
Convert sentences to lowercase and remove the trailing period.
"""
text = normalize(text).lower().strip()
if text.endswith("."):
text = text[:-1].strip()
return text
def extract_context(ctx: str) -> OrderedDict[str, str]:
"""
Extract supporting facts from string to dict.
"""
return OrderedDict(
{
ident.strip(): normalize_sentence(sent)
for ident, sent in re.findall(
r"(?P<ident>sent\d+): (?P<sent>.+?) (?=sent)", ctx + " sent"
)
}
)
def deserialize(
hypothesis: str, context: OrderedDict[str, str], proof: str, strict: bool = True
) -> Tree:
"""
Construct a tree from a text sequence.
"""
nodes = {}
for proof_step in proof.split(";"):
proof_step = proof_step.strip()
if proof_step == "":
continue
if proof_step.count(" -> ") != 1:
return None
premises, conclusion = proof_step.split(" -> ")
m = re.fullmatch(r"\((?P<score>.+?)\) (?P<concl>.+)", conclusion)
score: Optional[str]
if m is not None:
score = m["score"]
conclusion = m["concl"]
else:
score = None
if conclusion == "hypothesis":
ident = "hypothesis"
sent = hypothesis
else:
m = re.match(r"(?P<ident>int\d+): (?P<sent>.+)", conclusion)
if m is None:
return None
ident = m["ident"]
sent = m["sent"]
nodes[ident] = TreeNode(name=ident)
nodes[ident].add_feature("sent", sent)
if score is not None:
nodes[ident].add_feature("score", score)
for p in premises.split(" & "):
if p == ident:
return None
elif p not in nodes:
if re.fullmatch(r"sent\d+", p) is None:
return None
nodes[p] = TreeNode(name=p)
if p not in context:
if strict:
return None
else:
nodes[p].add_feature("sent", context[p])
if nodes[p] not in nodes[ident].children:
nodes[ident].add_child(nodes[p])
return nodes.get("hypothesis", None)
def serialize(tree: Tree) -> str:
"""
Serialize a proof tree as a text sequence.
"""
if tree is None:
return "INVALID"
elif tree.is_leaf():
return tree.name # type: ignore
prev_steps = [serialize(child) for child in tree.children if not child.is_leaf()]
random.shuffle(prev_steps)
if len(prev_steps) == 0:
seq = " & ".join(child.name for child in tree.children)
else:
seq = (
" ".join(prev_steps)
+ " "
+ " & ".join(child.name for child in tree.children)
)
seq += " -> "
if tree.name.startswith("int"):
seq += f"{tree.name}: {tree.sent};"
else:
assert tree.name == "hypothesis"
seq += f"hypothesis;"
return seq
def get_internal_nodes(tree: Tree) -> List[TreeNode]:
"""
Get the internal nodes of a tree as a list.
"""
return [node for node in tree.traverse() if not node.is_leaf()]
def rename_ints(proof: str) -> str:
"""
Rename the `int\d+` identifiers in a proof so that they increase from 1.
"""
assert "INT" not in proof
mapping: Dict[str, str] = dict()
while True:
m = re.search(r"int\d+", proof)
if m is None:
break
s = m.group()
assert s not in mapping
dst = f"INT{1 + len(mapping)}"
mapping[s] = dst
proof = proof.replace(f"{s}:", f"{dst}:").replace(f"{s} ", f"{dst} ")
return proof.replace("INT", "int")
def get_optimizers(
parameters: Iterable[torch.nn.parameter.Parameter],
lr: float,
num_warmup_steps: int,
num_training_steps: int,
) -> Dict[str, Any]:
"""
Get an AdamW optimizer with linear learning rate warmup and cosine decay.
"""
optimizer = torch.optim.AdamW(parameters, lr=lr)
scheduler = get_cosine_schedule_with_warmup(
optimizer,
num_warmup_steps=num_warmup_steps,
num_training_steps=num_training_steps,
)
return {
"optimizer": optimizer,
"lr_scheduler": {
"scheduler": scheduler,
"interval": "step",
},
}