### Running fairseq-train with Custom Module Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/overview.rst Command line example showing how to invoke the fairseq-train script and load a custom module containing new plug-ins using the --user-dir flag, specifying a custom architecture. ```Shell fairseq-train ... --user-dir /home/user/my-module -a my_transformer --task translation ``` -------------------------------- ### Custom Module __init__.py for Fairseq Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/overview.rst Example content for an __init__.py file within a custom user module, showing how to import necessary components and register a new model architecture based on an existing one. ```Python from fairseq.models import register_model_architecture from fairseq.models.transformer import transformer_vaswani_wmt_en_de_big @register_model_architecture('transformer', 'my_transformer') def transformer_mmt_big(args): transformer_vaswani_wmt_en_de_big(args) ``` -------------------------------- ### Setup Simple Classification Task (Python) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst Loads the input and label dictionaries required for the task from specified file paths. Prints the size of each loaded dictionary and returns an instance of the SimpleClassificationTask. ```Python # loading Dictionaries, initializing shared Embedding layers, etc. # In this case we'll just load the Dictionaries. input_vocab = Dictionary.load(os.path.join(args.data, 'dict.input.txt')) label_vocab = Dictionary.load(os.path.join(args.data, 'dict.label.txt')) print('| [input] dictionary: {} types'.format(len(input_vocab))) print('| [label] dictionary: {} types'.format(len(label_vocab))) return SimpleClassificationTask(args, input_vocab, label_vocab) ``` -------------------------------- ### Training Fairseq Model with Custom Architecture Console Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_simple_lstm.rst Example command-line instruction to train the Fairseq model using the `fairseq-train` tool. It specifies the data directory, the custom named architecture (`tutorial_simple_lstm`), dropout rates, optimizer settings, learning rate, and batch size parameters. ```console > fairseq-train data-bin/iwslt14.tokenized.de-en \ --arch tutorial_simple_lstm \ --encoder-dropout 0.2 --decoder-dropout 0.2 \ --optimizer adam --lr 0.005 --lr-shrink 0.5 \ --max-tokens 12000 ``` -------------------------------- ### Pre-process and Binarize IWSLT 2014 Dataset Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/getting_started.rst Navigates to the examples directory, runs a script to prepare the IWSLT 2014 German-English dataset, sets a text variable, and then uses fairseq-preprocess to tokenize and binarize the training, validation, and test sets, saving the output to a specified directory. ```console cd examples/translation/ bash prepare-iwslt14.sh cd ../.. TEXT=examples/translation/iwslt14.tokenized.de-en fairseq-preprocess --source-lang de --target-lang en \ --trainpref $TEXT/train --validpref $TEXT/valid --testpref $TEXT/test \ --destdir data-bin/iwslt14.tokenized.de-en ``` -------------------------------- ### Launch Distributed Training with torch.distributed.launch (Console) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/getting_started.rst Command to launch distributed training for a fairseq model across multiple nodes and GPUs using the `torch.distributed.launch` utility. This example demonstrates training a large Transformer model on 2 nodes with 8 GPUs each, configuring node rank, master address, and various training parameters. ```console > python -m torch.distributed.launch --nproc_per_node=8 \ --nnodes=2 --node_rank=0 --master_addr=\"192.168.1.1\" \ --master_port=1234 \ $(which fairseq-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-scheduler inverse_sqrt --warmup-init-lr 1e-07 --warmup-updates 4000 \ --lr 0.0005 --min-lr 1e-09 \ --dropout 0.3 --weight-decay 0.0 --criterion label_smoothed_cross_entropy --label-smoothing 0.1 \ --max-tokens 3584 \ --fp16 --distributed-no-spawn ``` -------------------------------- ### Registering a Fairseq Model Plug-in Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/overview.rst Example demonstrating how to register a new model plug-in in Fairseq using the @register_model decorator, making it available for use with command-line tools. ```Python @register_model('my_lstm') class MyLSTM(FairseqEncoderDecoderModel): (...) ``` -------------------------------- ### Generating Translations with fairseq Console Command Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_simple_lstm.rst Example command to generate translations using a trained fairseq model. It specifies the data binary, model path, beam size, and removes BPE tokens. ```console > fairseq-generate data-bin/iwslt14.tokenized.de-en \ --path checkpoints/checkpoint_best.pt \ --beam 5 \ --remove-bpe ``` -------------------------------- ### Run T-Fixup Training Script (Shell) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/README.md Executes the example shell script to build the IWSLT'14 De-En dataset and run the T-Fixup training process for the small Transformer model. This script removes layer normalization and learning rate warmup, setting a fixed starting learning rate. ```Shell ./Train_IWSLT_TFixup_example.sh ``` -------------------------------- ### Example Batch Iterator Override (Python) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst Commented-out example showing the signature of the `get_batch_iterator` method, which could be overridden for custom batch construction if needed, although it's not required for this simple task. ```Python # We could override this method if we wanted more control over how batches # are constructed, but it's not necessary for this tutorial since we can # reuse the batching provided by LanguagePairDataset. # # def get_batch_iterator( # self, dataset, max_tokens=None, max_sentences=None, max_positions=None, # ignore_invalid_inputs=False, required_batch_size_multiple=1, # seed=1, num_shards=1, shard_id=0, # ): # (...) ``` -------------------------------- ### Registering Simple Classification Task in Fairseq Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst Shows how to define and register a custom Fairseq task named 'simple_classification' using the `@register_task` decorator. It includes the `add_args` method to define command-line arguments for data location and max input length, and the `setup_task` method for task-specific setup. ```Python import os import torch from fairseq.data import Dictionary, LanguagePairDataset from fairseq.tasks import FairseqTask, register_task @register_task('simple_classification') class SimpleClassificationTask(FairseqTask): @staticmethod def add_args(parser): # Add some command-line arguments for specifying where the data is # located and the maximum supported input length. parser.add_argument('data', metavar='FILE', help='file prefix for data') parser.add_argument('--max-positions', default=1024, type=int, help='max input length') @classmethod def setup_task(cls, args, **kwargs): # Here we can perform any setup required for the task. This may include ``` -------------------------------- ### Get Source Dictionary Property (Python) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst A property that provides access to the source dictionary (input vocabulary) associated with the task. ```Python @property def source_dictionary(self): """Return the source :class:`~fairseq.data.Dictionary`.""" return self.input_vocab ``` -------------------------------- ### Train with Large Effective Batch Size (Console) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/getting_started.rst Command to simulate a larger batch size on a single GPU by accumulating gradients over multiple mini-batches before updating. This is achieved using the `--update-freq` option. The example shows how to achieve an effective batch size equivalent to 8 GPUs on a single device. ```console > CUDA_VISIBLE_DEVICES=0 fairseq-train --update-freq 8 (...) ``` -------------------------------- ### Get Target Dictionary Property (Python) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst A property that provides access to the target dictionary (label vocabulary) associated with the task. ```Python @property def target_dictionary(self): """Return the target :class:`~fairseq.data.Dictionary`.""" return self.label_vocab ``` -------------------------------- ### Running Fairseq Evaluation Script - Console Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst This console command demonstrates how to execute the `eval_classifier.py` script. It specifies the data directory (`names-bin`) for loading dictionaries and the path to the best model checkpoint (`checkpoints/checkpoint_best.pt`). The output shows the script loading dictionaries and the model, followed by interactive input prompts and model predictions for sample inputs ("Satoshi" and "Sinbad"). ```Console > python eval_classifier.py names-bin --path checkpoints/checkpoint_best.pt | [input] dictionary: 64 types | [label] dictionary: 24 types | loading model from checkpoints/checkpoint_best.pt Input: Satoshi (-0.61) Japanese (-1.20) Arabic (-2.86) Italian Input: Sinbad (-0.30) Arabic (-1.76) English (-4.08) Russian ``` -------------------------------- ### Launch Tensorboard for Monitoring (Shell) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/README.md Launches Tensorboard to visualize and monitor the training progress. Replace `` with the directory where training logs are saved. ```Shell tensorboard --logdir= ``` -------------------------------- ### Preprocessing Data with fairseq-preprocess (Console) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst Demonstrates how to use the `fairseq-preprocess` command-line tool to prepare data for a classification task. It specifies input, validation, and test prefixes, source and target languages, destination directory, and dataset implementation format. ```Console fairseq-preprocess \ --trainpref names/train --validpref names/valid --testpref names/test \ --source-lang input --target-lang label \ --destdir names-bin --dataset-impl raw ``` -------------------------------- ### Fairseq High-Level Training Loop Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/overview.rst Illustrates the high-level training flow implemented by fairseq, showing the iteration over epochs and batches, calling task.train_step, gradient handling, and optimizer/scheduler steps. ```Python for epoch in range(num_epochs): itr = task.get_batch_iterator(task.dataset('train')) for num_updates, batch in enumerate(itr): task.train_step(batch, model, criterion, optimizer) average_and_clip_gradients() optimizer.step() lr_scheduler.step_update(num_updates) lr_scheduler.step(epoch) ``` -------------------------------- ### Train Simple Classification Model (Console) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst Command-line instruction using `fairseq-train` to train the model. It specifies the custom task, architecture, optimizer, learning rate settings, and maximum tokens per batch. ```Console fairseq-train names-bin \ --task simple_classification \ --arch pytorch_tutorial_rnn \ --optimizer adam --lr 0.001 --lr-shrink 0.5 \ --max-tokens 1000 ``` -------------------------------- ### Load Dataset for Simple Classification Task (Python) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst Loads a specific dataset split (e.g., train, valid, test) by reading input sentences and corresponding labels from files. It tokenizes inputs, converts labels to numeric IDs, and constructs a LanguagePairDataset suitable for classification. ```Python def load_dataset(self, split, **kwargs): """Load a given dataset split (e.g., train, valid, test).""" prefix = os.path.join(self.args.data, '{}.input-label'.format(split)) # Read input sentences. sentences, lengths = [], [] with open(prefix + '.input', encoding='utf-8') as file: for line in file: sentence = line.strip() # Tokenize the sentence, splitting on spaces tokens = self.input_vocab.encode_line( sentence, add_if_not_exist=False, ) sentences.append(tokens) lengths.append(tokens.numel()) # Read labels. labels = [] with open(prefix + '.label', encoding='utf-8') as file: for line in file: label = line.strip() labels.append( # Convert label to a numeric ID. torch.LongTensor([self.label_vocab.add_symbol(label)]) ) assert len(sentences) == len(labels) print('| {} {} {} examples'.format(self.args.data, split, len(sentences))) # We reuse LanguagePairDataset since classification can be modeled as a # sequence-to-sequence task where the target sequence has length 1. self.datasets[split] = LanguagePairDataset( src=sentences, src_sizes=lengths, src_dict=self.input_vocab, tgt=labels, tgt_sizes=torch.ones(len(labels)), # targets have length 1 tgt_dict=self.label_vocab, left_pad_source=False, max_source_positions=self.args.max_positions, max_target_positions=1, # Since our target is a single class label, there's no need for # teacher forcing. If we set this to ``True`` then our Model's # ``forward()`` method would receive an additional argument called # *prev_output_tokens* that would contain a shifted version of the # target sequence. input_feeding=False, ) ``` -------------------------------- ### Using Fairseq Tasks in Python Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tasks.rst This snippet demonstrates the typical workflow for using a Fairseq Task. It covers setting up the task, building the model and criterion, loading datasets, iterating over data batches, and computing the loss for a batch. ```Python # setup the task (e.g., load dictionaries) task = fairseq.tasks.setup_task(args) # build model and criterion model = task.build_model(args) criterion = task.build_criterion(args) # load datasets task.load_dataset('train') task.load_dataset('valid') # iterate over mini-batches of data batch_itr = task.get_batch_iterator( task.dataset('train'), max_tokens=4096, ) for batch in batch_itr: # compute the loss loss, sample_size, logging_output = task.get_loss( model, criterion, batch, ) loss.backward() ``` -------------------------------- ### Enable FP16 Training (Console) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/getting_started.rst Command to enable half-precision floating-point (FP16) training in fairseq. This feature requires a compatible GPU (e.g., Nvidia Volta) and CUDA 9.1 or greater to leverage efficient half-precision computation. ```console > fairseq-train --fp16 (...) ``` -------------------------------- ### Initialize Simple Classification Task (Python) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst Constructor for the SimpleClassificationTask class. It calls the constructor of the parent class and stores the provided input and label vocabulary dictionaries as instance attributes. ```Python def __init__(self, args, input_vocab, label_vocab): super().__init__(args) self.input_vocab = input_vocab self.label_vocab = label_vocab ``` -------------------------------- ### Download and Extract Pre-trained Fairseq Model Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/getting_started.rst Downloads a pre-trained Fairseq model (WMT14 English-French fconv-py) and extracts its contents using curl and tar. This provides the model weights and associated vocabulary files. ```console curl https://dl.fbaipublicfiles.com/fairseq/models/wmt14.v2.en-fr.fconv-py.tar.bz2 | tar xvjf - ``` -------------------------------- ### Interactive Translation with fairseq-interactive Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/getting_started.rst Uses the fairseq-interactive tool to perform interactive machine translation. It loads a specified model, sets beam size, source/target languages, and applies Moses tokenization and Subword NMT BPE using the provided codes file. It automatically handles BPE removal and detokenization. ```console MODEL_DIR=wmt14.en-fr.fconv-py fairseq-interactive \ --path $MODEL_DIR/model.pt $MODEL_DIR \ --beam 5 --source-lang en --target-lang fr \ --tokenizer moses \ --bpe subword_nmt --bpe-codes $MODEL_DIR/bpecodes ``` -------------------------------- ### Demonstrating fairseq-generate Speedup in Console Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_simple_lstm.rst This console output snippet shows the results of running the `fairseq-generate` command before and after an optimization. It compares the time taken to translate a set of sentences, clearly demonstrating a significant reduction in generation time (from 17.3s to 5.5s) and a corresponding increase in sentences/second and tokens/second throughput, while maintaining the same BLEU score. ```Console # Before > fairseq-generate data-bin/iwslt14.tokenized.de-en \ --path checkpoints/checkpoint_best.pt \ --beam 5 \ --remove-bpe (...) | Translated 6750 sentences (153132 tokens) in 17.3s (389.12 sentences/s, 8827.68 tokens/s) | Generate test with beam=5: BLEU4 = 8.18, 38.8/12.1/4.7/2.0 (BP=1.000, ratio=1.066, syslen=139865, reflen=131146) # After > fairseq-generate data-bin/iwslt14.tokenized.de-en \ --path checkpoints/checkpoint_best.pt \ --beam 5 \ --remove-bpe (...) | Translated 6750 sentences (153132 tokens) in 5.5s (1225.54 sentences/s, 27802.94 tokens/s) | Generate test with beam=5: BLEU4 = 8.18, 38.8/12.1/4.7/2.0 (BP=1.000, ratio=1.066, syslen=139865, reflen=131146) ``` -------------------------------- ### Building Fairseq Simple LSTM Model Python Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_simple_lstm.rst Implements the `build_model` class method required by Fairseq. It initializes the `SimpleLSTMEncoder` and `SimpleLSTMDecoder` instances with the provided arguments and dictionaries, then combines them into a `SimpleLSTMModel` instance. ```Python @classmethod def build_model(cls, args, task): # Fairseq initializes models by calling the ``build_model()`` # function. This provides more flexibility, since the returned model # instance can be of a different type than the one that was called. # In this case we'll just return a SimpleLSTMModel instance. # Initialize our Encoder and Decoder. encoder = SimpleLSTMEncoder( args=args, dictionary=task.source_dictionary, embed_dim=args.encoder_embed_dim, hidden_dim=args.encoder_hidden_dim, dropout=args.encoder_dropout, ) decoder = SimpleLSTMDecoder( dictionary=task.target_dictionary, encoder_hidden_dim=args.encoder_hidden_dim, embed_dim=args.decoder_embed_dim, hidden_dim=args.decoder_hidden_dim, dropout=args.decoder_dropout, ) model = SimpleLSTMModel(encoder, decoder) # Print the model architecture. print(model) return model ``` -------------------------------- ### Evaluating Fairseq Simple Classification Model - Python Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst This Python script uses the Fairseq library to load a pre-trained simple classification model and evaluate it interactively. It takes model path and data path as arguments, tokenizes user input character by character, builds a batch, feeds it to the model, and prints the top 3 predicted labels with their scores. ```Python from fairseq import checkpoint_utils, data, options, tasks # Parse command-line arguments for generation parser = options.get_generation_parser(default_task='simple_classification') args = options.parse_args_and_arch(parser) # Setup task task = tasks.setup_task(args) # Load model print('| loading model from {}'.format(args.path)) models, _model_args = checkpoint_utils.load_model_ensemble([args.path], task=task) model = models[0] while True: sentence = input('\nInput: ') # Tokenize into characters chars = ' '.join(list(sentence.strip())) tokens = task.source_dictionary.encode_line( chars, add_if_not_exist=False, ) # Build mini-batch to feed to the model batch = data.language_pair_dataset.collate( samples=[{'id': -1, 'source': tokens}], # bsz = 1 pad_idx=task.source_dictionary.pad(), eos_idx=task.source_dictionary.eos(), left_pad_source=False, input_feeding=False, ) # Feed batch to the model and get predictions preds = model(**batch['net_input']) # Print top 3 predictions and their log-probabilities top_scores, top_labels = preds[0].topk(k=3) for score, label_idx in zip(top_scores, top_labels): label_name = task.target_dictionary.string([label_idx]) print('({:.2f})\t{}'.format(score, label_name)) ``` -------------------------------- ### Train a New Fairseq Model Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/getting_started.rst Creates a directory for checkpoints, sets the CUDA_VISIBLE_DEVICES environment variable to use a specific GPU, and runs fairseq-train. It trains a model on the binarized IWSLT 2014 data using specified hyperparameters (learning rate, clip norm, dropout, max tokens) and architecture (fconv_iwslt_de_en), saving checkpoints to the specified directory. ```console mkdir -p checkpoints/fconv CUDA_VISIBLE_DEVICES=0 fairseq-train data-bin/iwslt14.tokenized.de-en \ --lr 0.25 --clip-norm 0.1 --dropout 0.2 --max-tokens 4000 \ --arch fconv_iwslt_de_en --save-dir checkpoints/fconv ``` -------------------------------- ### Generate Translations from Trained Model Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/getting_started.rst Uses the fairseq-generate tool to produce translations for a binarized test set using a trained model. It specifies the data directory, the path to the best checkpoint file, batch size, and beam size. ```console fairseq-generate data-bin/iwslt14.tokenized.de-en \ --path checkpoints/fconv/checkpoint_best.pt \ --batch-size 128 --beam 5 ``` -------------------------------- ### Fairseq Default task.train_step Implementation Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/overview.rst Shows the default implementation of the task.train_step method, which calculates the loss using the criterion and performs the backward pass on the optimizer. ```Python def train_step(self, batch, model, criterion, optimizer): loss = criterion(model, batch) optimizer.backward(loss) return loss ``` -------------------------------- ### Default Fairseq EncoderDecoderModel Forward Method Python Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_simple_lstm.rst Shows the default implementation of the `forward` method in the `FairseqEncoderDecoderModel` base class. It demonstrates how the encoder output is passed to the decoder. This method can be overridden for custom behavior. ```Python # We could override the ``forward()`` if we wanted more control over how # the encoder and decoder interact, but it's not necessary for this # tutorial since we can inherit the default implementation provided by # the FairseqEncoderDecoderModel base class, which looks like: # # def forward(self, src_tokens, src_lengths, prev_output_tokens): # encoder_out = self.encoder(src_tokens, src_lengths) # decoder_out = self.decoder(prev_output_tokens, encoder_out) # return decoder_out ``` -------------------------------- ### Registering RNN as Fairseq Model (Python) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst Defines a wrapper class `FairseqRNNClassifier` that inherits from `BaseFairseqModel` and is registered with Fairseq using the `@register_model` decorator. It includes methods `add_args` for adding command-line arguments and `build_model` for model initialization within the Fairseq framework. Note: The provided code snippet is incomplete. ```Python from fairseq.models import BaseFairseqModel, register_model # Note: the register_model "decorator" should immediately precede the # definition of the Model class. @register_model('rnn_classifier') class FairseqRNNClassifier(BaseFairseqModel): @staticmethod def add_args(parser): # Models can override this method to add new command-line arguments. # Here we'll add a new command-line argument to configure the # dimensionality of the hidden state. parser.add_argument( '--hidden-dim', type=int, metavar='N', help='dimensionality of the hidden state', ) @classmethod def build_model(cls, args, task): # Fairseq initializes models by calling the ``build_model()`` # function. This provides more flexibility, since the returned model # instance can be of a different type than the one that was called. # In this case we'll just return a FairseqRNNClassifier instance. # Initialize our RNN module rnn = RNN( ``` -------------------------------- ### Registering Simple LSTM Model in Fairseq (Python) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_simple_lstm.rst This class wraps the encoder and decoder components and registers the complete model with the fairseq framework using the @register_model decorator. It inherits from FairseqEncoderDecoderModel and includes a static add_args method to define command-line arguments for model configuration like dropout and dimensionality. ```Python from fairseq.models import FairseqEncoderDecoderModel, register_model # Note: the register_model "decorator" should immediately precede the # definition of the Model class. @register_model('simple_lstm') class SimpleLSTMModel(FairseqEncoderDecoderModel): @staticmethod def add_args(parser): # Models can override this method to add new command-line arguments. # Here we'll add some new command-line arguments to configure dropout # and the dimensionality of the embeddings and hidden states. parser.add_argument( ``` -------------------------------- ### Defining PyTorch Character-Level RNN Module (Python) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst Implements a basic RNN module for character-level processing, based on the PyTorch tutorial. It includes linear layers for input-to-hidden and input-to-output transformations and a LogSoftmax layer for output probabilities. It also provides a method to initialize the hidden state. ```Python import torch import torch.nn as nn class RNN(nn.Module): def __init__(self, input_size, hidden_size, output_size): super(RNN, self).__init__() self.hidden_size = hidden_size self.i2h = nn.Linear(input_size + hidden_size, hidden_size) self.i2o = nn.Linear(input_size + hidden_size, output_size) self.softmax = nn.LogSoftmax(dim=1) def forward(self, input, hidden): combined = torch.cat((input, hidden), 1) hidden = self.i2h(combined) output = self.i2o(combined) output = self.softmax(output) return output, hidden def initHidden(self): return torch.zeros(1, self.hidden_size) ``` -------------------------------- ### Implementing Fairseq Incremental Decoder in Python Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_simple_lstm.rst Defines a SimpleLSTMDecoder class that inherits from FairseqIncrementalDecoder. It includes an __init__ method for initialization and a forward method that handles incremental decoding by caching LSTM states using incremental_state. It also mentions the requirement for reorder_incremental_state. ```python import torch from fairseq.models import FairseqIncrementalDecoder class SimpleLSTMDecoder(FairseqIncrementalDecoder): def __init__( self, dictionary, encoder_hidden_dim=128, embed_dim=128, hidden_dim=128, dropout=0.1, ): # This remains the same as before. super().__init__(dictionary) self.embed_tokens = nn.Embedding( num_embeddings=len(dictionary), embedding_dim=embed_dim, padding_idx=dictionary.pad(), ) self.dropout = nn.Dropout(p=dropout) self.lstm = nn.LSTM( input_size=encoder_hidden_dim + embed_dim, hidden_size=hidden_dim, num_layers=1, bidirectional=False, ) self.output_projection = nn.Linear(hidden_dim, len(dictionary)) # We now take an additional kwarg (*incremental_state*) for caching the # previous hidden and cell states. def forward(self, prev_output_tokens, encoder_out, incremental_state=None): if incremental_state is not None: # If the *incremental_state* argument is not ``None`` then we are # in incremental inference mode. While *prev_output_tokens* will # still contain the entire decoded prefix, we will only use the # last step and assume that the rest of the state is cached. prev_output_tokens = prev_output_tokens[:, -1:] # This remains the same as before. bsz, tgt_len = prev_output_tokens.size() final_encoder_hidden = encoder_out['final_hidden'] x = self.embed_tokens(prev_output_tokens) x = self.dropout(x) x = torch.cat( [x, final_encoder_hidden.unsqueeze(1).expand(bsz, tgt_len, -1)], dim=2, ) # We will now check the cache and load the cached previous hidden and # cell states, if they exist, otherwise we will initialize them to # zeros (as before). We will use the ``utils.get_incremental_state()`` # and ``utils.set_incremental_state()`` helpers. initial_state = utils.get_incremental_state( self, incremental_state, 'prev_state', ) if initial_state is None: # first time initialization, same as the original version initial_state = ( final_encoder_hidden.unsqueeze(0), # hidden torch.zeros_like(final_encoder_hidden).unsqueeze(0), # cell ) # Run one step of our LSTM. output, latest_state = self.lstm(x.transpose(0, 1), initial_state) # Update the cache with the latest hidden and cell states. utils.set_incremental_state( self, incremental_state, 'prev_state', latest_state, ) # This remains the same as before x = output.transpose(0, 1) x = self.output_projection(x) return x, None # The ``FairseqIncrementalDecoder`` interface also requires implementing a # ``reorder_incremental_state()`` method, which is used during beam search # to select and reorder the incremental state. def reorder_incremental_state(self, incremental_state, new_order): # Load the cached state. pass # The original snippet was incomplete, adding pass to make it valid Python ``` -------------------------------- ### Defining Model Arguments with Fairseq Python Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_simple_lstm.rst Adds command-line arguments for configuring the encoder and decoder dimensions and dropout rates using Fairseq's argument parsing system. These arguments are typically added within a `add_args` class method. ```Python '--encoder-embed-dim', type=int, metavar='N', help='dimensionality of the encoder embeddings', ) parser.add_argument( '--encoder-hidden-dim', type=int, metavar='N', help='dimensionality of the encoder hidden state', ) parser.add_argument( '--encoder-dropout', type=float, default=0.1, help='encoder dropout probability', ) parser.add_argument( '--decoder-embed-dim', type=int, metavar='N', help='dimensionality of the decoder embeddings', ) parser.add_argument( '--decoder-hidden-dim', type=int, metavar='N', help='dimensionality of the decoder hidden state', ) parser.add_argument( '--decoder-dropout', type=float, default=0.1, help='decoder dropout probability', ) ``` -------------------------------- ### Fairseq RNN Classifier Model Definition Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst Defines the `FairseqRNNClassifier` class, including its initialization (`__init__`) and forward pass (`forward`) methods. It wraps a basic RNN module, handles one-hot encoding of inputs, manages batched hidden states, and processes sequential input tokens. ```Python input_size=len(task.source_dictionary), hidden_size=args.hidden_dim, output_size=len(task.target_dictionary), ) # Return the wrapped version of the module return FairseqRNNClassifier( rnn=rnn, input_vocab=task.source_dictionary, ) def __init__(self, rnn, input_vocab): super(FairseqRNNClassifier, self).__init__() self.rnn = rnn self.input_vocab = input_vocab # The RNN module in the tutorial expects one-hot inputs, so we can # precompute the identity matrix to help convert from indices to # one-hot vectors. We register it as a buffer so that it is moved to # the GPU when ``cuda()`` is called. self.register_buffer('one_hot_inputs', torch.eye(len(input_vocab))) def forward(self, src_tokens, src_lengths): # The inputs to the ``forward()`` function are determined by the # Task, and in particular the ``'net_input'`` key in each # mini-batch. We'll define the Task in the next section, but for # now just know that *src_tokens* has shape `(batch, src_len)` and # *src_lengths* has shape `(batch)`. bsz, max_src_len = src_tokens.size() # Initialize the RNN hidden state. Compared to the original PyTorch # tutorial we'll also handle batched inputs and work on the GPU. hidden = self.rnn.initHidden() hidden = hidden.repeat(bsz, 1) # expand for batched inputs hidden = hidden.to(src_tokens.device) # move to GPU for i in range(max_src_len): # WARNING: The inputs have padding, so we should mask those # elements here so that padding doesn't affect the results. # This is left as an exercise for the reader. The padding symbol # is given by ``self.input_vocab.pad()`` and the unpadded length # of each input is given by *src_lengths*. # One-hot encode a batch of input characters. input = self.one_hot_inputs[src_tokens[:, i].long()] # Feed the input to our RNN. output, hidden = self.rnn(input, hidden) # Return the final output state for making a prediction return output ``` -------------------------------- ### Implementing Simple LSTM Decoder in Fairseq (Python) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_simple_lstm.rst This class defines a simple LSTM-based decoder for sequence-to-sequence models within the fairseq framework. It inherits from FairseqDecoder, embeds input tokens, uses a single-layer LSTM, and projects the output to the vocabulary size. It takes previous output tokens and encoder output as input and returns logits and attention weights (None in this case). ```Python import torch from fairseq.models import FairseqDecoder class SimpleLSTMDecoder(FairseqDecoder): def __init__( self, dictionary, encoder_hidden_dim=128, embed_dim=128, hidden_dim=128, dropout=0.1, ): super().__init__(dictionary) # Our decoder will embed the inputs before feeding them to the LSTM. self.embed_tokens = nn.Embedding( num_embeddings=len(dictionary), embedding_dim=embed_dim, padding_idx=dictionary.pad(), ) self.dropout = nn.Dropout(p=dropout) # We'll use a single-layer, unidirectional LSTM for simplicity. self.lstm = nn.LSTM( # For the first layer we'll concatenate the Encoder's final hidden # state with the embedded target tokens. input_size=encoder_hidden_dim + embed_dim, hidden_size=hidden_dim, num_layers=1, bidirectional=False, ) # Define the output projection. self.output_projection = nn.Linear(hidden_dim, len(dictionary)) # During training Decoders are expected to take the entire target sequence # (shifted right by one position) and produce logits over the vocabulary. # The *prev_output_tokens* tensor begins with the end-of-sentence symbol, # ``dictionary.eos()``, followed by the target sequence. def forward(self, prev_output_tokens, encoder_out): """ Args: prev_output_tokens (LongTensor): previous decoder outputs of shape `(batch, tgt_len)`, for teacher forcing encoder_out (Tensor, optional): output from the encoder, used for encoder-side attention Returns: tuple: - the last decoder layer's output of shape `(batch, tgt_len, vocab)` - the last decoder layer's attention weights of shape `(batch, tgt_len, src_len)` """ bsz, tgt_len = prev_output_tokens.size() # Extract the final hidden state from the Encoder. final_encoder_hidden = encoder_out['final_hidden'] # Embed the target sequence, which has been shifted right by one # position and now starts with the end-of-sentence symbol. x = self.embed_tokens(prev_output_tokens) # Apply dropout. x = self.dropout(x) # Concatenate the Encoder's final hidden state to *every* embedded # target token. x = torch.cat( [x, final_encoder_hidden.unsqueeze(1).expand(bsz, tgt_len, -1)], dim=2, ) # Using PackedSequence objects in the Decoder is harder than in the # Encoder, since the targets are not sorted in descending length order, # which is a requirement of ``pack_padded_sequence()``. Instead we'll # feed nn.LSTM directly. initial_state = ( final_encoder_hidden.unsqueeze(0), # hidden torch.zeros_like(final_encoder_hidden).unsqueeze(0), # cell ) output, _ = self.lstm( x.transpose(0, 1), # convert to shape `(tgt_len, bsz, dim)` initial_state, ) x = output.transpose(0, 1) # convert to shape `(bsz, tgt_len, hidden)` # Project the outputs to the size of the vocabulary. x = self.output_projection(x) # Return the logits and ``None`` for the attention weights return x, None ``` -------------------------------- ### Registering Fairseq Named Architecture Python Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_simple_lstm.rst Registers a named model architecture ('tutorial_simple_lstm') for the 'simple_lstm' model using the `@register_model_architecture` decorator. The decorated function `tutorial_simple_lstm` modifies the input `args` object in-place to set default values for model dimensions if not provided via command line. ```Python from fairseq.models import register_model_architecture # The first argument to ``register_model_architecture()`` should be the name # of the model we registered above (i.e., 'simple_lstm'). The function we # register here should take a single argument *args* and modify it in-place # to match the desired architecture. @register_model_architecture('simple_lstm', 'tutorial_simple_lstm') def tutorial_simple_lstm(args): # We use ``getattr()`` to prioritize arguments that are explicitly given # on the command-line, so that the defaults defined below are only used # when no other value has been specified. args.encoder_embed_dim = getattr(args, 'encoder_embed_dim', 256) args.encoder_hidden_dim = getattr(args, 'encoder_hidden_dim', 256) args.decoder_embed_dim = getattr(args, 'decoder_embed_dim', 256) args.decoder_hidden_dim = getattr(args, 'decoder_hidden_dim', 256) ``` -------------------------------- ### Registering Fairseq Model Architecture Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst Demonstrates how to register a named model architecture ('pytorch_tutorial_rnn') for the 'rnn_classifier' model using the `@register_model_architecture` decorator. The function sets default values for model arguments like `hidden_dim`. ```Python from fairseq.models import register_model_architecture # The first argument to ``register_model_architecture()`` should be the name # of the model we registered above (i.e., 'rnn_classifier'). The function we # register here should take a single argument *args* and modify it in-place # to match the desired architecture. @register_model_architecture('rnn_classifier', 'pytorch_tutorial_rnn') def pytorch_tutorial_rnn(args): # We use ``getattr()`` to prioritize arguments that are explicitly given # on the command-line, so that the defaults defined below are only used # when no other value has been specified. args.hidden_dim = getattr(args, 'hidden_dim', 128) ``` -------------------------------- ### Implementing Simple LSTM Encoder in Fairseq Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_simple_lstm.rst Defines a custom encoder class `SimpleLSTMEncoder` that inherits from `FairseqEncoder`. It uses an embedding layer and a single-layer, unidirectional LSTM to process source tokens and returns the final hidden state. Includes methods for initialization, forward pass, and reordering encoder output for inference. ```Python import torch.nn as nn from fairseq import utils from fairseq.models import FairseqEncoder class SimpleLSTMEncoder(FairseqEncoder): def __init__( self, args, dictionary, embed_dim=128, hidden_dim=128, dropout=0.1, ): super().__init__(dictionary) self.args = args # Our encoder will embed the inputs before feeding them to the LSTM. self.embed_tokens = nn.Embedding( num_embeddings=len(dictionary), embedding_dim=embed_dim, padding_idx=dictionary.pad(), ) self.dropout = nn.Dropout(p=dropout) # We'll use a single-layer, unidirectional LSTM for simplicity. self.lstm = nn.LSTM( input_size=embed_dim, hidden_size=hidden_dim, num_layers=1, bidirectional=False, ) def forward(self, src_tokens, src_lengths): # The inputs to the ``forward()`` function are determined by the # Task, and in particular the ``'net_input'`` key in each # mini-batch. We discuss Tasks in the next tutorial, but for now just # know that *src_tokens* has shape `(batch, src_len)` and *src_lengths* # has shape `(batch)`. # Note that the source is typically padded on the left. This can be # configured by adding the `--left-pad-source "False"` command-line # argument, but here we'll make the Encoder handle either kind of # padding by converting everything to be right-padded. if self.args.left_pad_source: # Convert left-padding to right-padding. src_tokens = utils.convert_padding_direction( src_tokens, padding_idx=self.dictionary.pad(), left_to_right=True ) # Embed the source. x = self.embed_tokens(src_tokens) # Apply dropout. x = self.dropout(x) # Pack the sequence into a PackedSequence object to feed to the LSTM. x = nn.utils.rnn.pack_padded_sequence(x, src_lengths, batch_first=True) # Get the output from the LSTM. _outputs, (final_hidden, _final_cell) = self.lstm(x) # Return the Encoder's output. This can be any object and will be # passed directly to the Decoder. return { # this will have shape `(bsz, hidden_dim)` 'final_hidden': final_hidden.squeeze(0), } # Encoders are required to implement this method so that we can rearrange # the order of the batch elements during inference (e.g., beam search). def reorder_encoder_out(self, encoder_out, new_order): """ Reorder encoder output according to `new_order`. Args: encoder_out: output from the ``forward()`` method new_order (LongTensor): desired order Returns: `encoder_out` rearranged according to `new_order` """ final_hidden = encoder_out['final_hidden'] return { 'final_hidden': final_hidden.index_select(0, new_order), } ``` -------------------------------- ### Define Max Positions for Simple Classification Task (Python) Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_classifying_names.rst Returns a tuple specifying the maximum allowed length for the input sequence (source) and the target sequence. For this classification task, the target length is fixed at 1. ```Python def max_positions(self): """Return the max input length allowed by the task.""" # The source should be less than *args.max_positions* and the "target" # has max length 1. return (self.args.max_positions, 1) ``` -------------------------------- ### Computing Loss with Fairseq Criterion Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/criterions.rst This snippet demonstrates the basic usage of a fairseq criterion object to compute the loss value. It takes a model and a batch of data as input and returns the calculated loss. ```Python loss = criterion(model, batch) ``` -------------------------------- ### Reordering and Updating Incremental State in Python Source: https://github.com/layer6ai-labs/t-fixup/blob/master/docs/tutorial_simple_lstm.rst This Python snippet demonstrates how to retrieve, reorder, and update an incremental state within a model. It fetches the previous state, reorders its components (likely hidden and cell states in an RNN/LSTM context) based on a `new_order`, and then saves the reordered state back. This is typically used during beam search or other generation processes where the order of hypotheses changes. ```Python prev_state = utils.get_incremental_state( self, incremental_state, 'prev_state', ) # Reorder batches according to *new_order*. reordered_state = ( prev_state[0].index_select(1, new_order), # hidden prev_state[1].index_select(1, new_order), # cell ) # Update the cached state. utils.set_incremental_state( self, incremental_state, 'prev_state', reordered_state, ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.