Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- data/fairseq/examples/adaptive_span/README.mdAdded+90
- data/fairseq/examples/adaptive_span/__init__.pyAdded+19
- data/fairseq/examples/adaptive_span/adagrad_with_grad_clip.pyAdded+128
- data/fairseq/examples/adaptive_span/adaptive_span_attention.pyAdded+160
- data/fairseq/examples/adaptive_span/adaptive_span_loss.pyAdded+107
- data/fairseq/examples/adaptive_span/adaptive_span_model.pyAdded+263
- data/fairseq/examples/adaptive_span/adaptive_span_model_wrapper.pyAdded+145
- data/fairseq/examples/adaptive_span/truncated_bptt_lm_task.pyAdded+285
- data/fairseq/examples/backtranslation/README.mdAdded+297
- data/fairseq/examples/backtranslation/deduplicate_lines.pyAdded+41
- data/fairseq/examples/backtranslation/extract_bt_data.pyAdded+72
- data/fairseq/examples/backtranslation/prepare-de-monolingual.shAdded+98
- data/fairseq/examples/backtranslation/prepare-wmt18en2de.shAdded+135
- data/fairseq/examples/backtranslation/sacrebleu.shAdded+37
- data/fairseq/examples/backtranslation/tokenized_bleu.shAdded+46
- data/fairseq/examples/cross_lingual_language_model/README.mdAdded+77
- data/fairseq/examples/discriminative_reranking_nmt/README.mdAdded+202
- data/fairseq/examples/discriminative_reranking_nmt/__init__.pyAdded+1
- data/fairseq/examples/discriminative_reranking_nmt/config/deen.yamlAdded+56
- data/fairseq/examples/discriminative_reranking_nmt/criterions/__init__.pyAdded+6
- data/fairseq/examples/discriminative_reranking_nmt/criterions/discriminative_reranking_criterion.pyAdded+139
- data/fairseq/examples/discriminative_reranking_nmt/drnmt_rerank.pyAdded+364
- data/fairseq/examples/discriminative_reranking_nmt/models/__init__.pyAdded+6
- data/fairseq/examples/discriminative_reranking_nmt/models/discriminative_reranking_model.pyAdded+365
- data/fairseq/examples/discriminative_reranking_nmt/scripts/prep_data.pyAdded+136
- data/fairseq/examples/discriminative_reranking_nmt/tasks/__init__.pyAdded+6
- data/fairseq/examples/discriminative_reranking_nmt/tasks/discriminative_reranking_task.pyAdded+490
- data/fairseq/examples/mbart/README.mdAdded+123
- data/fairseq/examples/normformer/README.mdAdded+70
- data/fairseq/examples/normformer/train_lm.shAdded+78
- data/fairseq/examples/shuffled_word_order/README.finetuning.mdAdded+135
- data/fairseq/examples/shuffled_word_order/README.mdAdded+94
- data/fairseq/examples/simultaneous_translation/README.mdAdded+5
- data/fairseq/examples/simultaneous_translation/__init__.pyAdded+6
- data/fairseq/examples/simultaneous_translation/docs/ende-mma.mdAdded+74
- data/fairseq/examples/simultaneous_translation/docs/enja-waitk.mdAdded+106
- data/fairseq/examples/simultaneous_translation/eval/agents/simul_t2t_enja.pyAdded+226
- data/fairseq/examples/simultaneous_translation/models/__init__.pyAdded+15
- data/fairseq/examples/simultaneous_translation/models/convtransformer_simul_trans.pyAdded+204
- data/fairseq/examples/simultaneous_translation/models/transformer_monotonic_attention.pyAdded+302
- data/fairseq/examples/simultaneous_translation/modules/__init__.pyAdded+23
- data/fairseq/examples/simultaneous_translation/modules/fixed_pre_decision.pyAdded+190
- data/fairseq/examples/simultaneous_translation/modules/monotonic_multihead_attention.pyAdded+520
- data/fairseq/examples/simultaneous_translation/modules/monotonic_transformer_layer.pyAdded+182
- data/fairseq/examples/simultaneous_translation/tests/test_alignment_train.pyAdded+88
- data/fairseq/examples/simultaneous_translation/tests/test_text_models.pyAdded+407
- data/fairseq/examples/simultaneous_translation/utils/__init__.pyAdded+14
- data/fairseq/examples/simultaneous_translation/utils/functions.pyAdded+125
- data/fairseq/examples/simultaneous_translation/utils/monotonic_attention.pyAdded+180
- data/fairseq/examples/simultaneous_translation/utils/p_choose_strategy.pyAdded+126
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990# Adaptive Span
Adaptive Span is a novel self-attention mechanism that can learn its optimalattention span. This allows us to extend significantly the maximum context sizeused in Transformer, while maintaining control over their memory footprintand computational time. It uses the Truncated BPTT technique for training,as in [transformerXL](https://github.com/pytorch/fairseq/blob/main/examples/truncated_bptt/README.md).
Adaptive Span was introduced by paper:[Adaptive Attention Span in Transformers](https://arxiv.org/abs/1905.07799),which achieved state-of-the-art language modeling results at the time of publication.
We manage to reproduce their result in fairseq and keep most of the[original implementation](https://github.com/facebookresearch/adaptive-span) untouched.You can refer to the their sweep file as well if any combination of hyperparameter is not clear.
##### 0. Setup
First you need to process the Enwik8 dataset, we use the pre-tokenized datasetfrom [adaptive span paper](https://github.com/facebookresearch/adaptive-span/blob/master/get_data.sh).You can download the dataset, and then run:```bashfairseq-preprocess --only-source --trainpref ~/data/enwik8/train.txt \ --validpref ~/data/enwik8/valid.txt --testpref ~/data/enwik8/test.txt \ --destdir ~/data/enwik8/data-bin/ --joined-dictionary --workers 20```
##### 1. Train a Adaptive Span model on Enwik8
We will train a 12-layer Adaptive Span model following the [hyperparametersused in the originalpaper](https://github.com/facebookresearch/adaptive-span/blob/master/experiments/enwik8.sh).
The following command assumes 4 GPUs, so that the total batch size is 64sequences (4 x 16). Training should take 2-3 days on 4 V100 GPUs:```bashCUDA_VISIBLE_DEVICES=0,1,2,3 fairseq-train \ --user-dir examples/adaptive_span \ --data ~/data/enwik8/data-bin/ \ --fp16 --fp16-no-flatten-grads --max-update 600000 \ --task truncated_bptt_lm --tokens-per-sample 512 --arch adaptive_span \ --n-layer 12 --d-model 512 --n-head 8 --d-inner 2048 --dropout 0.3 \ --attn-span 8192 --optimizer adagrad_with_grad_clip --adagrad-clip 0.03 \ --validate-interval-updates 1000 \ --lr-scheduler fixed --warmup-updates 32000 --batch-size-valid 32 \ --lr 0.07 --criterion adaptive_span_loss --batch-size 16 --update-freq 1 \ --seed 2 --log-format json --log-interval 25 --aux-loss-scaler 5e-07```This should land around 1.05 on validation, 1.03 on test. You can lower the--aux-loss-scaler for better performance (longer span). It gives ~0.03 bpcimprovement to the transformerXL baseline here.If training on a single GPU, set `--update-freq=4` to accumulate 4x gradientsand simulate training on 4 GPUs.You can also reproduce the transformerXL result on enwik8 using this code base.It should land around 1.06 on test,matching the [original paper](https://github.com/kimiyoung/transformer-xl/blob/master/pytorch/run_enwik8_base.sh).You can try by```bashCUDA_VISIBLE_DEVICES=0,1,2,3 fairseq-train \ --user-dir examples/truncated_bptt \ ~/data/enwik8/data-bin/ \ --task truncated_bptt_lm --fp16 --max-update 400000 \ --tokens-per-sample 512 --arch transformer_xl --n-layer 12 \ --d-model 512 --n-head 8 --d-head 64 --d-inner 2048 --dropout 0.1 \ --dropatt 0.0 --mem-len 512 --optimizer adam --clip-norm 0.25 \ --lr-scheduler cosine --warmup-updates 0 \ --lr 0.0 --lr 0.00025 --batch-size 15 \ --update-freq 1 --seed 2 --log-format json --log-interval 25 \ --fp16```
##### 2. EvaluateFor Adaptive Span:```bashfairseq-eval-lm ~/data/enwik8/data-bin/ --path model/checkpoint_best.pt \ --user-dir examples/adaptive_span \ --task truncated_bptt_lm --batch-size 8 --tokens-per-sample 512 --gen-subset test```For Transformer-XL evaluation:```bashfairseq-eval-lm ~/data/enwik8/data-bin/ --path model/checkpoint_best.pt \ --user-dir examples/truncated_bptt/ --task truncated_bptt_lm --batch-size 8 \ --tokens-per-sample 80 \ --model-overrides '{"mem_len":2100,"clamp_len":820,"same_length":True}' \ --gen-subset valid```
*Note:* During training the model saw 512 tokens of context(``--tokens-per-sample=512``), with batch size 8. These settings match the evaluationsettings from [the originalpaper](https://github.com/facebookresearch/adaptive-span/blob/master/experiments/enwik8.sh).12345678910111213141516171819# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import importlibimport os
# automatically import any Python files in the current directorycur_dir = os.path.dirname(__file__)for file in os.listdir(cur_dir): path = os.path.join(cur_dir, file) if ( not file.startswith("_") and not file.startswith(".") and (file.endswith(".py") or os.path.isdir(path)) ): mod_name = file[: file.find(".py")] if file.endswith(".py") else file module = importlib.import_module(__name__ + "." + mod_name)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
from torch.optim import Adagrad
from fairseq.optim import LegacyFairseqOptimizer, register_optimizer
@register_optimizer("adagrad_with_grad_clip")class FairseqAdagradWithGradClip(LegacyFairseqOptimizer): def __init__(self, args, params): super().__init__(args) self._optimizer = AdagradWithGradClip(params, **self.optimizer_config)
@staticmethod def add_args(parser): """Add optimizer-specific arguments to the parser.""" # fmt: off parser.add_argument('--weight-decay', '--wd', default=0.0, type=float, metavar='WD', help='weight decay') parser.add_argument('--adagrad-clip', default=0.0, type=float, metavar='D', help='internal grad clip') # fmt: on
@property def optimizer_config(self): """ Return a kwarg dictionary that will be used to override optimizer args stored in checkpoints. This allows us to load a checkpoint and resume training using a different set of optimizer args, e.g., with a different learning rate. """ return { "lr": self.args.lr[0], "weight_decay": self.args.weight_decay, "grad_clip": self.args.adagrad_clip, }
@property def supports_flat_params(self): return False
def _clip_grad(clr, grad, group_grad_clip): if group_grad_clip > 0: norm = grad.norm(2).item() if norm > group_grad_clip: clr *= group_grad_clip / (norm + 1e-10) return clr
class AdagradWithGradClip(Adagrad): """Adagrad algorithm with custom gradient clipping"""
def __init__( self, params, lr=1e-2, lr_decay=0, weight_decay=0, initial_accumulator_value=0, grad_clip=0, ): Adagrad.__init__( self, params, lr=lr, lr_decay=lr_decay, weight_decay=weight_decay, initial_accumulator_value=initial_accumulator_value, ) self.defaults["grad_clip"] = grad_clip self.param_groups[0].setdefault("grad_clip", grad_clip)
def step(self, closure=None): loss = None if closure is not None: loss = closure()
for group in self.param_groups: for p in group["params"]: if p.grad is None: continue
grad = p.grad.data state = self.state[p]
state["step"] += 1
if group["weight_decay"] != 0: if p.grad.data.is_sparse: raise RuntimeError( "weight_decay option is " "not compatible with sparse " "gradients" ) grad = grad.add(group["weight_decay"], p.data)
clr = group["lr"] / (1 + (state["step"] - 1) * group["lr_decay"])
# clip clr = _clip_grad(clr=clr, grad=grad, group_grad_clip=group["grad_clip"])
if grad.is_sparse: # the update is non-linear so indices must be unique grad = grad.coalesce() grad_indices = grad._indices() grad_values = grad._values() size = grad.size()
def make_sparse(values): constructor = grad.new if grad_indices.dim() == 0 or values.dim() == 0: return constructor().resize_as_(grad) return constructor(grad_indices, values, size)
state["sum"].add_(make_sparse(grad_values.pow(2))) std = state["sum"]._sparse_mask(grad) std_values = std._values().sqrt_().add_(1e-10) p.data.add_(-clr, make_sparse(grad_values / std_values)) else: state["sum"].addcmul_(1, grad, grad) std = state["sum"].sqrt().add_(1e-10) p.data.addcdiv_(-clr, grad, std)
return loss123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.import math
import torchimport torch.nn as nnimport torch.nn.functional as F
class AdaptiveMask(nn.Module): """Soft masking function for adaptive size. It masks out the last K values of an input. The masking value goes from 1 to 0 gradually, so K can be learned with back-propagation. Args: max_size: maximum size (i.e. input dimension) ramp_size: size of the ramp going from 0 to 1 init_val: initial size proportion not to be masked out shape: learn multiple sizes independent of each other """
def __init__(self, max_size, ramp_size, init_val=0, shape=(1,)): nn.Module.__init__(self) self._max_size = max_size self._ramp_size = ramp_size self.current_val = nn.Parameter(torch.zeros(*shape) + init_val) mask_template = torch.linspace(1 - max_size, 0, steps=max_size) self.register_buffer("mask_template", mask_template)
def forward(self, x): mask = self.mask_template.float() + self.current_val.float() * self._max_size mask = mask / self._ramp_size + 1 mask = mask.clamp(0, 1) if x.size(-1) < self._max_size: # the input could have been trimmed beforehand to save computation mask = mask.narrow(-1, self._max_size - x.size(-1), x.size(-1)) x = (x * mask).type_as(x) return x
def get_current_max_size(self, include_ramp=True): current_size = math.ceil(self.current_val.max().item() * self._max_size) if include_ramp: current_size += self._ramp_size current_size = max(0, min(self._max_size, current_size)) return current_size
def get_current_avg_size(self, include_ramp=True): current_size = math.ceil( self.current_val.float().mean().item() * self._max_size ) if include_ramp: current_size += self._ramp_size current_size = max(0, min(self._max_size, current_size)) return current_size
def clamp_param(self): """this need to be called after each update""" self.current_val.data.clamp_(0, 1)
class AdaptiveSpan(nn.Module): """Adaptive attention span for Transformerself. This module learns an attention span length from data for each self-attention head. Args: attn_span: maximum attention span adapt_span_loss: loss coefficient for the span length adapt_span_ramp: length of the masking ramp adapt_span_init: initial size ratio adapt_span_cache: adapt cache size to reduce memory usage """
def __init__( self, attn_span, adapt_span_ramp, adapt_span_init, n_head, adapt_span_layer, **kargs ): nn.Module.__init__(self) self._max_span = attn_span self._n_head = n_head self._adapt_span_layer = adapt_span_layer if self._adapt_span_layer: self._mask = AdaptiveMask( max_size=self._max_span, ramp_size=adapt_span_ramp, init_val=adapt_span_init, ) else: self._mask = AdaptiveMask( max_size=self._max_span, ramp_size=adapt_span_ramp, init_val=adapt_span_init, shape=(n_head, 1, 1), )
def forward(self, attn, normalize=True): """mask attention with the right span""" # batch and head dimensions are merged together, so separate them first self.clamp_param() if self._adapt_span_layer: attn = self._mask(attn) else: B = attn.size(0) # batch size M = attn.size(1) # block size attn = attn.reshape(B // self._n_head, self._n_head, M, -1) attn = self._mask(attn) attn = attn.view(B, M, -1) return attn
def get_trim_len(self): """how much of memory can be trimmed to reduce computation""" L = self._max_span trim_len = min(L - 1, L - self._mask.get_current_max_size()) # too fine granularity might be bad for the memory management trim_len = math.floor(trim_len / 64) * 64 return trim_len
def trim_memory(self, query, key, value, key_pe): """trim out unnecessary memory beforehand to reduce computation""" trim_len = self.get_trim_len() cache_size = key.size(1) - query.size(1) trim_len_cache = trim_len - (self._max_span - cache_size) if trim_len_cache > 0: key = key[:, trim_len_cache:, :] value = value[:, trim_len_cache:, :] elif trim_len_cache < 0: # cache is too short! this happens when validation resumes # after a lot of updates. key = F.pad(key, [0, 0, -trim_len_cache, 0]) value = F.pad(value, [0, 0, -trim_len_cache, 0]) if trim_len > 0: if key_pe is not None: key_pe = key_pe[:, :, trim_len:] return key, value, key_pe
def get_cache_size(self): """determine how long the cache should be""" trim_len = self.get_trim_len() # give a buffer of 64 steps since a span might increase # in future updates return min(self._max_span, self._max_span - trim_len + 64)
def get_loss(self): """a loss term for regularizing the span length""" return self._max_span * self._mask.current_val.float().mean()
def get_current_max_span(self): return self._mask.get_current_max_size()
def get_current_avg_span(self): return self._mask.get_current_avg_size()
def clamp_param(self): self._mask.clamp_param()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import mathfrom dataclasses import dataclass
import torch.nn.functional as Ffrom fairseq import utilsfrom fairseq.logging import metricsfrom fairseq.criterions import register_criterionfrom fairseq.criterions.cross_entropy import CrossEntropyCriterionfrom fairseq.dataclass import FairseqDataclassfrom omegaconf import II
@dataclassclass AdaptiveSpanCriterionConfig(FairseqDataclass): sentence_avg: bool = II("optimization.sentence_avg")
@register_criterion("adaptive_span_loss", dataclass=AdaptiveSpanCriterionConfig)class AdaptiveSpanCriterion(CrossEntropyCriterion): def __init__(self, task, sentence_avg): super().__init__(task, sentence_avg)
def forward(self, model, sample, reduce=True): """Compute the loss for the given sample.
Returns a tuple with three elements: 1) the loss here is summed, different from the adaptive span code 2) the sample size, which is used as the denominator for the gradient 3) logging outputs to display while training """ net_output = model(**sample["net_input"]) loss, aux_loss, avg_span, max_span = self.compute_loss( model, net_output, sample, reduce=reduce ) sample_size = ( sample["target"].size(0) if self.sentence_avg else sample["ntokens"] ) loss /= sample_size total_loss = loss + aux_loss sample_size = 1
logging_output = { "loss": loss.data, "ntokens": sample["ntokens"], "nsentences": sample["target"].size(0), "sample_size": sample_size, "total_loss": total_loss.data, "avg_span": avg_span * sample_size, "max_span": max_span * sample_size, } return total_loss, sample_size, logging_output
def compute_loss(self, model, net_output, sample, reduce=True): loss, _ = super().compute_loss(model, net_output, sample, reduce) aux_loss = model.get_aux_loss() avg_span = model.get_current_avg_span() max_span = model.get_current_max_span() return loss, aux_loss, avg_span, max_span
@staticmethod def reduce_metrics(logging_outputs) -> None: """Aggregate logging outputs from data parallel training.""" loss_sum = sum(log.get("loss", 0) for log in logging_outputs) ntokens = sum(log.get("ntokens", 0) for log in logging_outputs) sample_size = sum(log.get("sample_size", 0) for log in logging_outputs) total_loss_sum = sum(log.get("total_loss", 0) for log in logging_outputs) avg_span_sum = sum(log.get("avg_span", 0) for log in logging_outputs) max_span_sum = sum(log.get("max_span", 0) for log in logging_outputs)
# we divide by log(2) to convert the loss from base e to base 2 metrics.log_scalar( "loss", loss_sum / sample_size / math.log(2), sample_size, round=3 ) metrics.log_scalar("avg_span", avg_span_sum / sample_size, sample_size, round=3) metrics.log_scalar("max_span", max_span_sum / sample_size, sample_size, round=3) # total loss contains the L1 norm on adaptive-span metrics.log_scalar( "total_loss", total_loss_sum / sample_size / math.log(2), sample_size, round=3, ) if sample_size != ntokens: metrics.log_scalar( "nll_loss", loss_sum / ntokens / math.log(2), ntokens, round=3 ) metrics.log_derived( "ppl", lambda meters: utils.get_perplexity(meters["nll_loss"].avg) ) else: metrics.log_derived( "ppl", lambda meters: utils.get_perplexity(meters["loss"].avg) )
@staticmethod def logging_outputs_can_be_summed() -> bool: """ Whether the logging outputs returned by `forward` can be summed across workers prior to calling `reduce_metrics`. Setting this to True will improves distributed training speed. """ return True123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263# Copyright (c) Facebook, Inc. and its affiliates.# All rights reserved.## This source code is licensed under the license found in the# LICENSE file in the root directory of this source tree.
import math
import torchimport torch.nn as nnimport torch.nn.functional as F
from fairseq.modules.layer_norm import LayerNorm
from .adaptive_span_attention import AdaptiveSpan
# Size notations:# B = batch_size, H = d_model, M = block_size, L = attn_span
def _skew(X, pad_value): """shift every row 1 step to right""" # X = B x M x L B, M, L = X.size() X = F.pad(X, (0, M + 1), value=pad_value) # B x M x (L+M+1) X = X.view(B, -1) # B x ML+MM+M X = X[:, :-M] # B x ML+MM X = X.view(B, M, M + L) # B x M x L+M return X
def _unskew(X): """reverse _skew operation""" # X = B x M x L+M B, M, L = X.size() L -= M X = X.view(B, -1) # B x ML+MM X = F.pad(X, (0, M)) # B x ML+MM+M X = X.view(B, M, M + L + 1) # B x M x L+M+1 X = X[:, :, :L] # B x M x L return X
class SeqAttention(nn.Module): """Sequential self-attention layer. Each token will attend to its previous fixed number of steps. Note that attention doesn't include the current step itself. """
def __init__(self, d_model, n_head, attn_span, dropout, adapt_span_layer, **kargs): nn.Module.__init__(self) self.dropout = nn.Dropout(dropout) self.d_model = d_model # size of a single head self.attn_span = attn_span self.adaptive_span = AdaptiveSpan( attn_span=attn_span, n_head=n_head, adapt_span_layer=adapt_span_layer, **kargs )
def forward(self, query, key, value, key_pe): # query size = B x M x H # key, value sizes = B x (M+L) x H
key, value, key_pe = self.adaptive_span.trim_memory(query, key, value, key_pe)
# compute attention from context # B x M (dest) x (M+L) (src) attn_cont = torch.matmul(query, key.transpose(-1, -2)) attn_cont = _unskew(attn_cont) # B x M x L
# compute the effect of position embedding attn_pos = torch.matmul(query, key_pe) # B x M x L_pos attn = attn_cont + attn_pos
attn = attn / math.sqrt(self.d_model) # B x M X L_pos
attn = F.softmax(attn.float(), dim=-1).type_as(attn)
# trim attention lengths according to the learned span attn = self.adaptive_span(attn)
attn = self.dropout(attn) # B x M X L_pos
attn_cont = _skew(attn, 0) # B x M X (L+M) out = torch.matmul(attn_cont, value) # B x M x H return out
def get_cache_size(self): return self.adaptive_span.get_cache_size()
class MultiHeadSeqAttention(nn.Module): def __init__(self, d_model, n_head, **kargs): nn.Module.__init__(self) assert d_model % n_head == 0 self.n_head = n_head self.head_dim = d_model // n_head self.attn = SeqAttention(d_model=self.head_dim, n_head=n_head, **kargs) self.proj_query = nn.Linear(d_model, d_model, bias=False) nn.init.xavier_normal_(self.proj_query.weight) self.proj_out = nn.Linear(d_model, d_model, bias=False) nn.init.xavier_normal_(self.proj_out.weight) self.proj_val = nn.Linear(d_model, d_model, bias=False) nn.init.xavier_normal_(self.proj_val.weight) self.proj_key = nn.Linear(d_model, d_model, bias=False) nn.init.xavier_normal_(self.proj_key.weight)
def head_reshape(self, x): K = self.n_head D = self.head_dim x = x.view(x.size()[:-1] + (K, D)) # B x (M+L) x K x D x = x.transpose(1, 2).contiguous() # B x K x (M+L) x D x = x.view(-1, x.size(-2), x.size(-1)) # B_K x (M+L) x D return x
def forward(self, query, key, value, key_pe): B = query.size(0) K = self.n_head D = self.head_dim M = query.size(1)
query = self.proj_query(query) query = self.head_reshape(query) value = self.proj_val(value) value = self.head_reshape(value) key = self.proj_key(key) key = self.head_reshape(key)
out = self.attn(query, key, value, key_pe) # B_K x M x D out = out.view(B, K, M, D) # B x K x M x D out = out.transpose(1, 2).contiguous() # B x M x K x D out = out.view(B, M, -1) # B x M x K_D out = self.proj_out(out) return out
class FeedForwardLayer(nn.Module): def __init__(self, d_model, d_inner, dropout, **kargs): nn.Module.__init__(self) self.fc1 = nn.Linear(d_model, d_inner) self.fc2 = nn.Linear(d_inner, d_model) nn.init.xavier_uniform_(self.fc1.weight) nn.init.xavier_uniform_(self.fc2.weight) self.dropout = nn.Dropout(dropout)
def forward(self, h): h1 = F.relu(self.fc1(h)) h1 = self.dropout(h1) h2 = self.fc2(h1) return h2
class TransformerSeqLayer(nn.Module): def __init__(self, d_model, **kargs): nn.Module.__init__(self) self.attn = MultiHeadSeqAttention(d_model=d_model, **kargs) self.norm1 = LayerNorm(d_model) self.ff = FeedForwardLayer(d_model=d_model, **kargs) self.norm2 = LayerNorm(d_model)
def forward(self, h, h_cache, key_pe): # h = B x M x H # h_cache = B x L x H h_all = torch.cat([h_cache, h], dim=1) # B x (M+L) x H attn_out = self.attn(h, h_all, h_all, key_pe) h = self.norm1(h + attn_out) # B x M x H if self.ff is not None: ff_out = self.ff(h) out = self.norm2(h + ff_out) # B x M x H else: out = h return out
def get_cache_size(self): return self.attn.attn.get_cache_size()
class TransformerSeq(nn.Module): def __init__( self, vocab_size, d_model, n_head, n_layer, attn_span, emb_dropout, aux_loss_scaler, adapt_span_layer, **kargs ): nn.Module.__init__(self) # token embeddings self.in_emb = nn.Embedding(vocab_size, d_model) nn.init.normal_(self.in_emb.weight, mean=0, std=d_model ** -0.5) self.out_emb = nn.Linear(d_model, vocab_size) self.aux_loss_scaler = aux_loss_scaler if emb_dropout > 0: self.emb_dropout = nn.Dropout(emb_dropout) else: self.emb_dropout = None # position embeddings self.key_pe = nn.Parameter(torch.randn(1, d_model // n_head, attn_span))
self.layers = nn.ModuleList() self.layers.extend( TransformerSeqLayer( d_model=d_model, n_head=n_head, attn_span=attn_span, adapt_span_layer=adapt_span_layer, **kargs ) for _ in range(n_layer) )
def forward(self, x, h_cache, target=None): # x size = B x M block_size = x.size(1) h = self.in_emb(x) # B x M x H if self.emb_dropout is not None: h = self.emb_dropout(h)
h_cache_next = [] for l, layer in enumerate(self.layers): cache_size = layer.attn.attn.get_cache_size() if cache_size > block_size: h_cache_next_l = torch.cat( [h_cache[l][:, -cache_size + block_size :, :], h], dim=1 ).detach() else: h_cache_next_l = h[:, -cache_size:, :].detach() h_cache_next.append(h_cache_next_l) h = layer(h, h_cache[l], self.key_pe) # B x M x H
if self.emb_dropout is not None: h = self.emb_dropout(h)
out = F.log_softmax(self.out_emb(h).float(), dim=-1).type_as(h) dummy_loss = None
return out, h_cache_next, dummy_loss
def get_aux_loss(self): loss = 0.0 for layer in self.layers: loss += layer.attn.attn.adaptive_span.get_loss() return self.aux_loss_scaler * loss
def get_current_max_span(self): max_span = 0.0 for layer in self.layers: max_span = max( max_span, layer.attn.attn.adaptive_span.get_current_max_span() ) return max_span
def get_current_avg_span(self): avg_span = 0.0 for layer in self.layers: avg_span += layer.attn.attn.adaptive_span.get_current_avg_span() return avg_span / len(self.layers)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import loggingfrom dataclasses import dataclassfrom typing import Dict, List, Optional
import torchfrom fairseq.dataclass import FairseqDataclassfrom fairseq.models import ( FairseqIncrementalDecoder, FairseqLanguageModel, register_model,)from .adaptive_span_model import TransformerSeq as AdaptiveSpanTransformerModel
logger = logging.getLogger(__name__)
@dataclassclass AdaptiveSpanSmallConfig(FairseqDataclass): # defaults come from https://github.com/facebookresearch/adaptive-span/blob/master/experiments/enwik8_small.sh vocab_size: int = 50 d_model: int = 256 n_head: int = 4 d_inner: int = 1024 n_layer: int = 8 attn_span: int = 1024 dropout: float = 0.0 emb_dropout: float = 0.0 adapt_span_ramp: int = 32 adapt_span_init: float = 0.0 aux_loss_scaler: float = 0.000002 adapt_span_layer: bool = False
@register_model("adaptive_span", dataclass=AdaptiveSpanSmallConfig)class AdaptiveSpanTransformer(FairseqLanguageModel): @classmethod def build_model(cls, cfg: AdaptiveSpanSmallConfig, task): return cls(AdaptiveSpanDecoder(cfg, task))
def get_aux_loss(self): return self.decoder.get_aux_loss()
def get_current_max_span(self): return self.decoder.get_current_max_span()
def get_current_avg_span(self): return self.decoder.get_current_avg_span()
class AdaptiveSpanDecoder(FairseqIncrementalDecoder): def __init__(self, cfg, task):
super().__init__(task.target_dictionary)
self.config = cfg config = AdaptiveSpanSmallConfig( vocab_size=len(task.target_dictionary), d_model=cfg.d_model, n_head=cfg.n_head, d_inner=cfg.d_inner, n_layer=cfg.n_layer, attn_span=cfg.attn_span, dropout=cfg.dropout, emb_dropout=cfg.emb_dropout, adapt_span_ramp=cfg.adapt_span_ramp, adapt_span_init=cfg.adapt_span_init, aux_loss_scaler=cfg.aux_loss_scaler, adapt_span_layer=cfg.adapt_span_layer, ) logger.info(config) self.model = AdaptiveSpanTransformerModel(**config.__dict__)
self._mems = None
def forward( self, src_tokens, incremental_state: Optional[Dict[str, List[torch.Tensor]]] = None, encoder_out=None, ): bsz = src_tokens.size(0) if incremental_state is not None: # used during inference mems = self.get_incremental_state("mems") src_tokens = src_tokens[:, -1:] # only keep the most recent token else: mems = self._mems
if mems is None: # first time init mems = self.init_hid_cache(bsz) output = self.model(x=src_tokens, h_cache=mems,) if incremental_state is not None: self.set_incremental_state(incremental_state, "mems", output[1]) else: self._mems = output[1] return (output[0],)
def max_positions(self): return self.config.attn_span
def init_hid_cache(self, batch_sz): hid = [] for layer in self.model.layers: param = next(self.model.parameters()) h = torch.zeros( batch_sz, layer.get_cache_size(), self.config.d_model, dtype=param.dtype, device=param.device, ) hid.append(h) return hid
def get_aux_loss(self): return self.model.get_aux_loss()
def get_current_max_span(self): return self.model.get_current_max_span()
def get_current_avg_span(self): return self.model.get_current_avg_span()
def reorder_incremental_state( self, incremental_state: Dict[str, Dict[str, Optional[torch.Tensor]]], new_order: torch.Tensor, ): """Reorder incremental state.
This will be called when the order of the input has changed from the previous time step. A typical use case is beam search, where the input order changes between time steps based on the selection of beams. """ raise NotImplementedError("This is required for generation/beam search") # mems = self.get_incremental_state(incremental_state, "mems") # if mems is not None: # new_mems = [mems_i.index_select(1, new_order) for mems_i in mems] # self.set_incremental_state(incremental_state, "mems", new_mems)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import loggingimport osfrom dataclasses import dataclass, fieldfrom typing import List, Optional, Tuple
import torchfrom fairseq import utilsfrom fairseq.data import ( Dictionary, TokenBlockDataset, data_utils, iterators,)from fairseq.dataclass import FairseqDataclassfrom fairseq.distributed import utils as dist_utilsfrom fairseq.tasks import FairseqTask, register_taskfrom omegaconf import II
logger = logging.getLogger(__name__)
@dataclassclass TruncatedBPTTLMConfig(FairseqDataclass): data: str = field(default="???", metadata={"help": "path to data directory"}) tokens_per_sample: int = field( default=1024, metadata={"help": "max number of tokens per sequence"}, ) batch_size: int = II("dataset.batch_size") # Some models use *max_target_positions* to know how many positional # embeddings to learn. We use II(...) to make it default to # *tokens_per_sample*, but in principle there could be more positional # embeddings than tokens in a single batch. This may also be irrelevant for # custom model implementations. max_target_positions: int = II("task.tokens_per_sample") # these will be populated automatically if not provided data_parallel_rank: Optional[int] = None data_parallel_size: Optional[int] = None
@register_task("truncated_bptt_lm", dataclass=TruncatedBPTTLMConfig)class TruncatedBPTTLMTask(FairseqTask): def __init__(self, cfg: TruncatedBPTTLMConfig): super().__init__(cfg)
if cfg.data_parallel_rank is None or cfg.data_parallel_size is None: if torch.distributed.is_initialized(): cfg.data_parallel_rank = dist_utils.get_data_parallel_rank() cfg.data_parallel_size = dist_utils.get_data_parallel_world_size() else: cfg.data_parallel_rank = 0 cfg.data_parallel_size = 1
# load the dictionary paths = utils.split_paths(cfg.data) assert len(paths) > 0 self.dictionary = Dictionary.load(os.path.join(paths[0], "dict.txt")) logger.info("dictionary: {} types".format(len(self.dictionary)))
def load_dataset(self, split, epoch=1, combine=False, **kwargs): """Load a given dataset split (e.g., train, valid, test)"""
# support sharded datasets paths = utils.split_paths(self.cfg.data) assert len(paths) > 0 data_path = paths[(epoch - 1) % len(paths)] split_path = os.path.join(data_path, split)
# each element of *data* will be a tensorized line from the original # text dataset, similar to ``open(split_path).readlines()`` data = data_utils.load_indexed_dataset( split_path, self.dictionary, combine=combine ) if data is None: raise FileNotFoundError( "Dataset not found: {} ({})".format(split, split_path) )
# this is similar to ``data.view(-1).split(tokens_per_sample)`` data = TokenBlockDataset( data, data.sizes, block_size=self.cfg.tokens_per_sample, pad=None, # unused eos=None, # unused break_mode="none", )
self.datasets[split] = TruncatedBPTTDataset( data=data, bsz_per_shard=self.cfg.batch_size, shard_id=self.cfg.data_parallel_rank, num_shards=self.cfg.data_parallel_size, )
def dataset(self, split): return self.datasets[split]
def get_batch_iterator( self, dataset, num_workers=0, epoch=1, data_buffer_size=0, skip_remainder_batch=False, **kwargs ): return iterators.EpochBatchIterator( dataset=dataset, collate_fn=self._collate_fn, num_workers=num_workers, epoch=epoch, buffer_size=data_buffer_size, # we don't use the batching functionality from EpochBatchIterator; # instead every item in *dataset* is a whole batch batch_sampler=[[i] for i in range(len(dataset))], disable_shuffling=True, skip_remainder_batch=skip_remainder_batch, )
def _collate_fn(self, items: List[List[torch.Tensor]]): # we don't use fairseq's batching functionality, so we expect a single # Tensor of type List[torch.Tensor] assert len(items) == 1
# item will have shape B x T (the last batch may have length < T) id, item = items[0] item = data_utils.collate_tokens(item, pad_idx=self.source_dictionary.pad()) B, T = item.size()
# shift item one position over and append a padding token for the target target = torch.nn.functional.pad( item[:, 1:], (0, 1, 0, 0), value=self.target_dictionary.pad() )
# fairseq expects batches to have the following structure return { "id": torch.tensor([id] * item.size(0)), "net_input": {"src_tokens": item,}, "target": target, "nsentences": item.size(0), "ntokens": item.numel(), }
def build_dataset_for_inference( self, src_tokens: List[torch.Tensor], src_lengths: List[int], **kwargs ) -> torch.utils.data.Dataset: eos = self.source_dictionary.eos() dataset = TokenBlockDataset( src_tokens, src_lengths, block_size=None, # ignored for "eos" break mode pad=self.source_dictionary.pad(), eos=eos, break_mode="eos", )
class Dataset(torch.utils.data.Dataset): def __getitem__(self, i): item = dataset[i] if item[-1] == eos: # remove eos to support generating with a prefix item = item[:-1] return (i, [item])
def __len__(self): return len(dataset)
return Dataset()
def inference_step( self, generator, models, sample, prefix_tokens=None, constraints=None ): with torch.no_grad(): if constraints is not None: raise NotImplementedError
# SequenceGenerator doesn't use *src_tokens* directly, we need to # pass the *prefix_tokens* argument instead. if prefix_tokens is None and sample["net_input"]["src_tokens"].nelement(): prefix_tokens = sample["net_input"]["src_tokens"]
# begin generation with the end-of-sentence token bos_token = self.source_dictionary.eos()
return generator.generate( models, sample, prefix_tokens=prefix_tokens, bos_token=bos_token )
def eval_lm_dataloader( self, dataset, max_tokens: Optional[int] = 36000, batch_size: Optional[int] = None, max_positions: Optional[int] = None, num_shards: int = 1, shard_id: int = 0, num_workers: int = 1, data_buffer_size: int = 10, context_window: int = 0, ): if context_window > 0: raise NotImplementedError( "Transformer-XL doesn't need --context-window, try " "--model-overrides '{\"mem_len\":42}' instead " ) return self.get_batch_iterator( dataset=dataset, max_tokens=max_tokens, max_sentences=batch_size, max_positions=max_positions, ignore_invalid_inputs=True, num_shards=num_shards, shard_id=shard_id, num_workers=num_workers, data_buffer_size=data_buffer_size, ).next_epoch_itr(shuffle=False)
@property def source_dictionary(self): return self.dictionary
@property def target_dictionary(self): return self.dictionary
class TruncatedBPTTDataset(torch.utils.data.Dataset): def __init__( self, data: List[torch.Tensor], # ordered list of items bsz_per_shard, # number of items processed per GPUs per forward shard_id, # current GPU ID num_shards, # number of GPUs ): super().__init__() self.data = data
def batchify(data, bsz): # Work out how cleanly we can divide the dataset into bsz parts. nbatch = data.size(0) // bsz # Trim off any extra elements that wouldn't cleanly fit (remainders). data = data.narrow(0, 0, nbatch * bsz) # Evenly divide the data across the bsz batches. data = data.view(bsz, -1).contiguous() return data
# total number of sequences processed by all GPUs in each forward pass global_batch_size = bsz_per_shard * num_shards
""" With a 16 item dataset, bsz_per_shard=2 and num_shards=3, *indices* might look like:
indices = [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10, 11]]
The size of the TruncatedBPTTDataset instance will be 2, and shard 1 will see items:
[(0, [data[4], data[6]]), (1, [data[5], data[7]])] """ indices = batchify(torch.arange(len(data)), global_batch_size) assert indices.size(0) == global_batch_size
self.my_indices = indices[ shard_id * bsz_per_shard : (shard_id + 1) * bsz_per_shard ] assert self.my_indices.size(0) == bsz_per_shard
def __len__(self): return self.my_indices.size(1)
def __getitem__(self, i) -> Tuple[int, List[torch.Tensor]]: return (i, [self.data[idx] for idx in self.my_indices[:, i]])123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297# Understanding Back-Translation at Scale (Edunov et al., 2018)
This page includes pre-trained models from the paper [Understanding Back-Translation at Scale (Edunov et al., 2018)](https://arxiv.org/abs/1808.09381).
## Pre-trained models
Model | Description | Dataset | Download---|---|---|---`transformer.wmt18.en-de` | Transformer <br> ([Edunov et al., 2018](https://arxiv.org/abs/1808.09381)) <br> WMT'18 winner | [WMT'18 English-German](http://www.statmt.org/wmt18/translation-task.html) | [download (.tar.gz)](https://dl.fbaipublicfiles.com/fairseq/models/wmt18.en-de.ensemble.tar.gz) <br> See NOTE in the archive
## Example usage (torch.hub)
We require a few additional Python dependencies for preprocessing:```bashpip install subword_nmt sacremoses```
Then to generate translations from the full model ensemble:```pythonimport torch
# List available modelstorch.hub.list('pytorch/fairseq') # [..., 'transformer.wmt18.en-de', ... ]
# Load the WMT'18 En-De ensembleen2de_ensemble = torch.hub.load( 'pytorch/fairseq', 'transformer.wmt18.en-de', checkpoint_file='wmt18.model1.pt:wmt18.model2.pt:wmt18.model3.pt:wmt18.model4.pt:wmt18.model5.pt', tokenizer='moses', bpe='subword_nmt')
# The ensemble contains 5 modelslen(en2de_ensemble.models)# 5
# Translateen2de_ensemble.translate('Hello world!')# 'Hallo Welt!'```
## Training your own model (WMT'18 English-German)
The following instructions can be adapted to reproduce the models from the paper.
#### Step 1. Prepare parallel data and optionally train a baseline (English-German) model
First download and preprocess the data:```bash# Download and prepare the datacd examples/backtranslation/bash prepare-wmt18en2de.shcd ../..
# Binarize the dataTEXT=examples/backtranslation/wmt18_en_defairseq-preprocess \ --joined-dictionary \ --source-lang en --target-lang de \ --trainpref $TEXT/train --validpref $TEXT/valid --testpref $TEXT/test \ --destdir data-bin/wmt18_en_de --thresholdtgt 0 --thresholdsrc 0 \ --workers 20
# Copy the BPE code into the data-bin directory for future usecp examples/backtranslation/wmt18_en_de/code data-bin/wmt18_en_de/code```
(Optionally) Train a baseline model (English-German) using just the parallel data:```bashCHECKPOINT_DIR=checkpoints_en_de_parallelfairseq-train --fp16 \ data-bin/wmt18_en_de \ --source-lang en --target-lang de \ --arch transformer_wmt_en_de_big --share-all-embeddings \ --dropout 0.3 --weight-decay 0.0 \ --criterion label_smoothed_cross_entropy --label-smoothing 0.1 \ --optimizer adam --adam-betas '(0.9, 0.98)' --clip-norm 0.0 \ --lr 0.001 --lr-scheduler inverse_sqrt --warmup-updates 4000 \ --max-tokens 3584 --update-freq 16 \ --max-update 30000 \ --save-dir $CHECKPOINT_DIR# Note: the above command assumes 8 GPUs. Adjust `--update-freq` if you have a# different number of GPUs.```
Average the last 10 checkpoints:```bashpython scripts/average_checkpoints.py \ --inputs $CHECKPOINT_DIR \ --num-epoch-checkpoints 10 \ --output $CHECKPOINT_DIR/checkpoint.avg10.pt```
Evaluate BLEU:```bash# tokenized BLEU on newstest2017:bash examples/backtranslation/tokenized_bleu.sh \ wmt17 \ en-de \ data-bin/wmt18_en_de \ data-bin/wmt18_en_de/code \ $CHECKPOINT_DIR/checkpoint.avg10.pt# BLEU4 = 29.57, 60.9/35.4/22.9/15.5 (BP=1.000, ratio=1.014, syslen=63049, reflen=62152)# compare to 29.46 in Table 1, which is also for tokenized BLEU
# generally it's better to report (detokenized) sacrebleu though:bash examples/backtranslation/sacrebleu.sh \ wmt17 \ en-de \ data-bin/wmt18_en_de \ data-bin/wmt18_en_de/code \ $CHECKPOINT_DIR/checkpoint.avg10.pt# BLEU+case.mixed+lang.en-de+numrefs.1+smooth.exp+test.wmt17+tok.13a+version.1.4.3 = 29.0 60.6/34.7/22.4/14.9 (BP = 1.000 ratio = 1.013 hyp_len = 62099 ref_len = 61287)```
#### Step 2. Back-translate monolingual German data
Train a reverse model (German-English) to do the back-translation:```bashCHECKPOINT_DIR=checkpoints_de_en_parallelfairseq-train --fp16 \ data-bin/wmt18_en_de \ --source-lang de --target-lang en \ --arch transformer_wmt_en_de_big --share-all-embeddings \ --dropout 0.3 --weight-decay 0.0 \ --criterion label_smoothed_cross_entropy --label-smoothing 0.1 \ --optimizer adam --adam-betas '(0.9, 0.98)' --clip-norm 0.0 \ --lr 0.001 --lr-scheduler inverse_sqrt --warmup-updates 4000 \ --max-tokens 3584 --update-freq 16 \ --max-update 30000 \ --save-dir $CHECKPOINT_DIR# Note: the above command assumes 8 GPUs. Adjust `--update-freq` if you have a# different number of GPUs.```
Let's evaluate the back-translation (BT) model to make sure it is well trained:```bashbash examples/backtranslation/sacrebleu.sh \ wmt17 \ de-en \ data-bin/wmt18_en_de \ data-bin/wmt18_en_de/code \ $CHECKPOINT_DIR/checkpoint_best.py# BLEU+case.mixed+lang.de-en+numrefs.1+smooth.exp+test.wmt17+tok.13a+version.1.4.3 = 34.9 66.9/41.8/28.5/19.9 (BP = 0.983 ratio = 0.984 hyp_len = 63342 ref_len = 64399)# compare to the best system from WMT'17 which scored 35.1: http://matrix.statmt.org/matrix/systems_list/1868```
Next prepare the monolingual data:```bash# Download and prepare the monolingual data# By default the script samples 25M monolingual sentences, which after# deduplication should be just over 24M sentences. These are split into 25# shards, each with 1M sentences (except for the last shard).cd examples/backtranslation/bash prepare-de-monolingual.shcd ../..
# Binarize each shard of the monolingual dataTEXT=examples/backtranslation/wmt18_de_monofor SHARD in $(seq -f "%02g" 0 24); do \ fairseq-preprocess \ --only-source \ --source-lang de --target-lang en \ --joined-dictionary \ --srcdict data-bin/wmt18_en_de/dict.de.txt \ --testpref $TEXT/bpe.monolingual.dedup.${SHARD} \ --destdir data-bin/wmt18_de_mono/shard${SHARD} \ --workers 20; \ cp data-bin/wmt18_en_de/dict.en.txt data-bin/wmt18_de_mono/shard${SHARD}/; \done```
Now we're ready to perform back-translation over the monolingual data. Thefollowing command generates via sampling, but it's possible to use greedydecoding (`--beam 1`), beam search (`--beam 5`),top-k sampling (`--sampling --beam 1 --sampling-topk 10`), etc.:```bashmkdir backtranslation_outputfor SHARD in $(seq -f "%02g" 0 24); do \ fairseq-generate --fp16 \ data-bin/wmt18_de_mono/shard${SHARD} \ --path $CHECKPOINT_DIR/checkpoint_best.pt \ --skip-invalid-size-inputs-valid-test \ --max-tokens 4096 \ --sampling --beam 1 \ > backtranslation_output/sampling.shard${SHARD}.out; \done```
After BT, use the `extract_bt_data.py` script to re-combine the shards, extractthe back-translations and apply length ratio filters:```bashpython examples/backtranslation/extract_bt_data.py \ --minlen 1 --maxlen 250 --ratio 1.5 \ --output backtranslation_output/bt_data --srclang en --tgtlang de \ backtranslation_output/sampling.shard*.out
# Ensure lengths are the same:# wc -l backtranslation_output/bt_data.{en,de}# 21795614 backtranslation_output/bt_data.en# 21795614 backtranslation_output/bt_data.de# 43591228 total```
Binarize the filtered BT data and combine it with the parallel data:```bashTEXT=backtranslation_outputfairseq-preprocess \ --source-lang en --target-lang de \ --joined-dictionary \ --srcdict data-bin/wmt18_en_de/dict.en.txt \ --trainpref $TEXT/bt_data \ --destdir data-bin/wmt18_en_de_bt \ --workers 20
# We want to train on the combined data, so we'll symlink the parallel + BT data# in the wmt18_en_de_para_plus_bt directory. We link the parallel data as "train"# and the BT data as "train1", so that fairseq will combine them automatically# and so that we can use the `--upsample-primary` option to upsample the# parallel data (if desired).PARA_DATA=$(readlink -f data-bin/wmt18_en_de)BT_DATA=$(readlink -f data-bin/wmt18_en_de_bt)COMB_DATA=data-bin/wmt18_en_de_para_plus_btmkdir -p $COMB_DATAfor LANG in en de; do \ ln -s ${PARA_DATA}/dict.$LANG.txt ${COMB_DATA}/dict.$LANG.txt; \ for EXT in bin idx; do \ ln -s ${PARA_DATA}/train.en-de.$LANG.$EXT ${COMB_DATA}/train.en-de.$LANG.$EXT; \ ln -s ${BT_DATA}/train.en-de.$LANG.$EXT ${COMB_DATA}/train1.en-de.$LANG.$EXT; \ ln -s ${PARA_DATA}/valid.en-de.$LANG.$EXT ${COMB_DATA}/valid.en-de.$LANG.$EXT; \ ln -s ${PARA_DATA}/test.en-de.$LANG.$EXT ${COMB_DATA}/test.en-de.$LANG.$EXT; \ done; \done```
#### 3. Train an English-German model over the combined parallel + BT data
Finally we can train a model over the parallel + BT data:```bashCHECKPOINT_DIR=checkpoints_en_de_parallel_plus_btfairseq-train --fp16 \ data-bin/wmt18_en_de_para_plus_bt \ --upsample-primary 16 \ --source-lang en --target-lang de \ --arch transformer_wmt_en_de_big --share-all-embeddings \ --dropout 0.3 --weight-decay 0.0 \ --criterion label_smoothed_cross_entropy --label-smoothing 0.1 \ --optimizer adam --adam-betas '(0.9, 0.98)' --clip-norm 0.0 \ --lr 0.0007 --lr-scheduler inverse_sqrt --warmup-updates 4000 \ --max-tokens 3584 --update-freq 16 \ --max-update 100000 \ --save-dir $CHECKPOINT_DIR# Note: the above command assumes 8 GPUs. Adjust `--update-freq` if you have a# different number of GPUs.```
Average the last 10 checkpoints:```bashpython scripts/average_checkpoints.py \ --inputs $CHECKPOINT_DIR \ --num-epoch-checkpoints 10 \ --output $CHECKPOINT_DIR/checkpoint.avg10.pt```
Evaluate BLEU:```bash# tokenized BLEU on newstest2017:bash examples/backtranslation/tokenized_bleu.sh \ wmt17 \ en-de \ data-bin/wmt18_en_de \ data-bin/wmt18_en_de/code \ $CHECKPOINT_DIR/checkpoint.avg10.pt# BLEU4 = 32.35, 64.4/38.9/26.2/18.3 (BP=0.977, ratio=0.977, syslen=60729, reflen=62152)# compare to 32.35 in Table 1, which is also for tokenized BLEU
# generally it's better to report (detokenized) sacrebleu:bash examples/backtranslation/sacrebleu.sh \ wmt17 \ en-de \ data-bin/wmt18_en_de \ data-bin/wmt18_en_de/code \ $CHECKPOINT_DIR/checkpoint.avg10.pt# BLEU+case.mixed+lang.en-de+numrefs.1+smooth.exp+test.wmt17+tok.13a+version.1.4.3 = 31.5 64.3/38.2/25.6/17.6 (BP = 0.971 ratio = 0.971 hyp_len = 59515 ref_len = 61287)```
## Citation```bibtex@inproceedings{edunov2018backtranslation, title = {Understanding Back-Translation at Scale}, author = {Edunov, Sergey and Ott, Myle and Auli, Michael and Grangier, David}, booktitle = {Conference of the Association for Computational Linguistics (ACL)}, year = 2018,}```1234567891011121314151617181920212223242526272829303132333435363738394041#!/usr/bin/python3# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import argparseimport fileinputimport hashlibimport sysfrom multiprocessing import Pool
def get_hashes_and_lines(raw_line): hash = hashlib.md5(raw_line).hexdigest() return hash, raw_line
def main(): parser = argparse.ArgumentParser() parser.add_argument("--workers", type=int, default=10) parser.add_argument("files", nargs="*", help="input files") args = parser.parse_args()
seen = set() with fileinput.input(args.files, mode="rb") as h: pool = Pool(args.workers) results = pool.imap_unordered(get_hashes_and_lines, h, 1000) for i, (hash, raw_line) in enumerate(results): if hash not in seen: seen.add(hash) sys.stdout.buffer.write(raw_line) if i % 1000000 == 0: print(i, file=sys.stderr, end="", flush=True) elif i % 100000 == 0: print(".", file=sys.stderr, end="", flush=True) print(file=sys.stderr, flush=True)
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172#!/usr/bin/env python# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import argparseimport fileinput
from tqdm import tqdm
def main(): parser = argparse.ArgumentParser( description=( "Extract back-translations from the stdout of fairseq-generate. " "If there are multiply hypotheses for a source, we only keep the first one. " ) ) parser.add_argument("--output", required=True, help="output prefix") parser.add_argument( "--srclang", required=True, help="source language (extracted from H-* lines)" ) parser.add_argument( "--tgtlang", required=True, help="target language (extracted from S-* lines)" ) parser.add_argument("--minlen", type=int, help="min length filter") parser.add_argument("--maxlen", type=int, help="max length filter") parser.add_argument("--ratio", type=float, help="ratio filter") parser.add_argument("files", nargs="*", help="input files") args = parser.parse_args()
def validate(src, tgt): srclen = len(src.split(" ")) if src != "" else 0 tgtlen = len(tgt.split(" ")) if tgt != "" else 0 if ( (args.minlen is not None and (srclen < args.minlen or tgtlen < args.minlen)) or ( args.maxlen is not None and (srclen > args.maxlen or tgtlen > args.maxlen) ) or ( args.ratio is not None and (max(srclen, tgtlen) / float(min(srclen, tgtlen)) > args.ratio) ) ): return False return True
def safe_index(toks, index, default): try: return toks[index] except IndexError: return default
with open(args.output + "." + args.srclang, "w") as src_h, open( args.output + "." + args.tgtlang, "w" ) as tgt_h: for line in tqdm(fileinput.input(args.files)): if line.startswith("S-"): tgt = safe_index(line.rstrip().split("\t"), 1, "") elif line.startswith("H-"): if tgt is not None: src = safe_index(line.rstrip().split("\t"), 2, "") if validate(src, tgt): print(src, file=src_h) print(tgt, file=tgt_h) tgt = None
if __name__ == "__main__": main()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798#!/bin/bash
SCRIPTS=mosesdecoder/scriptsTOKENIZER=$SCRIPTS/tokenizer/tokenizer.perlNORM_PUNC=$SCRIPTS/tokenizer/normalize-punctuation.perlREM_NON_PRINT_CHAR=$SCRIPTS/tokenizer/remove-non-printing-char.perlBPEROOT=subword-nmt/subword_nmt
BPE_CODE=wmt18_en_de/codeSUBSAMPLE_SIZE=25000000LANG=de
OUTDIR=wmt18_${LANG}_monoorig=origtmp=$OUTDIR/tmpmkdir -p $OUTDIR $tmp
URLS=( "http://www.statmt.org/wmt14/training-monolingual-news-crawl/news.2007.de.shuffled.gz" "http://www.statmt.org/wmt14/training-monolingual-news-crawl/news.2008.de.shuffled.gz" "http://www.statmt.org/wmt14/training-monolingual-news-crawl/news.2009.de.shuffled.gz" "http://www.statmt.org/wmt14/training-monolingual-news-crawl/news.2010.de.shuffled.gz" "http://www.statmt.org/wmt14/training-monolingual-news-crawl/news.2011.de.shuffled.gz" "http://www.statmt.org/wmt14/training-monolingual-news-crawl/news.2012.de.shuffled.gz" "http://www.statmt.org/wmt14/training-monolingual-news-crawl/news.2013.de.shuffled.gz" "http://www.statmt.org/wmt15/training-monolingual-news-crawl-v2/news.2014.de.shuffled.v2.gz" "http://data.statmt.org/wmt16/translation-task/news.2015.de.shuffled.gz" "http://data.statmt.org/wmt17/translation-task/news.2016.de.shuffled.gz" "http://data.statmt.org/wmt18/translation-task/news.2017.de.shuffled.deduped.gz")FILES=( "news.2007.de.shuffled.gz" "news.2008.de.shuffled.gz" "news.2009.de.shuffled.gz" "news.2010.de.shuffled.gz" "news.2011.de.shuffled.gz" "news.2012.de.shuffled.gz" "news.2013.de.shuffled.gz" "news.2014.de.shuffled.v2.gz" "news.2015.de.shuffled.gz" "news.2016.de.shuffled.gz" "news.2017.de.shuffled.deduped.gz")
cd $origfor ((i=0;i<${#URLS[@]};++i)); do file=${FILES[i]} if [ -f $file ]; then echo "$file already exists, skipping download" else url=${URLS[i]} wget "$url" fidonecd ..
if [ -f $tmp/monolingual.${SUBSAMPLE_SIZE}.${LANG} ]; then echo "found monolingual sample, skipping shuffle/sample/tokenize"else gzip -c -d -k $(for FILE in "${FILES[@]}"; do echo $orig/$FILE; done) \ | shuf -n $SUBSAMPLE_SIZE \ | perl $NORM_PUNC $LANG \ | perl $REM_NON_PRINT_CHAR \ | perl $TOKENIZER -threads 8 -a -l $LANG \ > $tmp/monolingual.${SUBSAMPLE_SIZE}.${LANG}fi
if [ -f $tmp/bpe.monolingual.${SUBSAMPLE_SIZE}.${LANG} ]; then echo "found BPE monolingual sample, skipping BPE step"else python $BPEROOT/apply_bpe.py -c $BPE_CODE \ < $tmp/monolingual.${SUBSAMPLE_SIZE}.${LANG} \ > $tmp/bpe.monolingual.${SUBSAMPLE_SIZE}.${LANG}fi
if [ -f $tmp/bpe.monolingual.dedup.${SUBSAMPLE_SIZE}.${LANG} ]; then echo "found deduplicated monolingual sample, skipping deduplication step"else python deduplicate_lines.py $tmp/bpe.monolingual.${SUBSAMPLE_SIZE}.${LANG} \ > $tmp/bpe.monolingual.dedup.${SUBSAMPLE_SIZE}.${LANG}fi
if [ -f $OUTDIR/bpe.monolingual.dedup.00.de ]; then echo "found sharded data, skipping sharding step"else split --lines 1000000 --numeric-suffixes \ --additional-suffix .${LANG} \ $tmp/bpe.monolingual.dedup.${SUBSAMPLE_SIZE}.${LANG} \ $OUTDIR/bpe.monolingual.dedup.fi123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135#!/bin/bash# Adapted from https://github.com/facebookresearch/MIXER/blob/master/prepareData.sh
echo 'Cloning Moses github repository (for tokenization scripts)...'git clone https://github.com/moses-smt/mosesdecoder.git
echo 'Cloning Subword NMT repository (for BPE pre-processing)...'git clone https://github.com/rsennrich/subword-nmt.git
SCRIPTS=mosesdecoder/scriptsTOKENIZER=$SCRIPTS/tokenizer/tokenizer.perlCLEAN=$SCRIPTS/training/clean-corpus-n.perlNORM_PUNC=$SCRIPTS/tokenizer/normalize-punctuation.perlREM_NON_PRINT_CHAR=$SCRIPTS/tokenizer/remove-non-printing-char.perlBPEROOT=subword-nmt/subword_nmtBPE_TOKENS=32000
URLS=( "http://statmt.org/wmt13/training-parallel-europarl-v7.tgz" "http://statmt.org/wmt13/training-parallel-commoncrawl.tgz" "http://data.statmt.org/wmt18/translation-task/training-parallel-nc-v13.tgz" "http://data.statmt.org/wmt18/translation-task/rapid2016.tgz" "http://data.statmt.org/wmt17/translation-task/dev.tgz" "http://statmt.org/wmt14/test-full.tgz")FILES=( "training-parallel-europarl-v7.tgz" "training-parallel-commoncrawl.tgz" "training-parallel-nc-v13.tgz" "rapid2016.tgz" "dev.tgz" "test-full.tgz")CORPORA=( "training/europarl-v7.de-en" "commoncrawl.de-en" "training-parallel-nc-v13/news-commentary-v13.de-en" "rapid2016.de-en")
if [ ! -d "$SCRIPTS" ]; then echo "Please set SCRIPTS variable correctly to point to Moses scripts." exit 1fi
OUTDIR=wmt18_en_de
src=entgt=delang=en-deprep=$OUTDIRtmp=$prep/tmporig=orig
mkdir -p $orig $tmp $prep
cd $orig
for ((i=0;i<${#URLS[@]};++i)); do file=${FILES[i]} if [ -f $file ]; then echo "$file already exists, skipping download" else url=${URLS[i]} wget "$url" if [ -f $file ]; then echo "$url successfully downloaded." else echo "$url not successfully downloaded." exit 1 fi if [ ${file: -4} == ".tgz" ]; then tar zxvf $file elif [ ${file: -4} == ".tar" ]; then tar xvf $file fi fidonecd ..
echo "pre-processing train data..."for l in $src $tgt; do rm $tmp/train.tags.$lang.tok.$l for f in "${CORPORA[@]}"; do cat $orig/$f.$l | \ perl $NORM_PUNC $l | \ perl $REM_NON_PRINT_CHAR | \ perl $TOKENIZER -threads 8 -a -l $l >> $tmp/train.tags.$lang.tok.$l donedone
echo "pre-processing test data..."for l in $src $tgt; do if [ "$l" == "$src" ]; then t="src" else t="ref" fi grep '<seg id' $orig/test-full/newstest2014-deen-$t.$l.sgm | \ sed -e 's/<seg id="[0-9]*">\s*//g' | \ sed -e 's/\s*<\/seg>\s*//g' | \ sed -e "s/\’/\'/g" | \ perl $TOKENIZER -threads 8 -a -l $l > $tmp/test.$l echo ""done
echo "splitting train and valid..."for l in $src $tgt; do awk '{if (NR%100 == 0) print $0; }' $tmp/train.tags.$lang.tok.$l > $tmp/valid.$l awk '{if (NR%100 != 0) print $0; }' $tmp/train.tags.$lang.tok.$l > $tmp/train.$ldone
TRAIN=$tmp/train.de-enBPE_CODE=$prep/coderm -f $TRAINfor l in $src $tgt; do cat $tmp/train.$l >> $TRAINdone
echo "learn_bpe.py on ${TRAIN}..."python $BPEROOT/learn_bpe.py -s $BPE_TOKENS < $TRAIN > $BPE_CODE
for L in $src $tgt; do for f in train.$L valid.$L test.$L; do echo "apply_bpe.py to ${f}..." python $BPEROOT/apply_bpe.py -c $BPE_CODE < $tmp/$f > $tmp/bpe.$f donedone
perl $CLEAN -ratio 1.5 $tmp/bpe.train $src $tgt $prep/train 1 250perl $CLEAN -ratio 1.5 $tmp/bpe.valid $src $tgt $prep/valid 1 250
for L in $src $tgt; do cp $tmp/bpe.test.$L $prep/test.$Ldone12345678910111213141516171819202122232425262728293031323334353637#!/bin/bash
if [ $# -ne 5 ]; then echo "usage: $0 [dataset=wmt14/full] [langpair=en-de] [databin] [bpecode] [model]" exitfi
DATASET=$1LANGPAIR=$2DATABIN=$3BPECODE=$4MODEL=$5
SRCLANG=$(echo $LANGPAIR | cut -d '-' -f 1)TGTLANG=$(echo $LANGPAIR | cut -d '-' -f 2)
BPEROOT=examples/backtranslation/subword-nmt/subword_nmtif [ ! -e $BPEROOT ]; then BPEROOT=subword-nmt/subword_nmt if [ ! -e $BPEROOT ]; then echo 'Cloning Subword NMT repository (for BPE pre-processing)...' git clone https://github.com/rsennrich/subword-nmt.git fifi
sacrebleu -t $DATASET -l $LANGPAIR --echo src \| sacremoses tokenize -a -l $SRCLANG -q \| python $BPEROOT/apply_bpe.py -c $BPECODE \| fairseq-interactive $DATABIN --path $MODEL \ -s $SRCLANG -t $TGTLANG \ --beam 5 --remove-bpe --buffer-size 1024 --max-tokens 8000 \| grep ^H- | cut -f 3- \| sacremoses detokenize -l $TGTLANG -q \| sacrebleu -t $DATASET -l $LANGPAIR12345678910111213141516171819202122232425262728293031323334353637383940414243444546#!/bin/bash
if [ $# -ne 5 ]; then echo "usage: $0 [dataset=wmt14/full] [langpair=en-de] [databin] [bpecode] [model]" exitfi
DATASET=$1LANGPAIR=$2DATABIN=$3BPECODE=$4MODEL=$5
SRCLANG=$(echo $LANGPAIR | cut -d '-' -f 1)TGTLANG=$(echo $LANGPAIR | cut -d '-' -f 2)
BPEROOT=examples/backtranslation/subword-nmt/subword_nmtif [ ! -e $BPEROOT ]; then BPEROOT=subword-nmt/subword_nmt if [ ! -e $BPEROOT ]; then echo 'Cloning Subword NMT repository (for BPE pre-processing)...' git clone https://github.com/rsennrich/subword-nmt.git fifi
TMP_REF=$(mktemp)
sacrebleu -t $DATASET -l $LANGPAIR --echo ref -q \| sacremoses normalize -l $TGTLANG -q \| sacremoses tokenize -a -l $TGTLANG -q \> $TMP_REF
sacrebleu -t $DATASET -l $LANGPAIR --echo src -q \| sacremoses normalize -l $SRCLANG -q \| sacremoses tokenize -a -l $SRCLANG -q \| python $BPEROOT/apply_bpe.py -c $BPECODE \| fairseq-interactive $DATABIN --path $MODEL \ -s $SRCLANG -t $TGTLANG \ --beam 5 --remove-bpe --buffer-size 1024 --max-tokens 8000 \| grep ^H- | cut -f 3- \| fairseq-score --ref $TMP_REF
rm -f $TMP_REF1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677# Cross-Lingual Language Model Pre-training
Below are some details for training Cross-Lingual Language Models (XLM) - similar to the ones presented in [Lample & Conneau, 2019](https://arxiv.org/pdf/1901.07291.pdf) - in Fairseq. The current implementation only supports the Masked Language Model (MLM) from the paper above.
## Downloading and Tokenizing Monolingual Data
Pointers to the monolingual data from wikipedia, used for training the XLM-style MLM model as well as details on processing (tokenization and BPE) it can be found in the [XLM Github Repository](https://github.com/facebookresearch/XLM#download--preprocess-monolingual-data).
Let's assume the following for the code snippets in later sections to work- Processed data is in the folder: monolingual_data/processed- Each language has 3 files for train, test and validation. For example we have the following files for English: train.en, valid.en- We are training a model for 5 languages: Arabic (ar), German (de), English (en), Hindi (hi) and French (fr)- The vocabulary file is monolingual_data/processed/vocab_mlm
## Fairseq Pre-processing and Binarization
Pre-process and binarize the data with the MaskedLMDictionary and cross_lingual_lm task
```bash# Ensure the output directory existsDATA_DIR=monolingual_data/fairseq_processedmkdir -p "$DATA_DIR"
for lg in ar de en hi frdo
fairseq-preprocess \ --task cross_lingual_lm \ --srcdict monolingual_data/processed/vocab_mlm \ --only-source \ --trainpref monolingual_data/processed/train \ --validpref monolingual_data/processed/valid \ --testpref monolingual_data/processed/test \ --destdir monolingual_data/fairseq_processed \ --workers 20 \ --source-lang $lg
# Since we only have a source language, the output file has a None for the # target language. Remove this
for stage in train test valid
sudo mv "$DATA_DIR/$stage.$lg-None.$lg.bin" "$stage.$lg.bin" sudo mv "$DATA_DIR/$stage.$lg-None.$lg.idx" "$stage.$lg.idx"
done
done```
## Train a Cross-lingual Language Model similar to the XLM MLM model
Use the following command to train the model on 5 languages.
```fairseq-train \--task cross_lingual_lm monolingual_data/fairseq_processed \--save-dir checkpoints/mlm \--max-update 2400000 --save-interval 1 --no-epoch-checkpoints \--arch xlm_base \--optimizer adam --lr-scheduler reduce_lr_on_plateau \--lr-shrink 0.5 --lr 0.0001 --stop-min-lr 1e-09 \--dropout 0.1 \--criterion legacy_masked_lm_loss \--max-tokens 2048 --tokens-per-sample 256 --attention-dropout 0.1 \--dataset-impl lazy --seed 0 \--masked-lm-only \--monolingual-langs 'ar,de,en,hi,fr' --num-segment 5 \--ddp-backend=legacy_ddp```
Some Notes:- Using tokens_per_sample greater than 256 can cause OOM (out-of-memory) issues. Usually since MLM packs in streams of text, this parameter doesn't need much tuning.- The Evaluation workflow for computing MLM Perplexity on test data is in progress.- Finetuning this model on a downstream task is something which is not currently available.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202# Discriminative Reranking for Neural Machine Translationhttps://aclanthology.org/2021.acl-long.563/
This folder contains source code for training DrNMT, a discriminatively trained reranker for neural machine translation.
## Data preparation1. Follow the instructions under `examples/translation` to build a base MT model. Prepare three files, one with source sentences, one with ground truth target sentences, and one with hypotheses generated from the base MT model. Each line in the file contains one sentence in raw text (i.e. no sentencepiece, etc.). Below is an example of the files with _N_ hypotheses for each source sentence.
```# Example of the source sentence file: (The file should contain L lines.)
source_sentence_1source_sentence_2source_sentence_3...source_sentence_L
# Example of the target sentence file: (The file should contain L lines.)
target_sentence_1target_sentence_2target_sentence_3...target_sentence_L
# Example of the hypotheses file: (The file should contain L*N lines.)
source_sentence_1_hypo_1source_sentence_1_hypo_2...source_sentence_1_hypo_Nsource_sentence_2_hypo_1...source_sentence_2_hypo_N...source_sentence_L_hypo_1...source_sentence_L_hypo_N```
2. Download the [XLMR model](https://github.com/fairinternal/fairseq-py/tree/main/examples/xlmr#pre-trained-models).```wget https://dl.fbaipublicfiles.com/fairseq/models/xlmr.base.tar.gztar zxvf xlmr.base.tar.gz
# The folder should contain dict.txt, model.pt and sentencepiece.bpe.model.```
3. Prepare scores and BPE data.* `N`: Number of hypotheses per each source sentence. We use 50 in the paper.* `SPLIT`: Name of the data split, i.e. train, valid, test. Use split_name, split_name1, split_name2, ..., if there are multiple datasets for a split, e.g. train, train1, valid, valid1.* `NUM_SHARDS`: Number of shards. Set this to 1 for non-train splits.* `METRIC`: The metric for DrNMT to optimize for. We support either `bleu` or `ter`.```# For each data split, e.g. train, valid, test, etc., run the following:
SOURCE_FILE=/path/to/source_sentence_fileTARGET_FILE=/path/to/target_sentence_fileHYPO_FILE=/path/to/hypo_fileXLMR_DIR=/path/to/xlmrOUTPUT_DIR=/path/to/output
python scripts/prep_data.py \ --input-source ${SOURCE_FILE} \ --input-target ${TARGET_FILE} \ --input-hypo ${HYPO_FILE} \ --output-dir ${OUTPUT_DIR} \ --split $SPLIT --beam $N \ --sentencepiece-model ${XLMR_DIR}/sentencepiece.bpe.model \ --metric $METRIC \ --num-shards ${NUM_SHARDS}
# The script will create ${OUTPUT_DIR}/$METRIC with ${NUM_SHARDS} splits.# Under split*/input_src, split*/input_tgt and split*/$METRIC, there will be $SPLIT.bpe and $SPLIT.$METRIC files, respectively.
```
4. Pre-process the data into fairseq format.```# use comma to separate if there are more than one train or valid setfor suffix in src tgt ; do fairseq-preprocess --only-source \ --trainpref ${OUTPUT_DIR}/$METRIC/split1/input_${suffix}/train.bpe \ --validpref ${OUTPUT_DIR}/$METRIC/split1/input_${suffix}/valid.bpe \ --destdir ${OUTPUT_DIR}/$METRIC/split1/input_${suffix} \ --workers 60 \ --srcdict ${XLMR_DIR}/dict.txtdone
for i in `seq 2 ${NUM_SHARDS}`; do for suffix in src tgt ; do fairseq-preprocess --only-source \ --trainpref ${OUTPUT_DIR}/$METRIC/split${i}/input_${suffix}/train.bpe \ --destdir ${OUTPUT_DIR}/$METRIC/split${i}/input_${suffix} \ --workers 60 \ --srcdict ${XLMR_DIR}/dict.txt
ln -s ${OUTPUT_DIR}/$METRIC/split1/input_${suffix}/valid* ${OUTPUT_DIR}/$METRIC/split${i}/input_${suffix}/. done
ln -s ${OUTPUT_DIR}/$METRIC/split1/$METRIC/valid* ${OUTPUT_DIR}/$METRIC/split${i}/$METRIC/.done```
## Training
```EXP_DIR=/path/to/exp
# An example of training the model with the config for De-En experiment in the paper.# The config uses 16 GPUs and 50 hypotheses.# For training with fewer number of GPUs, set# distributed_training.distributed_world_size=k +optimization.update_freq='[x]' where x = 16/k# For training with fewer number of hypotheses, set# task.mt_beam=N dataset.batch_size=N dataset.required_batch_size_multiple=N
fairseq-hydra-train -m \ --config-dir config/ --config-name deen \ task.data=${OUTPUT_DIR}/$METRIC/split1/ \ task.num_data_splits=${NUM_SHARDS} \ model.pretrained_model=${XLMR_DIR}/model.pt \ common.user_dir=${FAIRSEQ_ROOT}/examples/discriminative_reranking_nmt \ checkpoint.save_dir=${EXP_DIR}
```
## Inference & scoringPerform DrNMT reranking (fw + reranker score)1. Tune weights on valid sets.```# genrate N hypotheses with the base MT model (fw score)VALID_SOURCE_FILE=/path/to/source_sentences # one sentence per line, converted to the sentencepiece used by the base MT modelVALID_TARGET_FILE=/path/to/target_sentences # one sentence per line in raw text, i.e. no sentencepiece and tokenizationMT_MODEL=/path/to/mt_modelMT_DATA_PATH=/path/to/mt_data
cat ${VALID_SOURCE_FILE} | \ fairseq-interactive ${MT_DATA_PATH} \ --max-tokens 4000 --buffer-size 16 \ --num-workers 32 --path ${MT_MODEL} \ --beam $N --nbest $N \ --post-process sentencepiece &> valid-hypo.out
# replace "bleu" with "ter" to optimize for TERpython drnmt_rerank.py \ ${OUTPUT_DIR}/$METRIC/split1/ \ --path ${EXP_DIR}/checkpoint_best.pt \ --in-text valid-hypo.out \ --results-path ${EXP_DIR} \ --gen-subset valid \ --target-text ${VALID_TARGET_FILE} \ --user-dir ${FAIRSEQ_ROOT}/examples/discriminative_reranking_nmt \ --bpe sentencepiece \ --sentencepiece-model ${XLMR_DIR}/sentencepiece.bpe.model \ --beam $N \ --batch-size $N \ --metric bleu \ --tune
```
2. Apply best weights on test sets```# genrate N hypotheses with the base MT model (fw score)TEST_SOURCE_FILE=/path/to/source_sentences # one sentence per line, converted to the sentencepiece used by the base MT model
cat ${TEST_SOURCE_FILE} | \ fairseq-interactive ${MT_DATA_PATH} \ --max-tokens 4000 --buffer-size 16 \ --num-workers 32 --path ${MT_MODEL} \ --beam $N --nbest $N \ --post-process sentencepiece &> test-hypo.out
# replace "bleu" with "ter" to evaluate TER# Add --target-text for evaluating BLEU/TER,# otherwise the script will only generate the hypotheses with the highest scores only.python drnmt_rerank.py \ ${OUTPUT_DIR}/$METRIC/split1/ \ --path ${EXP_DIR}/checkpoint_best.pt \ --in-text test-hypo.out \ --results-path ${EXP_DIR} \ --gen-subset test \ --user-dir ${FAIRSEQ_ROOT}/examples/discriminative_reranking_nmt \ --bpe sentencepiece \ --sentencepiece-model ${XLMR_DIR}/sentencepiece.bpe.model \ --beam $N \ --batch-size $N \ --metric bleu \ --fw-weight ${BEST_FW_WEIGHT} \ --lenpen ${BEST_LENPEN}```
## Citation```bibtex@inproceedings{lee2021discriminative, title={Discriminative Reranking for Neural Machine Translation}, author={Lee, Ann and Auli, Michael and Ranzato, Marc'Aurelio}, booktitle={ACL}, year={2021}}```1from . import criterions, models, tasks # noqa1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556# @package _group_
common: fp16: true log_format: json log_interval: 50 seed: 2
checkpoint: no_epoch_checkpoints: true best_checkpoint_metric: bleu maximize_best_checkpoint_metric: true
task: _name: discriminative_reranking_nmt data: ??? num_data_splits: ??? include_src: true mt_beam: 50 eval_target_metric: true target_metric: bleu
dataset: batch_size: 50 num_workers: 6 required_batch_size_multiple: 50 valid_subset: ???
criterion: _name: kl_divergence_rereanking target_dist_norm: minmax temperature: 0.5
optimization: max_epoch: 200 lr: [0.00005] update_freq: [32]
optimizer: _name: adam adam_betas: (0.9,0.98) adam_eps: 1e-06
lr_scheduler: _name: polynomial_decay warmup_updates: 8000 total_num_update: 320000
model: _name: discriminative_nmt_reranker pretrained_model: ??? classifier_dropout: 0.2
distributed_training: ddp_backend: no_c10d distributed_world_size: 16123456from .discriminative_reranking_criterion import KLDivergenceRerankingCriterion
__all__ = [ "KLDivergenceRerankingCriterion",]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import mathfrom dataclasses import dataclass, field
import torchimport torch.nn.functional as F
from fairseq import utilsfrom fairseq.logging import metricsfrom fairseq.criterions import FairseqCriterion, register_criterionfrom fairseq.dataclass import ChoiceEnum, FairseqDataclass
_EPSILON = torch.finfo(torch.float32).epsTARGET_DIST_NORM_CHOICES = ChoiceEnum(["none", "minmax"])
@dataclassclass KLDivergenceRerankingCriterionConfig(FairseqDataclass): target_dist_norm: TARGET_DIST_NORM_CHOICES = field( default="none", metadata={"help": "method to normalize the range of target scores"}, ) temperature: float = field( default=1.0, metadata={"help": "temperature in softmax for target distributions"}, ) forward_batch_size: int = field( default=32, metadata={ "help": "number of hypotheses per batch for model forward (set a value smaller than --mt-beam to avoid OOM when training with a large beam size)" }, )
@register_criterion( "kl_divergence_rereanking", dataclass=KLDivergenceRerankingCriterionConfig)class KLDivergenceRerankingCriterion(FairseqCriterion): def __init__( self, task, target_dist_norm, temperature, forward_batch_size, ): super().__init__(task) self.target_dist_norm = target_dist_norm self.temperature = temperature self.forward_batch_size = forward_batch_size
def forward(self, model, sample, reduce=True): """Compute the loss for the given sample.
Returns a tuple with three elements: 1) the loss 2) the sample size, which is used as the denominator for the gradient 3) logging outputs to display while training """
sample_size = sample["id"].numel() assert sample_size % self.task.cfg.mt_beam == 0, ( f"sample_size ({sample_size}) cannot be divided by beam size ({self.task.cfg.mt_beam})." f"Please set --required-batch-size-multiple={self.task.cfg.mt_beam}." )
# split into smaller batches for model forward batch_out = [] for i in range(0, sample_size, self.forward_batch_size): j = min(i + self.forward_batch_size, sample_size)
out = model( src_tokens=sample["net_input"]["src_tokens"][i:j, :], src_lengths=sample["net_input"]["src_lengths"][i:j], )
batch_out.append( model.sentence_forward(out, sample["net_input"]["src_tokens"][i:j, :]) )
batch_out = torch.cat(batch_out, dim=0).view( self.task.cfg.mt_beam, sample_size // self.task.cfg.mt_beam, -1 ) # T x B x C if model.joint_classification == "sent": batch_out = model.joint_forward(batch_out) scores = model.classification_forward(batch_out.view(sample_size, 1, -1)).view( -1, self.task.cfg.mt_beam ) # input: B x T x C
loss = self.compute_kl_loss( scores, sample["target"][:, 0].view(-1, self.task.cfg.mt_beam) )
sample_size = sample_size // self.task.cfg.mt_beam
logging_output = { "loss": loss.detach(), "ntokens": sample["ntokens"], "nsentences": sample_size * self.task.cfg.mt_beam, "sample_size": sample_size, "scores": scores.detach(), }
return loss, sample_size, logging_output
def compute_kl_loss(self, logits, target): norm_target = target if self.target_dist_norm == "minmax": min_v = torch.min(target, 1, keepdim=True).values max_v = torch.max(target, 1, keepdim=True).values norm_target = (target - min_v) / (max_v - min_v + _EPSILON)
target_dist = F.softmax( norm_target / self.temperature, dim=-1, dtype=torch.float32 ) model_dist = F.log_softmax(logits, dim=-1, dtype=torch.float32) loss = -(target_dist * model_dist - target_dist * target_dist.log()).sum() return loss
@staticmethod def reduce_metrics(logging_outputs) -> None: """Aggregate logging outputs from data parallel training.""" loss_sum = utils.item(sum(log.get("loss", 0) for log in logging_outputs))
sample_size = utils.item( sum(log.get("sample_size", 0) for log in logging_outputs) )
loss = loss_sum / sample_size / math.log(2) metrics.log_scalar("loss", loss, sample_size, round=3)
@staticmethod def logging_outputs_can_be_summed() -> bool: """ Whether the logging outputs returned by `forward` can be summed across workers prior to calling `reduce_metrics`. Setting this to True will improves distributed training speed. """ return True123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364#!/usr/bin/env python3 -u# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree."""Score raw text with a trained model."""
from collections import namedtupleimport loggingfrom multiprocessing import Poolimport sysimport osimport random
import numpy as npimport sacrebleuimport torch
from fairseq import checkpoint_utils, options, utils
logger = logging.getLogger("fairseq_cli.drnmt_rerank")logger.setLevel(logging.INFO)
Batch = namedtuple("Batch", "ids src_tokens src_lengths")
pool_init_variables = {}
def init_loaded_scores(mt_scores, model_scores, hyp, ref): global pool_init_variables pool_init_variables["mt_scores"] = mt_scores pool_init_variables["model_scores"] = model_scores pool_init_variables["hyp"] = hyp pool_init_variables["ref"] = ref
def parse_fairseq_gen(filename, task): source = {} hypos = {} scores = {} with open(filename, "r", encoding="utf-8") as f: for line in f: line = line.strip() if line.startswith("S-"): # source uid, text = line.split("\t", 1) uid = int(uid[2:]) source[uid] = text elif line.startswith("D-"): # hypo uid, score, text = line.split("\t", 2) uid = int(uid[2:]) if uid not in hypos: hypos[uid] = [] scores[uid] = [] hypos[uid].append(text) scores[uid].append(float(score)) else: continue
source_out = [source[i] for i in range(len(hypos))] hypos_out = [h for i in range(len(hypos)) for h in hypos[i]] scores_out = [s for i in range(len(scores)) for s in scores[i]]
return source_out, hypos_out, scores_out
def read_target(filename): with open(filename, "r", encoding="utf-8") as f: output = [line.strip() for line in f] return output
def make_batches(args, src, hyp, task, max_positions, encode_fn): assert len(src) * args.beam == len( hyp ), f"Expect {len(src) * args.beam} hypotheses for {len(src)} source sentences with beam size {args.beam}. Got {len(hyp)} hypotheses intead." hyp_encode = [ task.source_dictionary.encode_line(encode_fn(h), add_if_not_exist=False).long() for h in hyp ] if task.cfg.include_src: src_encode = [ task.source_dictionary.encode_line( encode_fn(s), add_if_not_exist=False ).long() for s in src ] tokens = [(src_encode[i // args.beam], h) for i, h in enumerate(hyp_encode)] lengths = [(t1.numel(), t2.numel()) for t1, t2 in tokens] else: tokens = [(h,) for h in hyp_encode] lengths = [(h.numel(),) for h in hyp_encode]
itr = task.get_batch_iterator( dataset=task.build_dataset_for_inference(tokens, lengths), max_tokens=args.max_tokens, max_sentences=args.batch_size, max_positions=max_positions, ignore_invalid_inputs=args.skip_invalid_size_inputs_valid_test, ).next_epoch_itr(shuffle=False)
for batch in itr: yield Batch( ids=batch["id"], src_tokens=batch["net_input"]["src_tokens"], src_lengths=batch["net_input"]["src_lengths"], )
def decode_rerank_scores(args): if args.max_tokens is None and args.batch_size is None: args.batch_size = 1
logger.info(args)
use_cuda = torch.cuda.is_available() and not args.cpu
# Load ensemble logger.info("loading model(s) from {}".format(args.path)) models, _model_args, task = checkpoint_utils.load_model_ensemble_and_task( [args.path], arg_overrides=eval(args.model_overrides), )
for model in models: if args.fp16: model.half() if use_cuda: model.cuda()
# Initialize generator generator = task.build_generator(args)
# Handle tokenization and BPE tokenizer = task.build_tokenizer(args) bpe = task.build_bpe(args)
def encode_fn(x): if tokenizer is not None: x = tokenizer.encode(x) if bpe is not None: x = bpe.encode(x) return x
max_positions = utils.resolve_max_positions( task.max_positions(), *[model.max_positions() for model in models] )
src, hyp, mt_scores = parse_fairseq_gen(args.in_text, task) model_scores = {} logger.info("decode reranker score") for batch in make_batches(args, src, hyp, task, max_positions, encode_fn): src_tokens = batch.src_tokens src_lengths = batch.src_lengths if use_cuda: src_tokens = src_tokens.cuda() src_lengths = src_lengths.cuda()
sample = { "net_input": {"src_tokens": src_tokens, "src_lengths": src_lengths}, } scores = task.inference_step(generator, models, sample)
for id, sc in zip(batch.ids.tolist(), scores.tolist()): model_scores[id] = sc[0]
model_scores = [model_scores[i] for i in range(len(model_scores))]
return src, hyp, mt_scores, model_scores
def get_score(mt_s, md_s, w1, lp, tgt_len): return mt_s / (tgt_len ** lp) * w1 + md_s
def get_best_hyps(mt_scores, md_scores, hypos, fw_weight, lenpen, beam): assert len(mt_scores) == len(md_scores) and len(mt_scores) == len(hypos) hypo_scores = [] best_hypos = [] best_scores = [] offset = 0 for i in range(len(hypos)): tgt_len = len(hypos[i].split()) hypo_scores.append( get_score(mt_scores[i], md_scores[i], fw_weight, lenpen, tgt_len) )
if (i + 1) % beam == 0: max_i = np.argmax(hypo_scores) best_hypos.append(hypos[offset + max_i]) best_scores.append(hypo_scores[max_i]) hypo_scores = [] offset += beam return best_hypos, best_scores
def eval_metric(args, hypos, ref): if args.metric == "bleu": score = sacrebleu.corpus_bleu(hypos, [ref]).score else: score = sacrebleu.corpus_ter(hypos, [ref]).score
return score
def score_target_hypo(args, fw_weight, lp): mt_scores = pool_init_variables["mt_scores"] model_scores = pool_init_variables["model_scores"] hyp = pool_init_variables["hyp"] ref = pool_init_variables["ref"] best_hypos, _ = get_best_hyps( mt_scores, model_scores, hyp, fw_weight, lp, args.beam ) rerank_eval = None if ref: rerank_eval = eval_metric(args, best_hypos, ref) print(f"fw_weight {fw_weight}, lenpen {lp}, eval {rerank_eval}")
return rerank_eval
def print_result(best_scores, best_hypos, output_file): for i, (s, h) in enumerate(zip(best_scores, best_hypos)): print(f"{i}\t{s}\t{h}", file=output_file)
def main(args): utils.import_user_module(args)
src, hyp, mt_scores, model_scores = decode_rerank_scores(args)
assert ( not args.tune or args.target_text is not None ), "--target-text has to be set when tuning weights" if args.target_text: ref = read_target(args.target_text) assert len(src) == len( ref ), f"different numbers of source and target sentences ({len(src)} vs. {len(ref)})"
orig_best_hypos = [hyp[i] for i in range(0, len(hyp), args.beam)] orig_eval = eval_metric(args, orig_best_hypos, ref)
if args.tune: logger.info("tune weights for reranking")
random_params = np.array( [ [ random.uniform( args.lower_bound_fw_weight, args.upper_bound_fw_weight ), random.uniform(args.lower_bound_lenpen, args.upper_bound_lenpen), ] for k in range(args.num_trials) ] )
logger.info("launching pool") with Pool( 32, initializer=init_loaded_scores, initargs=(mt_scores, model_scores, hyp, ref), ) as p: rerank_scores = p.starmap( score_target_hypo, [ (args, random_params[i][0], random_params[i][1],) for i in range(args.num_trials) ], ) if args.metric == "bleu": best_index = np.argmax(rerank_scores) else: best_index = np.argmin(rerank_scores) best_fw_weight = random_params[best_index][0] best_lenpen = random_params[best_index][1] else: assert ( args.lenpen is not None and args.fw_weight is not None ), "--lenpen and --fw-weight should be set" best_fw_weight, best_lenpen = args.fw_weight, args.lenpen
best_hypos, best_scores = get_best_hyps( mt_scores, model_scores, hyp, best_fw_weight, best_lenpen, args.beam )
if args.results_path is not None: os.makedirs(args.results_path, exist_ok=True) output_path = os.path.join( args.results_path, "generate-{}.txt".format(args.gen_subset), ) with open(output_path, "w", buffering=1, encoding="utf-8") as o: print_result(best_scores, best_hypos, o) else: print_result(best_scores, best_hypos, sys.stdout)
if args.target_text: rerank_eval = eval_metric(args, best_hypos, ref) print(f"before reranking, {args.metric.upper()}:", orig_eval) print( f"after reranking with fw_weight={best_fw_weight}, lenpen={best_lenpen}, {args.metric.upper()}:", rerank_eval, )
def cli_main(): parser = options.get_generation_parser(interactive=True)
parser.add_argument( "--in-text", default=None, required=True, help="text from fairseq-interactive output, containing source sentences and hypotheses", ) parser.add_argument("--target-text", default=None, help="reference text") parser.add_argument("--metric", type=str, choices=["bleu", "ter"], default="bleu") parser.add_argument( "--tune", action="store_true", help="if set, tune weights on fw scores and lenpen instead of applying fixed weights for reranking", ) parser.add_argument( "--lower-bound-fw-weight", default=0.0, type=float, help="lower bound of search space", ) parser.add_argument( "--upper-bound-fw-weight", default=3, type=float, help="upper bound of search space", ) parser.add_argument( "--lower-bound-lenpen", default=0.0, type=float, help="lower bound of search space", ) parser.add_argument( "--upper-bound-lenpen", default=3, type=float, help="upper bound of search space", ) parser.add_argument( "--fw-weight", type=float, default=None, help="weight on the fw model score" ) parser.add_argument( "--num-trials", default=1000, type=int, help="number of trials to do for random search", )
args = options.parse_args_and_arch(parser) main(args)
if __name__ == "__main__": cli_main()123456from .discriminative_reranking_model import DiscriminativeNMTReranker
__all__ = [ "DiscriminativeNMTReranker",]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365from dataclasses import dataclass, fieldimport os
import torchimport torch.nn as nn
from fairseq import utilsfrom fairseq.dataclass import ChoiceEnum, FairseqDataclassfrom fairseq.models import ( BaseFairseqModel, register_model,)
from fairseq.models.roberta.model import RobertaClassificationHead
from fairseq.modules import ( LayerNorm, TransformerSentenceEncoder, TransformerSentenceEncoderLayer,)
ACTIVATION_FN_CHOICES = ChoiceEnum(utils.get_available_activation_fns())JOINT_CLASSIFICATION_CHOICES = ChoiceEnum(["none", "sent"])SENTENCE_REP_CHOICES = ChoiceEnum(["head", "meanpool", "maxpool"])
def update_init_roberta_model_state(state): """ update the state_dict of a Roberta model for initializing weights of the BertRanker """ for k in list(state.keys()): if ".lm_head." in k or "version" in k: del state[k] continue # remove 'encoder/decoder.sentence_encoder.' from the key assert k.startswith("encoder.sentence_encoder.") or k.startswith( "decoder.sentence_encoder." ), f"Cannot recognize parameter name {k}" if "layernorm_embedding" in k: new_k = k.replace(".layernorm_embedding.", ".emb_layer_norm.") state[new_k[25:]] = state[k] else: state[k[25:]] = state[k] del state[k]
class BaseRanker(nn.Module): def __init__(self, args, task): super().__init__()
self.separator_token = task.dictionary.eos() self.padding_idx = task.dictionary.pad()
def forward(self, src_tokens): raise NotImplementedError
def get_segment_labels(self, src_tokens): segment_boundary = (src_tokens == self.separator_token).long() segment_labels = ( segment_boundary.cumsum(dim=1) - segment_boundary - (src_tokens == self.padding_idx).long() )
return segment_labels
def get_positions(self, src_tokens, segment_labels): segment_positions = ( torch.arange(src_tokens.shape[1]) .to(src_tokens.device) .repeat(src_tokens.shape[0], 1) ) segment_boundary = (src_tokens == self.separator_token).long() _, col_idx = (segment_positions * segment_boundary).nonzero(as_tuple=True) col_idx = torch.cat([torch.zeros(1).type_as(col_idx), col_idx]) offset = torch.cat( [ torch.zeros(1).type_as(segment_boundary), segment_boundary.sum(dim=1).cumsum(dim=0)[:-1], ] ) segment_positions -= col_idx[segment_labels + offset.unsqueeze(1)] * ( segment_labels != 0 )
padding_mask = src_tokens.ne(self.padding_idx) segment_positions = (segment_positions + 1) * padding_mask.type_as( segment_positions ) + self.padding_idx
return segment_positions
class BertRanker(BaseRanker): def __init__(self, args, task): super(BertRanker, self).__init__(args, task)
init_model = getattr(args, "pretrained_model", "") self.joint_layers = nn.ModuleList() if os.path.isfile(init_model): print(f"initialize weight from {init_model}")
from fairseq import hub_utils
x = hub_utils.from_pretrained( os.path.dirname(init_model), checkpoint_file=os.path.basename(init_model), )
in_state_dict = x["models"][0].state_dict() init_args = x["args"].model
num_positional_emb = init_args.max_positions + task.dictionary.pad() + 1
# follow the setup in roberta self.model = TransformerSentenceEncoder( padding_idx=task.dictionary.pad(), vocab_size=len(task.dictionary), num_encoder_layers=getattr( args, "encoder_layers", init_args.encoder_layers ), embedding_dim=init_args.encoder_embed_dim, ffn_embedding_dim=init_args.encoder_ffn_embed_dim, num_attention_heads=init_args.encoder_attention_heads, dropout=init_args.dropout, attention_dropout=init_args.attention_dropout, activation_dropout=init_args.activation_dropout, num_segments=2, # add language embeddings max_seq_len=num_positional_emb, offset_positions_by_padding=False, encoder_normalize_before=True, apply_bert_init=True, activation_fn=init_args.activation_fn, freeze_embeddings=args.freeze_embeddings, n_trans_layers_to_freeze=args.n_trans_layers_to_freeze, )
# still need to learn segment embeddings as we added a second language embedding if args.freeze_embeddings: for p in self.model.segment_embeddings.parameters(): p.requires_grad = False
update_init_roberta_model_state(in_state_dict) print("loading weights from the pretrained model") self.model.load_state_dict( in_state_dict, strict=False ) # ignore mismatch in language embeddings
ffn_embedding_dim = init_args.encoder_ffn_embed_dim num_attention_heads = init_args.encoder_attention_heads dropout = init_args.dropout attention_dropout = init_args.attention_dropout activation_dropout = init_args.activation_dropout activation_fn = init_args.activation_fn
classifier_embed_dim = getattr( args, "embed_dim", init_args.encoder_embed_dim ) if classifier_embed_dim != init_args.encoder_embed_dim: self.transform_layer = nn.Linear( init_args.encoder_embed_dim, classifier_embed_dim ) else: self.model = TransformerSentenceEncoder( padding_idx=task.dictionary.pad(), vocab_size=len(task.dictionary), num_encoder_layers=args.encoder_layers, embedding_dim=args.embed_dim, ffn_embedding_dim=args.ffn_embed_dim, num_attention_heads=args.attention_heads, dropout=args.dropout, attention_dropout=args.attention_dropout, activation_dropout=args.activation_dropout, max_seq_len=task.max_positions() if task.max_positions() else args.tokens_per_sample, num_segments=2, offset_positions_by_padding=False, encoder_normalize_before=args.encoder_normalize_before, apply_bert_init=args.apply_bert_init, activation_fn=args.activation_fn, )
classifier_embed_dim = args.embed_dim ffn_embedding_dim = args.ffn_embed_dim num_attention_heads = args.attention_heads dropout = args.dropout attention_dropout = args.attention_dropout activation_dropout = args.activation_dropout activation_fn = args.activation_fn
self.joint_classification = args.joint_classification if args.joint_classification == "sent": if args.joint_normalize_before: self.joint_layer_norm = LayerNorm(classifier_embed_dim) else: self.joint_layer_norm = None
self.joint_layers = nn.ModuleList( [ TransformerSentenceEncoderLayer( embedding_dim=classifier_embed_dim, ffn_embedding_dim=ffn_embedding_dim, num_attention_heads=num_attention_heads, dropout=dropout, attention_dropout=attention_dropout, activation_dropout=activation_dropout, activation_fn=activation_fn, ) for _ in range(args.num_joint_layers) ] )
self.classifier = RobertaClassificationHead( classifier_embed_dim, classifier_embed_dim, 1, # num_classes "tanh", args.classifier_dropout, )
def forward(self, src_tokens, src_lengths): segment_labels = self.get_segment_labels(src_tokens) positions = self.get_positions(src_tokens, segment_labels)
inner_states, _ = self.model( tokens=src_tokens, segment_labels=segment_labels, last_state_only=True, positions=positions, )
return inner_states[-1].transpose(0, 1) # T x B x C -> B x T x C
def sentence_forward(self, encoder_out, src_tokens=None, sentence_rep="head"): # encoder_out: B x T x C if sentence_rep == "head": x = encoder_out[:, :1, :] else: # 'meanpool', 'maxpool' assert src_tokens is not None, "meanpool requires src_tokens input" segment_labels = self.get_segment_labels(src_tokens) padding_mask = src_tokens.ne(self.padding_idx) encoder_mask = segment_labels * padding_mask.type_as(segment_labels)
if sentence_rep == "meanpool": ntokens = torch.sum(encoder_mask, dim=1, keepdim=True) x = torch.sum( encoder_out * encoder_mask.unsqueeze(2), dim=1, keepdim=True ) / ntokens.unsqueeze(2).type_as(encoder_out) else: # 'maxpool' encoder_out[ (encoder_mask == 0).unsqueeze(2).repeat(1, 1, encoder_out.shape[-1]) ] = -float("inf") x, _ = torch.max(encoder_out, dim=1, keepdim=True)
if hasattr(self, "transform_layer"): x = self.transform_layer(x)
return x # B x 1 x C
def joint_forward(self, x): # x: T x B x C if self.joint_layer_norm: x = self.joint_layer_norm(x.transpose(0, 1)) x = x.transpose(0, 1)
for layer in self.joint_layers: x, _ = layer(x, self_attn_padding_mask=None) return x
def classification_forward(self, x): # x: B x T x C return self.classifier(x)
@dataclassclass DiscriminativeNMTRerankerConfig(FairseqDataclass): pretrained_model: str = field( default="", metadata={"help": "pretrained model to load"} ) sentence_rep: SENTENCE_REP_CHOICES = field( default="head", metadata={ "help": "method to transform the output of the transformer stack to a sentence-level representation" }, )
dropout: float = field(default=0.1, metadata={"help": "dropout probability"}) attention_dropout: float = field( default=0.0, metadata={"help": "dropout probability for attention weights"} ) activation_dropout: float = field( default=0.0, metadata={"help": "dropout probability after activation in FFN"} ) classifier_dropout: float = field( default=0.0, metadata={"help": "classifier dropout probability"} ) embed_dim: int = field(default=768, metadata={"help": "embedding dimension"}) ffn_embed_dim: int = field( default=2048, metadata={"help": "embedding dimension for FFN"} ) encoder_layers: int = field(default=12, metadata={"help": "num encoder layers"}) attention_heads: int = field(default=8, metadata={"help": "num attention heads"}) encoder_normalize_before: bool = field( default=False, metadata={"help": "apply layernorm before each encoder block"} ) apply_bert_init: bool = field( default=False, metadata={"help": "use custom param initialization for BERT"} ) activation_fn: ACTIVATION_FN_CHOICES = field( default="relu", metadata={"help": "activation function to use"} ) freeze_embeddings: bool = field( default=False, metadata={"help": "freeze embeddings in the pretrained model"} ) n_trans_layers_to_freeze: int = field( default=0, metadata={ "help": "number of layers to freeze in the pretrained transformer model" }, )
# joint classfication joint_classification: JOINT_CLASSIFICATION_CHOICES = field( default="none", metadata={"help": "method to compute joint features for classification"}, ) num_joint_layers: int = field( default=1, metadata={"help": "number of joint layers"} ) joint_normalize_before: bool = field( default=False, metadata={"help": "apply layer norm on the input to the joint layer"}, )
@register_model( "discriminative_nmt_reranker", dataclass=DiscriminativeNMTRerankerConfig)class DiscriminativeNMTReranker(BaseFairseqModel): @classmethod def build_model(cls, args, task): model = BertRanker(args, task) return DiscriminativeNMTReranker(args, model)
def __init__(self, args, model): super().__init__()
self.model = model self.sentence_rep = args.sentence_rep self.joint_classification = args.joint_classification
def forward(self, src_tokens, src_lengths, **kwargs): return self.model(src_tokens, src_lengths)
def sentence_forward(self, encoder_out, src_tokens): return self.model.sentence_forward(encoder_out, src_tokens, self.sentence_rep)
def joint_forward(self, x): return self.model.joint_forward(x)
def classification_forward(self, x): return self.model.classification_forward(x)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136#!/usr/bin/env python
import argparsefrom multiprocessing import Poolfrom pathlib import Path
import sacrebleuimport sentencepiece as spm
def read_text_file(filename): with open(filename, "r") as f: output = [line.strip() for line in f]
return output
def get_bleu(in_sent, target_sent): bleu = sacrebleu.corpus_bleu([in_sent], [[target_sent]]) out = " ".join( map(str, [bleu.score, bleu.sys_len, bleu.ref_len] + bleu.counts + bleu.totals) ) return out
def get_ter(in_sent, target_sent): ter = sacrebleu.corpus_ter([in_sent], [[target_sent]]) out = " ".join(map(str, [ter.score, ter.num_edits, ter.ref_length])) return out
def init(sp_model): global sp sp = spm.SentencePieceProcessor() sp.Load(sp_model)
def process(source_sent, target_sent, hypo_sent, metric): source_bpe = " ".join(sp.EncodeAsPieces(source_sent)) hypo_bpe = [" ".join(sp.EncodeAsPieces(h)) for h in hypo_sent]
if metric == "bleu": score_str = [get_bleu(h, target_sent) for h in hypo_sent] else: # ter score_str = [get_ter(h, target_sent) for h in hypo_sent]
return source_bpe, hypo_bpe, score_str
def main(args): assert ( args.split.startswith("train") or args.num_shards == 1 ), "--num-shards should be set to 1 for valid and test sets" assert ( args.split.startswith("train") or args.split.startswith("valid") or args.split.startswith("test") ), "--split should be set to train[n]/valid[n]/test[n]"
source_sents = read_text_file(args.input_source) target_sents = read_text_file(args.input_target)
num_sents = len(source_sents) assert num_sents == len( target_sents ), f"{args.input_source} and {args.input_target} should have the same number of sentences."
hypo_sents = read_text_file(args.input_hypo) assert ( len(hypo_sents) % args.beam == 0 ), f"Number of hypotheses ({len(hypo_sents)}) cannot be divided by beam size ({args.beam})."
hypo_sents = [ hypo_sents[i : i + args.beam] for i in range(0, len(hypo_sents), args.beam) ] assert num_sents == len( hypo_sents ), f"{args.input_hypo} should contain {num_sents * args.beam} hypotheses but only has {len(hypo_sents) * args.beam}. (--beam={args.beam})"
output_dir = args.output_dir / args.metric for ns in range(args.num_shards): print(f"processing shard {ns+1}/{args.num_shards}") shard_output_dir = output_dir / f"split{ns+1}" source_output_dir = shard_output_dir / "input_src" hypo_output_dir = shard_output_dir / "input_tgt" metric_output_dir = shard_output_dir / args.metric
source_output_dir.mkdir(parents=True, exist_ok=True) hypo_output_dir.mkdir(parents=True, exist_ok=True) metric_output_dir.mkdir(parents=True, exist_ok=True)
if args.n_proc > 1: with Pool( args.n_proc, initializer=init, initargs=(args.sentencepiece_model,) ) as p: output = p.starmap( process, [ (source_sents[i], target_sents[i], hypo_sents[i], args.metric) for i in range(ns, num_sents, args.num_shards) ], ) else: init(args.sentencepiece_model) output = [ process(source_sents[i], target_sents[i], hypo_sents[i], args.metric) for i in range(ns, num_sents, args.num_shards) ]
with open(source_output_dir / f"{args.split}.bpe", "w") as s_o, open( hypo_output_dir / f"{args.split}.bpe", "w" ) as h_o, open(metric_output_dir / f"{args.split}.{args.metric}", "w") as m_o: for source_bpe, hypo_bpe, score_str in output: assert len(hypo_bpe) == len(score_str) for h, m in zip(hypo_bpe, score_str): s_o.write(f"{source_bpe}\n") h_o.write(f"{h}\n") m_o.write(f"{m}\n")
if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--input-source", type=Path, required=True) parser.add_argument("--input-target", type=Path, required=True) parser.add_argument("--input-hypo", type=Path, required=True) parser.add_argument("--output-dir", type=Path, required=True) parser.add_argument("--split", type=str, required=True) parser.add_argument("--beam", type=int, required=True) parser.add_argument("--sentencepiece-model", type=str, required=True) parser.add_argument("--metric", type=str, choices=["bleu", "ter"], default="bleu") parser.add_argument("--num-shards", type=int, default=1) parser.add_argument("--n-proc", type=int, default=8)
args = parser.parse_args()
main(args)123456from .discriminative_reranking_task import DiscriminativeRerankingNMTTask
__all__ = [ "DiscriminativeRerankingNMTTask",]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
from dataclasses import dataclass, field
import itertoolsimport loggingimport os
import numpy as npimport torch
from fairseq.logging import metricsfrom fairseq.data import ( ConcatDataset, ConcatSentencesDataset, data_utils, Dictionary, IdDataset, indexed_dataset, NestedDictionaryDataset, NumSamplesDataset, NumelDataset, PrependTokenDataset, RawLabelDataset, RightPadDataset, SortDataset, TruncateDataset, TokenBlockDataset,)from fairseq.dataclass import ChoiceEnum, FairseqDataclassfrom fairseq.tasks import FairseqTask, register_taskfrom omegaconf import II, MISSING
EVAL_BLEU_ORDER = 4TARGET_METRIC_CHOICES = ChoiceEnum(["bleu", "ter"])
logger = logging.getLogger(__name__)
@dataclassclass DiscriminativeRerankingNMTConfig(FairseqDataclass): data: str = field(default=MISSING, metadata={"help": "path to data directory"}) num_data_splits: int = field( default=1, metadata={"help": "total number of data splits"} ) no_shuffle: bool = field( default=False, metadata={"help": "do not shuffle training data"} ) max_positions: int = field( default=512, metadata={"help": "number of positional embeddings to learn"} ) include_src: bool = field( default=False, metadata={"help": "include source sentence"} ) mt_beam: int = field(default=50, metadata={"help": "beam size of input hypotheses"}) eval_target_metric: bool = field( default=False, metadata={"help": "evaluation with the target metric during validation"}, ) target_metric: TARGET_METRIC_CHOICES = field( default="bleu", metadata={"help": "name of the target metric to optimize for"} ) train_subset: str = field( default=II("dataset.train_subset"), metadata={"help": "data subset to use for training (e.g. train, valid, test)"}, ) seed: int = field( default=II("common.seed"), metadata={"help": "pseudo random number generator seed"}, )
class RerankerScorer(object): """Scores the target for a given (source (optional), target) input."""
def __init__(self, args, mt_beam): self.mt_beam = mt_beam
@torch.no_grad() def generate(self, models, sample, **kwargs): """Score a batch of translations.""" net_input = sample["net_input"]
assert len(models) == 1, "does not support model ensemble" model = models[0]
bs = net_input["src_tokens"].shape[0] assert ( model.joint_classification == "none" or bs % self.mt_beam == 0 ), f"invalid batch size ({bs}) for joint classification with beam size ({self.mt_beam})"
model.eval() logits = model(**net_input)
batch_out = model.sentence_forward(logits, net_input["src_tokens"]) if model.joint_classification == "sent": batch_out = model.joint_forward( batch_out.view(self.mt_beam, bs // self.mt_beam, -1) ) scores = model.classification_forward( batch_out.view(bs, 1, -1) ) # input: B x T x C
return scores
@register_task( "discriminative_reranking_nmt", dataclass=DiscriminativeRerankingNMTConfig)class DiscriminativeRerankingNMTTask(FairseqTask): """ Translation rerank task. The input can be either (src, tgt) sentence pairs or tgt sentence only. """
cfg: DiscriminativeRerankingNMTConfig
def __init__(self, cfg: DiscriminativeRerankingNMTConfig, data_dictionary=None): super().__init__(cfg) self.dictionary = data_dictionary self._max_positions = cfg.max_positions # args.tokens_per_sample = self._max_positions # self.num_classes = 1 # for model
@classmethod def load_dictionary(cls, cfg, filename): """Load the dictionary from the filename""" dictionary = Dictionary.load(filename) dictionary.add_symbol("<mask>") # for loading pretrained XLMR model
return dictionary
@classmethod def setup_task(cls, cfg: DiscriminativeRerankingNMTConfig, **kwargs): # load data dictionary (assume joint dictionary) data_path = cfg.data data_dict = cls.load_dictionary( cfg, os.path.join(data_path, "input_src/dict.txt") )
logger.info("[input] src dictionary: {} types".format(len(data_dict)))
return DiscriminativeRerankingNMTTask(cfg, data_dict)
def load_dataset(self, split, epoch=0, combine=False, **kwargs): """Load a given dataset split (e.g., train, valid, test).""" if self.cfg.data.endswith("1"): data_shard = (epoch - 1) % self.cfg.num_data_splits + 1 data_path = self.cfg.data[:-1] + str(data_shard) else: data_path = self.cfg.data
def get_path(type, data_split): return os.path.join(data_path, str(type), data_split)
def make_dataset(type, dictionary, data_split, combine): split_path = get_path(type, data_split)
dataset = data_utils.load_indexed_dataset( split_path, dictionary, combine=combine, ) return dataset
def load_split(data_split, metric): input_src = None if self.cfg.include_src: input_src = make_dataset( "input_src", self.dictionary, data_split, combine=False ) assert input_src is not None, "could not find dataset: {}".format( get_path("input_src", data_split) )
input_tgt = make_dataset( "input_tgt", self.dictionary, data_split, combine=False ) assert input_tgt is not None, "could not find dataset: {}".format( get_path("input_tgt", data_split) )
label_path = f"{get_path(metric, data_split)}.{metric}" assert os.path.exists(label_path), f"could not find dataset: {label_path}"
np_labels = np.loadtxt(label_path) if self.cfg.target_metric == "ter": np_labels = -np_labels label = RawLabelDataset(np_labels)
return input_src, input_tgt, label
src_datasets = [] tgt_datasets = [] label_datasets = []
if split == self.cfg.train_subset: for k in itertools.count(): split_k = "train" + (str(k) if k > 0 else "") prefix = os.path.join(data_path, "input_tgt", split_k) if not indexed_dataset.dataset_exists(prefix, impl=None): if k > 0: break else: raise FileNotFoundError(f"Dataset not found: {prefix}") input_src, input_tgt, label = load_split( split_k, self.cfg.target_metric ) src_datasets.append(input_src) tgt_datasets.append(input_tgt) label_datasets.append(label) else: input_src, input_tgt, label = load_split(split, self.cfg.target_metric) src_datasets.append(input_src) tgt_datasets.append(input_tgt) label_datasets.append(label)
if len(tgt_datasets) == 1: input_tgt, label = tgt_datasets[0], label_datasets[0] if self.cfg.include_src: input_src = src_datasets[0] else: input_tgt = ConcatDataset(tgt_datasets) label = ConcatDataset(label_datasets) if self.cfg.include_src: input_src = ConcatDataset(src_datasets)
input_tgt = TruncateDataset(input_tgt, self.cfg.max_positions) if self.cfg.include_src: input_src = PrependTokenDataset(input_src, self.dictionary.bos()) input_src = TruncateDataset(input_src, self.cfg.max_positions) src_lengths = NumelDataset(input_src, reduce=False) src_tokens = ConcatSentencesDataset(input_src, input_tgt) else: src_tokens = PrependTokenDataset(input_tgt, self.dictionary.bos()) src_lengths = NumelDataset(src_tokens, reduce=False)
dataset = { "id": IdDataset(), "net_input": { "src_tokens": RightPadDataset( src_tokens, pad_idx=self.source_dictionary.pad(), ), "src_lengths": src_lengths, }, "nsentences": NumSamplesDataset(), "ntokens": NumelDataset(src_tokens, reduce=True), "target": label, }
dataset = NestedDictionaryDataset( dataset, sizes=[src_tokens.sizes], )
assert ( len(dataset) % self.cfg.mt_beam == 0 ), "dataset size (%d) is not a multiple of beam size (%d)" % ( len(dataset), self.cfg.mt_beam, )
# no need to shuffle valid/test sets if not self.cfg.no_shuffle and split == self.cfg.train_subset:
# need to keep all hypothese together start_idx = np.arange(0, len(dataset), self.cfg.mt_beam) with data_utils.numpy_seed(self.cfg.seed + epoch): np.random.shuffle(start_idx)
idx = np.arange(0, self.cfg.mt_beam) shuffle = np.tile(idx, (len(start_idx), 1)).reshape(-1) + np.tile( start_idx, (self.cfg.mt_beam, 1) ).transpose().reshape(-1)
dataset = SortDataset( dataset, sort_order=[shuffle], )
logger.info(f"Loaded {split} with #samples: {len(dataset)}")
self.datasets[split] = dataset return self.datasets[split]
def build_dataset_for_inference(self, src_tokens, src_lengths, **kwargs): assert not self.cfg.include_src or len(src_tokens[0]) == 2 input_src = None if self.cfg.include_src: input_src = TokenBlockDataset( [t[0] for t in src_tokens], [l[0] for l in src_lengths], block_size=None, # ignored for "eos" break mode pad=self.source_dictionary.pad(), eos=self.source_dictionary.eos(), break_mode="eos", ) input_src = PrependTokenDataset(input_src, self.dictionary.bos()) input_src = TruncateDataset(input_src, self.cfg.max_positions)
input_tgt = TokenBlockDataset( [t[-1] for t in src_tokens], [l[-1] for l in src_lengths], block_size=None, # ignored for "eos" break mode pad=self.source_dictionary.pad(), eos=self.source_dictionary.eos(), break_mode="eos", ) input_tgt = TruncateDataset(input_tgt, self.cfg.max_positions) if self.cfg.include_src: src_tokens = ConcatSentencesDataset(input_src, input_tgt) src_lengths = NumelDataset(input_src, reduce=False) else: input_tgt = PrependTokenDataset(input_tgt, self.dictionary.bos()) src_tokens = input_tgt src_lengths = NumelDataset(src_tokens, reduce=False)
dataset = { "id": IdDataset(), "net_input": { "src_tokens": RightPadDataset( src_tokens, pad_idx=self.source_dictionary.pad(), ), "src_lengths": src_lengths, }, "nsentences": NumSamplesDataset(), "ntokens": NumelDataset(src_tokens, reduce=True), }
return NestedDictionaryDataset( dataset, sizes=[src_tokens.sizes], )
def build_model(self, cfg: FairseqDataclass, from_checkpoint: bool = False): return super().build_model(cfg)
def build_generator(self, args): return RerankerScorer(args, mt_beam=self.cfg.mt_beam)
def max_positions(self): return self._max_positions
@property def source_dictionary(self): return self.dictionary
@property def target_dictionary(self): return self.dictionary
def create_dummy_batch(self, device): dummy_target = ( torch.zeros(self.cfg.mt_beam, EVAL_BLEU_ORDER * 2 + 3).long().to(device) if not self.cfg.eval_ter else torch.zeros(self.cfg.mt_beam, 3).long().to(device) )
return { "id": torch.zeros(self.cfg.mt_beam, 1).long().to(device), "net_input": { "src_tokens": torch.zeros(self.cfg.mt_beam, 4).long().to(device), "src_lengths": torch.ones(self.cfg.mt_beam, 1).long().to(device), }, "nsentences": 0, "ntokens": 0, "target": dummy_target, }
def train_step( self, sample, model, criterion, optimizer, update_num, ignore_grad=False ): if ignore_grad and sample is None: sample = self.create_dummy_batch(model.device)
return super().train_step( sample, model, criterion, optimizer, update_num, ignore_grad )
def valid_step(self, sample, model, criterion): if sample is None: sample = self.create_dummy_batch(model.device)
loss, sample_size, logging_output = super().valid_step(sample, model, criterion)
if not self.cfg.eval_target_metric: return loss, sample_size, logging_output
scores = logging_output["scores"]
if self.cfg.target_metric == "bleu": assert sample["target"].shape[1] == EVAL_BLEU_ORDER * 2 + 3, ( "target does not contain enough information (" + str(sample["target"].shape[1]) + "for evaluating BLEU" )
max_id = torch.argmax(scores, dim=1) select_id = max_id + torch.arange( 0, sample_size * self.cfg.mt_beam, self.cfg.mt_beam ).to(max_id.device) bleu_data = sample["target"][select_id, 1:].sum(0).data
logging_output["_bleu_sys_len"] = bleu_data[0] logging_output["_bleu_ref_len"] = bleu_data[1]
for i in range(EVAL_BLEU_ORDER): logging_output["_bleu_counts_" + str(i)] = bleu_data[2 + i] logging_output["_bleu_totals_" + str(i)] = bleu_data[ 2 + EVAL_BLEU_ORDER + i ]
elif self.cfg.target_metric == "ter": assert sample["target"].shape[1] == 3, ( "target does not contain enough information (" + str(sample["target"].shape[1]) + "for evaluating TER" )
max_id = torch.argmax(scores, dim=1) select_id = max_id + torch.arange( 0, sample_size * self.cfg.mt_beam, self.cfg.mt_beam ).to(max_id.device) ter_data = sample["target"][select_id, 1:].sum(0).data
logging_output["_ter_num_edits"] = -ter_data[0] logging_output["_ter_ref_len"] = -ter_data[1]
return loss, sample_size, logging_output
def reduce_metrics(self, logging_outputs, criterion): super().reduce_metrics(logging_outputs, criterion)
if not self.cfg.eval_target_metric: return
def sum_logs(key): return sum(log.get(key, 0) for log in logging_outputs)
if self.cfg.target_metric == "bleu": counts, totals = [], [] for i in range(EVAL_BLEU_ORDER): counts.append(sum_logs("_bleu_counts_" + str(i))) totals.append(sum_logs("_bleu_totals_" + str(i)))
if max(totals) > 0: # log counts as numpy arrays -- log_scalar will sum them correctly metrics.log_scalar("_bleu_counts", np.array(counts)) metrics.log_scalar("_bleu_totals", np.array(totals)) metrics.log_scalar("_bleu_sys_len", sum_logs("_bleu_sys_len")) metrics.log_scalar("_bleu_ref_len", sum_logs("_bleu_ref_len"))
def compute_bleu(meters): import inspect import sacrebleu
fn_sig = inspect.getfullargspec(sacrebleu.compute_bleu)[0] if "smooth_method" in fn_sig: smooth = {"smooth_method": "exp"} else: smooth = {"smooth": "exp"} bleu = sacrebleu.compute_bleu( correct=meters["_bleu_counts"].sum, total=meters["_bleu_totals"].sum, sys_len=meters["_bleu_sys_len"].sum, ref_len=meters["_bleu_ref_len"].sum, **smooth, ) return round(bleu.score, 2)
metrics.log_derived("bleu", compute_bleu) elif self.cfg.target_metric == "ter": num_edits = sum_logs("_ter_num_edits") ref_len = sum_logs("_ter_ref_len")
if ref_len > 0: metrics.log_scalar("_ter_num_edits", num_edits) metrics.log_scalar("_ter_ref_len", ref_len)
def compute_ter(meters): score = meters["_ter_num_edits"].sum / meters["_ter_ref_len"].sum return round(score.item(), 2)
metrics.log_derived("ter", compute_ter)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123# MBART: Multilingual Denoising Pre-training for Neural Machine Translation[https://arxiv.org/abs/2001.08210]
## Introduction
MBART is a sequence-to-sequence denoising auto-encoder pre-trained on large-scale monolingual corpora in many languages using the BART objective. mBART is one of the first methods for pre-training a complete sequence-to-sequence model by denoising full texts in multiple languages, while previous approaches have focused only on the encoder, decoder, or reconstructing parts of the text.
## Pre-trained models
Model | Description | # params | Download---|---|---|---`mbart.CC25` | mBART model with 12 encoder and decoder layers trained on 25 languages' monolingual corpus | 610M | [mbart.CC25.tar.gz](https://dl.fbaipublicfiles.com/fairseq/models/mbart/mbart.cc25.v2.tar.gz)`mbart.ft.ro_en` | finetune mBART cc25 model on ro-en language pairs | 610M | [mbart.cc25.ft.enro.tar.gz](https://dl.fbaipublicfiles.com/fairseq/models/mbart/mbart.cc25.ft.enro.tar.gz)
## Results
**[WMT16 EN-RO](https://www.statmt.org/wmt16/translation-task.html)**
_(test set, no additional data used)_
Model | en-ro | ro-en---|---|---`Random` | 34.3 | 34.0`mbart.cc25` | 37.7 | 37.8`mbart.enro.bilingual` | 38.5 | 38.5
## BPE data# download modelwget https://dl.fbaipublicfiles.com/fairseq/models/mbart/mbart.cc25.v2.tar.gztar -xzvf mbart.CC25.tar.gz# bpe datainstall SPM [here](https://github.com/google/sentencepiece)```bashSPM=/path/to/sentencepiece/build/src/spm_encodeMODEL=sentence.bpe.model${SPM} --model=${MODEL} < ${DATA}/${TRAIN}.${SRC} > ${DATA}/${TRAIN}.spm.${SRC} &${SPM} --model=${MODEL} < ${DATA}/${TRAIN}.${TGT} > ${DATA}/${TRAIN}.spm.${TGT} &${SPM} --model=${MODEL} < ${DATA}/${VALID}.${SRC} > ${DATA}/${VALID}.spm.${SRC} &${SPM} --model=${MODEL} < ${DATA}/${VALID}.${TGT} > ${DATA}/${VALID}.spm.${TGT} &${SPM} --model=${MODEL} < ${DATA}/${TEST}.${SRC} > ${DATA}/${TEST}.spm.${SRC} &${SPM} --model=${MODEL} < ${DATA}/${TEST}.${TGT} > ${DATA}/${TEST}.spm.${TGT} &```
## Preprocess data
```bashDICT=dict.txtfairseq-preprocess \ --source-lang ${SRC} \ --target-lang ${TGT} \ --trainpref ${DATA}/${TRAIN}.spm \ --validpref ${DATA}/${VALID}.spm \ --testpref ${DATA}/${TEST}.spm \ --destdir ${DEST}/${NAME} \ --thresholdtgt 0 \ --thresholdsrc 0 \ --srcdict ${DICT} \ --tgtdict ${DICT} \ --workers 70```
## Finetune on EN-ROFinetune on mbart CC25
```bashPRETRAIN=mbart.cc25 # fix if you moved the downloaded checkpointlangs=ar_AR,cs_CZ,de_DE,en_XX,es_XX,et_EE,fi_FI,fr_XX,gu_IN,hi_IN,it_IT,ja_XX,kk_KZ,ko_KR,lt_LT,lv_LV,my_MM,ne_NP,nl_XX,ro_RO,ru_RU,si_LK,tr_TR,vi_VN,zh_CN
fairseq-train path_2_data \ --encoder-normalize-before --decoder-normalize-before \ --arch mbart_large --layernorm-embedding \ --task translation_from_pretrained_bart \ --source-lang en_XX --target-lang ro_RO \ --criterion label_smoothed_cross_entropy --label-smoothing 0.2 \ --optimizer adam --adam-eps 1e-06 --adam-betas '(0.9, 0.98)' \ --lr-scheduler polynomial_decay --lr 3e-05 --warmup-updates 2500 --total-num-update 40000 \ --dropout 0.3 --attention-dropout 0.1 --weight-decay 0.0 \ --max-tokens 1024 --update-freq 2 \ --save-interval 1 --save-interval-updates 5000 --keep-interval-updates 10 --no-epoch-checkpoints \ --seed 222 --log-format simple --log-interval 2 \ --restore-file $PRETRAIN \ --reset-optimizer --reset-meters --reset-dataloader --reset-lr-scheduler \ --langs $langs \ --ddp-backend legacy_ddp```## Generate on EN-ROGet sacrebleu on finetuned en-ro model
get tokenizer [here](https://github.com/rsennrich/wmt16-scripts)```bash wget https://dl.fbaipublicfiles.com/fairseq/models/mbart/mbart.cc25.ft.enro.tar.gz tar -xzvf mbart.cc25.ft.enro.tar.gz```
```bashmodel_dir=MBART_finetuned_enro # fix if you moved the checkpoint
fairseq-generate path_2_data \ --path $model_dir/model.pt \ --task translation_from_pretrained_bart \ --gen-subset test \ -t ro_RO -s en_XX \ --bpe 'sentencepiece' --sentencepiece-model $model_dir/sentence.bpe.model \ --sacrebleu --remove-bpe 'sentencepiece' \ --batch-size 32 --langs $langs > en_ro
cat en_ro | grep -P "^H" |sort -V |cut -f 3- | sed 's/\[ro_RO\]//g' |$TOKENIZER ro > en_ro.hypcat en_ro | grep -P "^T" |sort -V |cut -f 2- | sed 's/\[ro_RO\]//g' |$TOKENIZER ro > en_ro.refsacrebleu -tok 'none' -s 'none' en_ro.ref < en_ro.hyp```
## Citation
```bibtex@article{liu2020multilingual, title={Multilingual Denoising Pre-training for Neural Machine Translation}, author={Yinhan Liu and Jiatao Gu and Naman Goyal and Xian Li and Sergey Edunov and Marjan Ghazvininejad and Mike Lewis and Luke Zettlemoyer}, year={2020}, eprint={2001.08210}, archivePrefix={arXiv}, primaryClass={cs.CL}}```12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970### NormFormerThis is the code for the ["NormFormer: Improved Transformer Pretraining with Extra Normalization"](https://arxiv.org/abs/2110.09456)- 2021-10-19: Commands for CLM Experiments- Coming soon: Commands for MLM experiments
If you have any issues or questions please post a github issue and tag `@sshleifer`.
### Data- To preprocess language modeling data, see [here](https://github.com/pytorch/fairseq/blob/d0fbcb0baef6f6ff3425ded62d8daea0e8b12114/examples/language_model/README.md#1-preprocess-the-data).- The replication commands below expect `$DATA` to be the path to the binarized data directory.- Note that NormFormer results in Table 2 use a much larger private dataset, and to get good results you should adapt the pre-processing instructions to your dataset and compare to a baseline on the same data, rather than Table 2.- The code uses `FSDP`, which requires `pip install fairscale>=0.4.0`.
### Modify existing CommandTo modify an existing `fairseq-train` command to use NormFormer, simply add the following flags:```bashfairseq-train ... \ --scale-attn --scale-fc --scale-heads```- you probably also want to increase your learning rate- if your model is small, you may want to add `--scale-resids`
### Exact Training Commands
- Note that NormFormer results in Table 2 use a much larger private dataset, and to get good results you should adapt the pre-processing instructions to your dataset.The full commands are functions defined here, so to run them you must `source examples/normformer/train_lm.sh`.- We default `--distributed-world-size 8`. You should adjust `--update-freq` and `--batch-size` and such that the effective batch size is (1024x1024x0.5) tokens for 125M and 355M, and (1024x1024) for 1.3B parameter and above. For small models, `--update-freq`=256/`global_bs`. For large models, `--update-freq`=512/`global_bs`, where `global_bs` = `--batch-size` * `--distributed-world-size`- The small models will all train on as few as 8 GPUs.
```bashtrain_125M --lr 6e-4 # GPT-3 Replicatedtrain_125M --lr 1e-3 # stronger high-lr baselinetrain_125M --lr 3e-3 --scale-attn --scale-fc --scale-heads # No scale-residstrain_125M --lr 3e-3 --scale-attn --scale-fc --scale-heads --scale-resids # Best command```
```bashtrain_355M --lr 6e-4 # GPT-3 Replicatedtrain_355M --lr 1e-3 # stronger high-lr baselinetrain_355M --lr 1e-3 --scale-attn --scale-fc --scale-heads # No scale-residstrain_355M --lr 1e-3 --scale-attn --scale-fc --scale-heads --scale-resids # Slightly better```
```bashtrain_1.3B --lr 2e-4 # GPT-3 Replicatedtrain_1.3B --lr 6e-4 # stronger high-lr baselinetrain_1.3B --lr 6e-4 --scale-attn --scale-fc --scale-heads # NormFormer```
```bashtrain_2.7B --lr 1.6e-4 # GPT-3 Replicatedtrain_2.7B --lr 1.6e-4 --activation-fn relu_squared # stronger Relu^2 baselinetrain_2.7B --lr 6e-4 --activation-fn relu_squared --scale-attn --scale-fc --scale-heads # NormFormer 2.7B```
### Citation```bibtex@misc{shleifer2021normformer, title={NormFormer: Improved Transformer Pretraining with Extra Normalization}, author={Sam Shleifer and Jason Weston and Myle Ott}, year={2021}, eprint={2110.09456}, archivePrefix={arXiv}, primaryClass={cs.CL}}```123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778#!/usr/bin/env bashtrain_common () { fairseq-train "$DATA" \ --combine-val \ --train-subset train \ --num-workers 2 \ --validate-interval-updates 1000 \ --save-interval-updates 1000 \ --no-epoch-checkpoints \ --ddp-backend fully_sharded \ --memory-efficient-fp16 \ --fp16-init-scale 4 \ --checkpoint-activations \ --arch transformer_lm_gpt \ --activation-fn gelu \ --share-decoder-input-output-embed \ --task language_modeling \ --sample-break-mode none \ --tokens-per-sample 2048 \ --optimizer adam --adam-betas "(0.9, 0.98)" \ --adam-eps 1e-08 \ --clip-norm 0.0 \ --lr-scheduler polynomial_decay \ --warmup-updates 750 \ --dropout 0.1 \ --attention-dropout 0.1 \ --weight-decay 0.01 \ --batch-size 16 \ --update-freq 2 \ --required-batch-size-multiple 1 \ --total-num-update 572204 \ --max-update 572204 \ --seed 1 \ --log-format json --log-interval 1 \ --distributed-world-size 8 --distributed-port 13177 \ "$@"}
train_125M () { train_common --decoder-layers 12 \ --decoder-embed-dim 768 \ --decoder-ffn-embed-dim 3072 \ --decoder-attention-heads 12 "$@"}
train_355M () { train_common --decoder-layers 24 \ --decoder-embed-dim 1024\ --decoder-ffn-embed-dim 4096 \ --decoder-attention-heads 16 \ --dropout 0.0 \ --attention-dropout 0.0 \ "$@"}
train_1.3B () { train_common --decoder-layers 24 \ --decoder-embed-dim 2048 \ --decoder-ffn-embed-dim 8192 \ --decoder-attention-heads 32 \ --batch-size 4 \ --update-freq 16 \ --total-num-update 286102 \ --max-update 286102 \ "$@"}
train_2.7B () { train_common --decoder-layers 32 \ --decoder-embed-dim 2560 \ --decoder-ffn-embed-dim 10240 \ --decoder-attention-heads 32 \ --batch-size 4 \ --update-freq 16 \ --total-num-update 286102 \ --max-update 286102 \ "$@"}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135# Fine-tuning details
For each task (GLUE and PAWS), we perform hyperparam search for each model, and report the mean and standard deviation across 5 seeds of the best model. First, get the datasets following the instructions in [RoBERTa fine-tuning README](../roberta/README.glue.md). Alternatively, you can use [huggingface datasets](https://huggingface.co/docs/datasets/) to get the task data:
```pythonfrom datasets import load_datasetimport pandas as pdfrom pathlib import Path
key2file = {"paws": { "loc": "paws_data", "columns": ["id", "sentence1", "sentence2", "label"], "train": "train.tsv", "validation": "dev.tsv", "test": "test.tsv" }}
task_data = load_dataset("paws", "labeled_final")task_config = key2file["paws"]save_path = Path(task_config["loc"])save_path.mkdir(exist_ok=True, parents=True)for key, fl in task_config.items(): if key in ["loc", "columns"]: continue print(f"Reading {key}") columns = task_config["columns"] df = pd.DataFrame(task_data[key]) print(df.columns) df = df[columns] print(f"Got {len(df)} records") save_loc = save_path / fl print(f"Saving to : {save_loc}") df.to_csv(save_loc, sep="\t", header=None, index=None)
```
- Preprocess using RoBERTa GLUE preprocessing script, while keeping in mind the column numbers for `sentence1`, `sentence2` and `label` (which is 0,1,2 if you save the data according to the above example.)- Then, fine-tuning is performed similarly to RoBERTa (for example, in case of RTE):
```bashTOTAL_NUM_UPDATES=30875 # 10 epochs through RTE for bsz 16WARMUP_UPDATES=1852 # 6 percent of the number of updatesLR=2e-05 # Peak LR for polynomial LR scheduler.NUM_CLASSES=2MAX_SENTENCES=16 # Batch size.SHUFFLED_ROBERTA_PATH=/path/to/shuffled_roberta/model.pt
CUDA_VISIBLE_DEVICES=0 fairseq-train RTE-bin/ \ --restore-file $SHUFFLED_ROBERTA_PATH \ --max-positions 512 \ --batch-size $MAX_SENTENCES \ --max-tokens 4400 \ --task sentence_prediction \ --reset-optimizer --reset-dataloader --reset-meters \ --required-batch-size-multiple 1 \ --init-token 0 --separator-token 2 \ --arch roberta_large \ --criterion sentence_prediction \ --num-classes $NUM_CLASSES \ --dropout 0.1 --attention-dropout 0.1 \ --weight-decay 0.1 --optimizer adam --adam-betas "(0.9, 0.98)" --adam-eps 1e-06 \ --clip-norm 0.0 \ --lr-scheduler polynomial_decay --lr $LR --total-num-update $TOTAL_NUM_UPDATES --warmup-updates $WARMUP_UPDATES \ --fp16 --fp16-init-scale 4 --threshold-loss-scale 1 --fp16-scale-window 128 \ --max-epoch 10 \ --find-unused-parameters \ --best-checkpoint-metric accuracy --maximize-best-checkpoint-metric;```
- `TOTAL_NUM_UPDATES` is computed based on the `--batch_size` value and the dataset size.- `WARMUP_UPDATES` is computed as 6% of `TOTAL_NUM_UPDATES`- Best hyperparam of `--lr` and `--batch_size` is reported below:
## `--lr`
| | name | RTE | MRPC | SST-2 | CoLA | QQP | QNLI | MNLI | PAWS || --: | :----------- | ----: | ----: | ----: | ----: | ----: | ----: | ----: | ----: || 0 | original | 2e-05 | 2e-05 | 1e-05 | 2e-05 | 1e-05 | 1e-05 | 1e-05 | 2e-05 || 1 | n_1 | 2e-05 | 1e-05 | 1e-05 | 1e-05 | 3e-05 | 1e-05 | 2e-05 | 2e-05 || 2 | n_2 | 2e-05 | 2e-05 | 1e-05 | 1e-05 | 2e-05 | 1e-05 | 1e-05 | 3e-05 || 3 | n_3 | 3e-05 | 1e-05 | 2e-05 | 2e-05 | 3e-05 | 1e-05 | 1e-05 | 2e-05 || 4 | n_4 | 3e-05 | 1e-05 | 2e-05 | 2e-05 | 2e-05 | 1e-05 | 1e-05 | 2e-05 || 5 | r512 | 1e-05 | 3e-05 | 2e-05 | 2e-05 | 3e-05 | 2e-05 | 3e-05 | 2e-05 || 6 | rand_corpus | 2e-05 | 1e-05 | 3e-05 | 1e-05 | 3e-05 | 3e-05 | 3e-05 | 2e-05 || 7 | rand_uniform | 2e-05 | 1e-05 | 3e-05 | 2e-05 | 3e-05 | 3e-05 | 3e-05 | 1e-05 || 8 | rand_init | 1e-05 | 1e-05 | 3e-05 | 1e-05 | 1e-05 | 1e-05 | 2e-05 | 1e-05 || 9 | no_pos | 1e-05 | 3e-05 | 2e-05 | 1e-05 | 1e-05 | 1e-05 | 1e-05 | 1e-05 |
## `--batch_size`
| | name | RTE | MRPC | SST-2 | CoLA | QQP | QNLI | MNLI | PAWS || --: | :----------- | --: | ---: | ----: | ---: | --: | ---: | ---: | ---: || 0 | orig | 16 | 16 | 32 | 16 | 16 | 32 | 32 | 16 || 1 | n_1 | 32 | 32 | 16 | 32 | 32 | 16 | 32 | 16 || 2 | n_2 | 32 | 16 | 32 | 16 | 32 | 32 | 16 | 32 || 3 | n_3 | 32 | 32 | 16 | 32 | 32 | 16 | 32 | 32 || 4 | n_4 | 32 | 16 | 32 | 16 | 32 | 32 | 32 | 32 || 5 | r512 | 32 | 16 | 16 | 32 | 32 | 16 | 16 | 16 || 6 | rand_corpus | 16 | 16 | 16 | 16 | 32 | 16 | 16 | 32 || 7 | rand_uniform | 16 | 32 | 16 | 16 | 32 | 16 | 16 | 16 || 8 | rand_init | 16 | 16 | 32 | 16 | 16 | 16 | 32 | 16 || 9 | no_pos | 16 | 32 | 16 | 16 | 32 | 16 | 16 | 16 |
- Perform inference similar to RoBERTa as well:
```pythonfrom fairseq.models.roberta import RobertaModel
roberta = RobertaModel.from_pretrained( 'checkpoints/', checkpoint_file='checkpoint_best.pt', data_name_or_path='PAWS-bin')
label_fn = lambda label: roberta.task.label_dictionary.string( [label + roberta.task.label_dictionary.nspecial])ncorrect, nsamples = 0, 0roberta.cuda()roberta.eval()with open('paws_data/dev.tsv') as fin: fin.readline() for index, line in enumerate(fin): tokens = line.strip().split('\t') sent1, sent2, target = tokens[0], tokens[1], tokens[2] tokens = roberta.encode(sent1, sent2) prediction = roberta.predict('sentence_classification_head', tokens).argmax().item() prediction_label = label_fn(prediction) ncorrect += int(prediction_label == target) nsamples += 1print('| Accuracy: ', float(ncorrect)/float(nsamples))
```12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394# Masked Language Modeling and the Distributional Hypothesis: Order Word Matters Pre-training for Little
[https://arxiv.org/abs/2104.06644](https://arxiv.org/abs/2104.06644)
## Introduction
In this work, we pre-train [RoBERTa](../roberta) base on various word shuffled variants of BookWiki corpus (16GB). We observe that a word shuffled pre-trained model achieves surprisingly good scores on GLUE, PAWS and several parametric probing tasks. Please read our paper for more details on the experiments.
## Pre-trained models
| Model | Description | Download || ------------------------------------- | -------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- || `roberta.base.orig` | RoBERTa (base) trained on natural corpus | [roberta.base.orig.tar.gz](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.orig.tar.gz) || `roberta.base.shuffle.n1` | RoBERTa (base) trained on n=1 gram sentence word shuffled data | [roberta.base.shuffle.n1.tar.gz](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.n1.tar.gz) || `roberta.base.shuffle.n2` | RoBERTa (base) trained on n=2 gram sentence word shuffled data | [roberta.base.shuffle.n2.tar.gz](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.n2.tar.gz) || `roberta.base.shuffle.n3` | RoBERTa (base) trained on n=3 gram sentence word shuffled data | [roberta.base.shuffle.n3.tar.gz](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.n3.tar.gz) || `roberta.base.shuffle.n4` | RoBERTa (base) trained on n=4 gram sentence word shuffled data | [roberta.base.shuffle.n4.tar.gz](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.n4.tar.gz) || `roberta.base.shuffle.512` | RoBERTa (base) trained on unigram 512 word block shuffled data | [roberta.base.shuffle.512.tar.gz](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.512.tar.gz) || `roberta.base.shuffle.corpus` | RoBERTa (base) trained on unigram corpus word shuffled data | [roberta.base.shuffle.corpus.tar.gz](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.corpus.tar.gz) || `roberta.base.shuffle.corpus_uniform` | RoBERTa (base) trained on unigram corpus word shuffled data, where all words are uniformly sampled | [roberta.base.shuffle.corpus_uniform.tar.gz](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.corpus_uniform.tar.gz) || `roberta.base.nopos` | RoBERTa (base) without positional embeddings, trained on natural corpus | [roberta.base.nopos.tar.gz](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.nopos.tar.gz) |
## Results
[GLUE (Wang et al, 2019)](https://gluebenchmark.com/) & [PAWS (Zhang et al, 2019)](https://github.com/google-research-datasets/paws) _(dev set, single model, single-task fine-tuning, median of 5 seeds)_
| name | CoLA | MNLI | MRPC | PAWS | QNLI | QQP | RTE | SST-2 || :----------------------------------- | ----: | ----: | ----: | ----: | ----: | ----: | ----: | ----: || `roberta.base.orig` | 61.4 | 86.11 | 89.19 | 94.46 | 92.53 | 91.26 | 74.64 | 93.92 || `roberta.base.shuffle.n1` | 35.15 | 82.64 | 86 | 89.97 | 89.02 | 91.01 | 69.02 | 90.47 || `roberta.base.shuffle.n2` | 54.37 | 83.43 | 86.24 | 93.46 | 90.44 | 91.36 | 70.83 | 91.79 || `roberta.base.shuffle.n3` | 48.72 | 83.85 | 86.36 | 94.05 | 91.69 | 91.24 | 70.65 | 92.02 || `roberta.base.shuffle.n4` | 58.64 | 83.77 | 86.98 | 94.32 | 91.69 | 91.4 | 70.83 | 92.48 || `roberta.base.shuffle.512` | 12.76 | 77.52 | 79.61 | 84.77 | 85.19 | 90.2 | 56.52 | 86.34 || `roberta.base.shuffle.corpus` | 0 | 71.9 | 70.52 | 58.52 | 71.11 | 85.52 | 53.99 | 83.35 || `roberta.base.shuffle.corpus_random` | 9.19 | 72.33 | 70.76 | 58.42 | 77.76 | 85.93 | 53.99 | 84.04 || `roberta.base.nopos` | 0 | 63.5 | 72.73 | 57.08 | 77.72 | 87.87 | 54.35 | 83.24 |
For more results on probing tasks, please refer to [our paper](https://arxiv.org/abs/2104.06644).
## Example Usage
Follow the same usage as in [RoBERTa](https://github.com/pytorch/fairseq/tree/main/examples/roberta) to load and test your models:
```python# Download roberta.base.shuffle.n1 modelwget https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.n1.tar.gztar -xzvf roberta.base.shuffle.n1.tar.gz# Copy the dictionary filescd roberta.base.shuffle.n1.tar.gzwget -O dict.txt https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/dict.txt && wget -O encoder.json https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/encoder.json && wget -O vocab.bpe https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/vocab.bpecd ..
# Load the model in fairseqfrom fairseq.models.roberta import RobertaModelroberta = RobertaModel.from_pretrained('/path/to/roberta.base.shuffle.n1', checkpoint_file='model.pt')roberta.eval() # disable dropout (or leave in train mode to finetune)```
We have also provided a [Google Colab](https://colab.research.google.com/drive/1IJDVfNVWdvRfLjphQKBGzmob84t-OXpm) notebook to demonstrate the loading of the model. The models were trained on top of Fairseq from the following commit: [62cff008ebeeed855093837507d5e6bf52065ee6](https://github.com/pytorch/fairseq/commit/62cff008ebeeed855093837507d5e6bf52065ee6).
**Note**: The model trained without positional embeddings (`roberta.base.nopos`) is a modified `RoBERTa` model, where the positional embeddings are not used. Thus, the typical `from_pretrained` method on fairseq version of RoBERTa will not be able to load the above model weights. To do so, construct a new `RoBERTaModel` object by setting the flag `use_positional_embeddings` to `False` (or [in the latest code](https://github.com/pytorch/fairseq/blob/main/fairseq/models/roberta/model.py#L543), set `no_token_positional_embeddings` to `True`), and then load the individual weights.
## Fine-tuning Evaluation
We provide the trained fine-tuned models on MNLI here for each model above for quick evaluation (1 seed for each model). Please refer to [finetuning details](README.finetuning.md) for the parameters of these models. Follow [RoBERTa](https://github.com/pytorch/fairseq/tree/main/examples/roberta) instructions to evaluate these models.
| Model | MNLI M Dev Accuracy | Link || :----------------------------------------- | :------------------ | :--------------------------------------------------------------------------------------------------------------- || `roberta.base.orig.mnli` | 86.14 | [Download](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.orig.mnli.tar.gz) || `roberta.base.shuffle.n1.mnli` | 82.55 | [Download](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.n1.mnli.tar.gz) || `roberta.base.shuffle.n2.mnli` | 83.21 | [Download](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.n2.mnli.tar.gz) || `roberta.base.shuffle.n3.mnli` | 83.89 | [Download](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.n3.mnli.tar.gz) || `roberta.base.shuffle.n4.mnli` | 84.00 | [Download](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.n4.mnli.tar.gz) || `roberta.base.shuffle.512.mnli` | 77.22 | [Download](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.512.mnli.tar.gz) || `roberta.base.shuffle.corpus.mnli` | 71.88 | [Download](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.corpus.mnli.tar.gz) || `roberta.base.shuffle.corpus_uniform.mnli` | 72.46 | [Download](https://dl.fbaipublicfiles.com/unnatural_pretraining/roberta.base.shuffle.corpus_uniform.mnli.tar.gz) |
## Citation
```bibtex@misc{sinha2021masked, title={Masked Language Modeling and the Distributional Hypothesis: Order Word Matters Pre-training for Little}, author={Koustuv Sinha and Robin Jia and Dieuwke Hupkes and Joelle Pineau and Adina Williams and Douwe Kiela}, year={2021}, eprint={2104.06644}, archivePrefix={arXiv}, primaryClass={cs.CL}}```
## Contact
For questions and comments, please reach out to Koustuv Sinha (koustuv.sinha@mail.mcgill.ca).12345# Simultaneous TranslationExamples of simultaneous translation in fairseq- [English-to-Japanese text-to-text wait-k model](docs/enja-waitk.md)- [English-to-Germen text-to-text monotonic multihead attention model](docs/ende-mma.md)- [English-to-Germen speech-to-text simultaneous translation model](../speech_to_text/docs/simulst_mustc_example.md)123456# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
from . import models # noqa1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374# Simultaneous Machine Translation
This directory contains the code for the paper [Monotonic Multihead Attention](https://openreview.net/forum?id=Hyg96gBKPS)
## Prepare Data
[Please follow the instructions to download and preprocess the WMT'15 En-De dataset.](https://github.com/pytorch/fairseq/tree/simulastsharedtask/examples/translation#prepare-wmt14en2desh)
Another example of training an English to Japanese model can be found [here](docs/enja.md)
## Training
- MMA-IL
```shellfairseq-train \ data-bin/wmt15_en_de_32k \ --simul-type infinite_lookback \ --user-dir $FAIRSEQ/example/simultaneous_translation \ --mass-preservation \ --criterion latency_augmented_label_smoothed_cross_entropy \ --latency-weight-avg 0.1 \ --max-update 50000 \ --arch transformer_monotonic_iwslt_de_en save_dir_key=lambda \ --optimizer adam --adam-betas '(0.9, 0.98)' \ --lr-scheduler 'inverse_sqrt' \ --warmup-init-lr 1e-7 --warmup-updates 4000 \ --lr 5e-4 --stop-min-lr 1e-9 --clip-norm 0.0 --weight-decay 0.0001\ --dropout 0.3 \ --label-smoothing 0.1\ --max-tokens 3584```
- MMA-H
```shellfairseq-train \ data-bin/wmt15_en_de_32k \ --simul-type hard_aligned \ --user-dir $FAIRSEQ/example/simultaneous_translation \ --mass-preservation \ --criterion latency_augmented_label_smoothed_cross_entropy \ --latency-weight-var 0.1 \ --max-update 50000 \ --arch transformer_monotonic_iwslt_de_en save_dir_key=lambda \ --optimizer adam --adam-betas '(0.9, 0.98)' \ --lr-scheduler 'inverse_sqrt' \ --warmup-init-lr 1e-7 --warmup-updates 4000 \ --lr 5e-4 --stop-min-lr 1e-9 --clip-norm 0.0 --weight-decay 0.0001\ --dropout 0.3 \ --label-smoothing 0.1\ --max-tokens 3584```
- wait-k
```shellfairseq-train \ data-bin/wmt15_en_de_32k \ --simul-type wait-k \ --waitk-lagging 3 \ --user-dir $FAIRSEQ/example/simultaneous_translation \ --mass-preservation \ --criterion latency_augmented_label_smoothed_cross_entropy \ --max-update 50000 \ --arch transformer_monotonic_iwslt_de_en save_dir_key=lambda \ --optimizer adam --adam-betas '(0.9, 0.98)' \ --lr-scheduler 'inverse_sqrt' \ --warmup-init-lr 1e-7 --warmup-updates 4000 \ --lr 5e-4 --stop-min-lr 1e-9 --clip-norm 0.0 --weight-decay 0.0001\ --dropout 0.3 \ --label-smoothing 0.1\ --max-tokens 3584```123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106# An example of English to Japaneses Simultaneous Translation System
This is an example of training and evaluating a transformer *wait-k* English to Japanese simultaneous text-to-text translation model.
## Data PreparationThis section introduces the data preparation for training and evaluation.If you only want to evaluate the model, please jump to [Inference & Evaluation](#inference-&-evaluation)
For illustration, we only use the following subsets of the available data from [WMT20 news translation task](http://www.statmt.org/wmt20/translation-task.html), which results in 7,815,391 sentence pairs.- News Commentary v16- Wiki Titles v3- WikiMatrix V1- Japanese-English Subtitle Corpus- The Kyoto Free Translation Task Corpus
We use WMT20 development data as development set. Training `transformer_vaswani_wmt_en_de_big` model on such amount of data will result in 17.3 BLEU with greedy search and 19.7 with beam (10) search. Notice that a better performance can be achieved with the full WMT training data.
We use [sentencepiece](https://github.com/google/sentencepiece) toolkit to tokenize the data with a vocabulary size of 32000.Additionally, we filtered out the sentences longer than 200 words after tokenization.Assuming the tokenized text data is saved at `${DATA_DIR}`,we prepare the data binary with the following command.
```bashfairseq-preprocess \ --source-lang en --target-lang ja \ --trainpref ${DATA_DIR}/train \ --validpref ${DATA_DIR}/dev \ --testpref ${DATA_DIR}/test \ --destdir ${WMT20_ENJA_DATA_BIN} \ --nwordstgt 32000 --nwordssrc 32000 \ --workers 20```
## Simultaneous Translation Model TrainingTo train a wait-k `(k=10)` model.```bashfairseq-train ${WMT20_ENJA_DATA_BIN} \ --save-dir ${SAVEDIR} --simul-type waitk \ --waitk-lagging 10 \ --max-epoch 70 \ --arch transformer_monotonic_vaswani_wmt_en_de_big \ --optimizer adam \ --adam-betas '(0.9, 0.98)' \ --lr-scheduler inverse_sqrt \ --warmup-init-lr 1e-07 \ --warmup-updates 4000 \ --lr 0.0005 \ --stop-min-lr 1e-09 \ --clip-norm 10.0 \ --dropout 0.3 \ --weight-decay 0.0 \ --criterion label_smoothed_cross_entropy \ --label-smoothing 0.1 \ --max-tokens 3584```This command is for training on 8 GPUs. Equivalently, the model can be trained on one GPU with `--update-freq 8`.
## Inference & EvaluationFirst of all, install [SimulEval](https://github.com/facebookresearch/SimulEval) for evaluation.
```bashgit clone https://github.com/facebookresearch/SimulEval.gitcd SimulEvalpip install -e .```
The following command is for the evaluation.Assuming the source and reference files are `${SRC_FILE}` and `${REF_FILE}`, the sentencepiece model file for English is saved at `${SRC_SPM_PATH}`
```bashsimuleval \ --source ${SRC_FILE} \ --target ${TGT_FILE} \ --data-bin ${WMT20_ENJA_DATA_BIN} \ --sacrebleu-tokenizer ja-mecab \ --eval-latency-unit char \ --no-space \ --src-splitter-type sentencepiecemodel \ --src-splitter-path ${SRC_SPM_PATH} \ --agent ${FAIRSEQ}/examples/simultaneous_translation/agents/simul_trans_text_agent_enja.py \ --model-path ${SAVE_DIR}/${CHECKPOINT_FILENAME} \ --output ${OUTPUT} \ --scores```
The `--data-bin` should be the same in previous sections if you prepare the data from the scratch.If only for evaluation, a prepared data directory can be found [here](https://dl.fbaipublicfiles.com/simultaneous_translation/wmt20_enja_medium_databin.tgz) and a pretrained checkpoint (wait-k=10 model) can be downloaded from [here](https://dl.fbaipublicfiles.com/simultaneous_translation/wmt20_enja_medium_wait10_ckpt.pt).
The output should look like this:```bash{ "Quality": { "BLEU": 11.442253287568398 }, "Latency": { "AL": 8.6587861866951, "AP": 0.7863304776251316, "DAL": 9.477850951194764 }}```The latency is evaluated by characters (`--eval-latency-unit`) on the target side. The latency is evaluated with `sacrebleu` with `MeCab` tokenizer `--sacrebleu-tokenizer ja-mecab`. `--no-space` indicates that do not add space when merging the predicted words.
If `--output ${OUTPUT}` option is used, the detailed log and scores will be stored under the `${OUTPUT}` directory.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import os
from fairseq import checkpoint_utils, tasksimport sentencepiece as spmimport torch
try: from simuleval import READ_ACTION, WRITE_ACTION, DEFAULT_EOS from simuleval.agents import TextAgentexcept ImportError: print("Please install simuleval 'pip install simuleval'")
BOS_PREFIX = "\u2581"
class SimulTransTextAgentJA(TextAgent): """ Simultaneous Translation Text agent for Japanese """ def __init__(self, args):
# Whether use gpu self.gpu = getattr(args, "gpu", False)
# Max len self.max_len = args.max_len
# Load Model self.load_model_vocab(args)
# build word splitter self.build_word_splitter(args)
self.eos = DEFAULT_EOS
def initialize_states(self, states): states.incremental_states = dict() states.incremental_states["online"] = dict()
def to_device(self, tensor): if self.gpu: return tensor.cuda() else: return tensor.cpu()
def load_model_vocab(self, args):
filename = args.model_path if not os.path.exists(filename): raise IOError("Model file not found: {}".format(filename))
state = checkpoint_utils.load_checkpoint_to_cpu(filename)
task_args = state["cfg"]["task"] task_args.data = args.data_bin
task = tasks.setup_task(task_args)
# build model for ensemble state["cfg"]["model"].load_pretrained_encoder_from = None state["cfg"]["model"].load_pretrained_decoder_from = None
self.model = task.build_model(state["cfg"]["model"]) self.model.load_state_dict(state["model"], strict=True) self.model.eval() self.model.share_memory()
if self.gpu: self.model.cuda()
# Set dictionary self.dict = {} self.dict["tgt"] = task.target_dictionary self.dict["src"] = task.source_dictionary
@staticmethod def add_args(parser): # fmt: off parser.add_argument('--model-path', type=str, required=True, help='path to your pretrained model.') parser.add_argument("--data-bin", type=str, required=True, help="Path of data binary") parser.add_argument("--max-len", type=int, default=100, help="Max length of translation") parser.add_argument("--tgt-splitter-type", type=str, default="SentencePiece", help="Subword splitter type for target text.") parser.add_argument("--tgt-splitter-path", type=str, default=None, help="Subword splitter model path for target text.") parser.add_argument("--src-splitter-type", type=str, default="SentencePiece", help="Subword splitter type for source text.") parser.add_argument("--src-splitter-path", type=str, default=None, help="Subword splitter model path for source text.") # fmt: on return parser
def build_word_splitter(self, args): self.spm = {} for lang in ['src', 'tgt']: if getattr(args, f'{lang}_splitter_type', None): path = getattr(args, f'{lang}_splitter_path', None) if path: self.spm[lang] = spm.SentencePieceProcessor() self.spm[lang].Load(path)
def segment_to_units(self, segment, states): # Split a full word (segment) into subwords (units) return self.spm['src'].EncodeAsPieces(segment)
def update_model_encoder(self, states): if len(states.units.source) == 0: return
src_indices = [ self.dict['src'].index(x) for x in states.units.source.value ]
if states.finish_read(): # Append the eos index when the prediction is over src_indices += [self.dict["tgt"].eos_index]
src_indices = self.to_device( torch.LongTensor(src_indices).unsqueeze(0) ) src_lengths = self.to_device( torch.LongTensor([src_indices.size(1)]) )
states.encoder_states = self.model.encoder(src_indices, src_lengths)
torch.cuda.empty_cache()
def update_states_read(self, states): # Happens after a read action. self.update_model_encoder(states)
def units_to_segment(self, units, states): # Merge sub words (units) to full word (segment). # For Japanese, we can directly send # the untokenized token to server except the BOS token # with following option # --sacrebleu-tokenizer MeCab # --eval-latency-unit char # --no-space token = units.value.pop()
if ( token == self.dict["tgt"].eos_word or len(states.segments.target) > self.max_len ): return DEFAULT_EOS
if BOS_PREFIX == token: return None if token[0] == BOS_PREFIX: return token[1:] else: return token
def policy(self, states):
if not getattr(states, "encoder_states", None): # No encoder states, read a token first return READ_ACTION
# encode previous predicted target tokens tgt_indices = self.to_device( torch.LongTensor( [self.model.decoder.dictionary.eos()] + [ self.dict['tgt'].index(x) for x in states.units.target.value if x is not None ] ).unsqueeze(0) )
# Current steps states.incremental_states["steps"] = { "src": states.encoder_states["encoder_out"][0].size(0), "tgt": 1 + len(states.units.target), }
# Online only means the reading is not finished states.incremental_states["online"]["only"] = ( torch.BoolTensor([not states.finish_read()]) )
x, outputs = self.model.decoder.forward( prev_output_tokens=tgt_indices, encoder_out=states.encoder_states, incremental_state=states.incremental_states, )
states.decoder_out = x
torch.cuda.empty_cache()
if outputs.action == 0: return READ_ACTION else: return WRITE_ACTION
def predict(self, states): # Predict target token from decoder states decoder_states = states.decoder_out
lprobs = self.model.get_normalized_probs( [decoder_states[:, -1:]], log_probs=True )
index = lprobs.argmax(dim=-1)[0, 0].item()
if index != self.dict['tgt'].eos_index: token = self.dict['tgt'].string([index]) else: token = self.dict['tgt'].eos_word
return token123456789101112131415# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import importlibimport os
for file in sorted(os.listdir(os.path.dirname(__file__))): if file.endswith(".py") and not file.startswith("_"): model_name = file[: file.find(".py")] importlib.import_module( "examples.simultaneous_translation.models." + model_name )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204# Copyright (c) 2017-present, Facebook, Inc.# All rights reserved.## This source code is licensed under the license found in the LICENSE file in# the root directory of this source tree. An additional grant of patent rights# can be found in the PATENTS file in the same directory.
from fairseq import checkpoint_utilsfrom fairseq.models import ( register_model, register_model_architecture,)from fairseq.models.speech_to_text import ( ConvTransformerModel, convtransformer_espnet, ConvTransformerEncoder,)from fairseq.models.speech_to_text.modules.augmented_memory_attention import ( augmented_memory, SequenceEncoder, AugmentedMemoryConvTransformerEncoder,)
from torch import nn, Tensorfrom typing import Dict, Listfrom fairseq.models.speech_to_text.modules.emformer import NoSegAugmentedMemoryTransformerEncoderLayer
@register_model("convtransformer_simul_trans")class SimulConvTransformerModel(ConvTransformerModel): """ Implementation of the paper:
SimulMT to SimulST: Adapting Simultaneous Text Translation to End-to-End Simultaneous Speech Translation
https://www.aclweb.org/anthology/2020.aacl-main.58.pdf """
@staticmethod def add_args(parser): super(SimulConvTransformerModel, SimulConvTransformerModel).add_args(parser) parser.add_argument( "--train-monotonic-only", action="store_true", default=False, help="Only train monotonic attention", )
@classmethod def build_decoder(cls, args, task, embed_tokens): tgt_dict = task.tgt_dict
from examples.simultaneous_translation.models.transformer_monotonic_attention import ( TransformerMonotonicDecoder, )
decoder = TransformerMonotonicDecoder(args, tgt_dict, embed_tokens)
if getattr(args, "load_pretrained_decoder_from", None): decoder = checkpoint_utils.load_pretrained_component_from_model( component=decoder, checkpoint=args.load_pretrained_decoder_from ) return decoder
@register_model_architecture( "convtransformer_simul_trans", "convtransformer_simul_trans_espnet")def convtransformer_simul_trans_espnet(args): convtransformer_espnet(args)
@register_model("convtransformer_augmented_memory")@augmented_memoryclass AugmentedMemoryConvTransformerModel(SimulConvTransformerModel): @classmethod def build_encoder(cls, args): encoder = SequenceEncoder(args, AugmentedMemoryConvTransformerEncoder(args))
if getattr(args, "load_pretrained_encoder_from", None) is not None: encoder = checkpoint_utils.load_pretrained_component_from_model( component=encoder, checkpoint=args.load_pretrained_encoder_from )
return encoder
@register_model_architecture( "convtransformer_augmented_memory", "convtransformer_augmented_memory")def augmented_memory_convtransformer_espnet(args): convtransformer_espnet(args)
# ============================================================================ ## Convtransformer# with monotonic attention decoder# with emformer encoder# ============================================================================ #
class ConvTransformerEmformerEncoder(ConvTransformerEncoder): def __init__(self, args): super().__init__(args) stride = self.conv_layer_stride(args) trf_left_context = args.segment_left_context // stride trf_right_context = args.segment_right_context // stride context_config = [trf_left_context, trf_right_context] self.transformer_layers = nn.ModuleList( [ NoSegAugmentedMemoryTransformerEncoderLayer( input_dim=args.encoder_embed_dim, num_heads=args.encoder_attention_heads, ffn_dim=args.encoder_ffn_embed_dim, num_layers=args.encoder_layers, dropout_in_attn=args.dropout, dropout_on_attn=args.dropout, dropout_on_fc1=args.dropout, dropout_on_fc2=args.dropout, activation_fn=args.activation_fn, context_config=context_config, segment_size=args.segment_length, max_memory_size=args.max_memory_size, scaled_init=True, # TODO: use constant for now. tanh_on_mem=args.amtrf_tanh_on_mem, ) ] ) self.conv_transformer_encoder = ConvTransformerEncoder(args)
def forward(self, src_tokens, src_lengths): encoder_out: Dict[str, List[Tensor]] = self.conv_transformer_encoder(src_tokens, src_lengths.to(src_tokens.device)) output = encoder_out["encoder_out"][0] encoder_padding_masks = encoder_out["encoder_padding_mask"]
return { "encoder_out": [output], # This is because that in the original implementation # the output didn't consider the last segment as right context. "encoder_padding_mask": [encoder_padding_masks[0][:, : output.size(0)]] if len(encoder_padding_masks) > 0 else [], "encoder_embedding": [], "encoder_states": [], "src_tokens": [], "src_lengths": [], }
@staticmethod def conv_layer_stride(args): # TODO: make it configurable from the args return 4
@register_model("convtransformer_emformer")class ConvtransformerEmformer(SimulConvTransformerModel): @staticmethod def add_args(parser): super(ConvtransformerEmformer, ConvtransformerEmformer).add_args(parser)
parser.add_argument( "--segment-length", type=int, metavar="N", help="length of each segment (not including left context / right context)", ) parser.add_argument( "--segment-left-context", type=int, help="length of left context in a segment", ) parser.add_argument( "--segment-right-context", type=int, help="length of right context in a segment", ) parser.add_argument( "--max-memory-size", type=int, default=-1, help="Right context for the segment.", ) parser.add_argument( "--amtrf-tanh-on-mem", default=False, action="store_true", help="whether to use tanh on memory vector", )
@classmethod def build_encoder(cls, args): encoder = ConvTransformerEmformerEncoder(args) if getattr(args, "load_pretrained_encoder_from", None): encoder = checkpoint_utils.load_pretrained_component_from_model( component=encoder, checkpoint=args.load_pretrained_encoder_from ) return encoder
@register_model_architecture( "convtransformer_emformer", "convtransformer_emformer",)def convtransformer_emformer_base(args): convtransformer_espnet(args)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
from typing import Dict, List, NamedTuple, Optional
import torchimport torch.nn as nnfrom examples.simultaneous_translation.modules.monotonic_transformer_layer import ( TransformerMonotonicDecoderLayer, TransformerMonotonicEncoderLayer,)from fairseq.models import ( register_model, register_model_architecture,)from fairseq.models.transformer import ( TransformerModel, TransformerEncoder, TransformerDecoder, base_architecture, transformer_iwslt_de_en, transformer_vaswani_wmt_en_de_big, tiny_architecture)from torch import Tensor
DEFAULT_MAX_SOURCE_POSITIONS = 1024DEFAULT_MAX_TARGET_POSITIONS = 1024READ_ACTION = 0WRITE_ACTION = 1
TransformerMonotonicDecoderOut = NamedTuple( "TransformerMonotonicDecoderOut", [ ("action", int), ("p_choose", Optional[Tensor]), ("attn_list", Optional[List[Optional[Dict[str, Tensor]]]]), ("encoder_out", Optional[Dict[str, List[Tensor]]]), ("encoder_padding_mask", Optional[Tensor]), ],)
@register_model("transformer_unidirectional")class TransformerUnidirectionalModel(TransformerModel): @classmethod def build_encoder(cls, args, src_dict, embed_tokens): return TransformerMonotonicEncoder(args, src_dict, embed_tokens)
@register_model("transformer_monotonic")class TransformerModelSimulTrans(TransformerModel): @classmethod def build_encoder(cls, args, src_dict, embed_tokens): return TransformerMonotonicEncoder(args, src_dict, embed_tokens)
@classmethod def build_decoder(cls, args, tgt_dict, embed_tokens): return TransformerMonotonicDecoder(args, tgt_dict, embed_tokens)
class TransformerMonotonicEncoder(TransformerEncoder): def __init__(self, args, dictionary, embed_tokens): super().__init__(args, dictionary, embed_tokens)
self.dictionary = dictionary self.layers = nn.ModuleList([]) self.layers.extend( [ TransformerMonotonicEncoderLayer(args) for i in range(args.encoder_layers) ] )
class TransformerMonotonicDecoder(TransformerDecoder): """ Transformer decoder consisting of *args.decoder_layers* layers. Each layer is a :class:`TransformerDecoderLayer`.
Args: args (argparse.Namespace): parsed command-line arguments dictionary (~fairseq.data.Dictionary): decoding dictionary embed_tokens (torch.nn.Embedding): output embedding no_encoder_attn (bool, optional): whether to attend to encoder outputs (default: False). """
def __init__(self, args, dictionary, embed_tokens, no_encoder_attn=False): super().__init__(args, dictionary, embed_tokens, no_encoder_attn=False)
self.dictionary = dictionary self.layers = nn.ModuleList([]) self.layers.extend( [ TransformerMonotonicDecoderLayer(args) for _ in range(args.decoder_layers) ] ) self.policy_criterion = getattr(args, "policy_criterion", "any") self.num_updates = None
def set_num_updates(self, num_updates): self.num_updates = num_updates
def pre_attention( self, prev_output_tokens, encoder_out_dict: Dict[str, List[Tensor]], incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] = None, ): positions = ( self.embed_positions( prev_output_tokens, incremental_state=incremental_state, ) if self.embed_positions is not None else None )
if incremental_state is not None: prev_output_tokens = prev_output_tokens[:, -1:] if positions is not None: positions = positions[:, -1:] # embed tokens and positions x = self.embed_scale * self.embed_tokens(prev_output_tokens)
if self.project_in_dim is not None: x = self.project_in_dim(x)
if positions is not None: x += positions
x = self.dropout_module(x)
# B x T x C -> T x B x C x = x.transpose(0, 1)
encoder_out = encoder_out_dict["encoder_out"][0]
if "encoder_padding_mask" in encoder_out_dict: encoder_padding_mask = ( encoder_out_dict["encoder_padding_mask"][0] if encoder_out_dict["encoder_padding_mask"] and len(encoder_out_dict["encoder_padding_mask"]) > 0 else None ) else: encoder_padding_mask = None
return x, encoder_out, encoder_padding_mask
def post_attention(self, x): if self.layer_norm is not None: x = self.layer_norm(x)
# T x B x C -> B x T x C x = x.transpose(0, 1)
if self.project_out_dim is not None: x = self.project_out_dim(x)
return x
def clean_cache( self, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]], end_id: Optional[int] = None, ): """ Clean cache in the monotonic layers. The cache is generated because of a forward pass of decoder has run but no prediction, so that the self attention key value in decoder is written in the incremental state. end_id is the last idx of the layers """ if end_id is None: end_id = len(self.layers)
for index, layer in enumerate(self.layers): if index < end_id: layer.prune_incremental_state(incremental_state)
def extract_features( self, prev_output_tokens, encoder_out: Optional[Dict[str, List[Tensor]]], incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] = None, full_context_alignment: bool = False, # unused alignment_layer: Optional[int] = None, # unused alignment_heads: Optional[int] = None, # unsed ): """ Similar to *forward* but only return features.
Returns: tuple: - the decoder's features of shape `(batch, tgt_len, embed_dim)` - a dictionary with any model-specific outputs """ # incremental_state = None assert encoder_out is not None (x, encoder_outs, encoder_padding_mask) = self.pre_attention( prev_output_tokens, encoder_out, incremental_state ) attn = None inner_states = [x] attn_list: List[Optional[Dict[str, Tensor]]] = []
p_choose = torch.tensor([1.0])
for i, layer in enumerate(self.layers):
x, attn, _ = layer( x=x, encoder_out=encoder_outs, encoder_padding_mask=encoder_padding_mask, incremental_state=incremental_state, self_attn_mask=self.buffered_future_mask(x) if incremental_state is None else None, )
inner_states.append(x) attn_list.append(attn)
if incremental_state is not None: if_online = incremental_state["online"]["only"] assert if_online is not None if if_online.to(torch.bool): # Online indicates that the encoder states are still changing assert attn is not None if self.policy_criterion == "any": # Any head decide to read than read head_read = layer.encoder_attn._get_monotonic_buffer(incremental_state)["head_read"] assert head_read is not None if head_read.any(): # We need to prune the last self_attn saved_state # if model decide not to read # otherwise there will be duplicated saved_state self.clean_cache(incremental_state, i + 1)
return x, TransformerMonotonicDecoderOut( action=0, p_choose=p_choose, attn_list=None, encoder_out=None, encoder_padding_mask=None, )
x = self.post_attention(x)
return x, TransformerMonotonicDecoderOut( action=1, p_choose=p_choose, attn_list=attn_list, encoder_out=encoder_out, encoder_padding_mask=encoder_padding_mask, )
@register_model_architecture("transformer_monotonic", "transformer_monotonic")def base_monotonic_architecture(args): base_architecture(args) args.encoder_unidirectional = getattr(args, "encoder_unidirectional", False)
@register_model_architecture( "transformer_monotonic", "transformer_monotonic_iwslt_de_en")def transformer_monotonic_iwslt_de_en(args): transformer_iwslt_de_en(args) base_monotonic_architecture(args)
# parameters used in the "Attention Is All You Need" paper (Vaswani et al., 2017)@register_model_architecture( "transformer_monotonic", "transformer_monotonic_vaswani_wmt_en_de_big")def transformer_monotonic_vaswani_wmt_en_de_big(args): transformer_vaswani_wmt_en_de_big(args)
@register_model_architecture( "transformer_monotonic", "transformer_monotonic_vaswani_wmt_en_fr_big")def transformer_monotonic_vaswani_wmt_en_fr_big(args): transformer_monotonic_vaswani_wmt_en_fr_big(args)
@register_model_architecture( "transformer_unidirectional", "transformer_unidirectional_iwslt_de_en")def transformer_unidirectional_iwslt_de_en(args): transformer_iwslt_de_en(args)
@register_model_architecture("transformer_monotonic", "transformer_monotonic_tiny")def monotonic_tiny_architecture(args): tiny_architecture(args) base_monotonic_architecture(args)1234567891011121314151617181920212223# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import osimport importlibfrom fairseq import registry
( build_monotonic_attention, register_monotonic_attention, MONOTONIC_ATTENTION_REGISTRY, _,) = registry.setup_registry("--simul-type")
for file in sorted(os.listdir(os.path.dirname(__file__))): if file.endswith(".py") and not file.startswith("_"): model_name = file[: file.find(".py")] importlib.import_module( "examples.simultaneous_translation.modules." + model_name )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190from functools import partial
import torchfrom torch import Tensorimport mathimport torch.nn.functional as F
from . import register_monotonic_attentionfrom .monotonic_multihead_attention import ( MonotonicAttention, MonotonicInfiniteLookbackAttention, WaitKAttention)from typing import Dict, Optional
def fixed_pooling_monotonic_attention(monotonic_attention): def create_model(monotonic_attention, klass): class FixedStrideMonotonicAttention(monotonic_attention): def __init__(self, args): self.waitk_lagging = 0 self.num_heads = 0 self.noise_mean = 0.0 self.noise_var = 0.0 super().__init__(args) self.pre_decision_type = args.fixed_pre_decision_type self.pre_decision_ratio = args.fixed_pre_decision_ratio self.pre_decision_pad_threshold = args.fixed_pre_decision_pad_threshold assert self.pre_decision_ratio > 1
if args.fixed_pre_decision_type == "average": self.pooling_layer = torch.nn.AvgPool1d( kernel_size=self.pre_decision_ratio, stride=self.pre_decision_ratio, ceil_mode=True, ) elif args.fixed_pre_decision_type == "last":
def last(key): if key.size(2) < self.pre_decision_ratio: return key else: k = key[ :, :, self.pre_decision_ratio - 1:: self.pre_decision_ratio, ].contiguous() if key.size(-1) % self.pre_decision_ratio != 0: k = torch.cat([k, key[:, :, -1:]], dim=-1).contiguous() return k
self.pooling_layer = last else: raise NotImplementedError
@staticmethod def add_args(parser): super( FixedStrideMonotonicAttention, FixedStrideMonotonicAttention ).add_args(parser) parser.add_argument( "--fixed-pre-decision-ratio", type=int, required=True, help=( "Ratio for the fixed pre-decision," "indicating how many encoder steps will start" "simultaneous decision making process." ), ) parser.add_argument( "--fixed-pre-decision-type", default="average", choices=["average", "last"], help="Pooling type", ) parser.add_argument( "--fixed-pre-decision-pad-threshold", type=float, default=0.3, help="If a part of the sequence has pad" ",the threshold the pooled part is a pad.", )
def insert_zeros(self, x): bsz_num_heads, tgt_len, src_len = x.size() stride = self.pre_decision_ratio weight = F.pad(torch.ones(1, 1, 1).to(x), (stride - 1, 0)) x_upsample = F.conv_transpose1d( x.view(-1, src_len).unsqueeze(1), weight, stride=stride, padding=0, ) return x_upsample.squeeze(1).view(bsz_num_heads, tgt_len, -1)
def p_choose( self, query: Optional[Tensor], key: Optional[Tensor], key_padding_mask: Optional[Tensor] = None, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] = None, ): assert key is not None assert query is not None src_len = key.size(0) tgt_len = query.size(0) batch_size = query.size(1)
key_pool = self.pooling_layer(key.transpose(0, 2)).transpose(0, 2)
if key_padding_mask is not None: key_padding_mask_pool = ( self.pooling_layer(key_padding_mask.unsqueeze(0).float()) .squeeze(0) .gt(self.pre_decision_pad_threshold) ) # Make sure at least one element is not pad key_padding_mask_pool[:, 0] = 0 else: key_padding_mask_pool = None
if incremental_state is not None: # The floor instead of ceil is used for inference # But make sure the length key_pool at least 1 if ( max(1, math.floor(key.size(0) / self.pre_decision_ratio)) ) < key_pool.size(0): key_pool = key_pool[:-1] if key_padding_mask_pool is not None: key_padding_mask_pool = key_padding_mask_pool[:-1]
p_choose_pooled = self.p_choose_from_qk( query, key_pool, key_padding_mask_pool, incremental_state=incremental_state, )
# Upsample, interpolate zeros p_choose = self.insert_zeros(p_choose_pooled)
if p_choose.size(-1) < src_len: # Append zeros if the upsampled p_choose is shorter than src_len p_choose = torch.cat( [ p_choose, torch.zeros( p_choose.size(0), tgt_len, src_len - p_choose.size(-1) ).to(p_choose) ], dim=2 ) else: # can be larger than src_len because we used ceil before p_choose = p_choose[:, :, :src_len] p_choose[:, :, -1] = p_choose_pooled[:, :, -1]
assert list(p_choose.size()) == [ batch_size * self.num_heads, tgt_len, src_len, ]
return p_choose
FixedStrideMonotonicAttention.__name__ = klass.__name__ return FixedStrideMonotonicAttention
return partial(create_model, monotonic_attention)
@register_monotonic_attention("waitk_fixed_pre_decision")@fixed_pooling_monotonic_attention(WaitKAttention)class WaitKAttentionFixedStride: pass
@register_monotonic_attention("hard_aligned_fixed_pre_decision")@fixed_pooling_monotonic_attention(MonotonicAttention)class MonotonicAttentionFixedStride: pass
@register_monotonic_attention("infinite_lookback_fixed_pre_decision")@fixed_pooling_monotonic_attention(MonotonicInfiniteLookbackAttention)class MonotonicInfiniteLookbackAttentionFixedStride: pass123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import math
import torchfrom torch import Tensorimport torch.nn as nn
from examples.simultaneous_translation.utils.p_choose_strategy import ( learnable_p_choose, waitk_p_choose)
from examples.simultaneous_translation.utils.monotonic_attention import ( expected_alignment_from_p_choose, expected_soft_attention, mass_preservation,)from fairseq.modules import MultiheadAttention
from . import register_monotonic_attentionfrom typing import Dict, Optional
@register_monotonic_attention("hard_aligned")class MonotonicAttention(MultiheadAttention): """ Abstract class of monotonic attentions """ k_in_proj: Dict[str, nn.Linear] q_in_proj: Dict[str, nn.Linear]
def __init__(self, args): super().__init__( embed_dim=args.decoder_embed_dim, num_heads=args.decoder_attention_heads, kdim=getattr(args, "encoder_embed_dim", None), vdim=getattr(args, "encoder_embed_dim", None), dropout=args.attention_dropout, encoder_decoder_attention=True, )
self.soft_attention = False
self.eps = getattr(args, "attention_eps", True) self.mass_preservation = getattr(args, "mass_preservation", True)
self.noise_type = args.noise_type self.noise_mean = args.noise_mean self.noise_var = args.noise_var
self.energy_bias_init = args.energy_bias_init self.energy_bias = ( nn.Parameter(self.energy_bias_init * torch.ones([1])) if args.energy_bias is True else 0 )
self.k_in_proj = {"monotonic": self.k_proj} self.q_in_proj = {"monotonic": self.q_proj} self.chunk_size = None
@staticmethod def add_args(parser): # fmt: off parser.add_argument('--no-mass-preservation', action="store_false", dest="mass_preservation", help='Do not stay on the last token when decoding') parser.add_argument('--mass-preservation', action="store_true", dest="mass_preservation", help='Stay on the last token when decoding') parser.set_defaults(mass_preservation=True) parser.add_argument('--noise-var', type=float, default=1.0, help='Variance of discretness noise') parser.add_argument('--noise-mean', type=float, default=0.0, help='Mean of discretness noise') parser.add_argument('--noise-type', type=str, default="flat", help='Type of discretness noise') parser.add_argument('--energy-bias', action="store_true", default=False, help='Bias for energy') parser.add_argument('--energy-bias-init', type=float, default=-2.0, help='Initial value of the bias for energy') parser.add_argument('--attention-eps', type=float, default=1e-6, help='Epsilon when calculating expected attention')
def energy_from_qk( self, query: Tensor, key: Tensor, energy_type: str, key_padding_mask: Optional[Tensor] = None, bias: int = 0 ): """ Compute energy from query and key q_func_value is a tuple looks like (q_proj_func, q_tensor) q_tensor size: bsz, tgt_len, emb_dim k_tensor size: bsz, src_len, emb_dim key_padding_mask size: bsz, src_len attn_mask: bsz, src_len """
length, bsz, _ = query.size() q = self.q_in_proj[energy_type].forward(query) q = ( q.contiguous() .view(length, bsz * self.num_heads, self.head_dim) .transpose(0, 1) ) q = q * self.scaling length, bsz, _ = key.size() k = self.k_in_proj[energy_type].forward(key) k = ( k.contiguous() .view(length, bsz * self.num_heads, self.head_dim) .transpose(0, 1) )
energy = torch.bmm(q, k.transpose(1, 2)) + bias
if key_padding_mask is not None: energy = energy.masked_fill( key_padding_mask.unsqueeze(1).to(torch.bool), - float("inf") )
return energy
def p_choose_from_qk(self, query, key, key_padding_mask, incremental_states=None): monotonic_energy = self.energy_from_qk( query, key, "monotonic", key_padding_mask=key_padding_mask, bias=self.energy_bias, )
p_choose = learnable_p_choose( monotonic_energy, self.noise_mean, self.noise_var, self.training ) return p_choose
def p_choose(self, query, key, key_padding_mask, incremental_states=None): return self.p_choose_from_qk(self, query, key, key_padding_mask)
def monotonic_attention_process_infer( self, query: Optional[Tensor], key: Optional[Tensor], incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]], ): """ Monotonic attention at inference time Notice that this function is designed for simuleval not sequence_generator """ assert query is not None assert key is not None
if query.size(1) != 1: raise RuntimeError( "Simultaneous translation models don't support batch decoding." ) # 1. compute stepwise probability p_choose = self.p_choose( query, key, None, incremental_state ).squeeze(1)
# 2. Compute the alpha src_len = key.size(0) # Maximum steps allows in this iteration max_steps = src_len - 1 if self.mass_preservation else src_len monotonic_cache = self._get_monotonic_buffer(incremental_state) # Step for each head monotonic_step = monotonic_cache.get( 'head_step', p_choose.new_zeros(1, self.num_heads).long() ) assert monotonic_step is not None finish_read = monotonic_step.eq(max_steps) p_choose_i = torch.tensor(1)
while finish_read.sum().item() < self.num_heads: # p_choose: self.num_heads, src_len # only choose the p at monotonic steps # p_choose_i: 1, self.num_heads p_choose_i = ( p_choose.gather( 1, monotonic_step .clamp(0, src_len - 1), ) )
read_one_step = ( (p_choose_i < 0.5) .type_as(monotonic_step) .masked_fill(finish_read, 0) ) # 1 x bsz # sample actions on unfinished seq # 0 means stay, finish reading # 1 means leave, continue reading
monotonic_step += read_one_step
finish_read = monotonic_step.eq(max_steps) | (read_one_step == 0)
# p_choose at last steps p_choose_i = ( p_choose.gather( 1, monotonic_step .clamp(0, src_len - 1), ) )
monotonic_cache["head_step"] = monotonic_step # Whether a head is looking for new input monotonic_cache["head_read"] = ( monotonic_step.eq(max_steps) & (p_choose_i < 0.5) ) self._set_monotonic_buffer(incremental_state, monotonic_cache)
# 2. Update alpha alpha = ( p_choose .new_zeros([self.num_heads, src_len]) .scatter( 1, (monotonic_step) .view(self.num_heads, 1).clamp(0, src_len - 1), 1 ) )
if not self.mass_preservation: alpha = alpha.masked_fill( (monotonic_step == max_steps) .view(self.num_heads, 1), 0 )
# 4. Compute Beta if self.soft_attention: monotonic_step = monotonic_step.t() beta_mask = torch.arange(src_len).expand_as(alpha).gt(monotonic_step).unsqueeze(1) # If it's soft attention just do softmax on current context soft_energy = self.energy_from_qk( query, key, "soft" ) beta = torch.nn.functional.softmax( soft_energy.masked_fill(beta_mask, -float("inf")), dim=-1 ) # It could happen that a head doesn't move at all beta = beta.masked_fill(monotonic_step.eq(0).unsqueeze(1), 0) else: # If it's hard attention just select the last state beta = alpha
return p_choose, alpha, beta
def monotonic_attention_process_train( self, query: Optional[Tensor], key: Optional[Tensor], key_padding_mask: Optional[Tensor] = None, ): """ Calculating monotonic attention process for training Including: stepwise probability: p_choose expected hard alignment: alpha expected soft attention: beta """ assert query is not None assert key is not None
# 1. compute stepwise probability p_choose = self.p_choose_from_qk(query, key, key_padding_mask)
# 2. compute expected_alignment alpha = expected_alignment_from_p_choose( p_choose, key_padding_mask, eps=self.eps, )
if self.mass_preservation: alpha = mass_preservation( alpha, key_padding_mask )
# 3. compute expected soft attention (soft aligned model only) if self.soft_attention: soft_energy = self.energy_from_qk( query, key, "soft", key_padding_mask=None, )
beta = expected_soft_attention( alpha, soft_energy, padding_mask=key_padding_mask, chunk_size=self.chunk_size, eps=self.eps, ) else: beta = alpha soft_energy = alpha
return p_choose, alpha, beta, soft_energy
def forward( self, query: Optional[Tensor], key: Optional[Tensor], value: Optional[Tensor], key_padding_mask: Optional[Tensor] = None, attn_mask: Optional[Tensor] = None, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] = None, need_weights: bool = True, static_kv: bool = False, need_head_weights: bool = False, ): """ query: tgt_len, bsz, embed_dim key: src_len, bsz, embed_dim value: src_len, bsz, embed_dim """
assert attn_mask is None assert query is not None assert key is not None assert value is not None
tgt_len, bsz, embed_dim = query.size() src_len = value.size(0)
if key_padding_mask is not None: assert not key_padding_mask[:, 0].any(), ( "Only right padding is supported." ) key_padding_mask = ( key_padding_mask .unsqueeze(1) .expand([bsz, self.num_heads, src_len]) .contiguous() .view(-1, src_len) )
if incremental_state is not None: # Inference ( p_choose, alpha, beta ) = self.monotonic_attention_process_infer( query, key, incremental_state ) soft_energy = beta else: # Train ( p_choose, alpha, beta, soft_energy ) = self.monotonic_attention_process_train( query, key, key_padding_mask )
v = self.v_proj(value) length, bsz, _ = v.size() v = ( v.contiguous() .view(length, bsz * self.num_heads, self.head_dim) .transpose(0, 1) )
attn = torch.bmm(beta.type_as(v), v)
attn = attn.transpose(0, 1).contiguous().view(tgt_len, bsz, embed_dim)
attn = self.out_proj(attn)
p_choose = p_choose.view(bsz, self.num_heads, tgt_len, src_len) alpha = alpha.view(bsz, self.num_heads, tgt_len, src_len) beta = beta.view(bsz, self.num_heads, tgt_len, src_len)
return attn, { "p_choose": p_choose, "alpha": alpha, "beta": beta, "soft_energy": soft_energy, }
def _get_monotonic_buffer(self, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]]): maybe_incremental_state = self.get_incremental_state( incremental_state, 'monotonic', ) if maybe_incremental_state is None: typed_empty_dict: Dict[str, Optional[Tensor]] = {} return typed_empty_dict else: return maybe_incremental_state
def _set_monotonic_buffer(self, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]], buffer: Dict[str, Optional[Tensor]]): self.set_incremental_state( incremental_state, 'monotonic', buffer, )
@register_monotonic_attention("infinite_lookback")class MonotonicInfiniteLookbackAttention( MonotonicAttention): def __init__(self, args): super().__init__(args) self.soft_attention = True self.init_soft_attention()
def init_soft_attention(self): self.k_proj_soft = nn.Linear(self.kdim, self.embed_dim, bias=True) self.q_proj_soft = nn.Linear(self.embed_dim, self.embed_dim, bias=True) self.k_in_proj["soft"] = self.k_proj_soft self.q_in_proj["soft"] = self.q_proj_soft
if self.qkv_same_dim: # Empirically observed the convergence to be much better with # the scaled initialization nn.init.xavier_uniform_( self.k_in_proj["soft"].weight, gain=1 / math.sqrt(2) ) nn.init.xavier_uniform_( self.q_in_proj["soft"].weight, gain=1 / math.sqrt(2) ) else: nn.init.xavier_uniform_(self.k_in_proj["soft"].weight) nn.init.xavier_uniform_(self.q_in_proj["soft"].weight)
@register_monotonic_attention("waitk")class WaitKAttention( MonotonicInfiniteLookbackAttention): """ STACL: Simultaneous Translation with Implicit Anticipation and Controllable Latency using Prefix-to-Prefix Framework https://www.aclweb.org/anthology/P19-1289/ """ def __init__(self, args): super().__init__(args) self.q_in_proj["soft"] = self.q_in_proj["monotonic"] self.k_in_proj["soft"] = self.k_in_proj["monotonic"]
self.waitk_lagging = args.waitk_lagging assert self.waitk_lagging > 0, ( f"Lagging has to been larger than 0, get {self.waitk_lagging}." )
@staticmethod def add_args(parser): super( MonotonicInfiniteLookbackAttention, MonotonicInfiniteLookbackAttention ).add_args(parser)
parser.add_argument( "--waitk-lagging", type=int, required=True, help="Wait K lagging" )
def p_choose_from_qk( self, query: Optional[Tensor], key: Optional[Tensor], key_padding_mask: Optional[Tensor] = None, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] = None, ): assert query is not None assert key is not None
p_choose = waitk_p_choose( tgt_len=query.size(0), src_len=key.size(0), bsz=query.size(1) * self.num_heads, waitk_lagging=self.waitk_lagging, key_padding_mask=key_padding_mask, incremental_state=incremental_state, )
return p_choose.to(query)
@register_monotonic_attention("chunkwise")class ChunkwiseAttention( MonotonicInfiniteLookbackAttention): def __init__(self, args): super().__init__(args) self.chunk_size = args.mocha_chunk_size assert self.chunk_size > 1
@staticmethod def add_args(parser): super( MonotonicInfiniteLookbackAttention ).add_args(parser)
parser.add_argument( "--mocha-chunk-size", type=int, required=True, help="Mocha chunk size" )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
from fairseq.modules import TransformerDecoderLayer, TransformerEncoderLayer
from . import build_monotonic_attention
from typing import Dict, Optional, List
from torch import Tensorimport torch
class TransformerMonotonicEncoderLayer(TransformerEncoderLayer): def forward(self, x, encoder_padding_mask): seq_len, _, _ = x.size() attn_mask = x.new_ones([seq_len, seq_len]).triu(1) attn_mask = attn_mask.masked_fill(attn_mask.bool(), float("-inf")) return super().forward(x, encoder_padding_mask, attn_mask)
class TransformerMonotonicDecoderLayer(TransformerDecoderLayer): def __init__(self, args): super().__init__(args)
assert args.simul_type is not None, "A --simul-type is needed." self.encoder_attn = build_monotonic_attention(args)
def prune_incremental_state( self, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] ): input_buffer = self.self_attn._get_input_buffer(incremental_state) for key in ["prev_key", "prev_value"]: input_buffer_key = input_buffer[key] assert input_buffer_key is not None if input_buffer_key.size(2) > 1: input_buffer[key] = input_buffer_key[:, :, :-1, :] else: typed_empty_dict: Dict[str, Optional[Tensor]] = {} input_buffer = typed_empty_dict break assert incremental_state is not None self.self_attn._set_input_buffer(incremental_state, input_buffer)
def forward( self, x, encoder_out: Optional[Tensor] = None, encoder_padding_mask: Optional[Tensor] = None, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] = None, prev_self_attn_state: Optional[List[Tensor]] = None, prev_attn_state: Optional[List[Tensor]] = None, self_attn_mask: Optional[Tensor] = None, self_attn_padding_mask: Optional[Tensor] = None, need_attn: bool = False, need_head_weights: bool = False, ): """ Args: x (Tensor): input to the layer of shape `(seq_len, batch, embed_dim)` encoder_padding_mask (ByteTensor, optional): binary ByteTensor of shape `(batch, src_len)` where padding elements are indicated by ``1``. need_attn (bool, optional): return attention weights need_head_weights (bool, optional): return attention weights for each head (default: return average over heads).
Returns: encoded output of shape `(seq_len, batch, embed_dim)` """ if need_head_weights: need_attn = True
residual = x if self.normalize_before: x = self.self_attn_layer_norm(x) if prev_self_attn_state is not None: prev_key, prev_value = prev_self_attn_state[:2] saved_state: Dict[str, Optional[Tensor]] = { "prev_key": prev_key, "prev_value": prev_value, } if len(prev_self_attn_state) >= 3: saved_state["prev_key_padding_mask"] = prev_self_attn_state[2] assert incremental_state is not None self.self_attn._set_input_buffer(incremental_state, saved_state) _self_attn_input_buffer = self.self_attn._get_input_buffer(incremental_state) if self.cross_self_attention and not ( incremental_state is not None and _self_attn_input_buffer is not None and "prev_key" in _self_attn_input_buffer ): if self_attn_mask is not None: assert encoder_out is not None self_attn_mask = torch.cat( (x.new_zeros(x.size(0), encoder_out.size(0)), self_attn_mask), dim=1 ) if self_attn_padding_mask is not None: if encoder_padding_mask is None: assert encoder_out is not None encoder_padding_mask = self_attn_padding_mask.new_zeros( encoder_out.size(1), encoder_out.size(0) ) self_attn_padding_mask = torch.cat( (encoder_padding_mask, self_attn_padding_mask), dim=1 ) assert encoder_out is not None y = torch.cat((encoder_out, x), dim=0) else: y = x
x, attn = self.self_attn( query=x, key=y, value=y, key_padding_mask=self_attn_padding_mask, incremental_state=incremental_state, need_weights=False, attn_mask=self_attn_mask, ) x = self.dropout_module(x) x = self.residual_connection(x, residual) if not self.normalize_before: x = self.self_attn_layer_norm(x)
assert self.encoder_attn is not None residual = x if self.normalize_before: x = self.encoder_attn_layer_norm(x) if prev_attn_state is not None: prev_key, prev_value = prev_attn_state[:2] saved_state: Dict[str, Optional[Tensor]] = { "prev_key": prev_key, "prev_value": prev_value, } if len(prev_attn_state) >= 3: saved_state["prev_key_padding_mask"] = prev_attn_state[2] assert incremental_state is not None self.encoder_attn._set_input_buffer(incremental_state, saved_state)
x, attn = self.encoder_attn( query=x, key=encoder_out, value=encoder_out, key_padding_mask=encoder_padding_mask, incremental_state=incremental_state, static_kv=True, need_weights=need_attn or (not self.training and self.need_attn), need_head_weights=need_head_weights, ) x = self.dropout_module(x) x = self.residual_connection(x, residual) if not self.normalize_before: x = self.encoder_attn_layer_norm(x)
residual = x if self.normalize_before: x = self.final_layer_norm(x)
x = self.activation_fn(self.fc1(x)) x = self.activation_dropout_module(x) x = self.fc2(x) x = self.dropout_module(x) x = self.residual_connection(x, residual) if not self.normalize_before: x = self.final_layer_norm(x) if self.onnx_trace and incremental_state is not None: saved_state = self.self_attn._get_input_buffer(incremental_state) assert saved_state is not None if self_attn_padding_mask is not None: self_attn_state = [ saved_state["prev_key"], saved_state["prev_value"], saved_state["prev_key_padding_mask"], ] else: self_attn_state = [saved_state["prev_key"], saved_state["prev_value"]] return x, attn, self_attn_state return x, attn, None12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788import unittest
import numpy as npimport torch
import hypothesis.strategies as stfrom hypothesis import assume, given, settingsfrom torch.testing._internal.common_utils import TestCasefrom examples.simultaneous_translation.utils.functions import exclusive_cumprod
TEST_CUDA = torch.cuda.is_available()
class AlignmentTrainTest(TestCase): def _test_custom_alignment_train_ref(self, p_choose, eps): cumprod_1mp = exclusive_cumprod(1 - p_choose, dim=2, eps=eps) cumprod_1mp_clamp = torch.clamp(cumprod_1mp, eps, 1.0)
bsz = p_choose.size(0) tgt_len = p_choose.size(1) src_len = p_choose.size(2)
alpha_0 = p_choose.new_zeros([bsz, 1, src_len]) alpha_0[:, :, 0] = 1.0
previous_alpha = [alpha_0]
for i in range(tgt_len): # p_choose: bsz , tgt_len, src_len # cumprod_1mp_clamp : bsz, tgt_len, src_len # previous_alpha[i]: bsz, 1, src_len # alpha_i: bsz, src_len alpha_i = ( p_choose[:, i] * cumprod_1mp[:, i] * torch.cumsum( previous_alpha[i][:, 0] / cumprod_1mp_clamp[:, i], dim=1 ) ).clamp(0, 1.0)
previous_alpha.append(alpha_i.unsqueeze(1))
# alpha: bsz * num_heads, tgt_len, src_len alpha = torch.cat(previous_alpha[1:], dim=1) return alpha
def _test_custom_alignment_train_impl(self, p_choose, alpha, eps): if p_choose.is_cuda: from alignment_train_cuda_binding import alignment_train_cuda # @manual=//deeplearning/projects/fairseq-py:alignment_train_cuda_binding alignment_train_cuda(p_choose, alpha, eps) else: from alignment_train_cpu_binding import alignment_train_cpu # @manual=//deeplearning/projects/fairseq-py:alignment_train_cpu_binding alignment_train_cpu(p_choose, alpha, eps)
@settings(deadline=None) @given( bsz=st.integers(1, 100), tgt_len=st.integers(1, 100), src_len=st.integers(1, 550), device=st.sampled_from(["cpu", "cuda"]), ) def test_alignment_train(self, bsz, tgt_len, src_len, device): eps = 1e-6
assume(device == "cpu" or TEST_CUDA) p_choose = torch.rand(bsz, tgt_len, src_len, device=device)
# run the alignment with the custom operator alpha_act = p_choose.new_zeros([bsz, tgt_len, src_len]) self._test_custom_alignment_train_impl(p_choose, alpha_act, eps)
# runu the alignment with the ref implementation alpha_ref = self._test_custom_alignment_train_ref(p_choose, eps)
# verify the results alpha_act = alpha_act.cpu().detach().numpy() alpha_ref = alpha_ref.cpu().detach().numpy() np.testing.assert_allclose( alpha_act, alpha_ref, atol=1e-3, rtol=1e-3, )
if __name__ == "__main__": unittest.main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407import argparseimport unittestfrom typing import Any, Dict
import torchfrom examples.simultaneous_translation.models import ( transformer_monotonic_attention)
from tests.test_roberta import FakeTask
DEFAULT_CONFIG = { "attention_eps": 1e-6, "mass_preservation": True, "noise_type": "flat", "noise_mean": 0.0, "noise_var": 1.0, "energy_bias_init": -2, "energy_bias": True}
PAD_INDEX = 1
def generate_config(overrides_kv): new_dict = {key: value for key, value in DEFAULT_CONFIG.items()} for key, value in overrides_kv.items(): new_dict[key] = value return new_dict
def make_sample_with_padding(longer_src=False) -> Dict[str, Any]: tokens_1 = torch.LongTensor( [ [2, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15, 2], [ 2, 11, 12, 14, 15, 10, 11, 12, 13, 14, 15, 2, PAD_INDEX, PAD_INDEX ], ] ) tokens_2 = torch.LongTensor( [ [2, 11, 12, 13, 14, 2, PAD_INDEX, PAD_INDEX], [2, 11, 22, 33, 2, PAD_INDEX, PAD_INDEX, PAD_INDEX] ] ) if longer_src: src_tokens = tokens_1[:, 1:] prev_output_tokens = tokens_2 else: src_tokens = tokens_2[:, 1:8] prev_output_tokens = tokens_1
src_lengths = src_tokens.ne(PAD_INDEX).sum(dim=1).long()
sample = { "net_input": { "src_tokens": src_tokens, "prev_output_tokens": prev_output_tokens, "src_lengths": src_lengths, }, "target": prev_output_tokens[:, 1:], } return sample
def build_transformer_monotonic_attention(**extra_args: Any): overrides = { # Use characteristics dimensions "encoder_embed_dim": 12, "encoder_ffn_embed_dim": 14, "decoder_embed_dim": 12, "decoder_ffn_embed_dim": 14, # Disable dropout so we have comparable tests. "dropout": 0, "attention_dropout": 0, "activation_dropout": 0, "encoder_layerdrop": 0, } overrides.update(extra_args) # Overrides the defaults from the parser args = argparse.Namespace(**overrides) transformer_monotonic_attention.monotonic_tiny_architecture(args)
torch.manual_seed(0) task = FakeTask(args) return ( transformer_monotonic_attention .TransformerModelSimulTrans .build_model(args, task) )
def expected_alignment_formula( p_choose, mass_perservation=True, padding_mask=None): # Online and Linear-Time Attention by Enforcing Monotonic Alignments # https://arxiv.org/pdf/1704.00784.pdf # Eq 18, 19 bsz, tgt_len, src_len = p_choose.size() alpha = torch.zeros_like(p_choose)
if padding_mask is not None: bsz_pad = padding_mask.size(0) num_heads = int(bsz / bsz_pad) padding_mask = ( padding_mask .unsqueeze(1) .expand([bsz_pad, num_heads, src_len]) .contiguous() .view(-1, src_len) )
p_choose = p_choose.masked_fill(padding_mask.unsqueeze(1), 0)
for bsz_i in range(bsz): for i in range(tgt_len): for j in range(src_len): if i == 0: if j == 0: # First source token alpha[bsz_i, i, j] = p_choose[bsz_i, i, j] else: # First target token alpha[bsz_i, i, j] = ( p_choose[bsz_i, i, j] * torch.prod( 1 - p_choose[bsz_i, i, :j] ) ) else: alpha[bsz_i, i, j] = alpha[bsz_i, i - 1, j] for k in range(j): alpha[bsz_i, i, j] += ( alpha[bsz_i, i - 1, k] * torch.prod( 1 - p_choose[bsz_i, i, k:j] ) ) alpha[bsz_i, i, j] *= p_choose[bsz_i, i, j]
alpha = alpha.masked_fill(padding_mask.unsqueeze(1), 0)
if mass_perservation: alpha = mass_perservation_formula(alpha, False, padding_mask)
return alpha
def mass_perservation_formula(alpha, left_padding=False, padding_mask=None): if padding_mask is None or alpha.size(-1) == 1: if alpha.size(-1) > 1: alpha[:, :, -1] = 1 - alpha[:, :, :-1].sum(dim=-1) return alpha
src_lens = (padding_mask.logical_not()).sum(dim=1).long()
bsz, tgt_len, src_len = alpha.size()
assert ( not left_padding or (left_padding and (not padding_mask[:, 0].any())) )
alpha = alpha.masked_fill(padding_mask.unsqueeze(1), 0)
for bsz_i in range(bsz): if left_padding: alpha[bsz_i, :, -1] = ( 1 - alpha[bsz_i, :, :-1].sum(dim=-1) ) else: alpha[bsz_i, :, src_lens[bsz_i] - 1] = ( 1 - alpha[bsz_i, :, :src_lens[bsz_i] - 1].sum(dim=-1) )
return alpha
def expected_soft_attention_formula( alpha, soft_energy, padding_mask=None, chunksize=1e10,): # Monotonic Infinite Lookback Attention for Simultaneous Machine Translation # https://arxiv.org/pdf/1906.05218.pdf # Eq 14
# Monotonic Chunkwise Attention # https://arxiv.org/abs/1712.05382 # Eq 17 bsz, tgt_len, src_len = alpha.size() beta = torch.zeros_like(alpha)
if padding_mask is not None: bsz_pad = padding_mask.size(0) num_heads = int(bsz / bsz_pad) # Expanding for potential head dimension padding_mask = ( padding_mask .unsqueeze(1) .expand([bsz_pad, num_heads, src_len]) .contiguous() .view(-1, src_len) ) soft_energy = soft_energy.masked_fill(padding_mask.unsqueeze(1), float('-inf'))
for bsz_i in range(bsz): for i in range(tgt_len): for j in range(src_len): for k in range(j, min([src_len, j + chunksize])): if not padding_mask[bsz_i, j]: beta[bsz_i, i, j] += ( alpha[bsz_i, i, k] * torch.exp(soft_energy[bsz_i, i, j]) / torch.sum(torch.exp(soft_energy[bsz_i, i, max([0, k - chunksize + 1]):k + 1])) ) return beta
class MonotonicAttentionTestAbstractClass(object): def test_forward(self): sample = make_sample_with_padding() out, _ = self.model.forward(**sample["net_input"]) loss = out.sum() loss.backward()
def test_p_choose(self): sample = make_sample_with_padding() _, extra_out = self.model.forward(**sample["net_input"]) for item in extra_out.attn_list: p_choose = item["p_choose"] self.assertTrue(p_choose.le(1.0).all()) self.assertTrue(p_choose.ge(0.0).all())
def test_expected_alignment(self): for longer_src in [True, False]: sample = make_sample_with_padding(longer_src) _, extra_out = self.model.forward(**sample["net_input"]) for item in extra_out.attn_list: p_choose = item["p_choose"] alpha_system = item["alpha"] self.assertTrue(p_choose.size() == alpha_system.size()) bsz, num_head, tgt_len, src_len = alpha_system.size() alpha_system = alpha_system.view(-1, tgt_len, src_len) p_choose = p_choose.view(-1, tgt_len, src_len)
alpha_real = expected_alignment_formula( p_choose, self.model.decoder.layers[0].encoder_attn.mass_preservation, sample["net_input"]["src_tokens"].eq(PAD_INDEX) )
self.assertTrue( torch.abs(alpha_system - alpha_real).le(5e-5).all(), )
class HardMonotonicAttentionTestCase( unittest.TestCase, MonotonicAttentionTestAbstractClass): def setUp(self): self.model = build_transformer_monotonic_attention( **generate_config({"simul_type": "hard_aligned"}) )
class InfiniteLookbackTestCase( unittest.TestCase, MonotonicAttentionTestAbstractClass): def setUp(self): self.model = build_transformer_monotonic_attention( **generate_config( { "simul_type": "infinite_lookback" } ) ) self.model.train()
def test_fp16_for_long_input(self): sample = { "net_input": { "src_tokens": torch.LongTensor([7] * 1000 + [2]).cuda().unsqueeze(0), "prev_output_tokens": torch.LongTensor([7] * 1000 + [2]).cuda().unsqueeze(0), "src_lengths": torch.LongTensor([1000]).cuda(), }, "target": torch.LongTensor([2] + [7] * 1000).unsqueeze(0).cuda() } self.model.cuda().half() _, extra_out = self.model.forward(**sample["net_input"]) for item in extra_out.attn_list: for key in ["p_choose", "alpha", "beta", "soft_energy"]: self.assertFalse(torch.isnan(item[key]).any())
def test_expected_attention(self): for longer_src in [True, False]: sample = make_sample_with_padding(longer_src) _, extra_out = self.model.forward(**sample["net_input"]) for item in extra_out.attn_list: p_choose = item["p_choose"] alpha_system = item["alpha"] beta_system = item["beta"] soft_energy_system = item["soft_energy"] self.assertTrue(beta_system.size() == alpha_system.size()) self.assertTrue(p_choose.size() == alpha_system.size())
bsz, num_head, tgt_len, src_len = alpha_system.size()
alpha_system = alpha_system.view(-1, tgt_len, src_len) beta_system = beta_system.view(-1, tgt_len, src_len) p_choose = p_choose.view(-1, tgt_len, src_len) soft_energy_system = soft_energy_system.view(-1, tgt_len, src_len)
alpha_real = expected_alignment_formula( p_choose, self.model.decoder.layers[0].encoder_attn.mass_preservation, sample["net_input"]["src_tokens"].eq(PAD_INDEX) )
beta_real = expected_soft_attention_formula( alpha_real, soft_energy_system, sample["net_input"]["src_tokens"].eq(PAD_INDEX), chunksize=getattr( self.model.decoder.layers[0].encoder_attn, "chunk_size", int(1e10) ) or int(1e10) )
self.assertTrue( torch.abs(beta_system - beta_real).le(1e-5).all(), )
class ChunkwiswTestCase( InfiniteLookbackTestCase): def setUp(self): self.model = build_transformer_monotonic_attention( **generate_config( { "simul_type": "chunkwise", "mocha_chunk_size": 3 } ) )
class WaitkTestCase(InfiniteLookbackTestCase): def setUp(self): self.model = build_transformer_monotonic_attention( **generate_config( { "simul_type": "waitk", "waitk_lagging": 3, } ) )
def check_waitk(self, p_choose, lagging, padding_mask): bsz, tgt_len, src_len = p_choose.size() for bsz_i in range(bsz): for i in range(tgt_len): for j in range(src_len): if not padding_mask[bsz_i, j]: if j - i == lagging - 1: self.assertTrue(p_choose[bsz_i, i, j] == 1) else: self.assertTrue(p_choose[bsz_i, i, j] == 0)
def test_waitk_p_choose(self): for longer_src in [True, False]: for k in [1, 3, 10, 20, 100]: sample = make_sample_with_padding(longer_src) model = build_transformer_monotonic_attention( **generate_config( { "simul_type": "waitk", "waitk_lagging": k, } ) ) model.train() _, extra_out = model.forward(**sample["net_input"]) for item in extra_out.attn_list: p_choose = item["p_choose"] bsz, num_heads, tgt_len, src_len = p_choose.size() padding_mask = sample["net_input"]["src_tokens"].eq(PAD_INDEX) padding_mask = ( padding_mask .unsqueeze(1) .expand([bsz, num_heads, src_len]) .contiguous() .view(-1, src_len) ) p_choose = p_choose.view(bsz * num_heads, tgt_len, src_len) self.check_waitk(p_choose, k, padding_mask)1234567891011121314# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import importlibimport os
# automatically import any Python files in the criterions/ directoryfor file in sorted(os.listdir(os.path.dirname(__file__))): if file.endswith(".py") and not file.startswith("_"): module = file[: file.find(".py")] importlib.import_module("examples.simultaneous_translation.utils." + module)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125# Copyright (c) Facebook, Inc. and its affiliates.## This source code is licensed under the MIT license found in the# LICENSE file in the root directory of this source tree.
import torch
def prob_check(tensor, eps=1e-10): assert not torch.isnan(tensor).any(), ( "Nan in a probability tensor." ) # Add the eps here to prevent errors introduced by precision assert tensor.le(1.0 + eps).all() and tensor.ge(0.0 - eps).all(), ( "Incorrect values in a probability tensor" ", 0.0 <= tensor <= 1.0" )
def exclusive_cumprod(tensor, dim: int, eps: float = 1e-10): """ Implementing exclusive cumprod. There is cumprod in pytorch, however there is no exclusive mode. cumprod(x) = [x1, x1x2, x2x3x4, ..., prod_{i=1}^n x_i] exclusive means cumprod(x) = [1, x1, x1x2, x1x2x3, ..., prod_{i=1}^{n-1} x_i] """ tensor_size = list(tensor.size()) tensor_size[dim] = 1 return_tensor = safe_cumprod( torch.cat([torch.ones(tensor_size).type_as(tensor), tensor], dim=dim), dim=dim, eps=eps, )
if dim == 0: return return_tensor[:-1] elif dim == 1: return return_tensor[:, :-1] elif dim == 2: return return_tensor[:, :, :-1] else: raise RuntimeError( "Cumprod on dimension 3 and more is not implemented" )
def safe_cumprod(tensor, dim: int, eps: float = 1e-10): """ An implementation of cumprod to prevent precision issue. cumprod(x) = [x1, x1x2, x1x2x3, ....] = [exp(log(x1)), exp(log(x1) + log(x2)), exp(log(x1) + log(x2) + log(x3)), ...] = exp(cumsum(log(x))) """
if (tensor + eps < 0).any().item(): raise RuntimeError( "Safe cumprod can only take non-negative tensors as input." "Consider use torch.cumprod if you want to calculate negative values." )
log_tensor = torch.log(tensor + eps) cumsum_log_tensor = torch.cumsum(log_tensor, dim) exp_cumsum_log_tensor = torch.exp(cumsum_log_tensor) return exp_cumsum_log_tensor
def moving_sum(x, start_idx: int, end_idx: int): """ From MONOTONIC CHUNKWISE ATTENTION https://arxiv.org/pdf/1712.05382.pdf Equation (18)
x = [x_1, x_2, ..., x_N] MovingSum(x, start_idx, end_idx)_n = Sigma_{m=n−(start_idx−1)}^{n+end_idx-1} x_m for n in {1, 2, 3, ..., N}
x : src_len, batch_size start_idx : start idx end_idx : end idx
Example src_len = 5 batch_size = 3 x = [[ 0, 5, 10], [ 1, 6, 11], [ 2, 7, 12], [ 3, 8, 13], [ 4, 9, 14]]
MovingSum(x, 3, 1) = [[ 0, 5, 10], [ 1, 11, 21], [ 3, 18, 33], [ 6, 21, 36], [ 9, 24, 39]]
MovingSum(x, 1, 3) = [[ 3, 18, 33], [ 6, 21, 36], [ 9, 24, 39], [ 7, 17, 27], [ 4, 9, 14]] """ # TODO: Make dimension configurable assert start_idx > 0 and end_idx > 0 batch_size, tgt_len, src_len = x.size() x = x.view(-1, src_len).unsqueeze(1) # batch_size, 1, src_len moving_sum_weight = torch.ones([1, 1, end_idx + start_idx - 1]).type_as(x)
moving_sum = torch.nn.functional.conv1d( x, moving_sum_weight, padding=start_idx + end_idx - 1 ).squeeze(1)
moving_sum = moving_sum[:, end_idx:-start_idx]
assert src_len == moving_sum.size(1) assert batch_size * tgt_len == moving_sum.size(0)
moving_sum = moving_sum.view(batch_size, tgt_len, src_len)
return moving_sum123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180from typing import Optionalimport torchfrom torch import Tensor
from examples.simultaneous_translation.utils.functions import ( exclusive_cumprod, prob_check, moving_sum,)
def expected_alignment_from_p_choose( p_choose: Tensor, padding_mask: Optional[Tensor] = None, eps: float = 1e-6): """ Calculating expected alignment for from stepwise probability
Reference: Online and Linear-Time Attention by Enforcing Monotonic Alignments https://arxiv.org/pdf/1704.00784.pdf
q_ij = (1 − p_{ij−1})q_{ij−1} + a+{i−1j} a_ij = p_ij q_ij
Parallel solution: ai = p_i * cumprod(1 − pi) * cumsum(a_i / cumprod(1 − pi))
============================================================ Expected input size p_choose: bsz, tgt_len, src_len """ prob_check(p_choose)
# p_choose: bsz, tgt_len, src_len bsz, tgt_len, src_len = p_choose.size() dtype = p_choose.dtype
p_choose = p_choose.float()
if padding_mask is not None: p_choose = p_choose.masked_fill(padding_mask.unsqueeze(1), 0.0)
if p_choose.is_cuda: p_choose = p_choose.contiguous() from alignment_train_cuda_binding import alignment_train_cuda as alignment_train else: from alignment_train_cpu_binding import alignment_train_cpu as alignment_train
alpha = p_choose.new_zeros([bsz, tgt_len, src_len]) alignment_train(p_choose, alpha, eps)
# Mix precision to prevent overflow for fp16 alpha = alpha.type(dtype)
prob_check(alpha)
return alpha
def expected_soft_attention( alpha: Tensor, soft_energy: Tensor, padding_mask: Optional[Tensor] = None, chunk_size: Optional[int] = None, eps: float = 1e-10): """ Function to compute expected soft attention for monotonic infinite lookback attention from expected alignment and soft energy.
Reference: Monotonic Chunkwise Attention https://arxiv.org/abs/1712.05382
Monotonic Infinite Lookback Attention for Simultaneous Machine Translation https://arxiv.org/abs/1906.05218
alpha: bsz, tgt_len, src_len soft_energy: bsz, tgt_len, src_len padding_mask: bsz, src_len left_padding: bool """ if padding_mask is not None: alpha = alpha.masked_fill(padding_mask.unsqueeze(1), 0.0) soft_energy = soft_energy.masked_fill( padding_mask.unsqueeze(1), -float("inf") )
prob_check(alpha)
dtype = alpha.dtype
alpha = alpha.float() soft_energy = soft_energy.float()
soft_energy = soft_energy - soft_energy.max(dim=2, keepdim=True)[0] exp_soft_energy = torch.exp(soft_energy) + eps
if chunk_size is not None: # Chunkwise beta = ( exp_soft_energy * moving_sum( alpha / (eps + moving_sum(exp_soft_energy, chunk_size, 1)), 1, chunk_size ) ) else: # Infinite lookback # Notice that infinite lookback is a special case of chunkwise # where chunksize = inf inner_items = alpha / (eps + torch.cumsum(exp_soft_energy, dim=2))
beta = ( exp_soft_energy * torch.cumsum(inner_items.flip(dims=[2]), dim=2) .flip(dims=[2]) )
if padding_mask is not None: beta = beta.masked_fill( padding_mask.unsqueeze(1).to(torch.bool), 0.0)
# Mix precision to prevent overflow for fp16 beta = beta.type(dtype)
beta = beta.clamp(0, 1)
prob_check(beta)
return beta
def mass_preservation( alpha: Tensor, padding_mask: Optional[Tensor] = None, left_padding: bool = False): """ Function to compute the mass perservation for alpha. This means that the residual weights of alpha will be assigned to the last token.
Reference: Monotonic Infinite Lookback Attention for Simultaneous Machine Translation https://arxiv.org/abs/1906.05218
alpha: bsz, tgt_len, src_len padding_mask: bsz, src_len left_padding: bool """
prob_check(alpha)
if padding_mask is not None: if not left_padding: assert not padding_mask[:, 0].any(), ( "Find padding on the beginning of the sequence." ) alpha = alpha.masked_fill(padding_mask.unsqueeze(1), 0.0)
if left_padding or padding_mask is None: residuals = 1 - alpha[:, :, :-1].sum(dim=-1).clamp(0, 1) alpha[:, :, -1] = residuals else: # right padding _, tgt_len, src_len = alpha.size() residuals = 1 - alpha.sum(dim=-1, keepdim=True).clamp(0, 1) src_lens = src_len - padding_mask.sum(dim=1, keepdim=True) src_lens = src_lens.expand(-1, tgt_len).contiguous() # add back the last value residuals += alpha.gather(2, src_lens.unsqueeze(2) - 1) alpha = alpha.scatter(2, src_lens.unsqueeze(2) - 1, residuals)
prob_check(alpha)
return alpha123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126from typing import Optional, Dictfrom torch import Tensorimport torch
def waitk_p_choose( tgt_len: int, src_len: int, bsz: int, waitk_lagging: int, key_padding_mask: Optional[Tensor] = None, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] = None):
max_src_len = src_len if incremental_state is not None: # Retrieve target length from incremental states # For inference the length of query is always 1 max_tgt_len = incremental_state["steps"]["tgt"] assert max_tgt_len is not None max_tgt_len = int(max_tgt_len) else: max_tgt_len = tgt_len
if max_src_len < waitk_lagging: if incremental_state is not None: max_tgt_len = 1 return torch.zeros( bsz, max_tgt_len, max_src_len )
# Assuming the p_choose looks like this for wait k=3 # src_len = 6, max_tgt_len = 5 # [0, 0, 1, 0, 0, 0, 0] # [0, 0, 0, 1, 0, 0, 0] # [0, 0, 0, 0, 1, 0, 0] # [0, 0, 0, 0, 0, 1, 0] # [0, 0, 0, 0, 0, 0, 1] # linearize the p_choose matrix: # [0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0...] # The indices of linearized matrix that equals 1 is # 2 + 6 * 0 # 3 + 6 * 1 # ... # n + src_len * n + k - 1 = n * (src_len + 1) + k - 1 # n from 0 to max_tgt_len - 1 # # First, generate the indices (activate_indices_offset: bsz, max_tgt_len) # Second, scatter a zeros tensor (bsz, max_tgt_len * src_len) # with activate_indices_offset # Third, resize the tensor to (bsz, max_tgt_len, src_len)
activate_indices_offset = ( ( torch.arange(max_tgt_len) * (max_src_len + 1) + waitk_lagging - 1 ) .unsqueeze(0) .expand(bsz, max_tgt_len) .long() )
if key_padding_mask is not None: if key_padding_mask[:, 0].any(): # Left padding activate_indices_offset += ( key_padding_mask.sum(dim=1, keepdim=True) )
# Need to clamp the indices that are too large activate_indices_offset = ( activate_indices_offset .clamp( 0, min( [ max_tgt_len, max_src_len - waitk_lagging + 1 ] ) * max_src_len - 1 ) )
p_choose = torch.zeros(bsz, max_tgt_len * max_src_len)
p_choose = p_choose.scatter( 1, activate_indices_offset, 1.0 ).view(bsz, max_tgt_len, max_src_len)
if key_padding_mask is not None: p_choose = p_choose.to(key_padding_mask) p_choose = p_choose.masked_fill(key_padding_mask.unsqueeze(1), 0)
if incremental_state is not None: p_choose = p_choose[:, -1:]
return p_choose.float()
def learnable_p_choose( energy, noise_mean: float = 0.0, noise_var: float = 0.0, training: bool = True): """ Calculating step wise prob for reading and writing 1 to read, 0 to write energy: bsz, tgt_len, src_len """
noise = 0 if training: # add noise here to encourage discretness noise = ( torch.normal(noise_mean, noise_var, energy.size()) .type_as(energy) .to(energy.device) )
p_choose = torch.sigmoid(energy + noise)
# p_choose: bsz * self.num_heads, tgt_len, src_len return p_choosedata/fairseq/examples/adaptive_span/README.mdAdded+90
data/fairseq/examples/adaptive_span/__init__.pyAdded+19
data/fairseq/examples/adaptive_span/adagrad_with_grad_clip.pyAdded+128
data/fairseq/examples/adaptive_span/adaptive_span_attention.pyAdded+160
data/fairseq/examples/adaptive_span/adaptive_span_loss.pyAdded+107
data/fairseq/examples/adaptive_span/adaptive_span_model.pyAdded+263
data/fairseq/examples/adaptive_span/adaptive_span_model_wrapper.pyAdded+145
data/fairseq/examples/adaptive_span/truncated_bptt_lm_task.pyAdded+285
data/fairseq/examples/backtranslation/README.mdAdded+297
data/fairseq/examples/backtranslation/deduplicate_lines.pyAdded+41
data/fairseq/examples/backtranslation/extract_bt_data.pyAdded+72
data/fairseq/examples/backtranslation/prepare-de-monolingual.shAdded+98
data/fairseq/examples/backtranslation/prepare-wmt18en2de.shAdded+135
data/fairseq/examples/backtranslation/sacrebleu.shAdded+37
data/fairseq/examples/backtranslation/tokenized_bleu.shAdded+46
data/fairseq/examples/cross_lingual_language_model/README.mdAdded+77
data/fairseq/examples/discriminative_reranking_nmt/README.mdAdded+202
data/fairseq/examples/discriminative_reranking_nmt/__init__.pyAdded+1
data/fairseq/examples/discriminative_reranking_nmt/config/deen.yamlAdded+56
data/fairseq/examples/discriminative_reranking_nmt/criterions/__init__.pyAdded+6
data/fairseq/examples/discriminative_reranking_nmt/criterions/discriminative_reranking_criterion.pyAdded+139
data/fairseq/examples/discriminative_reranking_nmt/drnmt_rerank.pyAdded+364
data/fairseq/examples/discriminative_reranking_nmt/models/__init__.pyAdded+6
data/fairseq/examples/discriminative_reranking_nmt/models/discriminative_reranking_model.pyAdded+365
data/fairseq/examples/discriminative_reranking_nmt/scripts/prep_data.pyAdded+136
data/fairseq/examples/discriminative_reranking_nmt/tasks/__init__.pyAdded+6
data/fairseq/examples/discriminative_reranking_nmt/tasks/discriminative_reranking_task.pyAdded+490
data/fairseq/examples/mbart/README.mdAdded+123
data/fairseq/examples/normformer/README.mdAdded+70
data/fairseq/examples/normformer/train_lm.shAdded+78
data/fairseq/examples/shuffled_word_order/README.finetuning.mdAdded+135
data/fairseq/examples/shuffled_word_order/README.mdAdded+94
data/fairseq/examples/simultaneous_translation/README.mdAdded+5
data/fairseq/examples/simultaneous_translation/__init__.pyAdded+6
data/fairseq/examples/simultaneous_translation/docs/ende-mma.mdAdded+74
data/fairseq/examples/simultaneous_translation/docs/enja-waitk.mdAdded+106
data/fairseq/examples/simultaneous_translation/eval/agents/simul_t2t_enja.pyAdded+226
data/fairseq/examples/simultaneous_translation/models/__init__.pyAdded+15
data/fairseq/examples/simultaneous_translation/models/convtransformer_simul_trans.pyAdded+204
data/fairseq/examples/simultaneous_translation/models/transformer_monotonic_attention.pyAdded+302
data/fairseq/examples/simultaneous_translation/modules/__init__.pyAdded+23
data/fairseq/examples/simultaneous_translation/modules/fixed_pre_decision.pyAdded+190
data/fairseq/examples/simultaneous_translation/modules/monotonic_multihead_attention.pyAdded+520
data/fairseq/examples/simultaneous_translation/modules/monotonic_transformer_layer.pyAdded+182
data/fairseq/examples/simultaneous_translation/tests/test_alignment_train.pyAdded+88
data/fairseq/examples/simultaneous_translation/tests/test_text_models.pyAdded+407
data/fairseq/examples/simultaneous_translation/utils/__init__.pyAdded+14
data/fairseq/examples/simultaneous_translation/utils/functions.pyAdded+125
data/fairseq/examples/simultaneous_translation/utils/monotonic_attention.pyAdded+180
data/fairseq/examples/simultaneous_translation/utils/p_choose_strategy.pyAdded+126