| import itertools |
| import math |
| import os |
| import typing |
| from dataclasses import dataclass |
|
|
| import hydra.utils |
| import lightning as L |
| import numpy as np |
| import torch |
| import torch.nn.functional as F |
| import torchmetrics |
| import transformers |
| from torch import Tensor |
|
|
| import dataloader |
| import models |
| import noise_schedule |
| import utils |
|
|
| import wandb |
| wandb.Table.BIND_ONCE = False |
|
|
| from dataset import AbstractDataset |
| from tokenizer import AbstractTokenizer |
|
|
|
|
| LOG2 = math.log(2) |
|
|
|
|
| def _sample_categorical(categorical_probs): |
| gumbel_norm = ( |
| 1e-10 |
| - (torch.rand_like(categorical_probs) + 1e-10).log()) |
| return (categorical_probs / gumbel_norm).argmax(dim=-1) |
|
|
|
|
| def _unsqueeze(x, reference): |
| return x.view( |
| * x.shape, |
| * ((1,) * (len(reference.shape) - len(x.shape)))) |
|
|
|
|
| @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, |
| config, |
| tokenizer: AbstractTokenizer): |
| super().__init__() |
| self.save_hyperparameters() |
| self.config = config |
|
|
| self.tokenizer = tokenizer |
| self.vocab_size = self.tokenizer.vocab_size |
| self.sampler = self.config.sampling.predictor |
| self.gen_ppl_eval_model_name_or_path = self.config.eval.\ |
| gen_ppl_eval_model_name_or_path |
| self.antithetic_sampling = self.config.training.antithetic_sampling |
| self.importance_sampling = self.config.training.importance_sampling |
| self.change_of_variables = self.config.training.change_of_variables |
| self.track_high_noise_loss = self.config.training.get( |
| 'track_high_noise_loss', False) |
| self.high_noise_threshold = self.config.training.get( |
| 'high_noise_threshold', 0.8) |
| self.skip_high_noise_training = self.config.training.get( |
| 'skip_high_noise_training', False) |
| if (not hasattr(self.tokenizer, 'mask_token') |
| or self.tokenizer.mask_token is None): |
| self.bos_index = self.tokenizer.bos_token |
| self.eos_index = self.tokenizer.eos_token |
| self.boi_index = getattr(self.tokenizer, "boi_token", None) |
| self.mask_index = self.vocab_size |
| self.vocab_size += 1 |
| else: |
| self.mask_index = self.tokenizer.mask_token_id |
| self.parameterization = self.config.parameterization |
| if self.config.backbone == 'dit': |
| self.backbone = models.dit.DIT( |
| self.config,vocab_size=self.vocab_size) |
| elif self.config.backbone == 'dimamba': |
| self.backbone = models.dimamba.DiMamba( |
| self.config, |
| vocab_size=self.vocab_size, |
| pad_token_id=self.tokenizer.pad_token_id) |
| elif self.config.backbone == 'ar': |
| self.backbone = models.autoregressive.AR( |
| self.config, |
| vocab_size=self.vocab_size, |
| mask_index=self.mask_index) |
| elif self.config.backbone == 'hf_dit': |
| self.backbone = transformers.AutoModelForMaskedLM.from_pretrained( |
| config.eval.checkpoint_path, trust_remote_code=True) |
| else: |
| raise ValueError( |
| f'Unknown backbone: {self.config.backbone}') |
|
|
| self.T = self.config.T |
| self.subs_masking = self.config.subs_masking |
|
|
| self.softplus = torch.nn.Softplus() |
|
|
| self.train_metrics = torchmetrics.MetricCollection({ |
| 'nll': NLL(), |
| 'bpd': BPD(), |
| 'ppl': Perplexity(), |
| }).clone(prefix='train/') |
|
|
| self.valid_metrics = torchmetrics.MetricCollection({ |
| 'nll': NLL(), |
| 'bpd': BPD(), |
| 'ppl': Perplexity(), |
| }).clone(prefix='val/') |
|
|
| |
| 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(self.config, |
| dtype=self.dtype) |
| if self.config.training.ema > 0: |
| self.ema = models.ema.ExponentialMovingAverage( |
| itertools.chain(self.backbone.parameters(), |
| self.noise.parameters()), |
| decay=self.config.training.ema) |
| else: |
| self.ema = None |
| |
| self.lr = self.config.optim.lr |
| self.sampling_eps = self.config.training.sampling_eps |
| self.time_conditioning = self.config.time_conditioning |
| self.neg_infinity = -1000000.0 |
| self.fast_forward_epochs = None |
| self.fast_forward_batches = None |
| self._validate_configuration() |
|
|
| def _validate_configuration(self): |
| assert not (self.change_of_variables |
| and self.importance_sampling) |
| if self.parameterization == 'sedd': |
| assert not self.importance_sampling |
| assert not self.change_of_variables |
| if self.parameterization == 'd3pm': |
| assert self.T > 0 |
| if self.T > 0: |
| assert self.parameterization in {'d3pm', 'subs'} |
| if self.subs_masking: |
| assert self.parameterization == 'd3pm' |
|
|
| def on_load_checkpoint(self, checkpoint): |
| if self.ema: |
| self.ema.load_state_dict(checkpoint['ema']) |
| |
| |
| 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() |
| |
| |
| |
| |
| 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 |
| |
| |
| 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) |
| |
| |
| 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.config.loader.batch_size)}) |
| updated_dls.append( |
| torch.utils.data.DataLoader( |
| dl.dataset, |
| batch_size=self.config.loader.batch_size, |
| num_workers=self.config.loader.num_workers, |
| pin_memory=self.config.loader.pin_memory, |
| 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 _subs_parameterization(self, logits, xt): |
| |
| logits[:, :, self.mask_index] += self.neg_infinity |
| |
| |
| |
| logits = logits - torch.logsumexp(logits, dim=-1, |
| keepdim=True) |
|
|
| |
| |
| |
| |
| unmasked_indices = (xt != self.mask_index) |
| logits[unmasked_indices] = self.neg_infinity |
| |
| logits[unmasked_indices, xt[unmasked_indices]] = 0 |
| return logits |
|
|
|
|
|
|
| def _d3pm_parameterization(self, logits): |
| if self.subs_masking: |
| logits[:, :, self.mask_index] += self.neg_infinity |
| logits = logits - torch.logsumexp(logits, dim=-1, |
| keepdim=True) |
| return logits |
|
|
| def _sedd_parameterization(self, logits, xt, sigma): |
| esigm1_log = torch.where( |
| sigma < 0.5, |
| torch.expm1(sigma), |
| sigma.exp() - 1).log().to(logits.dtype) |
| |
| |
| logits = logits - esigm1_log[:, None, None] - np.log( |
| logits.shape[-1] - 1) |
| |
| |
| logits = torch.scatter(logits, -1, xt[..., None], |
| torch.zeros_like(logits[..., :1])) |
| return logits |
|
|
| def _process_sigma(self, sigma): |
| if sigma is None: |
| assert self.parameterization == 'ar' |
| return 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 |
|
|
| 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) |
| |
| if self.parameterization == 'subs': |
| return self._subs_parameterization(logits=logits, |
| xt=x) |
| |
| elif self.parameterization == 'sedd': |
| return self._sedd_parameterization(logits=logits, |
| xt=x, |
| sigma=sigma) |
| elif self.parameterization == 'd3pm': |
| return self._d3pm_parameterization(logits=logits) |
|
|
| def _d3pm_loss(self, model_output, xt, x0, t): |
| dt = 1 / self.T |
|
|
| if torch.is_tensor(t): |
| t = t[:, None] |
| assert t.ndim == 2 |
| t = t.clamp(0., 1. - 1e-4) |
| alpha_t = 1 - t + torch.zeros_like(xt) |
| alpha_s = 1 - (t - dt) + torch.zeros_like(xt) |
|
|
| log_x_theta_at_x0 = torch.gather( |
| model_output, -1, x0[:, :, None]).squeeze(-1) |
| log_x_theta_at_m = model_output[:, :, self.mask_index] |
| x_theta_at_m = log_x_theta_at_m.exp() |
| |
| term_1_coef = dt / t |
| term_1_log_nr = torch.log(alpha_t * x_theta_at_m / t + 1) |
| term_1_log_dr = log_x_theta_at_x0 |
| |
| term_2_coef = 1 - dt / t |
| term_2_log_nr = term_1_log_nr |
| term_2_log_dr = torch.log(alpha_s * x_theta_at_m / (t - dt) + 1) |
|
|
| L_vb_masked = ( |
| term_1_coef * (term_1_log_nr - term_1_log_dr) |
| + term_2_coef * (term_2_log_nr - term_2_log_dr)) |
|
|
| L_vb = L_vb_masked * (xt == self.mask_index) |
|
|
| return self.T * L_vb |
|
|
| def _maybe_report_high_noise(self, losses, attention_mask, noise_levels): |
| if (not self.track_high_noise_loss |
| or not self.training |
| or noise_levels is None): |
| return |
| high_noise_mask = noise_levels > self.high_noise_threshold |
| if not bool(high_noise_mask.any()): |
| return |
| token_mask = None |
| if attention_mask is not None: |
| token_mask = attention_mask.to(losses.dtype) |
| noise_mask = high_noise_mask[:, None].to(losses.dtype) |
| combined_mask = noise_mask if token_mask is None else noise_mask * token_mask |
| token_count = combined_mask.sum() |
| if token_count.item() <= 0: |
| return |
| selected_losses = losses * combined_mask |
| loss_norm = torch.linalg.vector_norm(selected_losses) |
| total_loss = selected_losses.sum() |
| print( |
| '[HighNoiseLoss] ' |
| f'step={int(self.global_step)} ' |
| f'norm={loss_norm.item():.6f} ' |
| f'sum={total_loss.item():.6f} ' |
| f'tokens={int(token_count.item())}') |
|
|
| def _reject_high_noise_timesteps(self, t): |
| if (not self.skip_high_noise_training |
| or self.high_noise_threshold is None): |
| return t |
| threshold = float(self.high_noise_threshold) |
| if threshold >= 1.0: |
| return t |
| if threshold <= self.sampling_eps: |
| raise ValueError( |
| 'high_noise_threshold must be greater than sampling_eps ' |
| 'when skip_high_noise_training is enabled.') |
| high_mask = t > threshold |
| attempt = 0 |
| while bool(high_mask.any()) and attempt < 16: |
| num = int(high_mask.sum().item()) |
| t_new = self._sample_t(num, t.device) |
| t[high_mask] = t_new |
| high_mask = t > threshold |
| attempt += 1 |
| if bool(high_mask.any()): |
| t[high_mask] = threshold - 1e-6 |
| return t |
|
|
| def _compute_loss(self, batch, prefix): |
| |
| losses, preds = self._loss(batch['input_ids'], batch['attention_mask']) |
| loss = losses.loss |
|
|
|
|
| if prefix == 'train': |
| self.train_metrics.update(losses.nlls.detach(), losses.token_mask.detach()) |
| metrics = self.train_metrics |
| elif prefix == 'val': |
| self.valid_metrics.update(losses.nlls.detach(), losses.token_mask.detach()) |
| metrics = self.valid_metrics |
| else: |
| raise ValueError(f'Invalid prefix: {prefix}') |
|
|
| self.log_dict(metrics, |
| on_step=False, |
| on_epoch=True, |
| sync_dist=True) |
| return loss |
|
|
| 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 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') |
|
|
| @torch.no_grad() |
| def on_validation_epoch_end(self): |
| if ((self.config.eval.compute_perplexity_on_sanity |
| or not self.trainer.sanity_checking) |
| and self.config.eval.generate_samples |
| and not self.parameterization == 'ar'): |
| |
| samples, text_samples = None, None |
| for _ in range( |
| self.config.sampling.num_sample_batches): |
| samples = self._sample(stride_length=self.config.model.length) |
| |
| text_samples = samples.tolist() |
| |
| if self.config.eval.compute_generative_perplexity: |
| self.compute_generative_perplexity(text_samples) |
| if self.trainer.global_rank == 0 and hasattr( |
| self.trainer.logger, 'log_table'): |
| |
| text_samples = text_samples[ |
| : self.config.sampling.num_sample_log] |
|
|
| self.trainer.logger.log_table( |
| key=f'samples@global_step{self.global_step}', |
| columns=['Generated Samples'], |
| data=[[s] for s in text_samples]) |
| if self.config.eval.compute_generative_perplexity: |
| self.log('val/gen_ppl', |
| self.gen_ppl_metric, |
| on_epoch=True, |
| on_step=False, |
| sync_dist=True) |
| if self.ema: |
| self.ema.restore( |
| itertools.chain(self.backbone.parameters(), |
| self.noise.parameters())) |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| def configure_optimizers(self): |
| |
| |
| |
| |
| optimizer = torch.optim.AdamW( |
| itertools.chain(self.backbone.parameters(), |
| self.noise.parameters()), |
| lr=self.config.optim.lr, |
| betas=(self.config.optim.beta1, |
| self.config.optim.beta2), |
| eps=self.config.optim.eps, |
| weight_decay=self.config.optim.weight_decay) |
|
|
| scheduler = hydra.utils.instantiate( |
| self.config.lr_scheduler, optimizer=optimizer) |
| scheduler_dict = { |
| 'scheduler': scheduler, |
| 'interval': 'step', |
| 'monitor': 'val/loss', |
| 'name': 'trainer/lr', |
| } |
| return [optimizer], [scheduler_dict] |
|
|
| @torch.no_grad() |
| def eval_retokenize(self, text_samples, max_length): |
| """Retokenizes samples for the eval model. |
| |
| Args: |
| text_samples: List of sentences generated by the model. |
| Returns: |
| samples: Samples re-tokenized for the eval model |
| attn_mask: Attention mask for the eval model |
| eval_context_size: Size of the context for the eval model |
| """ |
| if 'llama2' in self.gen_ppl_eval_model_name_or_path: |
| tokenizer_kwargs = { |
| 'text_samples': text_samples, |
| 'return_tensors': 'pt', |
| 'return_token_type_ids': False, |
| 'return_attention_mask': True, |
| 'truncation': True, |
| 'padding': True, |
| 'max_length': max_length, |
| } |
| eval_context_size = 4096 |
| else: |
| tokenizer_kwargs = { |
| 'return_tensors': 'pt', |
| 'return_token_type_ids': False, |
| 'return_attention_mask': True, |
| 'truncation': True, |
| 'padding': True, |
| 'max_length': max_length, |
| } |
| eval_context_size = 1024 |
| samples = self.eval_model_tokenizer( |
| text_samples, ** tokenizer_kwargs) |
| attn_mask = samples['attention_mask'] |
| samples = samples['input_ids'] |
| if 'llama2' not in self.gen_ppl_eval_model_name_or_path: |
| attn_mask = attn_mask.to(self.device) |
| samples = samples.to(self.device) |
| return samples, attn_mask, eval_context_size |
|
|
| @torch.no_grad() |
| def compute_generative_perplexity( |
| self, |
| text_samples: typing.List[str], |
| retokenize: bool = True, |
| max_length: typing.Optional[int] = None) -> None: |
| """Compute the generative perplexity of the model. |
| |
| Args: |
| text_samples: List of sentences generated by the model. |
| |
| Returns: |
| Perplexity of the generated text under a different |
| pre-trained AR model (e.g., GPT2). |
| """ |
| os.environ['TOKENIZERS_PARALLELISM'] = 'false' |
| eval_model = transformers.AutoModelForCausalLM.from_pretrained( |
| self.gen_ppl_eval_model_name_or_path).eval() |
| if max_length is None: |
| max_length = self.config.model.length |
| if 'llama2' not in self.gen_ppl_eval_model_name_or_path: |
| eval_model = eval_model.to(self.device) |
| |
| if retokenize: |
| (samples, attn_mask, |
| eval_context_size) = self.eval_retokenize( |
| text_samples, max_length=max_length) |
| else: |
| samples = text_samples |
| attn_mask = torch.ones(samples.shape).to(self.device) |
| eval_context_size = samples.shape[-1] |
| batch_size = min( |
| self.config.eval.perplexity_batch_size, |
| samples.shape[0]) |
| num_batches = samples.shape[0] // batch_size |
| for i in range(num_batches): |
| _samples = torch.split( |
| samples[i * batch_size: (i + 1) * batch_size], |
| eval_context_size, |
| dim=-1) |
| _attn_mask = torch.split( |
| attn_mask[i * batch_size: (i + 1) * batch_size], |
| eval_context_size, |
| dim=-1) |
| for (sample_chunk, attn_mask_chunk) in zip( |
| _samples, _attn_mask): |
| logits = eval_model( |
| sample_chunk, attention_mask=attn_mask_chunk)[0] |
| logits = logits.transpose(-1, -2) |
| |
| nlls = F.cross_entropy(logits[..., :-1], |
| sample_chunk[..., 1:], |
| reduction='none') |
| first_eos = (sample_chunk == self.eval_model_tokenizer\ |
| .eos_token_id).cumsum(-1) == 1 |
| token_mask = ( |
| sample_chunk |
| != self.eval_model_tokenizer.eos_token_id) |
| self.gen_ppl_metric.update( |
| nlls, first_eos[..., 1:] + token_mask[..., 1:]) |
|
|
| 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 _sample_prior(self, *batch_dims): |
| total_len = batch_dims[-1] |
| result = self.mask_index * torch.ones( |
| *batch_dims, dtype=torch.int64, device=self.device |
| ) |
| |
| |
|
|
| k = 5 |
| n = total_len //k |
| ni = total_len % k |
| for b in range(result.shape[0]): |
| token_list = [] |
|
|
| if ni == 2: |
| |
| token_list.append(self.bos_index) |
| for i in range(n): |
| token_list.extend([self.boi_index]+[self.mask_index] * (k-1)) |
| token_list.append(self.eos_index) |
|
|
| elif ni==0 or ni==4: |
| |
| half_n = math.ceil(n // 2) |
| token_list.append(self.bos_index) |
| for i in range(half_n): |
| token_list.extend([self.boi_index]+[self.mask_index] * (k-1)) |
| token_list.append(self.eos_index) |
| |
| token_list.append(self.bos_index) |
| for i in range(n-half_n): |
| token_list.extend([self.boi_index]+[self.mask_index] * (k-1)) |
| token_list.append(self.eos_index) |
| else: |
| raise ValueError(f"Unsupported remainder ni = {ni} for sequence length {total_len}") |
|
|
| if len(token_list) != total_len: |
| raise ValueError(f"Expected sequence length {total_len}, got {len(token_list)}") |
| result[b, :] = torch.tensor(token_list, dtype=torch.int64, device=self.device) |
|
|
| return result |
|
|
| def _apply_illegal_mask(self, logits, tokenizer, stride_length): |
| """ |
| logits: (batch, seq_len, vocab_size) |
| Apply masking to filter out invalid tokens based on `pos % 5`. |
| The first position (bos), the last position (eos), and the first token of every 5-token block (boi) |
| should remain unchanged. |
| |
| If `cir` is None, it means each item corresponds to a single token(no RQ-VAE) and `boi` does not exist. |
| In this case, only ensure that neither `bos` nor `eos` is generated. |
| |
| Constraints are applied only to the positions that are actually being unmasked at the current step |
| to avoid affecting the probability distribution of other masked positions. |
| """ |
|
|
| masked_logits = logits.clone() |
| codebook_size = tokenizer.config['rq_codebook_size'] |
| seq_len = masked_logits.size(1) |
| vocab_size = masked_logits.size(-1) |
|
|
| mask_index = self.mask_index |
| active_positions = ( |
| masked_logits.sum(dim=-1) - masked_logits[..., mask_index] |
| ) > 0 |
|
|
| if not bool(active_positions.any()): |
| return masked_logits |
|
|
| legal_mask = torch.ones_like(masked_logits, dtype=torch.bool) |
| start_index = seq_len - stride_length |
|
|
| if self.config['cir'] == 'none': |
| legal_indices = torch.arange(1, vocab_size - 2, device=logits.device) |
| for pos in range(start_index, seq_len): |
| active_batches = active_positions[:, pos] |
| if not bool(active_batches.any()): |
| continue |
| if pos == start_index or pos == seq_len - 1: |
| continue |
| legal_mask[active_batches, pos, :] = False |
| legal_mask[active_batches, pos, legal_indices] = True |
| else: |
| k = 5 |
| for pos in range(start_index, seq_len): |
| active_batches = active_positions[:, pos] |
| if not bool(active_batches.any()): |
| continue |
| |
| if pos == start_index or pos == seq_len - 1 or pos % k == 3: |
| continue |
|
|
| if pos % k == 4: |
| |
| legal_range_start = 1 |
| legal_range_end = codebook_size + 1 |
| elif pos % k == 0: |
| |
| legal_range_start = codebook_size + 1 |
| legal_range_end = 2 * codebook_size + 1 |
| elif pos % k == 1: |
| |
| legal_range_start = 2 * codebook_size + 1 |
| legal_range_end = 3 * codebook_size + 1 |
| else: |
| |
| legal_range_start = 3 * codebook_size + 1 |
| legal_range_end = 4 * codebook_size + 1 |
| |
|
|
| temp_mask = torch.zeros(vocab_size, dtype=torch.bool, device=logits.device) |
|
|
| temp_mask[legal_range_start:legal_range_end] = True |
|
|
| temp_mask[0] = True |
| temp_mask[-1] = True |
| legal_mask[active_batches, pos, :] = temp_mask |
|
|
| masked_logits[~legal_mask] = -1e9 |
| return masked_logits |
|
|
| def _ddpm_caching_update(self, x, t, dt, stride_length, p_x0=None): |
| assert self.config.noise.type == 'loglinear' |
| sigma_t, _ = self.noise(t) |
| if t.ndim > 1: |
| t = t.squeeze(-1) |
| assert t.ndim == 1 |
| move_chance_t = t[:, None, None] |
| move_chance_s = (t - dt)[:, None, None] |
| assert move_chance_t.ndim == 3, move_chance_t.shape |
|
|
| if p_x0 is None: |
| p_x0 = self.forward(x, sigma_t).exp() |
| |
| assert move_chance_t.ndim == p_x0.ndim |
| q_xs = p_x0 * (move_chance_t - move_chance_s) |
| q_xs[:, :, self.mask_index] = move_chance_s[:, :, 0] |
| |
| |
| q_xs = self._apply_illegal_mask(q_xs, self.tokenizer,stride_length) |
| |
| _x = _sample_categorical(q_xs) |
| |
| copy_flag = (x != self.mask_index).to(x.dtype) |
| return p_x0, copy_flag * x + (1 - copy_flag) * _x |
|
|
| def _ddpm_update(self, x, t, dt): |
| sigma_t, _ = self.noise(t) |
| sigma_s, _ = self.noise(t - dt) |
| if sigma_t.ndim > 1: |
| sigma_t = sigma_t.squeeze(-1) |
| if sigma_s.ndim > 1: |
| sigma_s = sigma_s.squeeze(-1) |
| assert sigma_t.ndim == 1, sigma_t.shape |
| assert sigma_s.ndim == 1, sigma_s.shape |
| move_chance_t = 1 - torch.exp(-sigma_t) |
| move_chance_s = 1 - torch.exp(-sigma_s) |
| move_chance_t = move_chance_t[:, None, None] |
| move_chance_s = move_chance_s[:, None, None] |
| unet_conditioning = sigma_t |
| log_p_x0 = self.forward(x, unet_conditioning) |
| assert move_chance_t.ndim == log_p_x0.ndim |
| |
| |
| |
| q_xs = log_p_x0.exp() * (move_chance_t |
| - move_chance_s) |
| q_xs[:, :, self.mask_index] = move_chance_s[:, :, 0] |
| _x = _sample_categorical(q_xs) |
|
|
| copy_flag = (x != self.mask_index).to(x.dtype) |
| return copy_flag * x + (1 - copy_flag) * _x |
|
|
| def _ar_sampler(self, bsz): |
| |
| num_pred_tokens = self.config.model.length - 1 |
| x = torch.zeros( |
| (bsz, num_pred_tokens + 1), |
| dtype=torch.long, |
| device=self.device) |
| x[:, 0] = self.tokenizer.bos_token_id |
| |
| noise = (torch.distributions.Gumbel(0, 1) |
| .sample((bsz, num_pred_tokens, self.vocab_size)) |
| .to(self.device)) |
| for i in range(num_pred_tokens): |
| next_logits = self.forward(x[:, :i + 1], None)[:, -1] |
| y = (next_logits + noise[:, i]).argmax(-1) |
| x[:, i + 1] = y |
| return x |
|
|
| @torch.no_grad() |
| def _sample(self, stride_length, num_steps=None, eps=1e-5): |
| """Generate samples from the model.""" |
| batch_size_per_gpu = self.config.loader.eval_batch_size |
| if self.parameterization == 'ar': |
| return self._ar_sampler(batch_size_per_gpu) |
| |
| if num_steps is None: |
| num_steps = self.config.sampling.steps |
| x = self._sample_prior( |
| batch_size_per_gpu, |
| self.config.model.length).to(self.device) |
| timesteps = torch.linspace( |
| 1, eps, num_steps + 1, device=self.device) |
| dt = (1 - eps) / num_steps |
| p_x0_cache = None |
|
|
| for i in range(num_steps): |
| t = timesteps[i] * torch.ones( |
| x.shape[0], 1, device=self.device) |
| if self.sampler == 'ddpm': |
| x = self._ddpm_update(x, t, dt) |
| elif self.sampler == 'ddpm_cache': |
| p_x0_cache, x_next = self._ddpm_caching_update( |
| x, t, dt, stride_length, p_x0=p_x0_cache) |
| if (not torch.allclose(x_next, x) |
| or self.time_conditioning): |
| |
| p_x0_cache = None |
| x = x_next |
| else: |
| x = self._analytic_update(x, t, dt) |
|
|
| if self.config.sampling.noise_removal: |
| t = timesteps[-1] * torch.ones(x.shape[0], 1, |
| device=self.device) |
| if self.sampler == 'analytic': |
| x = self._denoiser_update(x, t) |
| else: |
| unet_conditioning = self.noise(t)[0] |
| |
| x = self.forward(x, unet_conditioning).argmax(dim=-1) |
| return x |
|
|
| def restore_model_and_sample(self, num_steps, eps=1e-5): |
| """Generate samples from the model.""" |
| |
| 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() |
| samples = self._sample(num_steps=num_steps, eps=eps) |
| if self.ema: |
| self.ema.restore(itertools.chain( |
| self.backbone.parameters(), |
| self.noise.parameters())) |
| self.backbone.train() |
| self.noise.train() |
| return samples |
|
|
| def get_score(self, x, sigma): |
| model_output = self.forward(x, sigma) |
| if self.parameterization == 'subs': |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| log_k = - torch.log(torch.expm1(sigma)).squeeze(-1) |
| assert log_k.ndim == 1 |
| |
| masked_score = model_output + log_k[:, None, None] |
| masked_score[:, :, self.mask_index] = 0 |
|
|
| unmasked_score = self.neg_infinity * torch.ones_like( |
| model_output) |
| unmasked_score = torch.scatter( |
| unmasked_score, |
| -1, |
| x[..., None], |
| torch.zeros_like(unmasked_score[..., :1])) |
| unmasked_score[:, :, self.mask_index] = - ( |
| log_k[:, None] * torch.ones_like(x)) |
| |
| masked_indices = (x == self.mask_index).to( |
| model_output.dtype)[:, :, None] |
| model_output = ( |
| masked_score * masked_indices |
| + unmasked_score * (1 - masked_indices)) |
| return model_output.exp() |
|
|
| def _staggered_score(self, score, dsigma): |
| score = score.clone() |
| extra_const = (1 - dsigma.exp()) * score.sum(dim=-1) |
| score *= dsigma.exp()[:, None] |
| score[..., self.mask_index] += extra_const |
| return score |
|
|
| def _analytic_update(self, x, t, step_size): |
| curr_sigma, _ = self.noise(t) |
| next_sigma, _ = self.noise(t - step_size) |
| dsigma = curr_sigma - next_sigma |
| score = self.get_score(x, curr_sigma) |
| stag_score = self._staggered_score(score, dsigma) |
| probs = stag_score * self._transp_transition(x, dsigma) |
| return _sample_categorical(probs) |
|
|
| def _denoiser_update(self, x, t): |
| sigma, _ = self.noise(t) |
| score = self.get_score(x, sigma) |
| stag_score = self._staggered_score(score, sigma) |
| probs = stag_score * self._transp_transition(x, sigma) |
| probs[..., self.mask_index] = 0 |
| samples = _sample_categorical(probs) |
| return samples |
|
|
| def _transp_transition(self, i, sigma): |
| sigma = _unsqueeze(sigma, reference=i[..., None]) |
| edge = torch.exp(-sigma) * F.one_hot( |
| i, num_classes=self.vocab_size) |
| edge += torch.where(i == self.mask_index, |
| 1 - torch.exp(-sigma).squeeze(-1), |
| 0)[..., None] |
| return edge |
|
|
| 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 |
| if self.importance_sampling: |
| return self.noise.importance_sampling_transformation(t) |
| return t |
|
|
| def _maybe_sub_sample(self, x0, attention_mask): |
| seqlen = x0.shape[1] |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| input_tokens = x0 |
| output_tokens = None |
| new_attention_mask = attention_mask |
| return input_tokens, output_tokens, new_attention_mask |
|
|
| def _reconstruction_loss(self, x0): |
| t0 = torch.zeros(x0.shape[0], dtype=self.dtype, |
| device=self.device) |
| assert self.config.noise.type == 'loglinear' |
| |
| unet_conditioning = self.noise(t0)[0][:, None] |
| model_output_t0 = self.forward(x0, unet_conditioning) |
| return - torch.gather(input=model_output_t0, |
| dim=-1, |
| index=x0[:, :, None]).squeeze(-1) |
|
|
| def _forward_pass_diffusion(self, x0, attention_mask): |
| |
| t = self._sample_t(x0.shape[0], x0.device) |
| if self.training: |
| t = self._reject_high_noise_timesteps(t) |
| if self.T > 0: |
| t = (t * self.T).to(torch.int) |
| t = t / self.T |
| |
| t += (1 / self.T) |
|
|
| if self.change_of_variables: |
| unet_conditioning = t[:, None] |
| f_T = torch.log1p(- torch.exp(- self.noise.sigma_max)) |
| f_0 = torch.log1p(- torch.exp(- self.noise.sigma_min)) |
| move_chance = torch.exp(f_0 + t * (f_T - f_0)) |
| move_chance = move_chance[:, None] |
| else: |
| sigma, dsigma = self.noise(t) |
| unet_conditioning = sigma[:, None] |
| move_chance = 1 - torch.exp(-sigma[:, None]) |
| special_ids_list = [self.eos_index, self.bos_index] + ([self.boi_index] if self.boi_index is not None else []) |
| special_ids = torch.tensor(special_ids_list, device=x0.device) |
| special_mask = torch.isin(x0, special_ids) |
| move_chance = move_chance.masked_fill(special_mask, 0.0) |
| xt = self.q_xt(x0, move_chance) |
| model_output = self.forward(xt, unet_conditioning) |
| utils.print_nans(model_output,'model_output') |
|
|
| |
| |
| |
| |
| if self.T > 0: |
| diffusion_loss = self._d3pm_loss( |
| model_output=model_output, xt=xt, x0=x0, t=t) |
|
|
| |
| |
| |
| reconstruction_loss = 0 |
| loss = reconstruction_loss + diffusion_loss |
| self._maybe_report_high_noise( |
| losses=loss, |
| attention_mask=attention_mask, |
| noise_levels=t.detach()) |
| return loss |
| |
| |
| log_p_theta = torch.gather( |
| input=model_output, |
| dim=-1, |
| index=x0[:, :, None]).squeeze(-1) |
| |
| if self.change_of_variables or self.importance_sampling: |
| loss = log_p_theta * torch.log1p( |
| - torch.exp(- self.noise.sigma_min)) |
| else: |
| loss = - log_p_theta * ( |
| dsigma / torch.expm1(sigma))[:, None] |
|
|
| self._maybe_report_high_noise( |
| losses=loss, |
| attention_mask=attention_mask, |
| noise_levels=t.detach()) |
| return loss |
|
|
| def _loss(self, x0, attention_mask): |
| (input_tokens, output_tokens, |
| attention_mask) = self._maybe_sub_sample( |
| x0, attention_mask) |
|
|
| if self.parameterization == 'ar': |
| logprobs = self.backbone(input_tokens, None) |
| loss = - logprobs.gather( |
| -1, output_tokens[:, :, None])[:, :, 0] |
| else: |
| loss = self._forward_pass_diffusion(input_tokens, attention_mask) |
| |
| |
| nlls = loss |
| count = loss.shape[1] |
|
|
| batch_nll = nlls.sum() |
| token_nll = batch_nll / count |
|
|
| return Loss(loss=token_nll, |
| nlls=nlls, |
| token_mask=attention_mask), output_tokens |
|
|
| def _score_entropy(self, log_score, sigma, xt, x0): |
| """Computes the SEDD loss. |
| |
| Args: |
| log_score: float torch.Tensor with shape (batch_size, |
| diffusion_model_input_length, vocab_size), |
| log score, output of the denoising network. |
| xt: int torch.Tensor with shape (batch_size, |
| diffusion_model_input_length), input. |
| x0: int torch.Tensor with shape (batch_size, |
| diffusion_model_input_length), input. |
| sigma: float torch.Tensor with shape (batch_size, 1). |
| |
| Returns: |
| loss with shape (batch_size, diffusion_model_input_length) |
| """ |
| masked_indices = xt == self.mask_index |
|
|
| expsig_minus_1 = torch.expm1(sigma).expand_as(xt) |
| q_ratio = 1 / expsig_minus_1[masked_indices] |
|
|
| words_that_were_masked = x0[masked_indices] |
|
|
| neg_term = q_ratio * torch.gather( |
| log_score[masked_indices], |
| -1, |
| words_that_were_masked[..., None]).squeeze(-1) |
| score = log_score[masked_indices].exp() |
| if self.mask_index == self.vocab_size - 1: |
| pos_term = score[:, :-1].sum(dim=-1) |
| else: |
| pos_term = score[:, : self.mask_index].sum( |
| dim=-1) + score[:, self.mask_index + 1:].sum(dim=-1) |
| const = q_ratio * (q_ratio.log() - 1) |
|
|
| entropy = torch.zeros(* xt.shape, device=xt.device) |
| entropy[masked_indices] += pos_term - neg_term + const |
| return entropy |
|
|
| @torch.no_grad |
| def sample_subs_guidance( |
| self, input_ids, stride_length, num_strides, dt=0.001): |
| n_samples = input_ids.shape[0] |
| ones = torch.ones(n_samples, dtype=self.dtype, |
| device=self.device) |
|
|
| num_steps = int(1 / dt) |
| sampling_steps = 0 |
| intermediate_tokens = [] |
| |
| target = input_ids.to(self.device) |
| |
| for _ in range(num_strides): |
| test_list = [] |
| p_x0_cache = None |
| x_tmp = self._sample_prior( |
| n_samples, |
| stride_length).to(self.device) |
| x = torch.cat((target,x_tmp),dim=1) |
| |
| |
| |
| for i in range(num_steps + 1): |
| p_x0_cache, x_next = self._ddpm_caching_update( |
| x=x, t=(1 - i * dt) * ones, dt=dt, stride_length=stride_length, p_x0=p_x0_cache) |
| |
| if (not torch.allclose(x_next, x) |
| or self.time_conditioning): |
| p_x0_cache = None |
| sampling_steps += 1 |
| x = x_next |
| x = self.forward(x, 0 * ones).argmax(dim=-1) |
| |
| intermediate_tokens.append( |
| x[:, :-stride_length].cpu().numpy()) |
| target = x[:, -stride_length:] |
| |
| intermediate_tokens.append(target.cpu().numpy()) |
| |
| sequence_lengths = (( |
| np.concatenate(intermediate_tokens, axis=1)[:, 1:] |
| == self.tokenizer.eos_token).cumsum(-1) == 0).sum(-1) |
|
|
| |
| |
| |
| |
| |
| return (sampling_steps, intermediate_tokens, |
| sequence_lengths) |
|
|
|
|
| def restore_model_and_semi_ar_sample( |
| self, input_ids, stride_length, num_strides, dt=0.001): |
| """Generate samples from the model.""" |
| |
| 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() |
| (sampling_steps, samples, |
| sequence_lengths) = self.sample_subs_guidance( |
| input_ids = input_ids, |
| stride_length=stride_length, |
| num_strides=num_strides, |
| dt=dt) |
| if self.ema: |
| self.ema.restore(itertools.chain( |
| self.backbone.parameters(), |
| self.noise.parameters())) |
| self.backbone.train() |
| self.noise.train() |
| return sampling_steps, samples, sequence_lengths |
|
|