-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
150 lines (127 loc) · 4.18 KB
/
model.py
File metadata and controls
150 lines (127 loc) · 4.18 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
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
"""Lightning module for the tactic generator."""
import torch
from lean_dojo import Pos
from loguru import logger
import pytorch_lightning as pl
from abc import ABC, abstractmethod
from typing import List, Tuple
from transformers import T5ForConditionalGeneration, AutoTokenizer
from common import zip_strict, load_checkpoint
torch.set_float32_matmul_precision("medium")
class TacticGenerator(ABC):
"""A tactic generator takes a state and generates multiple tactic candidates."""
@abstractmethod
def generate(
self,
state: str,
file_path: str,
theorem_full_name: str,
theorem_pos: Pos,
num_samples: int,
) -> List[Tuple[str, float]]:
raise NotImplementedError
@abstractmethod
def batch_generate(
self,
state: List[str],
file_path: List[str],
theorem_full_name: List[str],
theorem_pos: List[Pos],
num_samples: int,
) -> List[List[Tuple[str, float]]]:
raise NotImplementedError
class DSProver(TacticGenerator, pl.LightningModule):
def __init__(
self,
model_name: str,
lr: float,
warmup_steps: int,
num_beams: int,
eval_num_retrieved: int,
eval_num_cpus: int,
eval_num_theorems: int,
max_seq_len: int,
length_penalty: float = 0.0
) -> None:
super().__init__()
self.save_hyperparameters()
self.lr = lr
self.warmup_steps = warmup_steps
self.num_beams = num_beams
self.length_penalty = length_penalty
self.eval_num_cpus = eval_num_cpus
self.eval_num_theorems = eval_num_theorems
self.max_seq_len = max_seq_len
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
self.generator = T5ForConditionalGeneration.from_pretrained(model_name)
@classmethod
def load(
cls, ckpt_path: str, device, freeze: bool
) -> "DSProver":
return load_checkpoint(cls, ckpt_path, device, freeze)
def forward(
self,
state_ids: torch.Tensor,
state_mask: torch.Tensor,
tactic_ids: torch.Tensor,
) -> torch.Tensor:
return self.generator(
input_ids=state_ids,
attention_mask=state_mask,
labels=tactic_ids,
).loss
##############
# Prediction #
##############
def generate(
self,
state: str,
num_samples: int,
) -> List[Tuple[str, float]]:
return self.batch_generate(
[state], num_samples
)[0]
def batch_generate(
self,
state: List[str],
num_samples: int,
) -> List[List[Tuple[str, float]]]:
logger.debug(state)
tokenized_state = self.tokenizer(
state,
padding="longest",
max_length=self.max_seq_len,
truncation=True,
return_tensors="pt",
)
state_ids = tokenized_state.input_ids.to(self.device)
state_mask = tokenized_state.attention_mask.to(self.device)
# Generate tactic candidates using beam search.
output = self.generator.generate(
input_ids=state_ids,
attention_mask=state_mask,
max_length=self.max_seq_len,
num_beams=num_samples,
length_penalty=self.length_penalty,
do_sample=False,
num_return_sequences=num_samples,
early_stopping=False,
output_scores=True,
return_dict_in_generate=True,
)
# Return the output.
raw_output_text = self.tokenizer.batch_decode(
output.sequences, skip_special_tokens=True
)
raw_scores = output.sequences_scores.tolist()
tactics_with_scores = []
for i in range(len(state)):
output_text = []
output_score = []
for j in range(i * num_samples, (i + 1) * num_samples):
t = raw_output_text[j]
if t not in output_text:
output_text.append(t)
output_score.append(raw_scores[j])
tactics_with_scores.append(list(zip_strict(output_text, output_score)))
return tactics_with_scores