Repository navigation
Expand file tree
/
Copy pathdiffusion.py
More file actions
386 lines (334 loc) · 12.6 KB
/
Copy pathdiffusion.py
File metadata and controls
386 lines (334 loc) · 12.6 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
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
import itertools
import math
import os
import typing
import lightning as L
import numpy as np
import torch
import torch.nn.functional as F
import transformers
from torch import Tensor
from dataclasses import dataclass
import torchmetrics
from dit import DIT
import noise_schedule
import ema_model
import dataloader
LOG2 = math.log(2)
@dataclass
class Loss:
loss: torch.FloatTensor
nlls: torch.FloatTensor
token_mask: torch.FloatTensor
class NLL(torchmetrics.aggregation.MeanMetric):
pass
class BPD(NLL):
def compute(self) -> Tensor:
"""Computes the bits per dimension.
Returns:
bpd
"""
return self.mean_value / self.weight / LOG2
class Perplexity(NLL):
def compute(self) -> Tensor:
"""Computes the Perplexity.
Returns:
Perplexity
"""
return torch.exp(self.mean_value / self.weight)
class Diffusion(L.LightningModule):
def __init__(self, tokenizer: transformers.PreTrainedTokenizer):
super().__init__()
self.save_hyperparameters()
self.batch_size = 1
self.num_workers = 1
self.tokenizer = tokenizer
self.vocab_size = self.tokenizer.vocab_size
self.sampler = 'ddpm_cache'
self.gen_ppl_eval_model_name_or_path = 'gpt2-large'
self.antithetic_sampling = True
self.importance_sampling = False
self.change_of_variables = False
self.warmup_steps = 2500
# assume we dont have it
if (not hasattr(self.tokenizer, 'mask_token')
or self.tokenizer.mask_token is None):
self.mask_index = self.vocab_size
self.vocab_size += 1
else:
self.mask_index = self.tokenizer.mask_token_id
self.parameterization = 'subs'
self.backbone_name = 'dit'
if self.backbone_name == 'dit':
self.backbone = DIT(vocab_size=self.vocab_size)
#elif self.backbone_name == 'dimamba':
#self.backbone = models.dimamba.DiMamba(
#self.config,
#vocab_size=self.vocab_size,
#pad_token_id=self.tokenizer.pad_token_id)
self.T = 0
self.subs_masking = False
self.softplus = torch.nn.Softplus()
# metrics are automatically reset at end of epoch
metrics = torchmetrics.MetricCollection({
'nll': NLL(),
'bpd': BPD(),
'ppl': Perplexity(),
})
metrics.set_dtype(torch.float64)
self.train_metrics = metrics.clone(prefix='train/')
self.valid_metrics = metrics.clone(prefix='val/')
self.test_metrics = metrics.clone(prefix='test/')
# generative perplexity
self.gen_ppl_metric = Perplexity()
self.eval_model_tokenizer = transformers.AutoTokenizer.\
from_pretrained(self.gen_ppl_eval_model_name_or_path)
if self.eval_model_tokenizer.pad_token is None:
self.eval_model_tokenizer.pad_token =\
self.eval_model_tokenizer.eos_token
self.eval_model_tokenizer.pad_token_id =\
self.eval_model_tokenizer.eos_token_id
self.noise = noise_schedule.get_noise(dtype=self.dtype)
self.ema_decay = 0.9999
self.ema = ema_model.ExponentialMovingAverage(
itertools.chain(self.backbone.parameters(), self.noise.parameters()),
decay=self.ema_decay)
self.lr = 3e-4
self.betas = (0.9, 0.999)
self.eps = 1e-8
self.weight_decay = 0
self.sampling_eps = 1e-3
self.time_conditioning = False
self.neg_infinity = -1000000.0
self.fast_forward_epochs = None
self.fast_forward_batches = None
#self._validate_configuration()
def on_load_checkpoint(self, checkpoint):
if self.ema:
self.ema.load_state_dict(checkpoint['ema'])
# Copied from:
# https://github.com/Dao-AILab/flash-attention/blob/main/training/src/datamodules/language_modeling_hf.py#L41
self.fast_forward_epochs = checkpoint['loops'][
'fit_loop']['epoch_progress']['current']['completed']
self.fast_forward_batches = checkpoint['loops'][
'fit_loop']['epoch_loop.batch_progress'][
'current']['completed']
def on_save_checkpoint(self, checkpoint):
if self.ema:
checkpoint['ema'] = self.ema.state_dict()
# Copied from:
# https://github.com/Dao-AILab/flash-attention/blob/main/training/src/tasks/seq.py
# ['epoch_loop.batch_progress']['total']['completed'] is 1 iteration
# behind, so we're using the optimizer's progress.
checkpoint['loops']['fit_loop'][
'epoch_loop.batch_progress']['total'][
'completed'] = checkpoint['loops']['fit_loop'][
'epoch_loop.automatic_optimization.optim_progress'][
'optimizer']['step']['total'][
'completed'] * self.trainer.accumulate_grad_batches
checkpoint['loops']['fit_loop'][
'epoch_loop.batch_progress']['current'][
'completed'] = checkpoint['loops']['fit_loop'][
'epoch_loop.automatic_optimization.optim_progress'][
'optimizer']['step']['current'][
'completed'] * self.trainer.accumulate_grad_batches
# _batches_that_stepped tracks the number of global steps, not the number
# of local steps, so we don't multiply with self.trainer.accumulate_grad_batches here.
checkpoint['loops']['fit_loop'][
'epoch_loop.state_dict'][
'_batches_that_stepped'] = checkpoint['loops']['fit_loop'][
'epoch_loop.automatic_optimization.optim_progress'][
'optimizer']['step']['total']['completed']
if 'sampler' not in checkpoint.keys():
checkpoint['sampler'] = {}
if hasattr(self.trainer.train_dataloader.sampler,
'state_dict'):
sampler_state_dict = self.trainer.\
train_dataloader.sampler.state_dict()
checkpoint['sampler'][
'random_state'] = sampler_state_dict.get(
'random_state', None)
else:
checkpoint['sampler']['random_state'] = None
def on_train_start(self):
if self.ema:
self.ema.move_shadow_params_to_device(self.device)
# Adapted from:
# https://github.com/Dao-AILab/flash-attention/blob/main/training/src/datamodules/language_modeling_hf.py
distributed = (
self.trainer._accelerator_connector.use_distributed_sampler
and self.trainer._accelerator_connector.is_distributed)
if distributed:
sampler_cls = dataloader.FaultTolerantDistributedSampler
else:
sampler_cls = dataloader.RandomFaultTolerantSampler
updated_dls = []
for dl in self.trainer.fit_loop._combined_loader.flattened:
if hasattr(dl.sampler, 'shuffle'):
dl_sampler = sampler_cls(
dl.dataset, shuffle=dl.sampler.shuffle)
else:
dl_sampler = sampler_cls(dl.dataset)
if (distributed
and self.fast_forward_epochs is not None
and self.fast_forward_batches is not None):
dl_sampler.load_state_dict({
'epoch': self.fast_forward_epochs,
'counter': (self.fast_forward_batches
* self.batch_size)})
updated_dls.append(
torch.utils.data.DataLoader(
dl.dataset,
batch_size=self.batch_size,
num_workers=self.num_workers,
pin_memory=True,
sampler=dl_sampler,
shuffle=False,
persistent_workers=True))
self.trainer.fit_loop._combined_loader.flattened = updated_dls
def optimizer_step(self, *args, **kwargs):
super().optimizer_step(*args, **kwargs)
if self.ema:
self.ema.update(itertools.chain(
self.backbone.parameters(),
self.noise.parameters()))
def forward(self, x, sigma):
"""Returns log score."""
sigma = self._process_sigma(sigma)
with torch.cuda.amp.autocast(dtype=torch.float32):
logits = self.backbone(x, sigma)
return self._subs_parameterization(logits=logits, xt=x)
def _subs_parameterization(self, logits, xt):
# log prob at the mask index = - infinity
logits[:, :, self.mask_index] += self.neg_infinity
# Normalize the logits such that x.exp() is
# a probability distribution over vocab_size.
logits = logits - torch.logsumexp(logits, dim=-1,
keepdim=True)
# Apply updates directly in the logits matrix.
# For the logits of the unmasked tokens, set all values
# to -infinity except for the indices corresponding to
# the unmasked tokens.
unmasked_indices = (xt != self.mask_index)
logits[unmasked_indices] = self.neg_infinity
logits[unmasked_indices, xt[unmasked_indices]] = 0
return logits
def on_train_epoch_start(self):
self.backbone.train()
self.noise.train()
def training_step(self, batch, batch_idx):
loss = self._compute_loss(batch, prefix='train')
self.log(name='trainer/loss',
value=loss.item(),
on_step=True,
on_epoch=False,
sync_dist=True)
return loss
def _compute_loss(self, batch, prefix):
if 'attention_mask' in batch:
attention_mask = batch['attention_mask']
else:
attention_mask = None
losses = self._loss(batch['input_ids'], attention_mask)
loss = losses.loss
if prefix == 'train':
self.train_metrics.update(losses.nlls, losses.token_mask)
metrics = self.train_metrics
elif prefix == 'val':
self.valid_metrics.update(losses.nlls, losses.token_mask)
metrics = self.valid_metrics
elif prefix == 'test':
self.test_metrics.update(losses.nlls, losses.token_mask)
metrics = self.test_metrics
else:
raise ValueError(f'Invalid prefix: {prefix}')
self.log_dict(metrics,
on_step=False,
on_epoch=True,
sync_dist=True)
return loss
def _loss(self, x0, attention_mask):
loss = self._forward_pass_diffusion(x0)
nlls = loss * attention_mask
count = attention_mask.sum()
batch_nll = nlls.sum()
token_nll = batch_nll / count
return Loss(loss=token_nll,
nlls=nlls,
token_mask=attention_mask)
def _forward_pass_diffusion(self, x0):
t = self._sample_t(x0.shape[0], x0.device)
sigma, dsigma = self.noise(t)
unet_conditioning = sigma[:, None]
move_chance = 1 - torch.exp(-sigma[:, None])
xt = self.q_xt(x0, move_chance)
model_output = self.forward(xt, unet_conditioning)
if torch.isnan(model_output).any():
print('model-output', model_output)
# SUBS parameterization, continuous time.
log_p_theta = torch.gather(
input=model_output,
dim=-1,
index=x0[:, :, None]).squeeze(-1)
return - log_p_theta * (
dsigma / torch.expm1(sigma))[:, None]
def _sample_t(self, n, device):
_eps_t = torch.rand(n, device=device)
if self.antithetic_sampling:
offset = torch.arange(n, device=device) / n
_eps_t = (_eps_t / n + offset) % 1
t = (1 - self.sampling_eps) * _eps_t + self.sampling_eps
return t
def q_xt(self, x, move_chance):
"""Computes the noisy sample xt.
Args:
x: int torch.Tensor with shape (batch_size,
diffusion_model_input_length), input.
move_chance: float torch.Tensor with shape (batch_size, 1).
"""
move_indices = torch.rand(
* x.shape, device=x.device) < move_chance
xt = torch.where(move_indices, self.mask_index, x)
return xt
def on_validation_epoch_start(self):
if self.ema:
self.ema.store(itertools.chain(
self.backbone.parameters(),
self.noise.parameters()))
self.ema.copy_to(itertools.chain(
self.backbone.parameters(),
self.noise.parameters()))
self.backbone.eval()
self.noise.eval()
assert self.valid_metrics.nll.mean_value == 0
assert self.valid_metrics.nll.weight == 0
def validation_step(self, batch, batch_idx):
return self._compute_loss(batch, prefix='val')
def configure_optimizers(self):
# TODO(yair): Lightning currently giving this warning when using `fp16`:
# "Detected call of `lr_scheduler.step()` before `optimizer.step()`. "
# Not clear if this is a problem or not.
# See: https://github.com/Lightning-AI/pytorch-lightning/issues/5558
optimizer = torch.optim.AdamW(
itertools.chain(self.backbone.parameters(),
self.noise.parameters()),
lr=self.lr,
betas=self.betas,
eps=self.eps,
weight_decay=self.weight_decay)
scheduler = transformers.get_constant_schedule_with_warmup(optimizer=optimizer,
num_warmup_steps=self.warmup_steps)
scheduler_dict = {
'scheduler': scheduler,
'interval': 'step',
'monitor': 'val/loss',
'name': 'trainer/lr',
}
return [optimizer], [scheduler_dict]
def _process_sigma(self, sigma):
if sigma.ndim > 1:
sigma = sigma.squeeze(-1)
if not self.time_conditioning:
sigma = torch.zeros_like(sigma)
assert sigma.ndim == 1, sigma.shape
return sigma