-
Notifications
You must be signed in to change notification settings - Fork 53
/
common.py
455 lines (363 loc) · 14.4 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import os
import re
import sys
import json
import random
import torch
import tempfile
import networkx as nx
from loguru import logger
from lean_dojo import Pos
import pytorch_lightning as pl
from dataclasses import dataclass, field
from pytorch_lightning.utilities.deepspeed import (
convert_zero_checkpoint_to_fp32_state_dict,
)
from transformers import get_constant_schedule_with_warmup
from deepspeed.ops.adam import FusedAdam, DeepSpeedCPUAdam
from typing import Optional, List, Dict, Any, Tuple, Generator
from pytorch_lightning.strategies.deepspeed import DeepSpeedStrategy
Example = Dict[str, Any]
Batch = Dict[str, Any]
MARK_START_SYMBOL = "<a>"
MARK_END_SYMBOL = "</a>"
def remove_marks(s: str) -> str:
"""Remove all :code:`<a>` and :code:`</a>` from ``s``."""
return s.replace(MARK_START_SYMBOL, "").replace(MARK_END_SYMBOL, "")
@dataclass(unsafe_hash=True)
class Context:
"""Contexts are "queries" in our retrieval setup."""
path: str
theorem_full_name: str
theorem_pos: Pos = field(compare=False)
state: str
def __post_init__(self) -> None:
assert isinstance(self.path, str)
assert isinstance(self.theorem_full_name, str)
assert isinstance(self.theorem_pos, Pos)
assert (
isinstance(self.state, str)
and "⊢" in self.state
and MARK_START_SYMBOL not in self.state
and MARK_END_SYMBOL not in self.state
)
def serialize(self) -> str:
"""Serialize the context into a string for Transformers."""
return self.state
@dataclass(unsafe_hash=True)
class Premise:
"""Premises are "documents" in our retrieval setup."""
path: str
"""The ``*.lean`` file this premise comes from.
"""
full_name: str
"""Fully qualified name.
"""
start: Pos = field(repr=False)
"""Start position of the premise's definition in the ``*.lean`` file.
"""
end: Pos = field(repr=False, compare=False)
"""End position of the premise's definition in the ``*.lean`` file.
"""
code: str = field(compare=False)
"""Raw, human-written code for defining the premise.
"""
def __post_init__(self) -> None:
assert isinstance(self.path, str)
assert isinstance(self.full_name, str)
assert (
isinstance(self.start, Pos)
and isinstance(self.end, Pos)
and self.start <= self.end
)
assert isinstance(self.code, str) and self.code != ""
def serialize(self) -> str:
"""Serialize the premise into a string for Transformers."""
annot_full_name = f"{MARK_START_SYMBOL}{self.full_name}{MARK_END_SYMBOL}"
code = self.code.replace(f"_root_.{self.full_name}", annot_full_name)
fields = self.full_name.split(".")
for i in range(len(fields)):
prefix = ".".join(fields[i:])
new_code = re.sub(f"(?<=\s)«?{prefix}»?", annot_full_name, code)
if new_code != code:
code = new_code
break
return code
class PremiseSet:
"""A set of premises indexed by their paths and full names."""
path2premises: Dict[str, Dict[str, Premise]]
def __init__(self) -> None:
self.path2premises = {}
def __iter__(self) -> Generator[Premise, None, None]:
for _, premises in self.path2premises.items():
for p in premises.values():
yield p
def add(self, p: Premise) -> None:
if p.path in self.path2premises:
self.path2premises[p.path][p.full_name] = p
else:
self.path2premises[p.path] = {p.full_name: p}
def update(self, premises: List[Premise]) -> None:
for p in premises:
self.add(p)
def __contains__(self, p: Premise) -> bool:
return (
p.path in self.path2premises and p.full_name in self.path2premises[p.path]
)
def __len__(self) -> int:
return sum(len(premises) for premises in self.path2premises.values())
@dataclass(frozen=True)
class File:
"""A file defines 0 or multiple premises."""
path: str
"""Path of the ``*.lean`` file.
"""
premises: List[Premise] = field(repr=False, compare=False)
"""A list of premises defined in this file.
"""
@classmethod
def from_data(cls, file_data: Dict[str, Any]) -> "File":
"""Construct a :class:`File` object from ``file_data``."""
path = file_data["path"]
premises = []
for p in file_data["premises"]:
full_name = p["full_name"]
if full_name is None:
continue
if "user__.n" in full_name or p["code"] == "":
# Ignore ill-formed premises (often due to errors in ASTs).
continue
if full_name.startswith("[") and full_name.endswith("]"):
# Ignore mutual definitions.
continue
premises.append(
Premise(
path, p["full_name"], Pos(*p["start"]), Pos(*p["end"]), p["code"]
)
)
return cls(path, premises)
@property
def is_empty(self) -> bool:
"""Check whether the file contains no premise."""
return self.premises == []
class Corpus:
"""Our retrieval corpus is a DAG of files. Each file consists of
premises (theorems, definitoins, etc.) that can be retrieved.
"""
transitive_dep_graph: nx.DiGraph
"""Transitive closure of the dependency graph among files.
There is an edge from file X to Y iff X import Y (directly or indirectly).
"""
all_premises: List[Premise]
"""All premises in the entire corpus.
"""
def __init__(self, jsonl_path: str) -> None:
"""Construct a :class:`Corpus` object from a ``corpus.jsonl`` data file."""
dep_graph = nx.DiGraph()
self.all_premises = []
logger.info(f"Building the corpus from {jsonl_path}")
for line in open(jsonl_path):
file_data = json.loads(line)
path = file_data["path"]
assert not dep_graph.has_node(path)
file = File.from_data(file_data)
dep_graph.add_node(path, file=file)
self.all_premises.extend(file.premises)
for p in file_data["imports"]:
assert dep_graph.has_node(p)
dep_graph.add_edge(path, p)
assert nx.is_directed_acyclic_graph(dep_graph)
self.transitive_dep_graph = nx.transitive_closure_dag(dep_graph)
self.imported_premises_cache = {}
self.fill_cache()
def _get_file(self, path: str) -> File:
return self.transitive_dep_graph.nodes[path]["file"]
def __len__(self) -> int:
return len(self.all_premises)
def __contains__(self, path: str) -> bool:
return path in self.transitive_dep_graph
def __getitem__(self, idx: int) -> Premise:
return self.all_premises[idx]
@property
def files(self) -> List[File]:
return [self._get_file(p) for p in self.transitive_dep_graph.nodes]
@property
def num_files(self) -> int:
return len(self.files)
def get_dependencies(self, path: str) -> List[str]:
"""Return a list of (direct and indirect) dependencies of the file ``path``."""
return list(self.transitive_dep_graph.successors(path))
def get_premises(self, path: str) -> List[Premise]:
"""Return a list of premises defined in the file ``path``."""
return self._get_file(path).premises
def num_premises(self, path: str) -> int:
"""Return the number of premises defined in the file ``path``."""
return len(self.get_premises(path))
def locate_premise(self, path: str, pos: Pos) -> Optional[Premise]:
"""Return a premise at position ``pos`` in file ``path``.
Return None if no such premise can be found.
"""
for p in self.get_premises(path):
assert p.path == path
if p.start <= pos <= p.end:
return p
return None
def fill_cache(self) -> None:
for path in self.transitive_dep_graph.nodes:
self._get_imported_premises(path)
def _get_imported_premises(self, path: str) -> List[Premise]:
"""Return a list of premises imported in file ``path``. The result is cached."""
premises = self.imported_premises_cache.get(path, None)
if premises is not None:
return premises
premises = []
for p in self.transitive_dep_graph.successors(path):
premises.extend(self._get_file(p).premises)
self.imported_premises_cache[path] = premises
return premises
def get_accessible_premises(self, path: str, pos: Pos) -> PremiseSet:
"""Return the set of premises accessible at position ``pos`` in file ``path``,
i.e., all premises defined in the (transitively) imported files or earlier in the same file.
"""
premises = PremiseSet()
for p in self.get_premises(path):
if p.end <= pos:
premises.add(p)
premises.update(self._get_imported_premises(path))
return premises
def get_accessible_premise_indexes(self, path: str, pos: Pos) -> List[int]:
return [
i
for i, p in enumerate(self.all_premises)
if (p.path == path and p.end <= pos)
or self.transitive_dep_graph.has_edge(path, p.path)
]
def get_nearest_premises(
self,
premise_embeddings: torch.FloatTensor,
batch_context: List[Context],
batch_context_emb: torch.Tensor,
k: int,
) -> Tuple[List[List[Premise]], List[List[float]]]:
"""Perform a batch of nearest neighbour search."""
similarities = batch_context_emb @ premise_embeddings.t()
idxs_batch = similarities.argsort(dim=1, descending=True).tolist()
results = [[] for _ in batch_context]
scores = [[] for _ in batch_context]
for j, (ctx, idxs) in enumerate(zip(batch_context, idxs_batch)):
accessible_premises = self.get_accessible_premises(
ctx.path, ctx.theorem_pos
)
for i in idxs:
p = self.all_premises[i]
if p in accessible_premises:
results[j].append(p)
scores[j].append(similarities[j, i].item())
if len(results[j]) >= k:
break
else:
raise ValueError
return results, scores
@dataclass(frozen=True)
class IndexedCorpus:
"""A corpus with premise embeddings."""
corpus: Corpus
embeddings: torch.FloatTensor
def __post_init__(self):
assert self.embeddings.device == torch.device("cpu")
assert len(self.embeddings) == len(self.corpus)
def get_all_pos_premises(annot_tac, corpus: Corpus) -> List[Premise]:
"""Return a list of all premises that are used in the tactic ``annot_tac``."""
_, provenances = annot_tac
all_pos_premises = set()
for prov in provenances:
def_path = prov["def_path"]
p = corpus.locate_premise(def_path, Pos(*prov["def_pos"]))
if p is not None:
all_pos_premises.add(p)
else:
logger.warning(f"Cannot locate premise: {prov}")
return list(all_pos_premises)
def format_augmented_state(
s: str, premises: List[Premise], max_len: Optional[int] = None, p_drop: float = 0.0
) -> str:
"""Format a state with retrieved premises and drop some of them with probability ``p_drop``."""
aug_s = ""
length = 0
if max_len is None:
max_len = 9999999999999999999999
max_premises_len = max_len - len(bytes(s.encode("utf-8")))
for p in premises:
if random.random() < p_drop:
continue
p_str = f"{p.serialize()}\n\n"
l = len(bytes(p_str.encode("utf-8")))
if length + l > max_premises_len:
continue
length += l
aug_s = p_str + aug_s
aug_s += s
return aug_s
def get_optimizers(
parameters, trainer: pl.Trainer, lr: float, warmup_steps: int
) -> Dict[str, Any]:
"""Return an AdamW optimizer with cosine warmup learning rate schedule."""
strategy = trainer.strategy
if isinstance(strategy, DeepSpeedStrategy):
if "offload_optimizer" in strategy.config["zero_optimization"]:
logger.info("Optimizing with DeepSpeedCPUAdam")
optimizer = DeepSpeedCPUAdam(parameters, lr=lr, adamw_mode=True)
else:
logger.info("Optimizing with FusedAdam")
optimizer = FusedAdam(parameters, lr=lr, adam_w_mode=True)
else:
logger.info("Optimizing with AdamW")
optimizer = torch.optim.AdamW(parameters, lr=lr)
scheduler = get_constant_schedule_with_warmup(optimizer, warmup_steps)
return {
"optimizer": optimizer,
"lr_scheduler": {
"scheduler": scheduler,
"interval": "step",
},
}
def _is_deepspeed_checkpoint(path: str):
if not os.path.exists(path):
raise FileExistsError(f"Checkpoint {path} does not exist.")
return os.path.isdir(path) and os.path.exists(os.path.join(path, "zero_to_fp32.py"))
def load_checkpoint(model_cls, ckpt_path: str, device, freeze: bool):
"""Handle DeepSpeed checkpoints in model loading."""
if _is_deepspeed_checkpoint(ckpt_path):
with tempfile.TemporaryDirectory() as dirname:
path = os.path.join(dirname, "lightning.cpkt")
convert_zero_checkpoint_to_fp32_state_dict(ckpt_path, path)
model = model_cls.load_from_checkpoint(path, strict=False).to(device)
else: # PyTorch Ligthning checkpoints
model = model_cls.load_from_checkpoint(ckpt_path, strict=False).to(device)
if freeze:
model.freeze()
return model
def zip_strict(*args):
assert len(args) > 1 and all(len(args[0]) == len(a) for a in args[1:])
return zip(*args)
def set_logger(verbose: bool) -> None:
"""
Set the logging level of loguru.
The effect of this function is global, and it should
be called only once in the main function
"""
logger.remove()
if verbose:
logger.add(sys.stderr, level="DEBUG")
else:
logger.add(sys.stderr, level="INFO")
def cpu_checkpointing_enabled(pl_module) -> bool:
try:
trainer = pl_module.trainer
return (
trainer.strategy is not None
and isinstance(trainer.strategy, DeepSpeedStrategy)
and trainer.strategy.config["activation_checkpointing"]["cpu_checkpointing"]
)
except RuntimeError:
return False