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
|
- # from email.generator import Generator
- import math
- from functools import partial
- import pytorch_lightning as pl
- import torch
- import torch.nn as nn
- from torch.optim.lr_scheduler import OneCycleLR
- from molbart.models.util import FuncLR
- # ----------------------------------------------------------------------------------------------------------
- # ----------------------------------------- Base Transformer Model -----------------------------------------
- # ----------------------------------------------------------------------------------------------------------
- class _AbsTransformerModel(pl.LightningModule):
- def __init__(
- self,
- pad_token_idx,
- vocabulary_size,
- d_model,
- num_layers,
- num_heads,
- d_feedforward,
- lr,
- weight_decay,
- activation,
- num_steps,
- max_seq_len,
- schedule,
- warm_up_steps,
- dropout=0.1,
- num_beams=10,
- **kwargs,
- ):
- super().__init__()
- self.pad_token_idx = pad_token_idx
- self.vocabulary_size = vocabulary_size
- self.d_model = d_model
- self.num_layers = num_layers
- self.num_heads = num_heads
- self.d_feedforward = d_feedforward
- self.lr = lr
- self.weight_decay = weight_decay
- self.activation = activation
- self.num_steps = num_steps
- self.max_seq_len = max_seq_len
- self.schedule = schedule
- self.warm_up_steps = warm_up_steps
- self.dropout = dropout
- if self.schedule == "transformer":
- assert warm_up_steps is not None, "A value for warm_up_steps is required for transformer LR schedule"
- # Additional args passed in to **kwargs in init will also be saved
- self.save_hyperparameters()
- # These must be set by subclasses
- self.sampler = None
- self.val_sampling_alg = "greedy"
- self.test_sampling_alg = "beam"
- self.num_beams = num_beams
- self.n_unique_beams = num_beams
- self.emb = nn.Embedding(vocabulary_size, d_model, padding_idx=pad_token_idx)
- self.dropout = nn.Dropout(dropout)
- self.register_buffer("pos_emb", self._positional_embs())
- def forward(self, x):
- raise NotImplementedError()
- def _calc_loss(self, batch_input, model_output):
- """Calculate the loss for the model
- Args:
- batch_input (dict): Input given to model,
- model_output (dict): Output from model
- Returns:
- loss (singleton tensor)
- """
- raise NotImplementedError()
- def sample_molecules(self, batch_input, sampling_alg="greedy"):
- """Sample molecules from the model
- Args:
- batch_input (dict): Input given to model
- sampling_alg (str): Algorithm to use to sample SMILES strings from model
- Returns:
- ([[str]], [[float]]): Tuple of molecule SMILES strings and log lhs (outer dimension is batch)
- """
- raise NotImplementedError()
- def training_step(self, batch, batch_idx):
- self.train()
- model_output = self.forward(batch)
- loss = self._calc_loss(batch, model_output)
- self.log("training_loss", loss, on_step=True, logger=True, sync_dist=True)
- return loss
- def validation_step(self, batch, batch_idx):
- self.eval()
- with torch.no_grad():
- model_output = self.forward(batch)
- target_smiles = batch["target_smiles"]
- loss = self._calc_loss(batch, model_output)
- token_acc = self._calc_token_acc(batch, model_output)
- sampled_smiles, _ = self.sample_molecules(batch, sampling_alg=self.val_sampling_alg)
- sampled_metrics = self.sampler.compute_sampling_metrics(sampled_smiles, target_smiles)
- metrics = {
- "validation_loss": loss,
- "val_token_accuracy": token_acc,
- }
- metrics.update(sampled_metrics)
- return metrics
- def validation_epoch_end(self, outputs):
- avg_outputs = self._avg_dicts(outputs)
- self._log_dict(avg_outputs)
- return
- def test_step(self, batch, batch_idx):
- self.eval()
- with torch.no_grad():
- model_output = self.forward(batch)
- target_smiles = batch["target_smiles"]
- loss = self._calc_loss(batch, model_output)
- token_acc = self._calc_token_acc(batch, model_output)
- sampled_smiles, log_likelihoods = self.sample_molecules(batch, sampling_alg=self.test_sampling_alg)
- sampled_metrics = self.sampler.compute_sampling_metrics(sampled_smiles, target_smiles)
- metrics = {
- "batch_idx": batch_idx,
- "test_loss": loss.item(),
- "test_token_accuracy": token_acc,
- "log_lhs": log_likelihoods,
- "sampled_molecules": sampled_smiles,
- "target_smiles": target_smiles,
- }
- metrics.update(sampled_metrics)
- return metrics
- def test_epoch_end(self, outputs):
- # avg_outputs = self._avg_dicts(outputs)
- # self._log_dict(avg_outputs)
- return
- def configure_optimizers(self):
- params = self.parameters()
- optim = torch.optim.Adam(params, lr=self.lr, weight_decay=self.weight_decay, betas=(0.9, 0.999))
- if self.schedule == "const":
- print("Using constant LR schedule.")
- const_sch = FuncLR(optim, lr_lambda=self._const_lr)
- sch = {"scheduler": const_sch, "interval": "step"}
- elif self.schedule == "cycle":
- print("Using cyclical LR schedule.")
- cycle_sch = OneCycleLR(optim, self.lr, total_steps=self.num_steps)
- sch = {"scheduler": cycle_sch, "interval": "step"}
- elif self.schedule == "transformer":
- print("Using original transformer schedule.")
- trans_sch = FuncLR(optim, lr_lambda=self._transformer_lr)
- sch = {"scheduler": trans_sch, "interval": "step"}
- else:
- raise ValueError(f"Unknown schedule {self.schedule}")
- return [optim], [sch]
- def _transformer_lr(self, step):
- mult = self.d_model**-0.5
- step = 1 if step == 0 else step # Stop div by zero errors
- lr = min(step**-0.5, step * (self.warm_up_steps**-1.5))
- return self.lr * mult * lr
- def _const_lr(self, step):
- if self.warm_up_steps is not None and step < self.warm_up_steps:
- return (self.lr / self.warm_up_steps) * step
- return self.lr
- def _construct_input(self, token_ids, sentence_masks=None):
- seq_len, _ = tuple(token_ids.size())
- token_embs = self.emb(token_ids)
- # Scaling the embeddings like this is done in other transformer libraries
- token_embs = token_embs * math.sqrt(self.d_model)
- positional_embs = self.pos_emb[:seq_len, :].unsqueeze(0).transpose(0, 1)
- embs = token_embs + positional_embs
- embs = self.dropout(embs)
- return embs
- def _positional_embs(self):
- """Produces a tensor of positional embeddings for the model
- Returns a tensor of shape (self.max_seq_len, self.d_model) filled with positional embeddings,
- which are created from sine and cosine waves of varying wavelength
- """
- encs = torch.tensor([dim / self.d_model for dim in range(0, self.d_model, 2)])
- encs = 10000**encs
- encs = [(torch.sin(pos / encs), torch.cos(pos / encs)) for pos in range(self.max_seq_len)]
- encs = [torch.stack(enc, dim=1).flatten()[: self.d_model] for enc in encs]
- encs = torch.stack(encs)
- return encs
- def _generate_square_subsequent_mask(self, sz, device="cpu"):
- """
- Method copied from Pytorch nn.Transformer.
- Generate a square mask for the sequence. The masked positions are filled with float('-inf').
- Unmasked positions are filled with float(0.0).
- Args:
- sz (int): Size of mask to generate
- Returns:
- torch.Tensor: Square autoregressive mask for decode
- """
- mask = (torch.triu(torch.ones((sz, sz), device=device)) == 1).transpose(0, 1)
- mask = mask.float().masked_fill(mask == 0, float("-inf")).masked_fill(mask == 1, float(0.0))
- return mask
- def _init_params(self):
- """
- Apply Xavier uniform initialisation of learnable weights
- """
- for p in self.parameters():
- if p.dim() > 1:
- nn.init.xavier_uniform_(p)
- def _calc_perplexity(self, batch_input, model_output):
- target_ids = batch_input["target"]
- target_mask = batch_input["target_mask"]
- vocab_dist_output = model_output["token_output"]
- inv_target_mask = ~(target_mask > 0)
- log_probs = vocab_dist_output.gather(2, target_ids.unsqueeze(2)).squeeze(2)
- log_probs = log_probs * inv_target_mask
- log_probs = log_probs.sum(dim=0)
- seq_lengths = inv_target_mask.sum(dim=0)
- exp = -(1 / seq_lengths)
- perp = torch.pow(log_probs.exp(), exp)
- return perp.mean()
- def _calc_token_acc(self, batch_input, model_output):
- token_ids = batch_input["target"]
- target_mask = batch_input["target_mask"]
- token_output = model_output["token_output"]
- target_mask = ~(target_mask > 0)
- _, pred_ids = torch.max(token_output.float(), dim=2)
- correct_ids = torch.eq(token_ids, pred_ids)
- correct_ids = correct_ids * target_mask
- num_correct = correct_ids.sum().float()
- total = target_mask.sum().float()
- accuracy = num_correct / total
- return accuracy
- def _avg_dicts(self, colls):
- complete_dict = {key: [] for key, val in colls[0].items()}
- for coll in colls:
- [complete_dict[key].append(coll[key]) for key in complete_dict.keys()]
- avg_dict = {key: sum(l) / len(l) for key, l in complete_dict.items()}
- return avg_dict
- def _log_dict(self, coll):
- for key, val in coll.items():
- self.log(key, val, sync_dist=True)
|