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)
- .gitattributes+1
- fairseq/examples/hubert/tests/6313-76958-0021.flacAdded+3
- fairseq/examples/pointer_generator/README.xsum.mdAdded+180
- fairseq/examples/pointer_generator/pointer_generator_src/__init__.pyAdded+6
- fairseq/examples/pointer_generator/pointer_generator_src/transformer_pg.pyAdded+518
- fairseq/examples/pointer_generator/postprocess.pyAdded+96
- fairseq/examples/quant_noise/transformer_quantization_config.yamlAdded+33
- fairseq/examples/roberta/README.custom_classification.mdAdded+168
- fairseq/examples/roberta/README.glue.mdAdded+64
- fairseq/examples/roberta/README.mdAdded+296
- fairseq/examples/roberta/README.pretraining.mdAdded+84
- fairseq/examples/roberta/README.race.mdAdded+68
- fairseq/examples/roberta/commonsense_qa/README.mdAdded+99
- fairseq/examples/roberta/commonsense_qa/__init__.pyAdded+6
- fairseq/examples/roberta/commonsense_qa/commonsense_qa_task.pyAdded+190
- fairseq/examples/roberta/commonsense_qa/download_cqa_data.shAdded+14
- fairseq/examples/roberta/config/finetuning/cola.yamlAdded+59
- fairseq/examples/roberta/config/finetuning/mnli.yamlAdded+59
- fairseq/examples/roberta/config/finetuning/mrpc.yamlAdded+59
- fairseq/examples/roberta/config/finetuning/qnli.yamlAdded+59
- fairseq/examples/roberta/config/finetuning/qqp.yamlAdded+59
- fairseq/examples/roberta/config/finetuning/rte.yamlAdded+59
- fairseq/examples/roberta/config/finetuning/run_config/local.yamlAdded+15
- fairseq/examples/roberta/config/finetuning/run_config/slurm_1g.yamlAdded+28
- fairseq/examples/roberta/config/finetuning/run_config/slurm_1g_aws.yamlAdded+25
- fairseq/examples/roberta/config/finetuning/sst_2.yamlAdded+59
- fairseq/examples/roberta/config/finetuning/sts_b.yamlAdded+58
- fairseq/examples/roberta/config/pretraining/base.yamlAdded+42
- fairseq/examples/roberta/config/pretraining/run_config/local.yamlAdded+15
- fairseq/examples/roberta/config/pretraining/run_config/slurm_2.yamlAdded+37
- fairseq/examples/roberta/config/pretraining/run_config/slurm_2_aws.yamlAdded+39
- fairseq/examples/roberta/config/pretraining/run_config/slurm_3.yamlAdded+36
- fairseq/examples/roberta/config/pretraining/run_config/slurm_4.yamlAdded+36
- fairseq/examples/roberta/fb_multilingual/README.multilingual.pretraining.mdAdded+26
- fairseq/examples/roberta/multiprocessing_bpe_encoder.pyAdded+130
- fairseq/examples/roberta/preprocess_GLUE_tasks.shAdded+185
- fairseq/examples/roberta/preprocess_RACE.pyAdded+102
- fairseq/examples/roberta/preprocess_RACE.shAdded+59
- fairseq/examples/roberta/wsc/README.mdAdded+125
- fairseq/examples/roberta/wsc/__init__.pyAdded+7
- fairseq/examples/roberta/wsc/wsc_criterion.pyAdded+167
- fairseq/examples/roberta/wsc/wsc_task.pyAdded+401
- fairseq/examples/roberta/wsc/wsc_utils.pyAdded+241
- fairseq/examples/rxf/README.mdAdded+52
- fairseq/examples/rxf/__init__.pyAdded+6
- fairseq/examples/rxf/rxf_src/__init__.pyAdded+6
- fairseq/examples/rxf/rxf_src/label_smoothed_cross_entropy_r3f.pyAdded+158
- fairseq/examples/rxf/rxf_src/sentence_prediction_r3f.pyAdded+171
- fairseq/examples/scaling_nmt/README.mdAdded+114
- fairseq/examples/shuffled_word_order/README.finetuning.mdAdded+135
37 unmodified lines38394037 unmodified linesfairseq/alignment_train_cuda_binding.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -textfairseq/alignment_train_cpu_binding.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -textfairseq/docs/fairseq.gif filter=lfs diff=lfs merge=lfs -text37 unmodified lines3839404137 unmodified linesfairseq/alignment_train_cuda_binding.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -textfairseq/alignment_train_cpu_binding.cpython-310-x86_64-linux-gnu.so filter=lfs diff=lfs merge=lfs -textfairseq/docs/fairseq.gif filter=lfs diff=lfs merge=lfs -textfairseq/examples/hubert/tests/6313-76958-0021.flac filter=lfs diff=lfs merge=lfs -text
123version https://git-lfs.github.com/spec/v1oid sha256:0dd89e2bf8c60e05264bf2539f0ec35bc65efb55cd952b3510fa243de9cc16ffsize 223912123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180## Training a pointer-generator model on the Extreme Summarization dataset
##### 1. Download the Extreme Summarization data and preprocess it
Follow the instructions [here](https://github.com/EdinburghNLP/XSum) to obtainthe original Extreme Summarization dataset. You should have six files,{train,validation,test}.{document,summary}.
##### 2. Create a vocabulary and extend it with source position markers
```bashvocab_size=10000position_markers=1000export LC_ALL=Ccat train.document train.summary | tr -s '[:space:]' '\n' | sort | uniq -c | sort -k1,1bnr -k2 | head -n "$((vocab_size - 4))" | awk '{ print $2 " " $1 }' >dict.pg.txtpython3 -c "[print('<unk-{}> 0'.format(n)) for n in range($position_markers)]" >>dict.pg.txt```
This creates the file dict.pg.txt that contains the 10k most frequent words,followed by 1k source position markers:
```the 4954867. 4157552, 3439668to 2212159a 1916857of 1916820and 1823350...<unk-0> 0<unk-1> 0<unk-2> 0<unk-3> 0<unk-4> 0...```
##### 2. Preprocess the text data
```bash./preprocess.py --source train.document --target train.summary --vocab <(cut -d' ' -f1 dict.pg.txt) --source-out train.pg.src --target-out train.pg.tgt./preprocess.py --source validation.document --target validation.summary --vocab <(cut -d' ' -f1 dict.pg.txt) --source-out valid.pg.src --target-out valid.pg.tgt./preprocess.py --source test.document --vocab <(cut -d' ' -f1 dict.pg.txt) --source-out test.pg.src```
The data should now contain `<unk-N>` tokens in place of out-of-vocabulary words.
##### 3. Binarize the dataset:
```bashfairseq-preprocess \ --source-lang src \ --target-lang tgt \ --trainpref train.pg \ --validpref valid.pg \ --destdir bin \ --workers 60 \ --srcdict dict.pg.txt \ --joined-dictionary```
##### 3. Train a model
```bashtotal_updates=20000warmup_updates=500lr=0.001max_tokens=4096update_freq=4pointer_layer=-2
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 fairseq-train bin \ --user-dir examples/pointer_generator/pointer_generator_src \ --max-tokens "$max_tokens" \ --task translation \ --source-lang src --target-lang tgt \ --truncate-source \ --layernorm-embedding \ --share-all-embeddings \ --encoder-normalize-before \ --decoder-normalize-before \ --required-batch-size-multiple 1 \ --arch transformer_pointer_generator \ --alignment-layer "$pointer_layer" \ --alignment-heads 1 \ --source-position-markers 1000 \ --criterion label_smoothed_cross_entropy \ --label-smoothing 0.1 \ --dropout 0.1 --attention-dropout 0.1 \ --weight-decay 0.01 --optimizer adam --adam-betas "(0.9, 0.999)" --adam-eps 1e-08 \ --clip-norm 0.1 \ --lr-scheduler inverse_sqrt --lr "$lr" --max-update "$total_updates" --warmup-updates "$warmup_updates" \ --update-freq "$update_freq" \ --skip-invalid-size-inputs-valid-test```
Above we specify that our dictionary contains 1000 source position markers, andthat we want to use one attention head from the penultimate decoder layer forpointing. It should run in 5.5 hours on one node with eight 32GB V100 GPUs. Thelogged messages confirm that dictionary indices above 10000 will be mapped tothe `<unk>` embedding:
```2020-09-24 20:43:53 | INFO | fairseq.tasks.translation | [src] dictionary: 11000 types2020-09-24 20:43:53 | INFO | fairseq.tasks.translation | [tgt] dictionary: 11000 types2020-09-24 20:43:53 | INFO | fairseq.data.data_utils | loaded 11332 examples from: bin/valid.src-tgt.src2020-09-24 20:43:53 | INFO | fairseq.data.data_utils | loaded 11332 examples from: bin/valid.src-tgt.tgt2020-09-24 20:43:53 | INFO | fairseq.tasks.translation | bin valid src-tgt 11332 examples2020-09-24 20:43:53 | INFO | fairseq.models.transformer_pg | dictionary indices from 10000 to 10999 will be mapped to 3```
##### 4. Summarize the test sequences
```bashbatch_size=32beam_size=6max_length=60length_penalty=1.0
fairseq-interactive bin \ --user-dir examples/pointer_generator/pointer_generator_src \ --batch-size "$batch_size" \ --task translation \ --source-lang src --target-lang tgt \ --path checkpoints/checkpoint_last.pt \ --input test.pg.src \ --buffer-size 200 \ --max-len-a 0 \ --max-len-b "$max_length" \ --lenpen "$length_penalty" \ --beam "$beam_size" \ --skip-invalid-size-inputs-valid-test | tee generate.outgrep ^H generate.out | cut -f 3- >generate.hyp```
Now you should have the generated sequences in `generate.hyp`. They contain`<unk-N>` tokens that the model has copied from the source sequence. In order toretrieve the original words, we need the unprocessed source sequences from`test.document`.
##### 5. Process the generated output
Since we skipped too long inputs when producing `generate.hyp`, we also have toskip too long sequences now that we read `test.document`.
```bash./postprocess.py \ --source <(awk 'NF<1024' test.document) \ --target generate.hyp \ --target-out generate.hyp.processed```
Now you'll find the final sequences from `generate.hyp.processed`, with`<unk-N>` replaced with the original word from the source sequence.
##### An example of a summarized sequence
The original source document in `test.document`:
> de roon moved to teesside in june 2016 for an initial # 8.8 m fee and played 33 premier league games last term . the netherlands international , 26 , scored five goals in 36 league and cup games during his spell at boro . meanwhile , manager garry monk confirmed the championship club 's interest in signing chelsea midfielder lewis baker . `` he 's a target and one of many that we 've had throughout the summer months , '' said monk . find all the latest football transfers on our dedicated page .
The preprocessed source document in `test.src.pg`:
> de \<unk-1> moved to \<unk-4> in june 2016 for an initial # \<unk-12> m fee and played 33 premier league games last term . the netherlands international , 26 , scored five goals in 36 league and cup games during his spell at boro . meanwhile , manager garry monk confirmed the championship club 's interest in signing chelsea midfielder lewis baker . `` he 's a target and one of many that we 've had throughout the summer months , '' said monk . find all the latest football transfers on our dedicated page .
The generated summary in `generate.hyp`:
> middlesbrough striker \<unk> de \<unk-1> has joined spanish side \<unk> on a season-long loan .
The generated summary after postprocessing in `generate.hyp.processed`:
> middlesbrough striker \<unk> de roon has joined spanish side \<unk> on a season-long loan .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 transformer_pg # noqa123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518# 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 typing import Any, Dict, Optional, List, Tuple
import torchimport torch.nn as nnfrom fairseq import utilsfrom fairseq.models import register_model, register_model_architecturefrom fairseq.models.transformer import ( DEFAULT_MAX_SOURCE_POSITIONS, DEFAULT_MAX_TARGET_POSITIONS, TransformerDecoder, TransformerEncoder, TransformerModel, base_architecture,)from torch import Tensor
logger = logging.getLogger(__name__)
@register_model("transformer_pointer_generator")class TransformerPointerGeneratorModel(TransformerModel): """ Transformer model from `"Attention Is All You Need" (Vaswani et al, 2017) <https://arxiv.org/abs/1706.03762>`_, augmented with a pointer-generator network from `"Get To The Point: Summarization with Pointer-Generator Networks" (See et al, 2017) <https://arxiv.org/abs/1704.04368>`_.
Args: encoder (TransformerPointerGeneratorEncoder): the encoder decoder (TransformerPointerGeneratorDecoder): the decoder
The Transformer pointer-generator model provides the following named architectures and command-line arguments:
.. argparse:: :ref: fairseq.models.transformer_pointer_generator_parser :prog: """
@staticmethod def add_args(parser): """Add model-specific arguments to the parser.""" # fmt: off TransformerModel.add_args(parser) parser.add_argument('--alignment-heads', type=int, metavar='N', help='number of attention heads to be used for ' 'pointing') parser.add_argument('--alignment-layer', type=int, metavar='I', help='layer number to be used for pointing (0 ' 'corresponding to the bottommost layer)') parser.add_argument('--source-position-markers', type=int, metavar='N', help='dictionary includes N additional items that ' 'represent an OOV token at a particular input ' 'position') parser.add_argument('--force-generation', type=float, metavar='P', default=None, help='set the vocabulary distribution weight to P, ' 'instead of predicting it from the input (1.0 ' 'corresponding to generation, 0.0 to pointing)') # fmt: on
@classmethod def build_model(cls, args, task): """Build a new model instance."""
# make sure all arguments are present in older models base_architecture(args)
if args.encoder_layers_to_keep: args.encoder_layers = len(args.encoder_layers_to_keep.split(",")) if args.decoder_layers_to_keep: args.decoder_layers = len(args.decoder_layers_to_keep.split(","))
if getattr(args, "max_source_positions", None) is None: args.max_source_positions = DEFAULT_MAX_SOURCE_POSITIONS if getattr(args, "max_target_positions", None) is None: args.max_target_positions = DEFAULT_MAX_TARGET_POSITIONS if getattr(args, "source_position_markers", None) is None: args.source_position_markers = args.max_source_positions
src_dict, tgt_dict = task.source_dictionary, task.target_dictionary if src_dict != tgt_dict: raise ValueError("Pointer-generator requires a joined dictionary")
def build_embedding(dictionary, embed_dim, path=None): # The dictionary may include additional items that can be used in # place of the normal OOV token and that all map to the same # embedding. Using a different token for each input position allows # one to restore the word identities from the original source text. num_embeddings = len(dictionary) - args.source_position_markers padding_idx = dictionary.pad() unk_idx = dictionary.unk() logger.info( "dictionary indices from {0} to {1} will be mapped to {2}".format( num_embeddings, len(dictionary) - 1, unk_idx ) ) emb = Embedding(num_embeddings, embed_dim, padding_idx, unk_idx) # if provided, load from preloaded dictionaries if path: embed_dict = utils.parse_embedding(path) utils.load_embedding(embed_dict, dictionary, emb) return emb
if args.share_all_embeddings: if args.encoder_embed_dim != args.decoder_embed_dim: raise ValueError( "--share-all-embeddings requires --encoder-embed-dim to match --decoder-embed-dim" ) if args.decoder_embed_path and ( args.decoder_embed_path != args.encoder_embed_path ): raise ValueError( "--share-all-embeddings not compatible with --decoder-embed-path" ) encoder_embed_tokens = build_embedding( src_dict, args.encoder_embed_dim, args.encoder_embed_path ) decoder_embed_tokens = encoder_embed_tokens args.share_decoder_input_output_embed = True else: encoder_embed_tokens = build_embedding( src_dict, args.encoder_embed_dim, args.encoder_embed_path ) decoder_embed_tokens = build_embedding( tgt_dict, args.decoder_embed_dim, args.decoder_embed_path )
encoder = cls.build_encoder(args, src_dict, encoder_embed_tokens) decoder = cls.build_decoder(args, tgt_dict, decoder_embed_tokens) return cls(args, encoder, decoder)
@classmethod def build_encoder(cls, args, src_dict, embed_tokens): return TransformerPointerGeneratorEncoder(args, src_dict, embed_tokens)
@classmethod def build_decoder(cls, args, tgt_dict, embed_tokens): return TransformerPointerGeneratorDecoder(args, tgt_dict, embed_tokens)
class TransformerPointerGeneratorEncoder(TransformerEncoder): """ Transformer encoder consisting of *args.encoder_layers* layers. Each layer is a :class:`TransformerEncoderLayer`. The pointer-generator variant adds the source tokens to the encoder output as these are otherwise not passed to the decoder. """
def forward( self, src_tokens, src_lengths: Optional[Tensor] = None, return_all_hiddens: bool = False, token_embeddings: Optional[Tensor] = None ): """ Runs the `forward()` method of the parent Transformer class. Then adds the source tokens into the encoder output tuple.
While it might be more elegant that the model would pass the source tokens to the `forward()` method of the decoder too, this would require changes to `SequenceGenerator`.
Args: src_tokens (torch.LongTensor): tokens in the source language of shape `(batch, src_len)` src_lengths (torch.LongTensor): lengths of each source sentence of shape `(batch)` return_all_hiddens (bool, optional): also return all of the intermediate hidden states (default: False). token_embeddings (torch.Tensor, optional): precomputed embeddings default `None` will recompute embeddings
Returns: namedtuple: - **encoder_out** (Tensor): the last encoder layer's output of shape `(src_len, batch, embed_dim)` - **encoder_padding_mask** (ByteTensor): the positions of padding elements of shape `(batch, src_len)` - **encoder_embedding** (Tensor): the (scaled) embedding lookup of shape `(batch, src_len, embed_dim)` - **encoder_states** (List[Tensor]): all intermediate hidden states of shape `(src_len, batch, embed_dim)`. Only populated if *return_all_hiddens* is True. - **src_tokens** (Tensor): input token ids of shape `(batch, src_len)` """ encoder_out = self.forward_scriptable(src_tokens, src_lengths, return_all_hiddens, token_embeddings)
# The Pytorch Mobile lite interpreter does not supports returning NamedTuple in # `forward` so we use a dictionary instead. # TorchScript does not support mixed values so the values are all lists. # The empty list is equivalent to None. return { "encoder_out": encoder_out["encoder_out"], # T x B x C "encoder_padding_mask": encoder_out["encoder_padding_mask"], # B x T "encoder_embedding": encoder_out["encoder_embedding"], # B x T x C "encoder_states": encoder_out["encoder_states"], # List[T x B x C] "src_tokens": [src_tokens], # B x T "src_lengths": [], }
class TransformerPointerGeneratorDecoder(TransformerDecoder): """ Transformer decoder consisting of *args.decoder_layers* layers. Each layer is a :class:`TransformerDecoderLayer`. The pointer-generator variant mixes the output probabilities with an attention distribution in the output layer.
Args: args (argparse.Namespace): parsed command-line arguments dictionary (~fairseq.data.Dictionary): decoding dictionary embed_tokens (torch.nn.Embedding): output embedding """
def __init__(self, args, dictionary, embed_tokens): super().__init__(args, dictionary, embed_tokens, no_encoder_attn=False)
# In the pointer-generator model these arguments define the decoder # layer and the number of attention heads that will be averaged to # create the alignment for pointing. self.alignment_heads = args.alignment_heads self.alignment_layer = args.alignment_layer
input_embed_dim = embed_tokens.embedding_dim
# Generation probabilities / interpolation coefficients are predicted # from the current decoder input embedding and the decoder output, which # is the size of output_embed_dim. p_gen_input_size = input_embed_dim + self.output_embed_dim self.project_p_gens = nn.Linear(p_gen_input_size, 1) nn.init.zeros_(self.project_p_gens.bias)
# The dictionary may include a separate entry for an OOV token in each # input position, so that their identity can be restored from the # original source text. self.num_types = len(dictionary) self.num_oov_types = args.source_position_markers self.num_embeddings = self.num_types - self.num_oov_types self.force_p_gen = args.force_generation
def forward( self, prev_output_tokens, encoder_out: Optional[Dict[str, List[Tensor]]] = None, incremental_state: Optional[Dict[str, Dict[str, Optional[Tensor]]]] = None, features_only: bool = False, alignment_layer: Optional[int] = 0, alignment_heads: Optional[int] = 1, src_lengths: Optional[Any] = None, return_all_hiddens: bool = False, ): """ Args: prev_output_tokens (LongTensor): previous decoder outputs of shape `(batch, tgt_len)`, for teacher forcing encoder_out (optional): output from the encoder, used for encoder-side attention incremental_state (dict, optional): dictionary used for storing state during :ref:`Incremental decoding` features_only (bool, optional): only return features without applying output layer (default: False) alignment_layer (int, optional): 0-based index of the layer to be used for pointing (default: 0) alignment_heads (int, optional): number of attention heads to be used for pointing (default: 1)
Returns: tuple: - the decoder's output of shape `(batch, tgt_len, vocab)` - a dictionary with any model-specific outputs """ # The normal Transformer model doesn't pass the alignment_layer and # alignment_heads parameters correctly. We use our local variables. x, extra = self.extract_features( prev_output_tokens, encoder_out=encoder_out, incremental_state=incremental_state, alignment_layer=self.alignment_layer, alignment_heads=self.alignment_heads, ) if not features_only: # Embedding the tokens again for generation probability prediction, # so that we don't have to reimplement the whole extract_features() # method. if incremental_state is not None: prev_output_tokens = prev_output_tokens[:, -1:] prev_output_embed = self.embed_tokens(prev_output_tokens) prev_output_embed *= self.embed_scale predictors = torch.cat((prev_output_embed, x), 2) p_gens = self.project_p_gens(predictors) p_gens = torch.sigmoid(p_gens.float()) # Torchscript complains if encoder_out or attn are None because # `output_layer()` signature expects tensors instead attn: Optional[Tensor] = extra["attn"][0] assert encoder_out is not None assert attn is not None x = self.output_layer(x, attn, encoder_out["src_tokens"][0], p_gens) return x, extra
def output_layer( self, features: Tensor, attn: Tensor, src_tokens: Tensor, p_gens: Tensor ) -> Tensor: """ Project features to the vocabulary size and mix with the attention distributions. """ if self.force_p_gen is not None: p_gens = self.force_p_gen
# project back to size of vocabulary if self.adaptive_softmax is None: logits = self.output_projection(features) else: logits = features
batch_size = logits.shape[0] output_length = logits.shape[1] assert logits.shape[2] == self.num_embeddings assert src_tokens.shape[0] == batch_size src_length = src_tokens.shape[1]
# The final output distribution will be a mixture of the normal output # distribution (softmax of logits) and attention weights. gen_dists = self.get_normalized_probs_scriptable( (logits, None), log_probs=False, sample=None ) gen_dists = torch.mul(gen_dists, p_gens) padding_size = (batch_size, output_length, self.num_oov_types) padding = gen_dists.new_zeros(padding_size) gen_dists = torch.cat((gen_dists, padding), 2) assert gen_dists.shape[2] == self.num_types
# Scatter attention distributions to distributions over the extended # vocabulary in a tensor of shape [batch_size, output_length, # vocab_size]. Each attention weight will be written into a location # that is for other dimensions the same as in the index tensor, but for # the third dimension it's the value of the index tensor (the token ID). attn = torch.mul(attn.float(), 1 - p_gens) index = src_tokens[:, None, :] index = index.expand(batch_size, output_length, src_length) attn_dists_size = (batch_size, output_length, self.num_types) attn_dists = attn.new_zeros(attn_dists_size) attn_dists.scatter_add_(2, index, attn.float())
# Final distributions, [batch_size, output_length, num_types]. return gen_dists + attn_dists
def get_normalized_probs( self, net_output: Tuple[Tensor, Optional[Dict[str, List[Optional[Tensor]]]]], log_probs: bool, sample: Optional[Dict[str, Tensor]] = None, ): """ Get normalized probabilities (or log probs) from a net's output. Pointer-generator network output is already normalized. """ probs = net_output[0] # Make sure the probabilities are greater than zero when returning log # probabilities. return probs.clamp(1e-10, 1.0).log() if log_probs else probs
class Embedding(nn.Embedding): r"""A simple lookup table that stores embeddings of a fixed dictionary and size. This module is often used to store word embeddings and retrieve them using indices. The input to the module is a list of indices, and the output is the corresponding word embeddings. This subclass differs from the standard PyTorch Embedding class by allowing additional vocabulary entries that will be mapped to the unknown token embedding. Args: num_embeddings (int): size of the dictionary of embeddings embedding_dim (int): the size of each embedding vector padding_idx (int): Pads the output with the embedding vector at :attr:`padding_idx` (initialized to zeros) whenever it encounters the index. unk_idx (int): Maps all token indices that are greater than or equal to num_embeddings to this index. Attributes: weight (Tensor): the learnable weights of the module of shape (num_embeddings, embedding_dim) initialized from :math:`\mathcal{N}(0, 1)` Shape: - Input: :math:`(*)`, LongTensor of arbitrary shape containing the indices to extract - Output: :math:`(*, H)`, where `*` is the input shape and :math:`H=\text{embedding\_dim}` .. note:: Keep in mind that only a limited number of optimizers support sparse gradients: currently it's :class:`optim.SGD` (`CUDA` and `CPU`), :class:`optim.SparseAdam` (`CUDA` and `CPU`) and :class:`optim.Adagrad` (`CPU`) .. note:: With :attr:`padding_idx` set, the embedding vector at :attr:`padding_idx` is initialized to all zeros. However, note that this vector can be modified afterwards, e.g., using a customized initialization method, and thus changing the vector used to pad the output. The gradient for this vector from :class:`~torch.nn.Embedding` is always zero. """ __constants__ = ["unk_idx"]
# Torchscript: Inheriting from Embedding class produces an error when exporting to Torchscript # -> RuntimeError: Unable to cast Python instance to C++ type (compile in debug mode for details # It's happening because max_norm attribute from nn.Embedding is None by default and it cannot be # cast to a C++ type def __init__( self, num_embeddings: int, embedding_dim: int, padding_idx: Optional[int], unk_idx: int, max_norm: Optional[float] = float("inf"), ): super().__init__(num_embeddings, embedding_dim, padding_idx=padding_idx, max_norm=max_norm) self.unk_idx = unk_idx nn.init.normal_(self.weight, mean=0, std=embedding_dim ** -0.5) nn.init.constant_(self.weight[padding_idx], 0)
def forward(self, input): input = torch.where( input >= self.num_embeddings, torch.ones_like(input) * self.unk_idx, input ) return nn.functional.embedding( input, self.weight, self.padding_idx, self.max_norm, self.norm_type, self.scale_grad_by_freq, self.sparse )
@register_model_architecture( "transformer_pointer_generator", "transformer_pointer_generator")def transformer_pointer_generator(args): args.alignment_heads = getattr(args, "alignment_heads", 1) args.alignment_layer = getattr(args, "alignment_layer", -1) base_architecture(args) if args.alignment_layer < 0: args.alignment_layer = args.decoder_layers + args.alignment_layer
@register_model_architecture( "transformer_pointer_generator", "transformer_pointer_generator_iwslt_de_en")def transformer_pointer_generator_iwslt_de_en(args): args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 512) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 1024) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 4) args.encoder_layers = getattr(args, "encoder_layers", 6) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 512) args.decoder_ffn_embed_dim = getattr(args, "decoder_ffn_embed_dim", 1024) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 4) args.decoder_layers = getattr(args, "decoder_layers", 6) transformer_pointer_generator(args)
@register_model_architecture( "transformer_pointer_generator", "transformer_pointer_generator_wmt_en_de")def transformer_pointer_generator_wmt_en_de(args): transformer_pointer_generator(args)
# Transformer pointer-generator with the base Transformer parameters as used in# the "Attention Is All You Need" paper (Vaswani et al., 2017)@register_model_architecture( "transformer_pointer_generator", "transformer_pointer_generator_vaswani_wmt_en_de_big",)def transformer_pointer_generator_vaswani_wmt_en_de_big(args): args.encoder_embed_dim = getattr(args, "encoder_embed_dim", 1024) args.encoder_ffn_embed_dim = getattr(args, "encoder_ffn_embed_dim", 4096) args.encoder_attention_heads = getattr(args, "encoder_attention_heads", 16) args.encoder_normalize_before = getattr(args, "encoder_normalize_before", False) args.decoder_embed_dim = getattr(args, "decoder_embed_dim", 1024) args.decoder_ffn_embed_dim = getattr(args, "decoder_ffn_embed_dim", 4096) args.decoder_attention_heads = getattr(args, "decoder_attention_heads", 16) args.dropout = getattr(args, "dropout", 0.3) transformer_pointer_generator(args)
@register_model_architecture( "transformer_pointer_generator", "transformer_pointer_generator_vaswani_wmt_en_fr_big",)def transformer_pointer_generator_vaswani_wmt_en_fr_big(args): args.dropout = getattr(args, "dropout", 0.1) transformer_pointer_generator_vaswani_wmt_en_de_big(args)
@register_model_architecture( "transformer_pointer_generator", "transformer_pointer_generator_wmt_en_de_big")def transformer_pointer_generator_wmt_en_de_big(args): args.attention_dropout = getattr(args, "attention_dropout", 0.1) transformer_pointer_generator_vaswani_wmt_en_de_big(args)
# default parameters used in tensor2tensor implementation@register_model_architecture( "transformer_pointer_generator", "transformer_pointer_generator_wmt_en_de_big_t2t")def transformer_pointer_generator_wmt_en_de_big_t2t(args): args.encoder_normalize_before = getattr(args, "encoder_normalize_before", True) args.decoder_normalize_before = getattr(args, "decoder_normalize_before", True) args.attention_dropout = getattr(args, "attention_dropout", 0.1) args.activation_dropout = getattr(args, "activation_dropout", 0.1) transformer_pointer_generator_vaswani_wmt_en_de_big(args)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596#!/usr/bin/env 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 reimport sys
class OOVIndexError(IndexError): def __init__(self, pos, source_seq, target_seq): super(OOVIndexError, self).__init__( "A <unk-N> tag in the target sequence refers to a position that is " "outside the source sequence. Most likely there was a mismatch in " "provided source and target sequences. Otherwise this would mean that " "the pointing mechanism somehow attended to a position that is past " "the actual sequence end." ) self.source_pos = pos self.source_seq = source_seq self.target_seq = target_seq
def replace_oovs(source_in, target_in, target_out): """Replaces <unk-N> tokens in the target text with the corresponding word in the source text. """
oov_re = re.compile("^<unk-([0-9]+)>$")
for source_seq, target_seq in zip(source_in, target_in): target_seq_out = []
pos_to_word = source_seq.strip().split() for token in target_seq.strip().split(): m = oov_re.match(token) if m: pos = int(m.group(1)) if pos >= len(pos_to_word): raise OOVIndexError(pos, source_seq, target_seq) token_out = pos_to_word[pos] else: token_out = token target_seq_out.append(token_out) target_out.write(" ".join(target_seq_out) + "\n")
def main(): parser = argparse.ArgumentParser( description="Replaces <unk-N> tokens in target sequences with words from " "the corresponding position in the source sequence." ) parser.add_argument( "--source", type=str, help="text file with source sequences", required=True ) parser.add_argument( "--target", type=str, help="text file with target sequences", required=True ) parser.add_argument( "--target-out", type=str, help="where to write target sequences without <unk-N> " "entries", required=True, ) args = parser.parse_args()
target_in = ( open(args.target, "r", encoding="utf-8") if args.target is not None else None ) target_out = ( open(args.target_out, "w", encoding="utf-8") if args.target_out is not None else None ) with open(args.source, "r", encoding="utf-8") as source_in, open( args.target, "r", encoding="utf-8" ) as target_in, open(args.target_out, "w", encoding="utf-8") as target_out: replace_oovs(source_in, target_in, target_out)
if __name__ == "__main__": try: main() except OOVIndexError as e: print(e, file=sys.stderr) print("Source sequence:", e.source_seq.strip(), file=sys.stderr) print("Target sequence:", e.target_seq.strip(), file=sys.stderr) print( "Source sequence length:", len(e.source_seq.strip().split()), file=sys.stderr, ) print("The offending tag points to:", e.source_pos) sys.exit(2)123456789101112131415161718192021222324252627282930313233# 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.
# This file defines example configuration arguments for quantizing# a transformer model with product quantization
# Number of Centroids for Product Quantization, by default 256 (byte-aligned)n_centroids: Linear: key: in_features value: {"*": 256} Embedding: key: embedding_dim value: {"*": 256}
# Block Sizes for Product Quantization# We suggest: 8 for FFN, 4 for ATTN, 4 for embedding projections, 8 for embeddingsblock_sizes: Linear: key: fuzzy_name value: {fc: 8, attn: 4, emb: 4} Embedding: key: fuzzy_name value: {emb: 8}
# Layers to Quantize Sequentially# We suggest: first FFN, then EMB, then ATTNlayers_to_quantize: - decoder\\.layers\\.\d+\\.fc[12] - decoder\\.embed_tokens\\.embeddings\\.[012]\\.[01] - decoder\\.layers\\.\d+\\.self_attn\\.(k_proj|v_proj|q_proj|out_proj)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168# Finetuning RoBERTa on a custom classification task
This example shows how to finetune RoBERTa on the IMDB dataset, but should illustrate the process for most classification tasks.
### 1) Get the data
```bashwget http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gztar zxvf aclImdb_v1.tar.gz```
### 2) Format data
`IMDB` data has one data-sample in each file, below python code-snippet converts it one file for train and valid each for ease of processing. ```pythonimport argparseimport osimport randomfrom glob import glob
random.seed(0)
def main(args): for split in ['train', 'test']: samples = [] for class_label in ['pos', 'neg']: fnames = glob(os.path.join(args.datadir, split, class_label) + '/*.txt') for fname in fnames: with open(fname) as fin: line = fin.readline() samples.append((line, 1 if class_label == 'pos' else 0)) random.shuffle(samples) out_fname = 'train' if split == 'train' else 'dev' f1 = open(os.path.join(args.datadir, out_fname + '.input0'), 'w') f2 = open(os.path.join(args.datadir, out_fname + '.label'), 'w') for sample in samples: f1.write(sample[0] + '\n') f2.write(str(sample[1]) + '\n') f1.close() f2.close()
if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--datadir', default='aclImdb') args = parser.parse_args() main(args)```
### 3) BPE encode
Run `multiprocessing_bpe_encoder`, you can also do this in previous step for each sample but that might be slower.```bash# Download encoder.json and vocab.bpewget -N 'https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/encoder.json'wget -N 'https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/vocab.bpe'
for SPLIT in train dev; do python -m examples.roberta.multiprocessing_bpe_encoder \ --encoder-json encoder.json \ --vocab-bpe vocab.bpe \ --inputs "aclImdb/$SPLIT.input0" \ --outputs "aclImdb/$SPLIT.input0.bpe" \ --workers 60 \ --keep-emptydone```
### 4) Preprocess data
```bash# Download fairseq dictionary.wget -N 'https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/dict.txt'
fairseq-preprocess \ --only-source \ --trainpref "aclImdb/train.input0.bpe" \ --validpref "aclImdb/dev.input0.bpe" \ --destdir "IMDB-bin/input0" \ --workers 60 \ --srcdict dict.txt
fairseq-preprocess \ --only-source \ --trainpref "aclImdb/train.label" \ --validpref "aclImdb/dev.label" \ --destdir "IMDB-bin/label" \ --workers 60
```
### 5) Run training
```bashTOTAL_NUM_UPDATES=7812 # 10 epochs through IMDB for bsz 32WARMUP_UPDATES=469 # 6 percent of the number of updatesLR=1e-05 # Peak LR for polynomial LR scheduler.HEAD_NAME=imdb_head # Custom name for the classification head.NUM_CLASSES=2 # Number of classes for the classification task.MAX_SENTENCES=8 # Batch size.ROBERTA_PATH=/path/to/roberta.large/model.pt
CUDA_VISIBLE_DEVICES=0 fairseq-train IMDB-bin/ \ --restore-file $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 \ --classification-head-name $HEAD_NAME \ --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 \ --best-checkpoint-metric accuracy --maximize-best-checkpoint-metric \ --shorten-method "truncate" \ --find-unused-parameters \ --update-freq 4```
The above command will finetune RoBERTa-large with an effective batch-size of 32sentences (`--batch-size=8 --update-freq=4`). The expected`best-validation-accuracy` after 10 epochs is ~96.5%.
If you run out of GPU memory, try decreasing `--batch-size` and increase`--update-freq` to compensate.
### 6) Load model using hub interface
Now we can load the trained model checkpoint using the RoBERTa hub interface.
Assuming your checkpoints are stored in `checkpoints/`:```pythonfrom fairseq.models.roberta import RobertaModelroberta = RobertaModel.from_pretrained( 'checkpoints', checkpoint_file='checkpoint_best.pt', data_name_or_path='IMDB-bin')roberta.eval() # disable dropout```
Finally you can make predictions using the `imdb_head` (or whatever you set`--classification-head-name` to during training):```pythonlabel_fn = lambda label: roberta.task.label_dictionary.string( [label + roberta.task.label_dictionary.nspecial])
tokens = roberta.encode('Best movie this year')pred = label_fn(roberta.predict('imdb_head', tokens).argmax().item())assert pred == '1' # positive
tokens = roberta.encode('Worst movie ever')pred = label_fn(roberta.predict('imdb_head', tokens).argmax().item())assert pred == '0' # negative```12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364# Finetuning RoBERTa on GLUE tasks
### 1) Download the data from GLUE website (https://gluebenchmark.com/tasks) using following commands:```bashwget https://gist.githubusercontent.com/W4ngatang/60c2bdb54d156a41194446737ce03e2e/raw/17b8dd0d724281ed7c3b2aeeda662b92809aadd5/download_glue_data.pypython download_glue_data.py --data_dir glue_data --tasks all```
### 2) Preprocess GLUE task data:```bash./examples/roberta/preprocess_GLUE_tasks.sh glue_data <glue_task_name>````glue_task_name` is one of the following:`{ALL, QQP, MNLI, QNLI, MRPC, RTE, STS-B, SST-2, CoLA}`Use `ALL` for preprocessing all the glue tasks.
### 3) Fine-tuning on GLUE task:Example fine-tuning cmd for `RTE` task```bashROBERTA_PATH=/path/to/roberta/model.pt
CUDA_VISIBLE_DEVICES=0 fairseq-hydra-train -config-dir examples/roberta/config/finetuning --config-name rte \task.data=RTE-bin checkpoint.restore_file=$ROBERTA_PATH```
There are additional config files for each of the GLUE tasks in the examples/roberta/config/finetuning directory.
**Note:**
a) Above cmd-args and hyperparams are tested on one Nvidia `V100` GPU with `32gb` of memory for each task. Depending on the GPU memory resources available to you, you can use increase `--update-freq` and reduce `--batch-size`.
b) All the settings in above table are suggested settings based on our hyperparam search within a fixed search space (for careful comparison across models). You might be able to find better metrics with wider hyperparam search.
### Inference on GLUE taskAfter training the model as mentioned in previous step, you can perform inference with checkpoints in `checkpoints/` directory using following python code snippet:
```pythonfrom fairseq.models.roberta import RobertaModel
roberta = RobertaModel.from_pretrained( 'checkpoints/', checkpoint_file='checkpoint_best.pt', data_name_or_path='RTE-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('glue_data/RTE/dev.tsv') as fin: fin.readline() for index, line in enumerate(fin): tokens = line.strip().split('\t') sent1, sent2, target = tokens[1], tokens[2], tokens[3] 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))
```123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296# RoBERTa: A Robustly Optimized BERT Pretraining Approach
https://arxiv.org/abs/1907.11692
## Introduction
RoBERTa iterates on BERT's pretraining procedure, including training the model longer, with bigger batches over more data; removing the next sentence prediction objective; training on longer sequences; and dynamically changing the masking pattern applied to the training data. See the associated paper for more details.
### What's New:
- December 2020: German model (GottBERT) is available: [GottBERT](https://github.com/pytorch/fairseq/tree/main/examples/gottbert).- January 2020: Italian model (UmBERTo) is available from Musixmatch Research: [UmBERTo](https://github.com/musixmatchresearch/umberto).- November 2019: French model (CamemBERT) is available: [CamemBERT](https://github.com/pytorch/fairseq/tree/main/examples/camembert).- November 2019: Multilingual encoder (XLM-RoBERTa) is available: [XLM-R](https://github.com/pytorch/fairseq/tree/main/examples/xlmr).- September 2019: TensorFlow and TPU support via the [transformers library](https://github.com/huggingface/transformers).- August 2019: RoBERTa is now supported in the [pytorch-transformers library](https://github.com/huggingface/pytorch-transformers).- August 2019: Added [tutorial for finetuning on WinoGrande](https://github.com/pytorch/fairseq/tree/main/examples/roberta/wsc#roberta-training-on-winogrande-dataset).- August 2019: Added [tutorial for pretraining RoBERTa using your own data](README.pretraining.md).
## Pre-trained models
Model | Description | # params | Download---|---|---|---`roberta.base` | RoBERTa using the BERT-base architecture | 125M | [roberta.base.tar.gz](https://dl.fbaipublicfiles.com/fairseq/models/roberta.base.tar.gz)`roberta.large` | RoBERTa using the BERT-large architecture | 355M | [roberta.large.tar.gz](https://dl.fbaipublicfiles.com/fairseq/models/roberta.large.tar.gz)`roberta.large.mnli` | `roberta.large` finetuned on [MNLI](http://www.nyu.edu/projects/bowman/multinli) | 355M | [roberta.large.mnli.tar.gz](https://dl.fbaipublicfiles.com/fairseq/models/roberta.large.mnli.tar.gz)`roberta.large.wsc` | `roberta.large` finetuned on [WSC](wsc/README.md) | 355M | [roberta.large.wsc.tar.gz](https://dl.fbaipublicfiles.com/fairseq/models/roberta.large.wsc.tar.gz)
## Results
**[GLUE (Wang et al., 2019)](https://gluebenchmark.com/)**_(dev set, single model, single-task finetuning)_
Model | MNLI | QNLI | QQP | RTE | SST-2 | MRPC | CoLA | STS-B---|---|---|---|---|---|---|---|---`roberta.base` | 87.6 | 92.8 | 91.9 | 78.7 | 94.8 | 90.2 | 63.6 | 91.2`roberta.large` | 90.2 | 94.7 | 92.2 | 86.6 | 96.4 | 90.9 | 68.0 | 92.4`roberta.large.mnli` | 90.2 | - | - | - | - | - | - | -
**[SuperGLUE (Wang et al., 2019)](https://super.gluebenchmark.com/)**_(dev set, single model, single-task finetuning)_
Model | BoolQ | CB | COPA | MultiRC | RTE | WiC | WSC---|---|---|---|---|---|---|---`roberta.large` | 86.9 | 98.2 | 94.0 | 85.7 | 89.5 | 75.6 | -`roberta.large.wsc` | - | - | - | - | - | - | 91.3
**[SQuAD (Rajpurkar et al., 2018)](https://rajpurkar.github.io/SQuAD-explorer/)**_(dev set, no additional data used)_
Model | SQuAD 1.1 EM/F1 | SQuAD 2.0 EM/F1---|---|---`roberta.large` | 88.9/94.6 | 86.5/89.4
**[RACE (Lai et al., 2017)](http://www.qizhexie.com/data/RACE_leaderboard.html)**_(test set)_
Model | Accuracy | Middle | High---|---|---|---`roberta.large` | 83.2 | 86.5 | 81.3
**[HellaSwag (Zellers et al., 2019)](https://rowanzellers.com/hellaswag/)**_(test set)_
Model | Overall | In-domain | Zero-shot | ActivityNet | WikiHow---|---|---|---|---|---`roberta.large` | 85.2 | 87.3 | 83.1 | 74.6 | 90.9
**[Commonsense QA (Talmor et al., 2019)](https://www.tau-nlp.org/commonsenseqa)**_(test set)_
Model | Accuracy---|---`roberta.large` (single model) | 72.1`roberta.large` (ensemble) | 72.5
**[Winogrande (Sakaguchi et al., 2019)](https://arxiv.org/abs/1907.10641)**_(test set)_
Model | Accuracy---|---`roberta.large` | 78.1
**[XNLI (Conneau et al., 2018)](https://arxiv.org/abs/1809.05053)**_(TRANSLATE-TEST)_
Model | en | fr | es | de | el | bg | ru | tr | ar | vi | th | zh | hi | sw | ur---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---`roberta.large.mnli` | 91.3 | 82.91 | 84.27 | 81.24 | 81.74 | 83.13 | 78.28 | 76.79 | 76.64 | 74.17 | 74.05 | 77.5 | 70.9 | 66.65 | 66.81
## Example usage
##### Load RoBERTa from torch.hub (PyTorch >= 1.1):```pythonimport torchroberta = torch.hub.load('pytorch/fairseq', 'roberta.large')roberta.eval() # disable dropout (or leave in train mode to finetune)```
##### Load RoBERTa (for PyTorch 1.0 or custom models):```python# Download roberta.large modelwget https://dl.fbaipublicfiles.com/fairseq/models/roberta.large.tar.gztar -xzvf roberta.large.tar.gz
# Load the model in fairseqfrom fairseq.models.roberta import RobertaModelroberta = RobertaModel.from_pretrained('/path/to/roberta.large', checkpoint_file='model.pt')roberta.eval() # disable dropout (or leave in train mode to finetune)```
##### Apply Byte-Pair Encoding (BPE) to input text:```pythontokens = roberta.encode('Hello world!')assert tokens.tolist() == [0, 31414, 232, 328, 2]roberta.decode(tokens) # 'Hello world!'```
##### Extract features from RoBERTa:```python# Extract the last layer's featureslast_layer_features = roberta.extract_features(tokens)assert last_layer_features.size() == torch.Size([1, 5, 1024])
# Extract all layer's features (layer 0 is the embedding layer)all_layers = roberta.extract_features(tokens, return_all_hiddens=True)assert len(all_layers) == 25assert torch.all(all_layers[-1] == last_layer_features)```
##### Use RoBERTa for sentence-pair classification tasks:```python# Download RoBERTa already finetuned for MNLIroberta = torch.hub.load('pytorch/fairseq', 'roberta.large.mnli')roberta.eval() # disable dropout for evaluation
# Encode a pair of sentences and make a predictiontokens = roberta.encode('Roberta is a heavily optimized version of BERT.', 'Roberta is not very optimized.')roberta.predict('mnli', tokens).argmax() # 0: contradiction
# Encode another pair of sentencestokens = roberta.encode('Roberta is a heavily optimized version of BERT.', 'Roberta is based on BERT.')roberta.predict('mnli', tokens).argmax() # 2: entailment```
##### Register a new (randomly initialized) classification head:```pythonroberta.register_classification_head('new_task', num_classes=3)logprobs = roberta.predict('new_task', tokens) # tensor([[-1.1050, -1.0672, -1.1245]], grad_fn=<LogSoftmaxBackward>)```
##### Batched prediction:```pythonimport torchfrom fairseq.data.data_utils import collate_tokens
roberta = torch.hub.load('pytorch/fairseq', 'roberta.large.mnli')roberta.eval()
batch_of_pairs = [ ['Roberta is a heavily optimized version of BERT.', 'Roberta is not very optimized.'], ['Roberta is a heavily optimized version of BERT.', 'Roberta is based on BERT.'], ['potatoes are awesome.', 'I like to run.'], ['Mars is very far from earth.', 'Mars is very close.'],]
batch = collate_tokens( [roberta.encode(pair[0], pair[1]) for pair in batch_of_pairs], pad_idx=1)
logprobs = roberta.predict('mnli', batch)print(logprobs.argmax(dim=1))# tensor([0, 2, 1, 0])```
##### Using the GPU:```pythonroberta.cuda()roberta.predict('new_task', tokens) # tensor([[-1.1050, -1.0672, -1.1245]], device='cuda:0', grad_fn=<LogSoftmaxBackward>)```
## Advanced usage
#### Filling masks:
RoBERTa can be used to fill `<mask>` tokens in the input. Some examples from the[Natural Questions dataset](https://ai.google.com/research/NaturalQuestions/):```pythonroberta.fill_mask('The first Star wars movie came out in <mask>', topk=3)# [('The first Star wars movie came out in 1977', 0.9504708051681519, ' 1977'), ('The first Star wars movie came out in 1978', 0.009986862540245056, ' 1978'), ('The first Star wars movie came out in 1979', 0.009574787691235542, ' 1979')]
roberta.fill_mask('Vikram samvat calender is official in <mask>', topk=3)# [('Vikram samvat calender is official in India', 0.21878819167613983, ' India'), ('Vikram samvat calender is official in Delhi', 0.08547237515449524, ' Delhi'), ('Vikram samvat calender is official in Gujarat', 0.07556215673685074, ' Gujarat')]
roberta.fill_mask('<mask> is the common currency of the European Union', topk=3)# [('Euro is the common currency of the European Union', 0.9456493854522705, 'Euro'), ('euro is the common currency of the European Union', 0.025748178362846375, 'euro'), ('€ is the common currency of the European Union', 0.011183084920048714, '€')]```
#### Pronoun disambiguation (Winograd Schema Challenge):
RoBERTa can be used to disambiguate pronouns. First install spaCy and download the English-language model:```bashpip install spacypython -m spacy download en_core_web_lg```
Next load the `roberta.large.wsc` model and call the `disambiguate_pronoun`function. The pronoun should be surrounded by square brackets (`[]`) and thequery referent surrounded by underscores (`_`), or left blank to return thepredicted candidate text directly:```pythonroberta = torch.hub.load('pytorch/fairseq', 'roberta.large.wsc', user_dir='examples/roberta/wsc')roberta.cuda() # use the GPU (optional)
roberta.disambiguate_pronoun('The _trophy_ would not fit in the brown suitcase because [it] was too big.')# Trueroberta.disambiguate_pronoun('The trophy would not fit in the brown _suitcase_ because [it] was too big.')# False
roberta.disambiguate_pronoun('The city councilmen refused the demonstrators a permit because [they] feared violence.')# 'The city councilmen'roberta.disambiguate_pronoun('The city councilmen refused the demonstrators a permit because [they] advocated violence.')# 'demonstrators'```
See the [RoBERTA Winograd Schema Challenge (WSC) README](wsc/README.md) for more details on how to train this model.
#### Extract features aligned to words:
By default RoBERTa outputs one feature vector per BPE token. You can insteadrealign the features to match [spaCy's word-level tokenization](https://spacy.io/usage/linguistic-features#tokenization)with the `extract_features_aligned_to_words` method. This will compute aweighted average of the BPE-level features for each word and expose them inspaCy's `Token.vector` attribute:```pythondoc = roberta.extract_features_aligned_to_words('I said, "hello RoBERTa."')assert len(doc) == 10for tok in doc: print('{:10}{} (...)'.format(str(tok), tok.vector[:5]))# <s> tensor([-0.1316, -0.0386, -0.0832, -0.0477, 0.1943], grad_fn=<SliceBackward>) (...)# I tensor([ 0.0559, 0.1541, -0.4832, 0.0880, 0.0120], grad_fn=<SliceBackward>) (...)# said tensor([-0.1565, -0.0069, -0.8915, 0.0501, -0.0647], grad_fn=<SliceBackward>) (...)# , tensor([-0.1318, -0.0387, -0.0834, -0.0477, 0.1944], grad_fn=<SliceBackward>) (...)# " tensor([-0.0486, 0.1818, -0.3946, -0.0553, 0.0981], grad_fn=<SliceBackward>) (...)# hello tensor([ 0.0079, 0.1799, -0.6204, -0.0777, -0.0923], grad_fn=<SliceBackward>) (...)# RoBERTa tensor([-0.2339, -0.1184, -0.7343, -0.0492, 0.5829], grad_fn=<SliceBackward>) (...)# . tensor([-0.1341, -0.1203, -0.1012, -0.0621, 0.1892], grad_fn=<SliceBackward>) (...)# " tensor([-0.1341, -0.1203, -0.1012, -0.0621, 0.1892], grad_fn=<SliceBackward>) (...)# </s> tensor([-0.0930, -0.0392, -0.0821, 0.0158, 0.0649], grad_fn=<SliceBackward>) (...)```
#### Evaluating the `roberta.large.mnli` model:
Example python code snippet to evaluate accuracy on the MNLI `dev_matched` set.```pythonlabel_map = {0: 'contradiction', 1: 'neutral', 2: 'entailment'}ncorrect, nsamples = 0, 0roberta.cuda()roberta.eval()with open('glue_data/MNLI/dev_matched.tsv') as fin: fin.readline() for index, line in enumerate(fin): tokens = line.strip().split('\t') sent1, sent2, target = tokens[8], tokens[9], tokens[-1] tokens = roberta.encode(sent1, sent2) prediction = roberta.predict('mnli', tokens).argmax().item() prediction_label = label_map[prediction] ncorrect += int(prediction_label == target) nsamples += 1print('| Accuracy: ', float(ncorrect)/float(nsamples))# Expected output: 0.9060```
## Finetuning
- [Finetuning on GLUE](README.glue.md)- [Finetuning on custom classification tasks (e.g., IMDB)](README.custom_classification.md)- [Finetuning on Winograd Schema Challenge (WSC)](wsc/README.md)- [Finetuning on Commonsense QA (CQA)](commonsense_qa/README.md)
## Pretraining using your own data
See the [tutorial for pretraining RoBERTa using your own data](README.pretraining.md).
## Citation
```bibtex@article{liu2019roberta, title = {RoBERTa: A Robustly Optimized BERT Pretraining Approach}, author = {Yinhan Liu and Myle Ott and Naman Goyal and Jingfei Du and Mandar Joshi and Danqi Chen and Omer Levy and Mike Lewis and Luke Zettlemoyer and Veselin Stoyanov}, journal={arXiv preprint arXiv:1907.11692}, year = {2019},}```123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384# Pretraining RoBERTa using your own data
This tutorial will walk you through pretraining RoBERTa over your own data.
### 1) Preprocess the data
Data should be preprocessed following the [language modeling format](/examples/language_model), i.e. each document should be separated by an empty line (only useful with `--sample-break-mode complete_doc`). Lines will be concatenated as a 1D text stream during training.
We'll use the [WikiText-103 dataset](https://www.salesforce.com/products/einstein/ai-research/the-wikitext-dependency-language-modeling-dataset/)to demonstrate how to preprocess raw text data with the GPT-2 BPE. Of coursethis dataset is quite small, so the resulting pretrained model will performpoorly, but it gives the general idea.
First download the dataset:```bashwget https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-103-raw-v1.zipunzip wikitext-103-raw-v1.zip```
Next encode it with the GPT-2 BPE:```bashmkdir -p gpt2_bpewget -O gpt2_bpe/encoder.json https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/encoder.jsonwget -O gpt2_bpe/vocab.bpe https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/vocab.bpefor SPLIT in train valid test; do \ python -m examples.roberta.multiprocessing_bpe_encoder \ --encoder-json gpt2_bpe/encoder.json \ --vocab-bpe gpt2_bpe/vocab.bpe \ --inputs wikitext-103-raw/wiki.${SPLIT}.raw \ --outputs wikitext-103-raw/wiki.${SPLIT}.bpe \ --keep-empty \ --workers 60; \done```
Finally preprocess/binarize the data using the GPT-2 fairseq dictionary:```bashwget -O gpt2_bpe/dict.txt https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/dict.txtfairseq-preprocess \ --only-source \ --srcdict gpt2_bpe/dict.txt \ --trainpref wikitext-103-raw/wiki.train.bpe \ --validpref wikitext-103-raw/wiki.valid.bpe \ --testpref wikitext-103-raw/wiki.test.bpe \ --destdir data-bin/wikitext-103 \ --workers 60```
### 2) Train RoBERTa base```bashDATA_DIR=data-bin/wikitext-103
fairseq-hydra-train -m --config-dir examples/roberta/config/pretraining \--config-name base task.data=$DATA_DIR```
**Note:** You can optionally resume training the released RoBERTa base model byadding `checkpoint.restore_file=/path/to/roberta.base/model.pt`.
**Note:** The above command assumes training on 8x32GB V100 GPUs. Each GPU usesa batch size of 16 sequences (`dataset.batch_size`) and accumulates gradients tofurther increase the batch size by 16x (`optimization.update_freq`), for a total batch sizeof 2048 sequences. If you have fewer GPUs or GPUs with less memory you may needto reduce `dataset.batch_size` and increase dataset.update_freq to compensate.Alternatively if you have more GPUs you can decrease `dataset.update_freq` accordinglyto increase training speed.
**Note:** The learning rate and batch size are tightly connected and need to beadjusted together. We generally recommend increasing the learning rate as youincrease the batch size according to the following table (although it's alsodataset dependent, so don't rely on the following values too closely):
batch size | peak learning rate---|---256 | 0.00012048 | 0.00058192 | 0.0007
### 3) Load your pretrained model```pythonfrom fairseq.models.roberta import RobertaModelroberta = RobertaModel.from_pretrained('checkpoints', 'checkpoint_best.pt', 'path/to/data')assert isinstance(roberta.model, torch.nn.Module)```1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768# Finetuning RoBERTa on RACE tasks
### 1) Download the data from RACE website (http://www.cs.cmu.edu/~glai1/data/race/)
### 2) Preprocess RACE data:```bashpython ./examples/roberta/preprocess_RACE.py --input-dir <input-dir> --output-dir <extracted-data-dir>./examples/roberta/preprocess_RACE.sh <extracted-data-dir> <output-dir>```
### 3) Fine-tuning on RACE:
```bashMAX_EPOCH=5 # Number of training epochs.LR=1e-05 # Peak LR for fixed LR scheduler.NUM_CLASSES=4MAX_SENTENCES=1 # Batch size per GPU.UPDATE_FREQ=8 # Accumulate gradients to simulate training on 8 GPUs.DATA_DIR=/path/to/race-output-dirROBERTA_PATH=/path/to/roberta/model.pt
CUDA_VISIBLE_DEVICES=0,1 fairseq-train $DATA_DIR --ddp-backend=legacy_ddp \ --restore-file $ROBERTA_PATH \ --reset-optimizer --reset-dataloader --reset-meters \ --best-checkpoint-metric accuracy --maximize-best-checkpoint-metric \ --task sentence_ranking \ --num-classes $NUM_CLASSES \ --init-token 0 --separator-token 2 \ --max-option-length 128 \ --max-positions 512 \ --shorten-method "truncate" \ --arch roberta_large \ --dropout 0.1 --attention-dropout 0.1 --weight-decay 0.01 \ --criterion sentence_ranking \ --optimizer adam --adam-betas '(0.9, 0.98)' --adam-eps 1e-06 \ --clip-norm 0.0 \ --lr-scheduler fixed --lr $LR \ --fp16 --fp16-init-scale 4 --threshold-loss-scale 1 --fp16-scale-window 128 \ --batch-size $MAX_SENTENCES \ --required-batch-size-multiple 1 \ --update-freq $UPDATE_FREQ \ --max-epoch $MAX_EPOCH```
**Note:**
a) As contexts in RACE are relatively long, we are using smaller batch size per GPU while increasing update-freq to achieve larger effective batch size.
b) Above cmd-args and hyperparams are tested on one Nvidia `V100` GPU with `32gb` of memory for each task. Depending on the GPU memory resources available to you, you can use increase `--update-freq` and reduce `--batch-size`.
c) The setting in above command is based on our hyperparam search within a fixed search space (for careful comparison across models). You might be able to find better metrics with wider hyperparam search.
### 4) Evaluation:
```DATA_DIR=/path/to/race-output-dir # data directory used during trainingMODEL_PATH=/path/to/checkpoint_best.pt # path to the finetuned model checkpointPREDS_OUT=preds.tsv # output file path to save predictionTEST_SPLIT=test # can be test (Middle) or test1 (High)fairseq-validate \ $DATA_DIR \ --valid-subset $TEST_SPLIT \ --path $MODEL_PATH \ --batch-size 1 \ --task sentence_ranking \ --criterion sentence_ranking \ --save-predictions $PREDS_OUT```123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899# Finetuning RoBERTa on Commonsense QA
We follow a similar approach to [finetuning RACE](../README.race.md). Specificallyfor each question we construct five inputs, one for each of the five candidateanswer choices. Each input is constructed by concatenating the question andcandidate answer. We then encode each input and pass the resulting "[CLS]"representations through a fully-connected layer to predict the correct answer.We train with a standard cross-entropy loss.
We also found it helpful to prepend a prefix of `Q:` to the question and `A:` tothe answer. The complete input format is:```<s> Q: Where would I not want a fox? </s> A: hen house </s>```
Our final submission is based on a hyperparameter search over the learning rate(1e-5, 2e-5, 3e-5), batch size (8, 16), number of training steps (2000, 3000,4000) and random seed. We selected the model with the best performance on thedevelopment set after 100 trials.
### 1) Download data from the Commonsense QA website (https://www.tau-nlp.org/commonsenseqa)```bashbash examples/roberta/commonsense_qa/download_cqa_data.sh```
### 2) Finetune
```bashMAX_UPDATES=3000 # Number of training steps.WARMUP_UPDATES=150 # Linearly increase LR over this many steps.LR=1e-05 # Peak LR for polynomial LR scheduler.MAX_SENTENCES=16 # Batch size.SEED=1 # Random seed.ROBERTA_PATH=/path/to/roberta/model.ptDATA_DIR=data/CommonsenseQA
# we use the --user-dir option to load the task from# the examples/roberta/commonsense_qa directory:FAIRSEQ_PATH=/path/to/fairseqFAIRSEQ_USER_DIR=${FAIRSEQ_PATH}/examples/roberta/commonsense_qa
CUDA_VISIBLE_DEVICES=0 fairseq-train --fp16 --ddp-backend=legacy_ddp \ $DATA_DIR \ --user-dir $FAIRSEQ_USER_DIR \ --restore-file $ROBERTA_PATH \ --reset-optimizer --reset-dataloader --reset-meters \ --no-epoch-checkpoints --no-last-checkpoints --no-save-optimizer-state \ --best-checkpoint-metric accuracy --maximize-best-checkpoint-metric \ --task commonsense_qa --init-token 0 --bpe gpt2 \ --arch roberta_large --max-positions 512 \ --dropout 0.1 --attention-dropout 0.1 --weight-decay 0.01 \ --criterion sentence_ranking --num-classes 5 \ --optimizer adam --adam-betas '(0.9, 0.98)' --adam-eps 1e-06 --clip-norm 0.0 \ --lr-scheduler polynomial_decay --lr $LR \ --warmup-updates $WARMUP_UPDATES --total-num-update $MAX_UPDATES \ --batch-size $MAX_SENTENCES \ --max-update $MAX_UPDATES \ --log-format simple --log-interval 25 \ --seed $SEED```
The above command assumes training on 1 GPU with 32GB of RAM. For GPUs withless memory, decrease `--batch-size` and increase `--update-freq`accordingly to compensate.
### 3) Evaluate```pythonimport jsonimport torchfrom fairseq.models.roberta import RobertaModelfrom examples.roberta import commonsense_qa # load the Commonsense QA taskroberta = RobertaModel.from_pretrained('checkpoints', 'checkpoint_best.pt', 'data/CommonsenseQA')roberta.eval() # disable dropoutroberta.cuda() # use the GPU (optional)nsamples, ncorrect = 0, 0with open('data/CommonsenseQA/valid.jsonl') as h: for line in h: example = json.loads(line) scores = [] for choice in example['question']['choices']: input = roberta.encode( 'Q: ' + example['question']['stem'], 'A: ' + choice['text'], no_separator=True ) score = roberta.predict('sentence_classification_head', input, return_logits=True) scores.append(score) pred = torch.cat(scores).argmax() answer = ord(example['answerKey']) - ord('A') nsamples += 1 if pred == answer: ncorrect += 1
print('Accuracy: ' + str(ncorrect / float(nsamples)))# Accuracy: 0.7846027846027847```
The above snippet is not batched, which makes it quite slow. See [instructionsfor batched prediction with RoBERTa](https://github.com/pytorch/fairseq/tree/main/examples/roberta#batched-prediction).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 commonsense_qa_task # noqa123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190# 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 jsonimport os
import numpy as npimport torchfrom fairseq.data import ( Dictionary, IdDataset, ListDataset, NestedDictionaryDataset, NumelDataset, NumSamplesDataset, RawLabelDataset, RightPadDataset, SortDataset, data_utils, encoders,)from fairseq.tasks import LegacyFairseqTask, register_task
@register_task("commonsense_qa")class CommonsenseQATask(LegacyFairseqTask): """Task to finetune RoBERTa for Commonsense QA."""
@staticmethod def add_args(parser): """Add task-specific arguments to the parser.""" parser.add_argument( "data", metavar="DIR", help="path to data directory; we load <split>.jsonl" ) parser.add_argument( "--init-token", type=int, default=None, help="add token at the beginning of each batch item", ) parser.add_argument("--num-classes", type=int, default=5)
def __init__(self, args, vocab): super().__init__(args) self.vocab = vocab self.mask = vocab.add_symbol("<mask>")
self.bpe = encoders.build_bpe(args)
@classmethod def load_dictionary(cls, filename): """Load the dictionary from the filename
Args: filename (str): the filename """ dictionary = Dictionary.load(filename) dictionary.add_symbol("<mask>") return dictionary
@classmethod def setup_task(cls, args, **kwargs): assert ( args.criterion == "sentence_ranking" ), "Must set --criterion=sentence_ranking"
# load data and label dictionaries vocab = cls.load_dictionary(os.path.join(args.data, "dict.txt")) print("| dictionary: {} types".format(len(vocab)))
return cls(args, vocab)
def load_dataset( self, split, epoch=1, combine=False, data_path=None, return_only=False, **kwargs ): """Load a given dataset split.
Args: split (str): name of the split (e.g., train, valid, test) """
def binarize(s, append_bos=False): if self.bpe is not None: s = self.bpe.encode(s) tokens = self.vocab.encode_line( s, append_eos=True, add_if_not_exist=False, ).long() if append_bos and self.args.init_token is not None: tokens = torch.cat([tokens.new([self.args.init_token]), tokens]) return tokens
if data_path is None: data_path = os.path.join(self.args.data, split + ".jsonl") if not os.path.exists(data_path): raise FileNotFoundError("Cannot find data: {}".format(data_path))
src_tokens = [[] for i in range(self.args.num_classes)] src_lengths = [[] for i in range(self.args.num_classes)] labels = []
with open(data_path) as h: for line in h: example = json.loads(line.strip()) if "answerKey" in example: label = ord(example["answerKey"]) - ord("A") labels.append(label) question = example["question"]["stem"] assert len(example["question"]["choices"]) == self.args.num_classes # format: `<s> Q: Where would I not want a fox? </s> A: hen house </s>` question = "Q: " + question question_toks = binarize(question, append_bos=True) for i, choice in enumerate(example["question"]["choices"]): src = "A: " + choice["text"] src_bin = torch.cat([question_toks, binarize(src)]) src_tokens[i].append(src_bin) src_lengths[i].append(len(src_bin)) assert all( len(src_tokens[0]) == len(src_tokens[i]) for i in range(self.args.num_classes) ) assert len(src_tokens[0]) == len(src_lengths[0]) assert len(labels) == 0 or len(labels) == len(src_tokens[0])
for i in range(self.args.num_classes): src_lengths[i] = np.array(src_lengths[i]) src_tokens[i] = ListDataset(src_tokens[i], src_lengths[i]) src_lengths[i] = ListDataset(src_lengths[i])
dataset = { "id": IdDataset(), "nsentences": NumSamplesDataset(), "ntokens": NumelDataset(src_tokens[0], reduce=True), }
for i in range(self.args.num_classes): dataset.update( { "net_input{}".format(i + 1): { "src_tokens": RightPadDataset( src_tokens[i], pad_idx=self.source_dictionary.pad(), ), "src_lengths": src_lengths[i], } } )
if len(labels) > 0: dataset.update({"target": RawLabelDataset(labels)})
dataset = NestedDictionaryDataset( dataset, sizes=[np.maximum.reduce([src_token.sizes for src_token in src_tokens])], )
with data_utils.numpy_seed(self.args.seed): dataset = SortDataset( dataset, # shuffle sort_order=[np.random.permutation(len(dataset))], )
print("| Loaded {} with {} samples".format(split, len(dataset)))
self.datasets[split] = dataset return self.datasets[split]
def build_model(self, args, from_checkpoint=False): from fairseq import models
model = models.build_model(args, self)
model.register_classification_head( "sentence_classification_head", num_classes=1, )
return model
@property def source_dictionary(self): return self.vocab
@property def target_dictionary(self): return self.vocab1234567891011121314#!/bin/bash# 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.
OUTDIR=data/CommonsenseQA
mkdir -p $OUTDIR
wget -O $OUTDIR/train.jsonl https://s3.amazonaws.com/commensenseqa/train_rand_split.jsonlwget -O $OUTDIR/valid.jsonl https://s3.amazonaws.com/commensenseqa/dev_rand_split.jsonlwget -O $OUTDIR/test.jsonl https://s3.amazonaws.com/commensenseqa/test_rand_split_no_answers.jsonlwget -O $OUTDIR/dict.txt https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/dict.txt1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859# @package _group_
common: fp16: true fp16_init_scale: 4 threshold_loss_scale: 1 fp16_scale_window: 128 log_format: json log_interval: 200
task: _name: sentence_prediction data: ??? init_token: 0 separator_token: 2 num_classes: 2 max_positions: 512
checkpoint: restore_file: ??? reset_optimizer: true reset_dataloader: true reset_meters: true best_checkpoint_metric: accuracy maximize_best_checkpoint_metric: true no_epoch_checkpoints: true
distributed_training: find_unused_parameters: true distributed_world_size: 1
criterion: _name: sentence_prediction
dataset: batch_size: 16 required_batch_size_multiple: 1 max_tokens: 4400
optimizer: _name: adam weight_decay: 0.1 adam_betas: (0.9,0.98) adam_eps: 1e-06
lr_scheduler: _name: polynomial_decay warmup_updates: 320
optimization: clip_norm: 0.0 lr: [1e-05] max_update: 5336 max_epoch: 10
model: _name: roberta dropout: 0.1 attention_dropout: 0.11234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859# @package _group_
common: fp16: true fp16_init_scale: 4 threshold_loss_scale: 1 fp16_scale_window: 128 log_format: json log_interval: 200
task: _name: sentence_prediction data: ??? init_token: 0 separator_token: 2 num_classes: 3 max_positions: 512
checkpoint: restore_file: ??? reset_optimizer: true reset_dataloader: true reset_meters: true best_checkpoint_metric: accuracy maximize_best_checkpoint_metric: true no_epoch_checkpoints: true
distributed_training: find_unused_parameters: true distributed_world_size: 1
criterion: _name: sentence_prediction
dataset: batch_size: 32 required_batch_size_multiple: 1 max_tokens: 4400
optimizer: _name: adam weight_decay: 0.1 adam_betas: (0.9,0.98) adam_eps: 1e-06
lr_scheduler: _name: polynomial_decay warmup_updates: 7432
optimization: clip_norm: 0.0 lr: [1e-05] max_update: 123873 max_epoch: 10
model: _name: roberta dropout: 0.1 attention_dropout: 0.11234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859# @package _group_
common: fp16: true fp16_init_scale: 4 threshold_loss_scale: 1 fp16_scale_window: 128 log_format: json log_interval: 200
task: _name: sentence_prediction data: ??? init_token: 0 separator_token: 2 num_classes: 2 max_positions: 512
checkpoint: restore_file: ??? reset_optimizer: true reset_dataloader: true reset_meters: true best_checkpoint_metric: accuracy maximize_best_checkpoint_metric: true no_epoch_checkpoints: true
distributed_training: find_unused_parameters: true distributed_world_size: 1
criterion: _name: sentence_prediction
dataset: batch_size: 16 required_batch_size_multiple: 1 max_tokens: 4400
optimizer: _name: adam weight_decay: 0.1 adam_betas: (0.9,0.98) adam_eps: 1e-06
lr_scheduler: _name: polynomial_decay warmup_updates: 137
optimization: clip_norm: 0.0 lr: [1e-05] max_update: 2296 max_epoch: 10
model: _name: roberta dropout: 0.1 attention_dropout: 0.11234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859# @package _group_
common: fp16: true fp16_init_scale: 4 threshold_loss_scale: 1 fp16_scale_window: 128 log_format: json log_interval: 200
task: _name: sentence_prediction data: ??? init_token: 0 separator_token: 2 num_classes: 2 max_positions: 512
checkpoint: restore_file: ??? reset_optimizer: true reset_dataloader: true reset_meters: true best_checkpoint_metric: accuracy maximize_best_checkpoint_metric: true no_epoch_checkpoints: true
distributed_training: find_unused_parameters: true distributed_world_size: 1
criterion: _name: sentence_prediction
dataset: batch_size: 32 required_batch_size_multiple: 1 max_tokens: 4400
optimizer: _name: adam weight_decay: 0.1 adam_betas: (0.9,0.98) adam_eps: 1e-06
lr_scheduler: _name: polynomial_decay warmup_updates: 1986
optimization: clip_norm: 0.0 lr: [1e-05] max_update: 33112 max_epoch: 10
model: _name: roberta dropout: 0.1 attention_dropout: 0.11234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859# @package _group_
common: fp16: true fp16_init_scale: 4 threshold_loss_scale: 1 fp16_scale_window: 128 log_format: json log_interval: 200
task: _name: sentence_prediction data: ??? init_token: 0 separator_token: 2 num_classes: 2 max_positions: 512
checkpoint: restore_file: ??? reset_optimizer: true reset_dataloader: true reset_meters: true best_checkpoint_metric: accuracy maximize_best_checkpoint_metric: true no_epoch_checkpoints: true
distributed_training: find_unused_parameters: true distributed_world_size: 1
criterion: _name: sentence_prediction
dataset: batch_size: 32 required_batch_size_multiple: 1 max_tokens: 4400
optimizer: _name: adam weight_decay: 0.1 adam_betas: (0.9,0.98) adam_eps: 1e-06
lr_scheduler: _name: polynomial_decay warmup_updates: 28318
optimization: clip_norm: 0.0 lr: [1e-05] max_update: 113272 max_epoch: 10
model: _name: roberta dropout: 0.1 attention_dropout: 0.11234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859# @package _group_
common: fp16: true fp16_init_scale: 4 threshold_loss_scale: 1 fp16_scale_window: 128 log_format: json log_interval: 200
task: _name: sentence_prediction data: ??? init_token: 0 separator_token: 2 num_classes: 2 max_positions: 512
checkpoint: restore_file: ??? reset_optimizer: true reset_dataloader: true reset_meters: true best_checkpoint_metric: accuracy maximize_best_checkpoint_metric: true no_epoch_checkpoints: true
distributed_training: find_unused_parameters: true distributed_world_size: 1
criterion: _name: sentence_prediction
dataset: batch_size: 16 required_batch_size_multiple: 1 max_tokens: 4400
optimizer: _name: adam weight_decay: 0.1 adam_betas: (0.9,0.98) adam_eps: 1e-06
lr_scheduler: _name: polynomial_decay warmup_updates: 122
optimization: clip_norm: 0.0 lr: [2e-05] max_update: 2036 max_epoch: 10
model: _name: roberta dropout: 0.1 attention_dropout: 0.1123456789101112131415# @package _global_hydra: sweep: dir: ${env:PWD}/tmp_dbg/${now:%H-%M-%S}
distributed_training: distributed_world_size: 1 nprocs_per_node: 1 distributed_port: -1 common: log_interval: 1 dataset: num_workers: 012345678910111213141516171819202122232425262728
# @package _global_
hydra: job: config: override_dirname: kv_sep: '_' item_sep: '/' exclude_keys: - run_config - distributed_training.distributed_port sweep: dir: /checkpoint/${env:USER}/roberta_ft/${env:PREFIX}/${hydra.job.config_name}/${env:SUFFIX} subdir: ${hydra.job.num} launcher: submitit_folder: ${hydra.sweep.dir}/submitit timeout_min: 1000 cpus_per_task: 8 gpus_per_node: 1 tasks_per_node: 1 mem_gb: 60 nodes: 1 name: ${env:PREFIX}_${hydra.job.config_name} partition: devlab,learnlab,learnfair,scavenge constraint: volta32gb max_num_timeout: 30 exclude: learnfair1381,learnfair5192,learnfair230412345678910111213141516171819202122232425# @package _global_
hydra: job: config: override_dirname: kv_sep: '_' item_sep: '/' exclude_keys: - run_config - distributed_training.distributed_port sweep: dir: /fsx-wav2vec/${env:USER}/roberta_ft/${env:PREFIX}/${hydra.job.config_name}/${env:SUFFIX} subdir: ${hydra.job.num} launcher: submitit_folder: ${hydra.sweep.dir}/submitit timeout_min: 1000 cpus_per_task: 8 gpus_per_node: 1 tasks_per_node: 1 mem_gb: 0 nodes: 1 name: ${env:PREFIX}_${hydra.job.config_name} partition: learnfair,wav2vec max_num_timeout: 301234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859# @package _group_
common: fp16: true fp16_init_scale: 4 threshold_loss_scale: 1 fp16_scale_window: 128 log_format: json log_interval: 200
task: _name: sentence_prediction data: ??? init_token: 0 separator_token: 2 num_classes: 2 max_positions: 512
checkpoint: restore_file: ??? reset_optimizer: true reset_dataloader: true reset_meters: true best_checkpoint_metric: accuracy maximize_best_checkpoint_metric: true no_epoch_checkpoints: true
distributed_training: find_unused_parameters: true distributed_world_size: 1
criterion: _name: sentence_prediction
dataset: batch_size: 32 required_batch_size_multiple: 1 max_tokens: 4400
optimizer: _name: adam weight_decay: 0.1 adam_betas: (0.9,0.98) adam_eps: 1e-06
lr_scheduler: _name: polynomial_decay warmup_updates: 1256
optimization: clip_norm: 0.0 lr: [1e-05] max_update: 20935 max_epoch: 10
model: _name: roberta dropout: 0.1 attention_dropout: 0.112345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758# @package _group_
common: fp16: true fp16_init_scale: 4 threshold_loss_scale: 1 fp16_scale_window: 128 log_format: json log_interval: 200
task: _name: sentence_prediction data: ??? init_token: 0 separator_token: 2 num_classes: 1 max_positions: 512
checkpoint: restore_file: ??? reset_optimizer: true reset_dataloader: true reset_meters: true no_epoch_checkpoints: true
distributed_training: find_unused_parameters: true distributed_world_size: 1
criterion: _name: sentence_prediction regression_target: true
dataset: batch_size: 16 required_batch_size_multiple: 1 max_tokens: 4400
optimizer: _name: adam weight_decay: 0.1 adam_betas: (0.9,0.98) adam_eps: 1e-06
lr_scheduler: _name: polynomial_decay warmup_updates: 214
optimization: clip_norm: 0.0 lr: [2e-05] max_update: 3598 max_epoch: 10
model: _name: roberta dropout: 0.1 attention_dropout: 0.1123456789101112131415161718192021222324252627282930313233343536373839404142# @package _group_common: fp16: true log_format: json log_interval: 200
checkpoint: no_epoch_checkpoints: true
task: _name: masked_lm data: ??? sample_break_mode: complete tokens_per_sample: 512
criterion: masked_lm
dataset: batch_size: 16 ignore_unused_valid_subsets: true
optimizer: _name: adam weight_decay: 0.01 adam_betas: (0.9,0.98) adam_eps: 1e-06
lr_scheduler: _name: polynomial_decay warmup_updates: 10000
optimization: clip_norm: 0 lr: [0.0005] max_update: 125000 update_freq: [16]
model: _name: roberta max_positions: 512 dropout: 0.1 attention_dropout: 0.1123456789101112131415# @package _global_hydra: sweep: dir: ${env:PWD}/tmp_dbg/${now:%H-%M-%S}
distributed_training: distributed_world_size: 1 nprocs_per_node: 1 distributed_port: -1 common: log_interval: 1 dataset: num_workers: 012345678910111213141516171819202122232425262728293031323334353637# @package _global_
hydra: job: config: override_dirname: kv_sep: ':' item_sep: '/' exclude_keys: - run_config - distributed_training.distributed_port - distributed_training.distributed_world_size - model.pretrained_model_path - model.target_network_path - next_script - task.cache_in_scratch - task.data - checkpoint.save_interval_updates - checkpoint.keep_interval_updates - checkpoint.save_on_overflow - common.log_interval - common.user_dir sweep: dir: /checkpoint/${env:USER}/${env:PREFIX}/${hydra.job.config_name}_${hydra.launcher.gpus_per_node}/${hydra.job.override_dirname} subdir: '' launcher: submitit_folder: ${hydra.sweep.dir} timeout_min: 4320 cpus_per_task: 80 gpus_per_node: 8 tasks_per_node: 1 mem_gb: 450 nodes: 2 name: ${env:PREFIX}_${hydra.job.config_name} partition: devlab,learnlab,learnfair,scavenge constraint: volta32gb,ib4 max_num_timeout: 30123456789101112131415161718192021222324252627282930313233343536373839# @package _global_
hydra: job: config: override_dirname: kv_sep: ':' item_sep: '/' exclude_keys: - run_config - distributed_training.distributed_port - distributed_training.distributed_world_size - model.pretrained_model_path - model.target_network_path - next_script - task.cache_in_scratch - task.local_cache_path - task.data - task.post_save_script - checkpoint.save_interval_updates - checkpoint.keep_interval_updates - checkpoint.save_on_overflow - common.log_interval - common.user_dir - model.model_path sweep: dir: /fsx-wav2vec/${env:USER}/${env:PREFIX}/${hydra.job.config_name}_${hydra.launcher.gpus_per_node}/${hydra.job.override_dirname} subdir: '' launcher: submitit_folder: ${hydra.sweep.dir} timeout_min: 4320 cpus_per_task: 10 gpus_per_node: 8 tasks_per_node: 8 mem_gb: 0 nodes: 2 name: ${env:PREFIX}_${hydra.job.config_name} partition: wav2vec max_num_timeout: 30123456789101112131415161718192021222324252627282930313233343536# @package _global_
hydra: job: config: override_dirname: kv_sep: ':' item_sep: '/' exclude_keys: - run_config - distributed_training.distributed_port - distributed_training.distributed_world_size - model.pretrained_model_path - model.target_network_path - next_script - task.cache_in_scratch - task.data - checkpoint.save_interval_updates - checkpoint.keep_interval_updates - checkpoint.save_on_overflow - common.log_interval sweep: dir: /checkpoint/${env:USER}/${env:PREFIX}/${hydra.job.config_name}_${hydra.launcher.gpus_per_node}/${hydra.job.override_dirname} subdir: '' launcher: submitit_folder: ${hydra.sweep.dir} timeout_min: 4320 cpus_per_task: 10 gpus_per_node: 8 tasks_per_node: 8 mem_gb: 450 nodes: 3 name: ${env:PREFIX}_${hydra.job.config_name} partition: devlab,learnlab,learnfair,scavenge constraint: volta32gb,ib4 max_num_timeout: 30123456789101112131415161718192021222324252627282930313233343536# @package _global_
hydra: job: config: override_dirname: kv_sep: ':' item_sep: '/' exclude_keys: - run_config - distributed_training.distributed_port - distributed_training.distributed_world_size - model.pretrained_model_path - model.target_network_path - next_script - task.cache_in_scratch - task.data - checkpoint.save_interval_updates - checkpoint.keep_interval_updates - checkpoint.save_on_overflow - common.log_interval sweep: dir: /checkpoint/${env:USER}/${env:PREFIX}/${hydra.job.config_name}_${hydra.launcher.gpus_per_node}/${hydra.job.override_dirname} subdir: '' launcher: submitit_folder: ${hydra.sweep.dir} timeout_min: 4320 cpus_per_task: 10 gpus_per_node: 8 tasks_per_node: 8 mem_gb: 450 nodes: 4 name: ${env:PREFIX}_${hydra.job.config_name} partition: devlab,learnlab,learnfair,scavenge constraint: volta32gb,ib4 max_num_timeout: 301234567891011121314151617181920212223242526# Multilingual pretraining RoBERTa
This tutorial will walk you through pretraining multilingual RoBERTa.
### 1) Preprocess the data
```bashDICTIONARY="/private/home/namangoyal/dataset/XLM/wiki/17/175k/vocab"DATA_LOCATION="/private/home/namangoyal/dataset/XLM/wiki/17/175k"
for LANG in en es itdo fairseq-preprocess \ --only-source \ --srcdict $DICTIONARY \ --trainpref "$DATA_LOCATION/train.$LANG" \ --validpref "$DATA_LOCATION/valid.$LANG" \ --testpref "$DATA_LOCATION/test.$LANG" \ --destdir "wiki_17-bin/$LANG" \ --workers 60;done```
### 2) Train RoBERTa base
[COMING UP...]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130#!/usr/bin/env python# 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 argparseimport contextlibimport sysfrom collections import Counterfrom multiprocessing import Pool
from fairseq.data.encoders.gpt2_bpe import get_encoder
def main(): """ Helper script to encode raw text with the GPT-2 BPE using multiple processes.
The encoder.json and vocab.bpe files can be obtained here: - https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/encoder.json - https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/vocab.bpe """ parser = argparse.ArgumentParser() parser.add_argument( "--encoder-json", help="path to encoder.json", ) parser.add_argument( "--vocab-bpe", type=str, help="path to vocab.bpe", ) parser.add_argument( "--inputs", nargs="+", default=["-"], help="input files to filter/encode", ) parser.add_argument( "--outputs", nargs="+", default=["-"], help="path to save encoded outputs", ) parser.add_argument( "--keep-empty", action="store_true", help="keep empty lines", ) parser.add_argument("--workers", type=int, default=20) args = parser.parse_args()
assert len(args.inputs) == len( args.outputs ), "number of input and output paths should match"
with contextlib.ExitStack() as stack: inputs = [ stack.enter_context(open(input, "r", encoding="utf-8")) if input != "-" else sys.stdin for input in args.inputs ] outputs = [ stack.enter_context(open(output, "w", encoding="utf-8")) if output != "-" else sys.stdout for output in args.outputs ]
encoder = MultiprocessingEncoder(args) pool = Pool(args.workers, initializer=encoder.initializer) encoded_lines = pool.imap(encoder.encode_lines, zip(*inputs), 100)
stats = Counter() for i, (filt, enc_lines) in enumerate(encoded_lines, start=1): if filt == "PASS": for enc_line, output_h in zip(enc_lines, outputs): print(enc_line, file=output_h) else: stats["num_filtered_" + filt] += 1 if i % 10000 == 0: print("processed {} lines".format(i), file=sys.stderr)
for k, v in stats.most_common(): print("[{}] filtered {} lines".format(k, v), file=sys.stderr)
class MultiprocessingEncoder(object): def __init__(self, args): self.args = args
def initializer(self): global bpe bpe = get_encoder(self.args.encoder_json, self.args.vocab_bpe)
def encode(self, line): global bpe ids = bpe.encode(line) return list(map(str, ids))
def decode(self, tokens): global bpe return bpe.decode(tokens)
def encode_lines(self, lines): """ Encode a set of lines. All lines will be encoded together. """ enc_lines = [] for line in lines: line = line.strip() if len(line) == 0 and not self.args.keep_empty: return ["EMPTY", None] tokens = self.encode(line) enc_lines.append(" ".join(tokens)) return ["PASS", enc_lines]
def decode_lines(self, lines): dec_lines = [] for line in lines: tokens = map(int, line.strip().split()) dec_lines.append(self.decode(tokens)) return ["PASS", dec_lines]
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185#!/bin/bash# 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.
# raw glue data as downloaded by glue download script (https://gist.github.com/W4ngatang/60c2bdb54d156a41194446737ce03e2e)if [[ $# -ne 2 ]]; then echo "Run as following:" echo "./examples/roberta/preprocess_GLUE_tasks.sh <glud_data_folder> <task_name>" exit 1fi
GLUE_DATA_FOLDER=$1
# download bpe encoder.json, vocabulary and fairseq dictionarywget -N 'https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/encoder.json'wget -N 'https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/vocab.bpe'wget -N 'https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/dict.txt'
TASKS=$2 # QQP
if [ "$TASKS" = "ALL" ]then TASKS="QQP MNLI QNLI MRPC RTE STS-B SST-2 CoLA"fi
for TASK in $TASKSdo echo "Preprocessing $TASK"
TASK_DATA_FOLDER="$GLUE_DATA_FOLDER/$TASK" echo "Raw data as downloaded from glue website: $TASK_DATA_FOLDER"
SPLITS="train dev test" INPUT_COUNT=2 if [ "$TASK" = "QQP" ] then INPUT_COLUMNS=( 4 5 ) TEST_INPUT_COLUMNS=( 2 3 ) LABEL_COLUMN=6 elif [ "$TASK" = "MNLI" ] then SPLITS="train dev_matched dev_mismatched test_matched test_mismatched" INPUT_COLUMNS=( 9 10 ) TEST_INPUT_COLUMNS=( 9 10 ) DEV_LABEL_COLUMN=16 LABEL_COLUMN=12 elif [ "$TASK" = "QNLI" ] then INPUT_COLUMNS=( 2 3 ) TEST_INPUT_COLUMNS=( 2 3 ) LABEL_COLUMN=4 elif [ "$TASK" = "MRPC" ] then INPUT_COLUMNS=( 4 5 ) TEST_INPUT_COLUMNS=( 4 5 ) LABEL_COLUMN=1 elif [ "$TASK" = "RTE" ] then INPUT_COLUMNS=( 2 3 ) TEST_INPUT_COLUMNS=( 2 3 ) LABEL_COLUMN=4 elif [ "$TASK" = "STS-B" ] then INPUT_COLUMNS=( 8 9 ) TEST_INPUT_COLUMNS=( 8 9 ) LABEL_COLUMN=10 # Following are single sentence tasks. elif [ "$TASK" = "SST-2" ] then INPUT_COLUMNS=( 1 ) TEST_INPUT_COLUMNS=( 2 ) LABEL_COLUMN=2 INPUT_COUNT=1 elif [ "$TASK" = "CoLA" ] then INPUT_COLUMNS=( 4 ) TEST_INPUT_COLUMNS=( 2 ) LABEL_COLUMN=2 INPUT_COUNT=1 fi
# Strip out header and filter lines that don't have expected number of fields. rm -rf "$TASK_DATA_FOLDER/processed" mkdir -p "$TASK_DATA_FOLDER/processed" for SPLIT in $SPLITS do # CoLA train and dev doesn't have header. if [[ ( "$TASK" = "CoLA") && ( "$SPLIT" != "test" ) ]] then cp "$TASK_DATA_FOLDER/$SPLIT.tsv" "$TASK_DATA_FOLDER/processed/$SPLIT.tsv.temp"; else tail -n +2 "$TASK_DATA_FOLDER/$SPLIT.tsv" > "$TASK_DATA_FOLDER/processed/$SPLIT.tsv.temp"; fi
# Remove unformatted lines from train and dev files for QQP dataset. if [[ ( "$TASK" = "QQP") && ( "$SPLIT" != "test" ) ]] then awk -F '\t' -v NUM_FIELDS=6 'NF==NUM_FIELDS{print}{}' "$TASK_DATA_FOLDER/processed/$SPLIT.tsv.temp" > "$TASK_DATA_FOLDER/processed/$SPLIT.tsv"; else cp "$TASK_DATA_FOLDER/processed/$SPLIT.tsv.temp" "$TASK_DATA_FOLDER/processed/$SPLIT.tsv"; fi rm "$TASK_DATA_FOLDER/processed/$SPLIT.tsv.temp"; done
# Split into input0, input1 and label for SPLIT in $SPLITS do for INPUT_TYPE in $(seq 0 $((INPUT_COUNT-1))) do if [[ "$SPLIT" != test* ]] then COLUMN_NUMBER=${INPUT_COLUMNS[$INPUT_TYPE]} else COLUMN_NUMBER=${TEST_INPUT_COLUMNS[$INPUT_TYPE]} fi cut -f"$COLUMN_NUMBER" "$TASK_DATA_FOLDER/processed/$SPLIT.tsv" > "$TASK_DATA_FOLDER/processed/$SPLIT.raw.input$INPUT_TYPE"; done
if [[ "$SPLIT" != test* ]] then if [ "$TASK" = "MNLI" ] && [ "$SPLIT" != "train" ] then cut -f"$DEV_LABEL_COLUMN" "$TASK_DATA_FOLDER/processed/$SPLIT.tsv" > "$TASK_DATA_FOLDER/processed/$SPLIT.label"; else cut -f"$LABEL_COLUMN" "$TASK_DATA_FOLDER/processed/$SPLIT.tsv" > "$TASK_DATA_FOLDER/processed/$SPLIT.label"; fi fi
# BPE encode. for INPUT_TYPE in $(seq 0 $((INPUT_COUNT-1))) do LANG="input$INPUT_TYPE" echo "BPE encoding $SPLIT/$LANG" python -m examples.roberta.multiprocessing_bpe_encoder \ --encoder-json encoder.json \ --vocab-bpe vocab.bpe \ --inputs "$TASK_DATA_FOLDER/processed/$SPLIT.raw.$LANG" \ --outputs "$TASK_DATA_FOLDER/processed/$SPLIT.$LANG" \ --workers 60 \ --keep-empty; done done
# Remove output directory. rm -rf "$TASK-bin"
DEVPREF="$TASK_DATA_FOLDER/processed/dev.LANG" TESTPREF="$TASK_DATA_FOLDER/processed/test.LANG" if [ "$TASK" = "MNLI" ] then DEVPREF="$TASK_DATA_FOLDER/processed/dev_matched.LANG,$TASK_DATA_FOLDER/processed/dev_mismatched.LANG" TESTPREF="$TASK_DATA_FOLDER/processed/test_matched.LANG,$TASK_DATA_FOLDER/processed/test_mismatched.LANG" fi
# Run fairseq preprocessing: for INPUT_TYPE in $(seq 0 $((INPUT_COUNT-1))) do LANG="input$INPUT_TYPE" fairseq-preprocess \ --only-source \ --trainpref "$TASK_DATA_FOLDER/processed/train.$LANG" \ --validpref "${DEVPREF//LANG/$LANG}" \ --testpref "${TESTPREF//LANG/$LANG}" \ --destdir "$TASK-bin/$LANG" \ --workers 60 \ --srcdict dict.txt; done if [[ "$TASK" != "STS-B" ]] then fairseq-preprocess \ --only-source \ --trainpref "$TASK_DATA_FOLDER/processed/train.label" \ --validpref "${DEVPREF//LANG/label}" \ --destdir "$TASK-bin/label" \ --workers 60; else # For STS-B output range is converted to be between: [0.0, 1.0] mkdir -p "$TASK-bin/label" awk '{print $1 / 5.0 }' "$TASK_DATA_FOLDER/processed/train.label" > "$TASK-bin/label/train.label" awk '{print $1 / 5.0 }' "$TASK_DATA_FOLDER/processed/dev.label" > "$TASK-bin/label/valid.label" fidone123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102#!/usr/bin/env python# 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 argparseimport jsonimport osimport re
class InputExample: def __init__(self, paragraph, qa_list, label): self.paragraph = paragraph self.qa_list = qa_list self.label = label
def get_examples(data_dir, set_type): """ Extract paragraph and question-answer list from each json file """ examples = []
levels = ["middle", "high"] set_type_c = set_type.split("-") if len(set_type_c) == 2: levels = [set_type_c[1]] set_type = set_type_c[0] for level in levels: cur_dir = os.path.join(data_dir, set_type, level) for filename in os.listdir(cur_dir): cur_path = os.path.join(cur_dir, filename) with open(cur_path, "r") as f: cur_data = json.load(f) answers = cur_data["answers"] options = cur_data["options"] questions = cur_data["questions"] context = cur_data["article"].replace("\n", " ") context = re.sub(r"\s+", " ", context) for i in range(len(answers)): label = ord(answers[i]) - ord("A") qa_list = [] question = questions[i] for j in range(4): option = options[i][j] if "_" in question: qa_cat = question.replace("_", option) else: qa_cat = " ".join([question, option]) qa_cat = re.sub(r"\s+", " ", qa_cat) qa_list.append(qa_cat) examples.append(InputExample(context, qa_list, label))
return examples
def main(): """ Helper script to extract paragraphs questions and answers from RACE datasets. """ parser = argparse.ArgumentParser() parser.add_argument( "--input-dir", help="input directory for downloaded RACE dataset", ) parser.add_argument( "--output-dir", help="output directory for extracted data", ) args = parser.parse_args()
if not os.path.exists(args.output_dir): os.makedirs(args.output_dir, exist_ok=True)
for set_type in ["train", "dev", "test-middle", "test-high"]: examples = get_examples(args.input_dir, set_type) qa_file_paths = [ os.path.join(args.output_dir, set_type + ".input" + str(i + 1)) for i in range(4) ] qa_files = [open(qa_file_path, "w") for qa_file_path in qa_file_paths] outf_context_path = os.path.join(args.output_dir, set_type + ".input0") outf_label_path = os.path.join(args.output_dir, set_type + ".label") outf_context = open(outf_context_path, "w") outf_label = open(outf_label_path, "w") for example in examples: outf_context.write(example.paragraph + "\n") for i in range(4): qa_files[i].write(example.qa_list[i] + "\n") outf_label.write(str(example.label) + "\n")
for f in qa_files: f.close() outf_label.close() outf_context.close()
if __name__ == "__main__": main()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859#!/bin/bash# 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.
# data should be downloaded and processed with reprocess_RACE.pyif [[ $# -ne 2 ]]; then echo "Run as following:" echo "./examples/roberta/preprocess_RACE.sh <race_data_folder> <output_folder>" exit 1fi
RACE_DATA_FOLDER=$1OUT_DATA_FOLDER=$2
# download bpe encoder.json, vocabulary and fairseq dictionarywget -N 'https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/encoder.json'wget -N 'https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/vocab.bpe'wget -N 'https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/dict.txt'
SPLITS="train dev test-middle test-high"INPUT_TYPES="input0 input1 input2 input3 input4"for INPUT_TYPE in $INPUT_TYPESdo for SPLIT in $SPLITS do echo "BPE encoding $SPLIT/$INPUT_TYPE" python -m examples.roberta.multiprocessing_bpe_encoder \ --encoder-json encoder.json \ --vocab-bpe vocab.bpe \ --inputs "$RACE_DATA_FOLDER/$SPLIT.$INPUT_TYPE" \ --outputs "$RACE_DATA_FOLDER/$SPLIT.$INPUT_TYPE.bpe" \ --workers 10 \ --keep-empty;
donedone
for INPUT_TYPE in $INPUT_TYPES do LANG="input$INPUT_TYPE" fairseq-preprocess \ --only-source \ --trainpref "$RACE_DATA_FOLDER/train.$INPUT_TYPE.bpe" \ --validpref "$RACE_DATA_FOLDER/dev.$INPUT_TYPE.bpe" \ --testpref "$RACE_DATA_FOLDER/test-middle.$INPUT_TYPE.bpe,$RACE_DATA_FOLDER/test-high.$INPUT_TYPE.bpe" \ --destdir "$OUT_DATA_FOLDER/$INPUT_TYPE" \ --workers 10 \ --srcdict dict.txt;done
rm -rf "$OUT_DATA_FOLDER/label"mkdir -p "$OUT_DATA_FOLDER/label"cp "$RACE_DATA_FOLDER/train.label" "$OUT_DATA_FOLDER/label/"cp "$RACE_DATA_FOLDER/dev.label" "$OUT_DATA_FOLDER/label/valid.label"cp "$RACE_DATA_FOLDER/test-middle.label" "$OUT_DATA_FOLDER/label/test.label"cp "$RACE_DATA_FOLDER/test-high.label" "$OUT_DATA_FOLDER/label/test1.label"123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125# Finetuning RoBERTa on Winograd Schema Challenge (WSC) data
The following instructions can be used to finetune RoBERTa on the WSC trainingdata provided by [SuperGLUE](https://super.gluebenchmark.com/).
Note that there is high variance in the results. For our GLUE/SuperGLUEsubmission we swept over the learning rate (1e-5, 2e-5, 3e-5), batch size (16,32, 64) and total number of updates (500, 1000, 2000, 3000), as well as therandom seed. Out of ~100 runs we chose the best 7 models and ensembled them.
**Approach:** The instructions below use a slightly different loss function thanwhat's described in the original RoBERTa arXiv paper. In particular,[Kocijan et al. (2019)](https://arxiv.org/abs/1905.06290) introduce a marginranking loss between `(query, candidate)` pairs with tunable hyperparametersalpha and beta. This is supported in our code as well with the `--wsc-alpha` and`--wsc-beta` arguments. However, we achieved slightly better (and more robust)results on the development set by instead using a single cross entropy loss termover the log-probabilities for the query and all mined candidates. **Thecandidates are mined using spaCy from each input sentence in isolation, so theapproach remains strictly pointwise.** This reduces the number ofhyperparameters and our best model achieved 92.3% development set accuracy,compared to ~90% accuracy for the margin loss. Later versions of the RoBERTaarXiv paper will describe this updated formulation.
### 1) Download the WSC data from the SuperGLUE website:```bashwget https://dl.fbaipublicfiles.com/glue/superglue/data/v2/WSC.zipunzip WSC.zip
# we also need to copy the RoBERTa dictionary into the same directorywget -O WSC/dict.txt https://dl.fbaipublicfiles.com/fairseq/gpt2_bpe/dict.txt```
### 2) Finetune over the provided training data:```bashTOTAL_NUM_UPDATES=2000 # Total number of training steps.WARMUP_UPDATES=250 # Linearly increase LR over this many steps.LR=2e-05 # Peak LR for polynomial LR scheduler.MAX_SENTENCES=16 # Batch size per GPU.SEED=1 # Random seed.ROBERTA_PATH=/path/to/roberta/model.pt
# we use the --user-dir option to load the task and criterion# from the examples/roberta/wsc directory:FAIRSEQ_PATH=/path/to/fairseqFAIRSEQ_USER_DIR=${FAIRSEQ_PATH}/examples/roberta/wsc
CUDA_VISIBLE_DEVICES=0,1,2,3 fairseq-train WSC/ \ --restore-file $ROBERTA_PATH \ --reset-optimizer --reset-dataloader --reset-meters \ --no-epoch-checkpoints --no-last-checkpoints --no-save-optimizer-state \ --best-checkpoint-metric accuracy --maximize-best-checkpoint-metric \ --valid-subset val \ --fp16 --ddp-backend legacy_ddp \ --user-dir $FAIRSEQ_USER_DIR \ --task wsc --criterion wsc --wsc-cross-entropy \ --arch roberta_large --bpe gpt2 --max-positions 512 \ --dropout 0.1 --attention-dropout 0.1 --weight-decay 0.01 \ --optimizer adam --adam-betas '(0.9, 0.98)' --adam-eps 1e-06 \ --lr-scheduler polynomial_decay --lr $LR \ --warmup-updates $WARMUP_UPDATES --total-num-update $TOTAL_NUM_UPDATES \ --batch-size $MAX_SENTENCES \ --max-update $TOTAL_NUM_UPDATES \ --log-format simple --log-interval 100 \ --seed $SEED```
The above command assumes training on 4 GPUs, but you can achieve the sameresults on a single GPU by adding `--update-freq=4`.
### 3) Evaluate```pythonfrom fairseq.models.roberta import RobertaModelfrom examples.roberta.wsc import wsc_utils # also loads WSC task and criterionroberta = RobertaModel.from_pretrained('checkpoints', 'checkpoint_best.pt', 'WSC/')roberta.cuda()nsamples, ncorrect = 0, 0for sentence, label in wsc_utils.jsonl_iterator('WSC/val.jsonl', eval=True): pred = roberta.disambiguate_pronoun(sentence) nsamples += 1 if pred == label: ncorrect += 1print('Accuracy: ' + str(ncorrect / float(nsamples)))# Accuracy: 0.9230769230769231```
## RoBERTa training on WinoGrande datasetWe have also provided `winogrande` task and criterion for finetuning on the[WinoGrande](https://mosaic.allenai.org/projects/winogrande) like datasetswhere there are always two candidates and one is correct.It's more efficient implementation for such subcases.
```bashTOTAL_NUM_UPDATES=23750 # Total number of training steps.WARMUP_UPDATES=2375 # Linearly increase LR over this many steps.LR=1e-05 # Peak LR for polynomial LR scheduler.MAX_SENTENCES=32 # Batch size per GPU.SEED=1 # Random seed.ROBERTA_PATH=/path/to/roberta/model.pt
# we use the --user-dir option to load the task and criterion# from the examples/roberta/wsc directory:FAIRSEQ_PATH=/path/to/fairseqFAIRSEQ_USER_DIR=${FAIRSEQ_PATH}/examples/roberta/wsc
cd fairseqCUDA_VISIBLE_DEVICES=0 fairseq-train winogrande_1.0/ \ --restore-file $ROBERTA_PATH \ --reset-optimizer --reset-dataloader --reset-meters \ --no-epoch-checkpoints --no-last-checkpoints --no-save-optimizer-state \ --best-checkpoint-metric accuracy --maximize-best-checkpoint-metric \ --valid-subset val \ --fp16 --ddp-backend legacy_ddp \ --user-dir $FAIRSEQ_USER_DIR \ --task winogrande --criterion winogrande \ --wsc-margin-alpha 5.0 --wsc-margin-beta 0.4 \ --arch roberta_large --bpe gpt2 --max-positions 512 \ --dropout 0.1 --attention-dropout 0.1 --weight-decay 0.01 \ --optimizer adam --adam-betas '(0.9, 0.98)' --adam-eps 1e-06 \ --lr-scheduler polynomial_decay --lr $LR \ --warmup-updates $WARMUP_UPDATES --total-num-update $TOTAL_NUM_UPDATES \ --batch-size $MAX_SENTENCES \ --max-update $TOTAL_NUM_UPDATES \ --log-format simple --log-interval 100```1234567# 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 wsc_criterion # noqafrom . import wsc_task # noqa123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167# 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.functional as Ffrom fairseq import utilsfrom fairseq.criterions import LegacyFairseqCriterion, register_criterionfrom fairseq.data import encoders
@register_criterion("wsc")class WSCCriterion(LegacyFairseqCriterion): def __init__(self, args, task): super().__init__(args, task) if self.args.save_predictions is not None: self.prediction_h = open(self.args.save_predictions, "w") else: self.prediction_h = None self.bpe = encoders.build_bpe(args.bpe) self.tokenizer = encoders.build_tokenizer(args.tokenizer)
def __del__(self): if self.prediction_h is not None: self.prediction_h.close()
@staticmethod def add_args(parser): """Add criterion-specific arguments to the parser.""" parser.add_argument("--wsc-margin-alpha", type=float, metavar="A", default=1.0) parser.add_argument("--wsc-margin-beta", type=float, metavar="B", default=0.0) parser.add_argument( "--wsc-cross-entropy", action="store_true", help="use cross entropy formulation instead of margin loss", ) parser.add_argument( "--save-predictions", metavar="FILE", help="file to save predictions to" )
def get_masked_input(self, tokens, mask): masked_tokens = tokens.clone() masked_tokens[mask] = self.task.mask return masked_tokens
def get_lprobs(self, model, tokens, mask): logits, _ = model(src_tokens=self.get_masked_input(tokens, mask)) lprobs = F.log_softmax(logits, dim=-1, dtype=torch.float) scores = lprobs.gather(2, tokens.unsqueeze(-1)).squeeze(-1) mask = mask.type_as(scores) scores = (scores * mask).sum(dim=-1) / mask.sum(dim=-1) return scores
def get_loss(self, query_lprobs, cand_lprobs): if self.args.wsc_cross_entropy: return F.cross_entropy( torch.cat([query_lprobs, cand_lprobs]).unsqueeze(0), query_lprobs.new([0]).long(), ) else: return ( -query_lprobs + self.args.wsc_margin_alpha * (cand_lprobs - query_lprobs + self.args.wsc_margin_beta).clamp(min=0) ).sum()
def forward(self, model, sample, reduce=True): # compute loss and accuracy loss, nloss = 0.0, 0 ncorrect, nqueries = 0, 0
for i, label in enumerate(sample["labels"]): query_lprobs = self.get_lprobs( model, sample["query_tokens"][i].unsqueeze(0), sample["query_masks"][i].unsqueeze(0), ) cand_lprobs = self.get_lprobs( model, sample["candidate_tokens"][i], sample["candidate_masks"][i], )
pred = (query_lprobs >= cand_lprobs).all().item()
if label is not None: label = 1 if label else 0 ncorrect += 1 if pred == label else 0 nqueries += 1
if label: # only compute a loss for positive instances nloss += 1 loss += self.get_loss(query_lprobs, cand_lprobs)
id = sample["id"][i].item() if self.prediction_h is not None: print("{}\t{}\t{}".format(id, pred, label), file=self.prediction_h)
if nloss == 0: loss = torch.tensor(0.0, requires_grad=True)
sample_size = nqueries if nqueries > 0 else 1 logging_output = { "loss": utils.item(loss.data) if reduce else loss.data, "ntokens": sample["ntokens"], "nsentences": sample["nsentences"], "sample_size": sample_size, "ncorrect": ncorrect, "nqueries": nqueries, } return loss, sample_size, logging_output
@staticmethod def aggregate_logging_outputs(logging_outputs): """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) nsentences = sum(log.get("nsentences", 0) for log in logging_outputs) sample_size = sum(log.get("sample_size", 0) for log in logging_outputs)
agg_output = { "loss": loss_sum / sample_size / math.log(2), "ntokens": ntokens, "nsentences": nsentences, "sample_size": sample_size, }
ncorrect = sum(log.get("ncorrect", 0) for log in logging_outputs) nqueries = sum(log.get("nqueries", 0) for log in logging_outputs) if nqueries > 0: agg_output["accuracy"] = ncorrect / float(nqueries)
return agg_output
@register_criterion("winogrande")class WinograndeCriterion(WSCCriterion): def forward(self, model, sample, reduce=True): # compute loss and accuracy query_lprobs = self.get_lprobs( model, sample["query_tokens"], sample["query_masks"], ) cand_lprobs = self.get_lprobs( model, sample["candidate_tokens"], sample["candidate_masks"], ) pred = query_lprobs >= cand_lprobs loss = self.get_loss(query_lprobs, cand_lprobs)
sample_size = sample["query_tokens"].size(0) ncorrect = pred.sum().item() logging_output = { "loss": utils.item(loss.data) if reduce else loss.data, "ntokens": sample["ntokens"], "nsentences": sample["nsentences"], "sample_size": sample_size, "ncorrect": ncorrect, "nqueries": sample_size, } return loss, sample_size, logging_output123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401# 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 jsonimport osimport tempfile
import numpy as npimport torchimport torch.nn.functional as Ffrom fairseq import utilsfrom fairseq.data import ( Dictionary, IdDataset, ListDataset, NestedDictionaryDataset, NumelDataset, NumSamplesDataset, PadDataset, SortDataset, data_utils, encoders,)from fairseq.tasks import LegacyFairseqTask, register_task
from . import wsc_utils
@register_task("wsc")class WSCTask(LegacyFairseqTask): """Task to finetune RoBERTa for Winograd Schemas."""
@staticmethod def add_args(parser): """Add task-specific arguments to the parser.""" parser.add_argument( "data", metavar="DIR", help="path to data directory; we load <split>.jsonl" ) parser.add_argument( "--init-token", type=int, default=None, help="add token at the beginning of each batch item", )
def __init__(self, args, vocab): super().__init__(args) self.vocab = vocab self.mask = vocab.add_symbol("<mask>")
self.bpe = encoders.build_bpe(args) self.tokenizer = encoders.build_tokenizer(args)
# hack to handle GPT-2 BPE, which includes leading spaces if args.bpe == "gpt2": self.leading_space = True self.trailing_space = False else: self.leading_space = False self.trailing_space = True
@classmethod def load_dictionary(cls, filename): """Load the dictionary from the filename
Args: filename (str): the filename """ dictionary = Dictionary.load(filename) dictionary.add_symbol("<mask>") return dictionary
@classmethod def setup_task(cls, args, **kwargs): assert args.criterion == "wsc", "Must set --criterion=wsc"
# load data and label dictionaries vocab = cls.load_dictionary(os.path.join(args.data, "dict.txt")) print("| dictionary: {} types".format(len(vocab)))
return cls(args, vocab)
def binarize(self, s: str, append_eos: bool = False): if self.tokenizer is not None: s = self.tokenizer.encode(s) if self.bpe is not None: s = self.bpe.encode(s) tokens = self.vocab.encode_line( s, append_eos=append_eos, add_if_not_exist=False, ).long() if self.args.init_token is not None: tokens = torch.cat([tokens.new([self.args.init_token]), tokens]) return tokens
def binarize_with_mask(self, txt, prefix, suffix, leading_space, trailing_space): toks = self.binarize( prefix + leading_space + txt + trailing_space + suffix, append_eos=True, ) mask = torch.zeros_like(toks, dtype=torch.bool) mask_start = len(self.binarize(prefix)) mask_size = len(self.binarize(leading_space + txt)) mask[mask_start : mask_start + mask_size] = 1 return toks, mask
def load_dataset( self, split, epoch=1, combine=False, data_path=None, return_only=False, **kwargs ): """Load a given dataset split.
Args: split (str): name of the split (e.g., train, valid, test) """ if data_path is None: data_path = os.path.join(self.args.data, split + ".jsonl") if not os.path.exists(data_path): raise FileNotFoundError("Cannot find data: {}".format(data_path))
query_tokens = [] query_masks = [] query_lengths = [] candidate_tokens = [] candidate_masks = [] candidate_lengths = [] labels = []
for sentence, pronoun_span, query, label in wsc_utils.jsonl_iterator(data_path): prefix = sentence[: pronoun_span.start].text suffix = sentence[pronoun_span.end :].text_with_ws
# spaCy spans include trailing spaces, but we need to know about # leading spaces for the GPT-2 BPE leading_space = ( " " if sentence[: pronoun_span.start].text_with_ws.endswith(" ") else "" ) trailing_space = " " if pronoun_span.text_with_ws.endswith(" ") else ""
# get noun phrases, excluding pronouns and anything overlapping with the query cand_spans = wsc_utils.filter_noun_chunks( wsc_utils.extended_noun_chunks(sentence), exclude_pronouns=True, exclude_query=query, exact_match=False, )
if query is not None: query_toks, query_mask = self.binarize_with_mask( query, prefix, suffix, leading_space, trailing_space ) query_len = len(query_toks) else: query_toks, query_mask, query_len = None, None, 0
query_tokens.append(query_toks) query_masks.append(query_mask) query_lengths.append(query_len)
cand_toks, cand_masks = [], [] for cand_span in cand_spans: toks, mask = self.binarize_with_mask( cand_span.text, prefix, suffix, leading_space, trailing_space, ) cand_toks.append(toks) cand_masks.append(mask)
# collate candidates cand_toks = data_utils.collate_tokens(cand_toks, pad_idx=self.vocab.pad()) cand_masks = data_utils.collate_tokens(cand_masks, pad_idx=0) assert cand_toks.size() == cand_masks.size()
candidate_tokens.append(cand_toks) candidate_masks.append(cand_masks) candidate_lengths.append(cand_toks.size(1))
labels.append(label)
query_lengths = np.array(query_lengths) query_tokens = ListDataset(query_tokens, query_lengths) query_masks = ListDataset(query_masks, query_lengths)
candidate_lengths = np.array(candidate_lengths) candidate_tokens = ListDataset(candidate_tokens, candidate_lengths) candidate_masks = ListDataset(candidate_masks, candidate_lengths)
labels = ListDataset(labels, [1] * len(labels))
dataset = { "id": IdDataset(), "query_tokens": query_tokens, "query_masks": query_masks, "candidate_tokens": candidate_tokens, "candidate_masks": candidate_masks, "labels": labels, "nsentences": NumSamplesDataset(), "ntokens": NumelDataset(query_tokens, reduce=True), }
nested_dataset = NestedDictionaryDataset( dataset, sizes=[query_lengths], )
with data_utils.numpy_seed(self.args.seed): shuffle = np.random.permutation(len(query_tokens)) dataset = SortDataset( nested_dataset, # shuffle sort_order=[shuffle], )
if return_only: return dataset
self.datasets[split] = dataset return self.datasets[split]
def build_dataset_for_inference(self, sample_json): with tempfile.NamedTemporaryFile(buffering=0) as h: h.write((json.dumps(sample_json) + "\n").encode("utf-8")) dataset = self.load_dataset( "disambiguate_pronoun", data_path=h.name, return_only=True, ) return dataset
def disambiguate_pronoun(self, model, sentence, use_cuda=False): sample_json = wsc_utils.convert_sentence_to_json(sentence) dataset = self.build_dataset_for_inference(sample_json) sample = dataset.collater([dataset[0]]) if use_cuda: sample = utils.move_to_cuda(sample)
def get_masked_input(tokens, mask): masked_tokens = tokens.clone() masked_tokens[mask.bool()] = self.mask return masked_tokens
def get_lprobs(tokens, mask): logits, _ = model(src_tokens=get_masked_input(tokens, mask)) lprobs = F.log_softmax(logits, dim=-1, dtype=torch.float) scores = lprobs.gather(2, tokens.unsqueeze(-1)).squeeze(-1) mask = mask.type_as(scores) scores = (scores * mask).sum(dim=-1) / mask.sum(dim=-1) return scores
cand_lprobs = get_lprobs( sample["candidate_tokens"][0], sample["candidate_masks"][0], ) if sample["query_tokens"][0] is not None: query_lprobs = get_lprobs( sample["query_tokens"][0].unsqueeze(0), sample["query_masks"][0].unsqueeze(0), ) return (query_lprobs >= cand_lprobs).all().item() == 1 else: best_idx = cand_lprobs.argmax().item() full_cand = sample["candidate_tokens"][0][best_idx] mask = sample["candidate_masks"][0][best_idx] toks = full_cand[mask.bool()] return self.bpe.decode(self.source_dictionary.string(toks)).strip()
@property def source_dictionary(self): return self.vocab
@property def target_dictionary(self): return self.vocab
@register_task("winogrande")class WinograndeTask(WSCTask): """ Task for WinoGrande dataset. Efficient implementation for Winograd schema tasks with exactly two candidates, one of which is correct. """
@classmethod def setup_task(cls, args, **kwargs): assert args.criterion == "winogrande", "Must set --criterion=winogrande"
# load data and label dictionaries vocab = cls.load_dictionary(os.path.join(args.data, "dict.txt")) print("| dictionary: {} types".format(len(vocab)))
return cls(args, vocab)
def load_dataset( self, split, epoch=1, combine=False, data_path=None, return_only=False, **kwargs ): """Load a given dataset split.
Args: split (str): name of the split (e.g., train, valid, test) """ if data_path is None: data_path = os.path.join(self.args.data, split + ".jsonl") if not os.path.exists(data_path): raise FileNotFoundError("Cannot find data: {}".format(data_path))
query_tokens = [] query_masks = [] query_lengths = [] candidate_tokens = [] candidate_masks = [] candidate_lengths = []
itr = wsc_utils.winogrande_jsonl_iterator(data_path, eval=(split == "test"))
for sample in itr: sentence, pronoun_span, query, cand_text = sample prefix = sentence[: pronoun_span[0]].rstrip() suffix = sentence[pronoun_span[1] :]
leading_space = " " if sentence[: pronoun_span[0]].endswith(" ") else "" trailing_space = ""
if query is not None: query_toks, query_mask = self.binarize_with_mask( query, prefix, suffix, leading_space, trailing_space, ) query_len = len(query_toks) else: query_toks, query_mask, query_len = None, None, 0
query_tokens.append(query_toks) query_masks.append(query_mask) query_lengths.append(query_len)
cand_toks, cand_mask = self.binarize_with_mask( cand_text, prefix, suffix, leading_space, trailing_space, )
candidate_tokens.append(cand_toks) candidate_masks.append(cand_mask) candidate_lengths.append(cand_toks.size(0))
query_lengths = np.array(query_lengths)
def get_pad_dataset_fn(tokens, length, pad_idx): return PadDataset( ListDataset(tokens, length), pad_idx=pad_idx, left_pad=False, )
query_tokens = get_pad_dataset_fn(query_tokens, query_lengths, self.vocab.pad()) query_masks = get_pad_dataset_fn(query_masks, query_lengths, 0)
candidate_lengths = np.array(candidate_lengths) candidate_tokens = get_pad_dataset_fn( candidate_tokens, candidate_lengths, self.vocab.pad() ) candidate_masks = get_pad_dataset_fn(candidate_masks, candidate_lengths, 0)
dataset = { "id": IdDataset(), "query_tokens": query_tokens, "query_masks": query_masks, "candidate_tokens": candidate_tokens, "candidate_masks": candidate_masks, "nsentences": NumSamplesDataset(), "ntokens": NumelDataset(query_tokens, reduce=True), }
nested_dataset = NestedDictionaryDataset( dataset, sizes=[query_lengths], )
with data_utils.numpy_seed(self.args.seed): shuffle = np.random.permutation(len(query_tokens)) dataset = SortDataset( nested_dataset, # shuffle sort_order=[shuffle], )
if return_only: return dataset
self.datasets[split] = dataset return self.datasets[split]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241# 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 jsonfrom functools import lru_cache
def convert_sentence_to_json(sentence): if "_" in sentence: prefix, rest = sentence.split("_", 1) query, rest = rest.split("_", 1) query_index = len(prefix.rstrip().split(" ")) else: query, query_index = None, None
prefix, rest = sentence.split("[", 1) pronoun, rest = rest.split("]", 1) pronoun_index = len(prefix.rstrip().split(" "))
sentence = sentence.replace("_", "").replace("[", "").replace("]", "")
return { "idx": 0, "text": sentence, "target": { "span1_index": query_index, "span1_text": query, "span2_index": pronoun_index, "span2_text": pronoun, }, }
def extended_noun_chunks(sentence): noun_chunks = {(np.start, np.end) for np in sentence.noun_chunks} np_start, cur_np = 0, "NONE" for i, token in enumerate(sentence): np_type = token.pos_ if token.pos_ in {"NOUN", "PROPN"} else "NONE" if np_type != cur_np: if cur_np != "NONE": noun_chunks.add((np_start, i)) if np_type != "NONE": np_start = i cur_np = np_type if cur_np != "NONE": noun_chunks.add((np_start, len(sentence))) return [sentence[s:e] for (s, e) in sorted(noun_chunks)]
def find_token(sentence, start_pos): found_tok = None for tok in sentence: if tok.idx == start_pos: found_tok = tok break return found_tok
def find_span(sentence, search_text, start=0): search_text = search_text.lower() for tok in sentence[start:]: remainder = sentence[tok.i :].text.lower() if remainder.startswith(search_text): len_to_consume = len(search_text) start_idx = tok.idx for next_tok in sentence[tok.i :]: end_idx = next_tok.idx + len(next_tok.text) if end_idx - start_idx == len_to_consume: span = sentence[tok.i : next_tok.i + 1] return span return None
@lru_cache(maxsize=1)def get_detokenizer(): from sacremoses import MosesDetokenizer
detok = MosesDetokenizer(lang="en") return detok
@lru_cache(maxsize=1)def get_spacy_nlp(): import en_core_web_lg
nlp = en_core_web_lg.load() return nlp
def jsonl_iterator(input_fname, positive_only=False, ngram_order=3, eval=False): detok = get_detokenizer() nlp = get_spacy_nlp()
with open(input_fname) as fin: for line in fin: sample = json.loads(line.strip())
if positive_only and "label" in sample and not sample["label"]: # only consider examples where the query is correct continue
target = sample["target"]
# clean up the query query = target["span1_text"] if query is not None: if "\n" in query: continue if query.endswith(".") or query.endswith(","): query = query[:-1]
# split tokens tokens = sample["text"].split(" ")
def strip_pronoun(x): return x.rstrip('.,"')
# find the pronoun pronoun_idx = target["span2_index"] pronoun = strip_pronoun(target["span2_text"]) if strip_pronoun(tokens[pronoun_idx]) != pronoun: # hack: sometimes the index is misaligned if strip_pronoun(tokens[pronoun_idx + 1]) == pronoun: pronoun_idx += 1 else: raise Exception("Misaligned pronoun!") assert strip_pronoun(tokens[pronoun_idx]) == pronoun
# split tokens before and after the pronoun before = tokens[:pronoun_idx] after = tokens[pronoun_idx + 1 :]
# the GPT BPE attaches leading spaces to tokens, so we keep track # of whether we need spaces before or after the pronoun leading_space = " " if pronoun_idx > 0 else "" trailing_space = " " if len(after) > 0 else ""
# detokenize before = detok.detokenize(before, return_str=True) pronoun = detok.detokenize([pronoun], return_str=True) after = detok.detokenize(after, return_str=True)
# hack: when the pronoun ends in a period (or comma), move the # punctuation to the "after" part if pronoun.endswith(".") or pronoun.endswith(","): after = pronoun[-1] + trailing_space + after pronoun = pronoun[:-1]
# hack: when the "after" part begins with a comma or period, remove # the trailing space if after.startswith(".") or after.startswith(","): trailing_space = ""
# parse sentence with spacy sentence = nlp(before + leading_space + pronoun + trailing_space + after)
# find pronoun span start = len(before + leading_space) first_pronoun_tok = find_token(sentence, start_pos=start) pronoun_span = find_span(sentence, pronoun, start=first_pronoun_tok.i) assert pronoun_span.text == pronoun
if eval: # convert to format where pronoun is surrounded by "[]" and # query is surrounded by "_" query_span = find_span(sentence, query) query_with_ws = "_{}_{}".format( query_span.text, (" " if query_span.text_with_ws.endswith(" ") else ""), ) pronoun_with_ws = "[{}]{}".format( pronoun_span.text, (" " if pronoun_span.text_with_ws.endswith(" ") else ""), ) if query_span.start < pronoun_span.start: first = (query_span, query_with_ws) second = (pronoun_span, pronoun_with_ws) else: first = (pronoun_span, pronoun_with_ws) second = (query_span, query_with_ws) sentence = ( sentence[: first[0].start].text_with_ws + first[1] + sentence[first[0].end : second[0].start].text_with_ws + second[1] + sentence[second[0].end :].text ) yield sentence, sample.get("label", None) else: yield sentence, pronoun_span, query, sample.get("label", None)
def winogrande_jsonl_iterator(input_fname, eval=False): with open(input_fname) as fin: for line in fin: sample = json.loads(line.strip()) sentence, option1, option2 = ( sample["sentence"], sample["option1"], sample["option2"], )
pronoun_span = (sentence.index("_"), sentence.index("_") + 1)
if eval: query, cand = option1, option2 else: query = option1 if sample["answer"] == "1" else option2 cand = option2 if sample["answer"] == "1" else option1 yield sentence, pronoun_span, query, cand
def filter_noun_chunks( chunks, exclude_pronouns=False, exclude_query=None, exact_match=False): if exclude_pronouns: chunks = [ np for np in chunks if (np.lemma_ != "-PRON-" and not all(tok.pos_ == "PRON" for tok in np)) ]
if exclude_query is not None: excl_txt = [exclude_query.lower()] filtered_chunks = [] for chunk in chunks: lower_chunk = chunk.text.lower() found = False for excl in excl_txt: if ( not exact_match and (lower_chunk in excl or excl in lower_chunk) ) or lower_chunk == excl: found = True break if not found: filtered_chunks.append(chunk) chunks = filtered_chunks
return chunks12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152[Better Fine-Tuning by Reducing Representational Collapse](https://arxiv.org/abs/2008.03156)=====================This repo contains the code to replicate all experiments from the _Better Fine-Tuning by Reducing Representational Collapse_ paper excluding the probing results.
The R3F sentence prediction criterion is registered as `sentence_prediction_r3f` while the label smoothing version of it is implemented as `label_smoothed_cross_entropy_r3f`. The R4F version of the sentence prediction criterion can be achieved by applying spectral norm to the classification head via the `--spectral-norm-classification-head` parameter.
## Hyper-parametersOur methods introduce 3 new hyper-parameters; `--eps` which sets the standard deviation or range of the distribution we're sampling from, `--r3f-lambda` which controls the combining of logistic loss and noisy KL loss and `--noise-type` which controls which parametric distribution we use ('normal', 'uniform').
For example to run R3F on RTE from GLUE
```TOTAL_NUM_UPDATES=3120WARMUP_UPDATES=187LR=1e-05NUM_CLASSES=2MAX_SENTENCES=8 # Batch size.ROBERTA_PATH=/path/to/roberta/model.pt
CUDA_VISIBLE_DEVICES=0 fairseq-train RTE-bin \ --restore-file $ROBERTA_PATH \ --max-positions 512 \ --max-sentences $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_r3f \ --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 \ --noise-type uniform --r3f-lambda 0.7 \ --user-dir examples/rxf/rxf_src```
## Citation```bibtex@article{aghajanyan2020better, title={Better Fine-Tuning by Reducing Representational Collapse}, author={Aghajanyan, Armen and Shrivastava, Akshat and Gupta, Anchit and Goyal, Naman and Zettlemoyer, Luke and Gupta, Sonal}, journal={arXiv preprint arXiv:2008.03156}, year={2020}}```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 rxf_src # noqa123456# 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 label_smoothed_cross_entropy_r3f, sentence_prediction_r3f # noqa123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158# 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.functional as Ffrom fairseq import utilsfrom fairseq.logging import metricsfrom fairseq.criterions import FairseqCriterion, register_criterionfrom fairseq.criterions.label_smoothed_cross_entropy import label_smoothed_nll_loss
@register_criterion("label_smoothed_cross_entropy_r3f")class LabelSmoothedCrossEntropyR3FCriterion(FairseqCriterion): def __init__( self, task, sentence_avg, label_smoothing, eps, r3f_lambda, noise_type ): super().__init__(task) self.sentence_avg = sentence_avg self.label_smoothing = label_smoothing self.eps = eps self.r3f_lambda = r3f_lambda self.noise_type = noise_type if self.noise_type in {"normal"}: self.noise_sampler = torch.distributions.normal.Normal( loc=0.0, scale=self.eps ) elif self.noise_type == "uniform": self.noise_sampler = torch.distributions.uniform.Uniform( low=-self.eps, high=self.eps ) else: raise Exception(f"unrecognized noise type {self.noise_type}")
@staticmethod def add_args(parser): """Add criterion-specific arguments to the parser.""" # fmt: off parser.add_argument('--label-smoothing', default=0., type=float, metavar='D', help='epsilon for label smoothing, 0 means no label smoothing') parser.add_argument('--eps', type=float, default=1e-5, help='noise eps') parser.add_argument('--r3f-lambda', type=float, default=1.0, help='lambda for combining logistic loss and noisy KL loss') parser.add_argument('--noise-type', type=str, default='normal', choices=['normal', 'uniform'], help='type of noises') # fmt: on
def _get_symm_kl(self, noised_logits, input_logits): return ( F.kl_div( F.log_softmax(noised_logits, dim=-1, dtype=torch.float32), F.softmax(input_logits, dim=-1, dtype=torch.float32), None, None, "sum", ) + F.kl_div( F.log_softmax(input_logits, dim=-1, dtype=torch.float32), F.softmax(noised_logits, dim=-1, dtype=torch.float32), None, None, "sum", ) ) / noised_logits.size(0)
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 """ token_embeddings = model.encoder.embed_tokens(sample["net_input"]["src_tokens"]) input_logits, extra = model(**sample["net_input"]) loss, nll_loss = self.compute_loss( model, (input_logits, extra), sample, reduce=reduce ) sample_size = ( sample["target"].size(0) if self.sentence_avg else sample["ntokens"] )
if model.training: noise = self.noise_sampler.sample(sample_shape=token_embeddings.shape).to( token_embeddings ) noised_embeddings = token_embeddings.clone() + noise
noised_logits, _ = model( **sample["net_input"], token_embeddings=noised_embeddings ) symm_kl = self._get_symm_kl(noised_logits, input_logits)
if model.training: symm_kl = symm_kl * sample_size loss = loss + self.r3f_lambda * symm_kl
logging_output = { "loss": loss.data, "nll_loss": nll_loss.data, "ntokens": sample["ntokens"], "nsentences": sample["target"].size(0), "sample_size": sample_size, }
if model.training: logging_output.update( symm_kl=utils.item(symm_kl.data) if reduce else symm_kl.data )
return loss, sample_size, logging_output
def compute_loss(self, model, net_output, sample, reduce=True): lprobs = model.get_normalized_probs(net_output, log_probs=True) lprobs = lprobs.view(-1, lprobs.size(-1)) target = model.get_targets(sample, net_output).view(-1, 1) loss, nll_loss = label_smoothed_nll_loss( lprobs, target, self.label_smoothing, ignore_index=self.padding_idx, reduce=reduce, ) return loss, nll_loss
@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) nll_loss_sum = sum(log.get("nll_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) symm_kl_sum = sum(log.get("symm_kl", 0) for log in logging_outputs)
metrics.log_scalar("symm_kl", symm_kl_sum / sample_size, sample_size, round=3) metrics.log_scalar( "loss", loss_sum / sample_size / math.log(2), sample_size, round=3 ) metrics.log_scalar( "nll_loss", nll_loss_sum / ntokens / math.log(2), ntokens, round=3 ) metrics.log_derived( "ppl", lambda meters: utils.get_perplexity(meters["nll_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 True123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171# 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.functional as Ffrom fairseq import utilsfrom fairseq.criterions import FairseqCriterion, register_criterion
@register_criterion("sentence_prediction_r3f")class SentencePredictionR3F(FairseqCriterion): def __init__( self, task, eps, r3f_lambda, noise_type, classification_head_name, regression_target, ): super().__init__(task) self.eps = eps self.r3f_lambda = r3f_lambda self.noise_type = noise_type self.classification_head_name = classification_head_name self.regression_target = regression_target if self.noise_type in {"normal"}: self.noise_sampler = torch.distributions.normal.Normal( loc=0.0, scale=self.eps ) elif self.noise_type == "uniform": self.noise_sampler = torch.distributions.uniform.Uniform( low=-self.eps, high=self.eps ) else: raise Exception(f"unrecognized noise type {self.noise_type}")
@staticmethod def add_args(parser): # fmt: off parser.add_argument('--eps', type=float, default=1e-5, help='noise eps') parser.add_argument('--r3f-lambda', type=float, default=1.0, help='lambda for combining logistic loss and noisy KL loss') parser.add_argument('--noise-type', type=str, default='uniform', choices=['normal', 'uniform'], help='type of noises for RXF methods') parser.add_argument('--classification-head-name', default='sentence_classification_head', help='name of the classification head to use') parser.add_argument('--regression-target', action='store_true') # fmt: on
def _get_symm_kl(self, noised_logits, input_logits): return ( F.kl_div( F.log_softmax(noised_logits, dim=-1, dtype=torch.float32), F.softmax(input_logits, dim=-1, dtype=torch.float32), None, None, "sum", ) + F.kl_div( F.log_softmax(input_logits, dim=-1, dtype=torch.float32), F.softmax(noised_logits, dim=-1, dtype=torch.float32), None, None, "sum", ) ) / noised_logits.size(0)
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 """ assert ( hasattr(model, "classification_heads") and self.classification_head_name in model.classification_heads ), "model must provide sentence classification head for --criterion=sentence_prediction"
token_embeddings = model.encoder.sentence_encoder.embed_tokens( sample["net_input"]["src_tokens"] ) input_logits, _ = model( **sample["net_input"], features_only=True, classification_head_name=self.classification_head_name, token_embeddings=token_embeddings, ) if model.training and self.noise_sampler: noise = self.noise_sampler.sample(sample_shape=token_embeddings.shape).to( token_embeddings ) noised_embeddings = token_embeddings.detach().clone() + noise
noised_logits, _ = model( **sample["net_input"], features_only=True, classification_head_name=self.classification_head_name, token_embeddings=noised_embeddings, ) symm_kl = self._get_symm_kl(noised_logits, input_logits) else: symm_kl = 0
targets = model.get_targets(sample, [input_logits]).view(-1) sample_size = targets.numel()
if not self.regression_target: loss = F.nll_loss( F.log_softmax(input_logits, dim=-1, dtype=torch.float32), targets, reduction="sum", ) if model.training: symm_kl = symm_kl * sample_size loss = loss + self.r3f_lambda * symm_kl else: logits = input_logits.squeeze().float() targets = targets.float() loss = F.mse_loss(logits, targets, reduction="sum")
logging_output = { "loss": utils.item(loss.data) if reduce else loss.data, "ntokens": sample["ntokens"], "nsentences": sample_size, "sample_size": sample_size, }
if not self.regression_target: preds = input_logits.max(dim=1)[1] logging_output.update(ncorrect=(preds == targets).sum().item())
if model.training and self.noise_sampler: logging_output.update( symm_kl=utils.item(symm_kl.data) if reduce else symm_kl.data ) return loss, sample_size, logging_output
@staticmethod def aggregate_logging_outputs(logging_outputs): """Aggregate logging outputs from data parallel training.""" loss_sum = sum(log.get("loss", 0) for log in logging_outputs) symm_kl_sum = sum(log.get("symm_kl", 0) for log in logging_outputs) ntokens = sum(log.get("ntokens", 0) for log in logging_outputs) nsentences = sum(log.get("nsentences", 0) for log in logging_outputs) sample_size = sum(log.get("sample_size", 0) for log in logging_outputs)
agg_output = { "loss": loss_sum / sample_size / math.log(2), "symm_kl": symm_kl_sum / sample_size, "ntokens": ntokens, "nsentences": nsentences, "sample_size": sample_size, }
if len(logging_outputs) > 0 and "ncorrect" in logging_outputs[0]: ncorrect = sum(log.get("ncorrect", 0) for log in logging_outputs) agg_output.update(accuracy=ncorrect / nsentences)
if sample_size != ntokens: agg_output["nll_loss"] = loss_sum / ntokens / math.log(2) return agg_output123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114# Scaling Neural Machine Translation (Ott et al., 2018)
This page includes instructions for reproducing results from the paper [Scaling Neural Machine Translation (Ott et al., 2018)](https://arxiv.org/abs/1806.00187).
## Pre-trained models
Model | Description | Dataset | Download---|---|---|---`transformer.wmt14.en-fr` | Transformer <br> ([Ott et al., 2018](https://arxiv.org/abs/1806.00187)) | [WMT14 English-French](http://statmt.org/wmt14/translation-task.html#Download) | model: <br> [download (.tar.bz2)](https://dl.fbaipublicfiles.com/fairseq/models/wmt14.en-fr.joined-dict.transformer.tar.bz2) <br> newstest2014: <br> [download (.tar.bz2)](https://dl.fbaipublicfiles.com/fairseq/data/wmt14.en-fr.joined-dict.newstest2014.tar.bz2)`transformer.wmt16.en-de` | Transformer <br> ([Ott et al., 2018](https://arxiv.org/abs/1806.00187)) | [WMT16 English-German](https://drive.google.com/uc?export=download&id=0B_bZck-ksdkpM25jRUN2X2UxMm8) | model: <br> [download (.tar.bz2)](https://dl.fbaipublicfiles.com/fairseq/models/wmt16.en-de.joined-dict.transformer.tar.bz2) <br> newstest2014: <br> [download (.tar.bz2)](https://dl.fbaipublicfiles.com/fairseq/data/wmt16.en-de.joined-dict.newstest2014.tar.bz2)
## Training a new model on WMT'16 En-De
First download the [preprocessed WMT'16 En-De data provided by Google](https://drive.google.com/uc?export=download&id=0B_bZck-ksdkpM25jRUN2X2UxMm8).
Then:
##### 1. Extract the WMT'16 En-De data```bashTEXT=wmt16_en_de_bpe32kmkdir -p $TEXTtar -xzvf wmt16_en_de.tar.gz -C $TEXT```
##### 2. Preprocess the dataset with a joined dictionary```bashfairseq-preprocess \ --source-lang en --target-lang de \ --trainpref $TEXT/train.tok.clean.bpe.32000 \ --validpref $TEXT/newstest2013.tok.bpe.32000 \ --testpref $TEXT/newstest2014.tok.bpe.32000 \ --destdir data-bin/wmt16_en_de_bpe32k \ --nwordssrc 32768 --nwordstgt 32768 \ --joined-dictionary \ --workers 20```
##### 3. Train a model```bashfairseq-train \ data-bin/wmt16_en_de_bpe32k \ --arch transformer_vaswani_wmt_en_de_big --share-all-embeddings \ --optimizer adam --adam-betas '(0.9, 0.98)' --clip-norm 0.0 \ --lr 0.0005 --lr-scheduler inverse_sqrt --warmup-updates 4000 --warmup-init-lr 1e-07 \ --dropout 0.3 --weight-decay 0.0 \ --criterion label_smoothed_cross_entropy --label-smoothing 0.1 \ --max-tokens 3584 \ --fp16```
Note that the `--fp16` flag requires you have CUDA 9.1 or greater and a Volta GPU or newer.
***IMPORTANT:*** You will get better performance by training with big batches andincreasing the learning rate. If you want to train the above model with big batches(assuming your machine has 8 GPUs):- add `--update-freq 16` to simulate training on 8x16=128 GPUs- increase the learning rate; 0.001 works well for big batches
##### 4. Evaluate
Now we can evaluate our trained model.
Note that the original [Attention Is All You Need](https://arxiv.org/abs/1706.03762)paper used a couple tricks to achieve better BLEU scores. We use these same tricks inthe Scaling NMT paper, so it's important to apply them when reproducing our results.
First, use the [average_checkpoints.py](/scripts/average_checkpoints.py) script toaverage the last few checkpoints. Averaging the last 5-10 checkpoints is usuallygood, but you may need to adjust this depending on how long you've trained:```bashpython scripts/average_checkpoints \ --inputs /path/to/checkpoints \ --num-epoch-checkpoints 10 \ --output checkpoint.avg10.pt```
Next, generate translations using a beam width of 4 and length penalty of 0.6:```bashfairseq-generate \ data-bin/wmt16_en_de_bpe32k \ --path checkpoint.avg10.pt \ --beam 4 --lenpen 0.6 --remove-bpe > gen.out```
Finally, we apply the ["compound splitting" script](/scripts/compound_split_bleu.sh) toadd spaces around dashes. For example "Café-Liebhaber" would become three tokens:"Café - Liebhaber". This typically results in larger BLEU scores, but it is notappropriate to compare these inflated scores to work which does not include this trick.This trick was used in the [original AIAYN code](https://github.com/tensorflow/tensor2tensor/blob/fc9335c0203685cbbfe2b30c92db4352d8f60779/tensor2tensor/utils/get_ende_bleu.sh),so we used it in the Scaling NMT paper as well. That said, it's strongly advised toreport [sacrebleu](https://github.com/mjpost/sacrebleu) scores instead.
To compute "compound split" tokenized BLEU (not recommended!):```bashbash scripts/compound_split_bleu.sh gen.out# BLEU4 = 29.29, 60.3/35.0/22.8/15.3 (BP=1.000, ratio=1.004, syslen=64763, reflen=64496)```
To compute detokenized BLEU with sacrebleu (preferred):```bashbash scripts/sacrebleu.sh wmt14/full en de gen.out# BLEU+case.mixed+lang.en-de+numrefs.1+smooth.exp+test.wmt14/full+tok.13a+version.1.4.3 = 28.6 59.3/34.3/22.1/14.9 (BP = 1.000 ratio = 1.016 hyp_len = 63666 ref_len = 62688)```
## Citation
```bibtex@inproceedings{ott2018scaling, title = {Scaling Neural Machine Translation}, author = {Ott, Myle and Edunov, Sergey and Grangier, David and Auli, Michael}, booktitle = {Proceedings of the Third Conference on Machine Translation (WMT)}, year = 2018,}```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))
```.gitattributes+1
fairseq/examples/hubert/tests/6313-76958-0021.flacAdded+3
fairseq/examples/pointer_generator/README.xsum.mdAdded+180
fairseq/examples/pointer_generator/pointer_generator_src/__init__.pyAdded+6
fairseq/examples/pointer_generator/pointer_generator_src/transformer_pg.pyAdded+518
fairseq/examples/pointer_generator/postprocess.pyAdded+96
fairseq/examples/quant_noise/transformer_quantization_config.yamlAdded+33
fairseq/examples/roberta/README.custom_classification.mdAdded+168
fairseq/examples/roberta/README.glue.mdAdded+64
fairseq/examples/roberta/README.mdAdded+296
fairseq/examples/roberta/README.pretraining.mdAdded+84
fairseq/examples/roberta/README.race.mdAdded+68
fairseq/examples/roberta/commonsense_qa/README.mdAdded+99
fairseq/examples/roberta/commonsense_qa/__init__.pyAdded+6
fairseq/examples/roberta/commonsense_qa/commonsense_qa_task.pyAdded+190
fairseq/examples/roberta/commonsense_qa/download_cqa_data.shAdded+14
fairseq/examples/roberta/config/finetuning/cola.yamlAdded+59
fairseq/examples/roberta/config/finetuning/mnli.yamlAdded+59
fairseq/examples/roberta/config/finetuning/mrpc.yamlAdded+59
fairseq/examples/roberta/config/finetuning/qnli.yamlAdded+59
fairseq/examples/roberta/config/finetuning/qqp.yamlAdded+59
fairseq/examples/roberta/config/finetuning/rte.yamlAdded+59
fairseq/examples/roberta/config/finetuning/run_config/local.yamlAdded+15
fairseq/examples/roberta/config/finetuning/run_config/slurm_1g.yamlAdded+28
fairseq/examples/roberta/config/finetuning/run_config/slurm_1g_aws.yamlAdded+25
fairseq/examples/roberta/config/finetuning/sst_2.yamlAdded+59
fairseq/examples/roberta/config/finetuning/sts_b.yamlAdded+58
fairseq/examples/roberta/config/pretraining/base.yamlAdded+42
fairseq/examples/roberta/config/pretraining/run_config/local.yamlAdded+15
fairseq/examples/roberta/config/pretraining/run_config/slurm_2.yamlAdded+37
fairseq/examples/roberta/config/pretraining/run_config/slurm_2_aws.yamlAdded+39
fairseq/examples/roberta/config/pretraining/run_config/slurm_3.yamlAdded+36
fairseq/examples/roberta/config/pretraining/run_config/slurm_4.yamlAdded+36
fairseq/examples/roberta/fb_multilingual/README.multilingual.pretraining.mdAdded+26
fairseq/examples/roberta/multiprocessing_bpe_encoder.pyAdded+130
fairseq/examples/roberta/preprocess_GLUE_tasks.shAdded+185
fairseq/examples/roberta/preprocess_RACE.pyAdded+102
fairseq/examples/roberta/preprocess_RACE.shAdded+59
fairseq/examples/roberta/wsc/README.mdAdded+125
fairseq/examples/roberta/wsc/__init__.pyAdded+7
fairseq/examples/roberta/wsc/wsc_criterion.pyAdded+167
fairseq/examples/roberta/wsc/wsc_task.pyAdded+401
fairseq/examples/roberta/wsc/wsc_utils.pyAdded+241
fairseq/examples/rxf/README.mdAdded+52
fairseq/examples/rxf/__init__.pyAdded+6
fairseq/examples/rxf/rxf_src/__init__.pyAdded+6
fairseq/examples/rxf/rxf_src/label_smoothed_cross_entropy_r3f.pyAdded+158
fairseq/examples/rxf/rxf_src/sentence_prediction_r3f.pyAdded+171
fairseq/examples/scaling_nmt/README.mdAdded+114
fairseq/examples/shuffled_word_order/README.finetuning.mdAdded+135