diff --git a/beginner_source/transformer_tutorial.py b/beginner_source/transformer_tutorial.py deleted file mode 100644 index 437f6345241..00000000000 --- a/beginner_source/transformer_tutorial.py +++ /dev/null @@ -1,384 +0,0 @@ -""" -Language Modeling with ``nn.Transformer`` and torchtext -=============================================================== - -This is a tutorial on training a model to predict the next word in a sequence using the -`nn.Transformer `__ module. - -The PyTorch 1.2 release includes a standard transformer module based on the -paper `Attention is All You Need `__. -Compared to Recurrent Neural Networks (RNNs), the transformer model has proven -to be superior in quality for many sequence-to-sequence tasks while being more -parallelizable. The ``nn.Transformer`` module relies entirely on an attention -mechanism (implemented as -`nn.MultiheadAttention `__) -to draw global dependencies between input and output. The ``nn.Transformer`` -module is highly modularized such that a single component (e.g., -`nn.TransformerEncoder `__) -can be easily adapted/composed. - -.. image:: ../_static/img/transformer_architecture.jpg - -""" - -###################################################################### -# Define the model -# ---------------- -# - - -###################################################################### -# In this tutorial, we train a ``nn.TransformerEncoder`` model on a -# causal language modeling task. Please note that this tutorial does not cover -# the training of `nn.TransformerDecoder `__, as depicted in -# the right half of the diagram above. The language modeling task is to assign a -# probability for the likelihood of a given word (or a sequence of words) -# to follow a sequence of words. A sequence of tokens are passed to the embedding -# layer first, followed by a positional encoding layer to account for the order -# of the word (see the next paragraph for more details). The -# ``nn.TransformerEncoder`` consists of multiple layers of -# `nn.TransformerEncoderLayer `__. -# Along with the input sequence, a square attention mask is required because the -# self-attention layers in ``nn.TransformerDecoder`` are only allowed to attend -# the earlier positions in the sequence. For the language modeling task, any -# tokens on the future positions should be masked. This masking, combined with fact that -# the output embeddings are offset with later positions ensures that the -# predictions for position i can depend only on the known outputs at positions less than i. -# To produce a probability distribution over output words, the output of the ``nn.TransformerEncoder`` -# model is passed through a linear layer to output unnormalized logits. -# The log-softmax function isn't applied here due to the later use of -# `CrossEntropyLoss `__, -# which requires the inputs to be unnormalized logits. -# - -import math -import os -from tempfile import TemporaryDirectory -from typing import Tuple - -import torch -from torch import nn, Tensor -from torch.nn import TransformerEncoder, TransformerEncoderLayer -from torch.utils.data import dataset - -class TransformerModel(nn.Module): - - def __init__(self, ntoken: int, d_model: int, nhead: int, d_hid: int, - nlayers: int, dropout: float = 0.5): - super().__init__() - self.model_type = 'Transformer' - self.pos_encoder = PositionalEncoding(d_model, dropout) - encoder_layers = TransformerEncoderLayer(d_model, nhead, d_hid, dropout) - self.transformer_encoder = TransformerEncoder(encoder_layers, nlayers) - self.embedding = nn.Embedding(ntoken, d_model) - self.d_model = d_model - self.linear = nn.Linear(d_model, ntoken) - - self.init_weights() - - def init_weights(self) -> None: - initrange = 0.1 - self.embedding.weight.data.uniform_(-initrange, initrange) - self.linear.bias.data.zero_() - self.linear.weight.data.uniform_(-initrange, initrange) - - def forward(self, src: Tensor, src_mask: Tensor = None) -> Tensor: - """ - Arguments: - src: Tensor, shape ``[seq_len, batch_size]`` - src_mask: Tensor, shape ``[seq_len, seq_len]`` - - Returns: - output Tensor of shape ``[seq_len, batch_size, ntoken]`` - """ - src = self.embedding(src) * math.sqrt(self.d_model) - src = self.pos_encoder(src) - if src_mask is None: - """Generate a square causal mask for the sequence. The masked positions are filled with float('-inf'). - Unmasked positions are filled with float(0.0). - """ - src_mask = nn.Transformer.generate_square_subsequent_mask(len(src)).to(device) - output = self.transformer_encoder(src, src_mask) - output = self.linear(output) - return output - - -###################################################################### -# ``PositionalEncoding`` module injects some information about the -# relative or absolute position of the tokens in the sequence. The -# positional encodings have the same dimension as the embeddings so that -# the two can be summed. Here, we use ``sine`` and ``cosine`` functions of -# different frequencies. -# - -class PositionalEncoding(nn.Module): - - def __init__(self, d_model: int, dropout: float = 0.1, max_len: int = 5000): - super().__init__() - self.dropout = nn.Dropout(p=dropout) - - position = torch.arange(max_len).unsqueeze(1) - div_term = torch.exp(torch.arange(0, d_model, 2) * (-math.log(10000.0) / d_model)) - pe = torch.zeros(max_len, 1, d_model) - pe[:, 0, 0::2] = torch.sin(position * div_term) - pe[:, 0, 1::2] = torch.cos(position * div_term) - self.register_buffer('pe', pe) - - def forward(self, x: Tensor) -> Tensor: - """ - Arguments: - x: Tensor, shape ``[seq_len, batch_size, embedding_dim]`` - """ - x = x + self.pe[:x.size(0)] - return self.dropout(x) - - -###################################################################### -# Load and batch data -# ------------------- -# - - -###################################################################### -# This tutorial uses ``torchtext`` to generate Wikitext-2 dataset. -# To access torchtext datasets, please install torchdata following instructions at https://github.com/pytorch/data. -# %% -# .. code-block:: bash -# -# %%bash -# pip install portalocker -# pip install torchdata -# -# The vocab object is built based on the train dataset and is used to numericalize -# tokens into tensors. Wikitext-2 represents rare tokens as ``. -# -# Given a 1-D vector of sequential data, ``batchify()`` arranges the data -# into ``batch_size`` columns. If the data does not divide evenly into -# ``batch_size`` columns, then the data is trimmed to fit. For instance, with -# the alphabet as the data (total length of 26) and ``batch_size=4``, we would -# divide the alphabet into sequences of length 6, resulting in 4 of such sequences. -# -# .. math:: -# \begin{bmatrix} -# \text{A} & \text{B} & \text{C} & \ldots & \text{X} & \text{Y} & \text{Z} -# \end{bmatrix} -# \Rightarrow -# \begin{bmatrix} -# \begin{bmatrix}\text{A} \\ \text{B} \\ \text{C} \\ \text{D} \\ \text{E} \\ \text{F}\end{bmatrix} & -# \begin{bmatrix}\text{G} \\ \text{H} \\ \text{I} \\ \text{J} \\ \text{K} \\ \text{L}\end{bmatrix} & -# \begin{bmatrix}\text{M} \\ \text{N} \\ \text{O} \\ \text{P} \\ \text{Q} \\ \text{R}\end{bmatrix} & -# \begin{bmatrix}\text{S} \\ \text{T} \\ \text{U} \\ \text{V} \\ \text{W} \\ \text{X}\end{bmatrix} -# \end{bmatrix} -# -# Batching enables more parallelizable processing. However, batching means that -# the model treats each column independently; for example, the dependence of -# ``G`` and ``F`` can not be learned in the example above. -# - -from torchtext.datasets import WikiText2 -from torchtext.data.utils import get_tokenizer -from torchtext.vocab import build_vocab_from_iterator - -train_iter = WikiText2(split='train') -tokenizer = get_tokenizer('basic_english') -vocab = build_vocab_from_iterator(map(tokenizer, train_iter), specials=['']) -vocab.set_default_index(vocab['']) - -def data_process(raw_text_iter: dataset.IterableDataset) -> Tensor: - """Converts raw text into a flat Tensor.""" - data = [torch.tensor(vocab(tokenizer(item)), dtype=torch.long) for item in raw_text_iter] - return torch.cat(tuple(filter(lambda t: t.numel() > 0, data))) - -# ``train_iter`` was "consumed" by the process of building the vocab, -# so we have to create it again -train_iter, val_iter, test_iter = WikiText2() -train_data = data_process(train_iter) -val_data = data_process(val_iter) -test_data = data_process(test_iter) - -device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') - -def batchify(data: Tensor, bsz: int) -> Tensor: - """Divides the data into ``bsz`` separate sequences, removing extra elements - that wouldn't cleanly fit. - - Arguments: - data: Tensor, shape ``[N]`` - bsz: int, batch size - - Returns: - Tensor of shape ``[N // bsz, bsz]`` - """ - seq_len = data.size(0) // bsz - data = data[:seq_len * bsz] - data = data.view(bsz, seq_len).t().contiguous() - return data.to(device) - -batch_size = 20 -eval_batch_size = 10 -train_data = batchify(train_data, batch_size) # shape ``[seq_len, batch_size]`` -val_data = batchify(val_data, eval_batch_size) -test_data = batchify(test_data, eval_batch_size) - - -###################################################################### -# Functions to generate input and target sequence -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# - - -###################################################################### -# ``get_batch()`` generates a pair of input-target sequences for -# the transformer model. It subdivides the source data into chunks of -# length ``bptt``. For the language modeling task, the model needs the -# following words as ``Target``. For example, with a ``bptt`` value of 2, -# we’d get the following two Variables for ``i`` = 0: -# -# .. image:: ../_static/img/transformer_input_target.png -# -# It should be noted that the chunks are along dimension 0, consistent -# with the ``S`` dimension in the Transformer model. The batch dimension -# ``N`` is along dimension 1. -# - -bptt = 35 -def get_batch(source: Tensor, i: int) -> Tuple[Tensor, Tensor]: - """ - Args: - source: Tensor, shape ``[full_seq_len, batch_size]`` - i: int - - Returns: - tuple (data, target), where data has shape ``[seq_len, batch_size]`` and - target has shape ``[seq_len * batch_size]`` - """ - seq_len = min(bptt, len(source) - 1 - i) - data = source[i:i+seq_len] - target = source[i+1:i+1+seq_len].reshape(-1) - return data, target - - -###################################################################### -# Initiate an instance -# -------------------- -# - - -###################################################################### -# The model hyperparameters are defined below. The ``vocab`` size is -# equal to the length of the vocab object. -# - -ntokens = len(vocab) # size of vocabulary -emsize = 200 # embedding dimension -d_hid = 200 # dimension of the feedforward network model in ``nn.TransformerEncoder`` -nlayers = 2 # number of ``nn.TransformerEncoderLayer`` in ``nn.TransformerEncoder`` -nhead = 2 # number of heads in ``nn.MultiheadAttention`` -dropout = 0.2 # dropout probability -model = TransformerModel(ntokens, emsize, nhead, d_hid, nlayers, dropout).to(device) - - -###################################################################### -# Run the model -# ------------- -# - - -###################################################################### -# We use `CrossEntropyLoss `__ -# with the `SGD `__ -# (stochastic gradient descent) optimizer. The learning rate is initially set to -# 5.0 and follows a `StepLR `__ -# schedule. During training, we use `nn.utils.clip_grad_norm\_ `__ -# to prevent gradients from exploding. -# - -import time - -criterion = nn.CrossEntropyLoss() -lr = 5.0 # learning rate -optimizer = torch.optim.SGD(model.parameters(), lr=lr) -scheduler = torch.optim.lr_scheduler.StepLR(optimizer, 1.0, gamma=0.95) - -def train(model: nn.Module) -> None: - model.train() # turn on train mode - total_loss = 0. - log_interval = 200 - start_time = time.time() - - num_batches = len(train_data) // bptt - for batch, i in enumerate(range(0, train_data.size(0) - 1, bptt)): - data, targets = get_batch(train_data, i) - output = model(data) - output_flat = output.view(-1, ntokens) - loss = criterion(output_flat, targets) - - optimizer.zero_grad() - loss.backward() - torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5) - optimizer.step() - - total_loss += loss.item() - if batch % log_interval == 0 and batch > 0: - lr = scheduler.get_last_lr()[0] - ms_per_batch = (time.time() - start_time) * 1000 / log_interval - cur_loss = total_loss / log_interval - ppl = math.exp(cur_loss) - print(f'| epoch {epoch:3d} | {batch:5d}/{num_batches:5d} batches | ' - f'lr {lr:02.2f} | ms/batch {ms_per_batch:5.2f} | ' - f'loss {cur_loss:5.2f} | ppl {ppl:8.2f}') - total_loss = 0 - start_time = time.time() - -def evaluate(model: nn.Module, eval_data: Tensor) -> float: - model.eval() # turn on evaluation mode - total_loss = 0. - with torch.no_grad(): - for i in range(0, eval_data.size(0) - 1, bptt): - data, targets = get_batch(eval_data, i) - seq_len = data.size(0) - output = model(data) - output_flat = output.view(-1, ntokens) - total_loss += seq_len * criterion(output_flat, targets).item() - return total_loss / (len(eval_data) - 1) - -###################################################################### -# Loop over epochs. Save the model if the validation loss is the best -# we've seen so far. Adjust the learning rate after each epoch. - -best_val_loss = float('inf') -epochs = 3 - -with TemporaryDirectory() as tempdir: - best_model_params_path = os.path.join(tempdir, "best_model_params.pt") - - for epoch in range(1, epochs + 1): - epoch_start_time = time.time() - train(model) - val_loss = evaluate(model, val_data) - val_ppl = math.exp(val_loss) - elapsed = time.time() - epoch_start_time - print('-' * 89) - print(f'| end of epoch {epoch:3d} | time: {elapsed:5.2f}s | ' - f'valid loss {val_loss:5.2f} | valid ppl {val_ppl:8.2f}') - print('-' * 89) - - if val_loss < best_val_loss: - best_val_loss = val_loss - torch.save(model.state_dict(), best_model_params_path) - - scheduler.step() - model.load_state_dict(torch.load(best_model_params_path)) # load best model states - - -###################################################################### -# Evaluate the best model on the test dataset -# ------------------------------------------- -# - -test_loss = evaluate(model, test_data) -test_ppl = math.exp(test_loss) -print('=' * 89) -print(f'| End of training | test loss {test_loss:5.2f} | ' - f'test ppl {test_ppl:8.2f}') -print('=' * 89) diff --git a/index.rst b/index.rst index e5f0d1eaaa8..ab98379303b 100644 --- a/index.rst +++ b/index.rst @@ -233,13 +233,6 @@ What's new in PyTorch tutorials? :link: beginner/bettertransformer_tutorial.html :tags: Production,Text -.. customcarditem:: - :header: Sequence-to-Sequence Modeling with nn.Transformer and torchtext - :card_description: Learn how to train a sequence-to-sequence model that uses the nn.Transformer module. - :image: _static/img/thumbnails/cropped/Sequence-to-Sequence-Modeling-with-nnTransformer-andTorchText.png - :link: beginner/transformer_tutorial.html - :tags: Text - .. customcarditem:: :header: NLP from Scratch: Classifying Names with a Character-level RNN :card_description: Build and train a basic character-level RNN to classify word from scratch without the use of torchtext. First in a series of three tutorials. @@ -745,13 +738,6 @@ What's new in PyTorch tutorials? :link: advanced/rpc_ddp_tutorial.html :tags: Parallel-and-Distributed-Training -.. customcarditem:: - :header: Training Transformer models using Pipeline Parallelism - :card_description: Walk through a through a simple example of how to train a transformer model using pipeline parallelism. - :image: _static/img/thumbnails/cropped/Training-Transformer-models-using-Pipeline-Parallelism.png - :link: intermediate/pipeline_tutorial.html - :tags: Parallel-and-Distributed-Training - .. customcarditem:: :header: Training Transformer models using Distributed Data Parallel and Pipeline Parallelism :card_description: Walk through a through a simple example of how to train a transformer model using Distributed Data Parallel and Pipeline Parallelism @@ -964,7 +950,6 @@ Additional Resources :hidden: :caption: Text - beginner/transformer_tutorial beginner/bettertransformer_tutorial intermediate/char_rnn_classification_tutorial intermediate/char_rnn_generation_tutorial @@ -1102,7 +1087,6 @@ Additional Resources intermediate/dist_pipeline_parallel_tutorial intermediate/rpc_async_execution advanced/rpc_ddp_tutorial - intermediate/pipeline_tutorial advanced/ddp_pipeline advanced/generic_join diff --git a/intermediate_source/pipeline_tutorial.py b/intermediate_source/pipeline_tutorial.py deleted file mode 100644 index 4cebeba63a9..00000000000 --- a/intermediate_source/pipeline_tutorial.py +++ /dev/null @@ -1,420 +0,0 @@ -""" -Training Transformer models using Pipeline Parallelism -====================================================== - -**Author**: `Pritam Damania `_ - -This tutorial demonstrates how to train a large Transformer model across -multiple GPUs using pipeline parallelism. This tutorial is an extension of the -`Sequence-to-Sequence Modeling with nn.Transformer and TorchText `__ tutorial -and scales up the same model to demonstrate how pipeline parallelism can be -used to train Transformer models. - -Prerequisites: - - * `Pipeline Parallelism `__ - * `Sequence-to-Sequence Modeling with nn.Transformer and TorchText `__ -""" - - -###################################################################### -# Define the model -# ---------------- -# - - -###################################################################### -# In this tutorial, we will split a Transformer model across two GPUs and use -# pipeline parallelism to train the model. The model is exactly the same model -# used in the `Sequence-to-Sequence Modeling with nn.Transformer and TorchText -# `__ tutorial, -# but is split into two stages. The largest number of parameters belong to the -# `nn.TransformerEncoder `__ layer. -# The `nn.TransformerEncoder `__ -# itself consists of ``nlayers`` of `nn.TransformerEncoderLayer `__. -# As a result, our focus is on ``nn.TransformerEncoder`` and we split the model -# such that half of the ``nn.TransformerEncoderLayer`` are on one GPU and the -# other half are on another. To do this, we pull out the ``Encoder`` and -# ``Decoder`` sections into separate modules and then build an ``nn.Sequential`` -# representing the original Transformer module. - -import sys -import math -import torch -import torch.nn as nn -import torch.nn.functional as F -import tempfile -from torch.nn import TransformerEncoder, TransformerEncoderLayer - -if sys.platform == 'win32': - print('Windows platform is not supported for pipeline parallelism') - sys.exit(0) -if torch.cuda.device_count() < 2: - print('Need at least two GPU devices for this tutorial') - sys.exit(0) - -class Encoder(nn.Module): - def __init__(self, ntoken, ninp, dropout=0.5): - super(Encoder, self).__init__() - self.pos_encoder = PositionalEncoding(ninp, dropout) - self.encoder = nn.Embedding(ntoken, ninp) - self.ninp = ninp - self.init_weights() - - def init_weights(self): - initrange = 0.1 - self.encoder.weight.data.uniform_(-initrange, initrange) - - def forward(self, src): - # Need (S, N) format for encoder. - src = src.t() - src = self.encoder(src) * math.sqrt(self.ninp) - return self.pos_encoder(src) - -class Decoder(nn.Module): - def __init__(self, ntoken, ninp): - super(Decoder, self).__init__() - self.decoder = nn.Linear(ninp, ntoken) - self.init_weights() - - def init_weights(self): - initrange = 0.1 - self.decoder.bias.data.zero_() - self.decoder.weight.data.uniform_(-initrange, initrange) - - def forward(self, inp): - # Need batch dimension first for output of pipeline. - return self.decoder(inp).permute(1, 0, 2) - - -###################################################################### -# ``PositionalEncoding`` module injects some information about the -# relative or absolute position of the tokens in the sequence. The -# positional encodings have the same dimension as the embeddings so that -# the two can be summed. Here, we use ``sine`` and ``cosine`` functions of -# different frequencies. - - -class PositionalEncoding(nn.Module): - - def __init__(self, d_model, dropout=0.1, max_len=5000): - super(PositionalEncoding, self).__init__() - self.dropout = nn.Dropout(p=dropout) - - pe = torch.zeros(max_len, d_model) - position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1) - div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model)) - pe[:, 0::2] = torch.sin(position * div_term) - pe[:, 1::2] = torch.cos(position * div_term) - pe = pe.unsqueeze(0).transpose(0, 1) - self.register_buffer('pe', pe) - - def forward(self, x): - x = x + self.pe[:x.size(0), :] - return self.dropout(x) - - - -###################################################################### -# Load and batch data -# ------------------- -# - - -###################################################################### -# The training process uses Wikitext-2 dataset from ``torchtext``. -# To access torchtext datasets, please install torchdata following instructions at https://github.com/pytorch/data. -# -# The vocab object is built based on the train dataset and is used to numericalize -# tokens into tensors. Starting from sequential data, the ``batchify()`` -# function arranges the dataset into columns, trimming off any tokens remaining -# after the data has been divided into batches of size ``batch_size``. -# For instance, with the alphabet as the sequence (total length of 26) -# and a batch size of 4, we would divide the alphabet into 4 sequences of -# length 6: -# -# .. math:: -# -# \begin{bmatrix} -# \text{A} & \text{B} & \text{C} & \ldots & \text{X} & \text{Y} & \text{Z} -# \end{bmatrix} -# \Rightarrow -# \begin{bmatrix} -# \begin{bmatrix}\text{A} \\ \text{B} \\ \text{C} \\ \text{D} \\ \text{E} \\ \text{F}\end{bmatrix} & -# \begin{bmatrix}\text{G} \\ \text{H} \\ \text{I} \\ \text{J} \\ \text{K} \\ \text{L}\end{bmatrix} & -# \begin{bmatrix}\text{M} \\ \text{N} \\ \text{O} \\ \text{P} \\ \text{Q} \\ \text{R}\end{bmatrix} & -# \begin{bmatrix}\text{S} \\ \text{T} \\ \text{U} \\ \text{V} \\ \text{W} \\ \text{X}\end{bmatrix} -# \end{bmatrix} -# -# These columns are treated as independent by the model, which means that -# the dependence of ``G`` and ``F`` can not be learned, but allows more -# efficient batch processing. -# - -import torch -from torchtext.datasets import WikiText2 -from torchtext.data.utils import get_tokenizer -from torchtext.vocab import build_vocab_from_iterator - -train_iter = WikiText2(split='train') -tokenizer = get_tokenizer('basic_english') -vocab = build_vocab_from_iterator(map(tokenizer, train_iter), specials=[""]) -vocab.set_default_index(vocab[""]) - -def data_process(raw_text_iter): - data = [torch.tensor(vocab(tokenizer(item)), dtype=torch.long) for item in raw_text_iter] - return torch.cat(tuple(filter(lambda t: t.numel() > 0, data))) - -train_iter, val_iter, test_iter = WikiText2() -train_data = data_process(train_iter) -val_data = data_process(val_iter) -test_data = data_process(test_iter) - -device = torch.device("cuda") - -def batchify(data, bsz): - # Divide the dataset into ``bsz`` parts. - nbatch = data.size(0) // bsz - # Trim off any extra elements that wouldn't cleanly fit (remainders). - data = data.narrow(0, 0, nbatch * bsz) - # Evenly divide the data across the ``bsz` batches. - data = data.view(bsz, -1).t().contiguous() - return data.to(device) - -batch_size = 20 -eval_batch_size = 10 -train_data = batchify(train_data, batch_size) -val_data = batchify(val_data, eval_batch_size) -test_data = batchify(test_data, eval_batch_size) - - -###################################################################### -# Functions to generate input and target sequence -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# - - -###################################################################### -# ``get_batch()`` function generates the input and target sequence for -# the transformer model. It subdivides the source data into chunks of -# length ``bptt``. For the language modeling task, the model needs the -# following words as ``Target``. For example, with a ``bptt`` value of 2, -# we'd get the following two Variables for ``i`` = 0: -# -# .. image:: ../_static/img/transformer_input_target.png -# -# It should be noted that the chunks are along dimension 0, consistent -# with the ``S`` dimension in the Transformer model. The batch dimension -# ``N`` is along dimension 1. -# - -bptt = 25 -def get_batch(source, i): - seq_len = min(bptt, len(source) - 1 - i) - data = source[i:i+seq_len] - target = source[i+1:i+1+seq_len].view(-1) - # Need batch dimension first for pipeline parallelism. - return data.t(), target - -###################################################################### -# Model scale and Pipe initialization -# ----------------------------------- -# - - -###################################################################### -# To demonstrate training large Transformer models using pipeline parallelism, -# we scale up the Transformer layers appropriately. We use an embedding -# dimension of 4096, hidden size of 4096, 16 attention heads and 12 total -# transformer layers (``nn.TransformerEncoderLayer``). This creates a model with -# **~1.4 billion** parameters. -# -# We need to initialize the `RPC Framework `__ -# since Pipe depends on the RPC framework via `RRef `__ -# which allows for future expansion to cross host pipelining. We need to -# initialize the RPC framework with only a single worker since we're using a -# single process to drive multiple GPUs. -# -# The pipeline is then initialized with 8 transformer layers on one GPU and 8 -# transformer layers on the other GPU. -# -# .. note:: -# For efficiency purposes we ensure that the ``nn.Sequential`` passed to -# ``Pipe`` only consists of two elements (corresponding to two GPUs), this -# allows the Pipe to work with only two partitions and avoid any -# cross-partition overheads. - -ntokens = len(vocab) # the size of vocabulary -emsize = 4096 # embedding dimension -nhid = 4096 # the dimension of the feedforward network model in ``nn.TransformerEncoder`` -nlayers = 12 # the number of ``nn.TransformerEncoderLayer`` in ``nn.TransformerEncoder`` -nhead = 16 # the number of heads in the Multihead Attention models -dropout = 0.2 # the dropout value - -from torch.distributed import rpc -tmpfile = tempfile.NamedTemporaryFile() -rpc.init_rpc( - name="worker", - rank=0, - world_size=1, - rpc_backend_options=rpc.TensorPipeRpcBackendOptions( - init_method="file://{}".format(tmpfile.name), - # Specifying _transports and _channels is a workaround and we no longer - # will have to specify _transports and _channels for PyTorch - # versions >= 1.8.1 - _transports=["ibv", "uv"], - _channels=["cuda_ipc", "cuda_basic"], - ) -) - -num_gpus = 2 -partition_len = ((nlayers - 1) // num_gpus) + 1 - -# Add encoder in the beginning. -tmp_list = [Encoder(ntokens, emsize, dropout).cuda(0)] -module_list = [] - -# Add all the necessary transformer blocks. -for i in range(nlayers): - transformer_block = TransformerEncoderLayer(emsize, nhead, nhid, dropout) - if i != 0 and i % (partition_len) == 0: - module_list.append(nn.Sequential(*tmp_list)) - tmp_list = [] - device = i // (partition_len) - tmp_list.append(transformer_block.to(device)) - -# Add decoder in the end. -tmp_list.append(Decoder(ntokens, emsize).cuda(num_gpus - 1)) -module_list.append(nn.Sequential(*tmp_list)) - -from torch.distributed.pipeline.sync import Pipe - -# Build the pipeline. -chunks = 8 -model = Pipe(torch.nn.Sequential(*module_list), chunks = chunks) - - -def get_total_params(module: torch.nn.Module): - total_params = 0 - for param in module.parameters(): - total_params += param.numel() - return total_params - -print ('Total parameters in model: {:,}'.format(get_total_params(model))) - -###################################################################### -# Run the model -# ------------- -# - - -###################################################################### -# `CrossEntropyLoss `__ -# is applied to track the loss and -# `SGD `__ -# implements stochastic gradient descent method as the optimizer. The initial -# learning rate is set to 5.0. `StepLR `__ is -# applied to adjust the learn rate through epochs. During the -# training, we use -# `nn.utils.clip_grad_norm\_ `__ -# function to scale all the gradient together to prevent exploding. -# - -criterion = nn.CrossEntropyLoss() -lr = 5.0 # learning rate -optimizer = torch.optim.SGD(model.parameters(), lr=lr) -scheduler = torch.optim.lr_scheduler.StepLR(optimizer, 1.0, gamma=0.95) - -import time -def train(): - model.train() # Turn on the train mode - total_loss = 0. - start_time = time.time() - ntokens = len(vocab) - - # Train only for 50 batches to keep script execution time low. - nbatches = min(50 * bptt, train_data.size(0) - 1) - - for batch, i in enumerate(range(0, nbatches, bptt)): - data, targets = get_batch(train_data, i) - optimizer.zero_grad() - # Since the Pipe is only within a single host and process the ``RRef`` - # returned by forward method is local to this node and can simply - # retrieved via ``RRef.local_value()``. - output = model(data).local_value() - # Need to move targets to the device where the output of the - # pipeline resides. - loss = criterion(output.view(-1, ntokens), targets.cuda(1)) - loss.backward() - torch.nn.utils.clip_grad_norm_(model.parameters(), 0.5) - optimizer.step() - - total_loss += loss.item() - log_interval = 10 - if batch % log_interval == 0 and batch > 0: - cur_loss = total_loss / log_interval - elapsed = time.time() - start_time - print('| epoch {:3d} | {:5d}/{:5d} batches | ' - 'lr {:02.2f} | ms/batch {:5.2f} | ' - 'loss {:5.2f} | ppl {:8.2f}'.format( - epoch, batch, nbatches // bptt, scheduler.get_lr()[0], - elapsed * 1000 / log_interval, - cur_loss, math.exp(cur_loss))) - total_loss = 0 - start_time = time.time() - -def evaluate(eval_model, data_source): - eval_model.eval() # Turn on the evaluation mode - total_loss = 0. - ntokens = len(vocab) - # Evaluate only for 50 batches to keep script execution time low. - nbatches = min(50 * bptt, data_source.size(0) - 1) - with torch.no_grad(): - for i in range(0, nbatches, bptt): - data, targets = get_batch(data_source, i) - output = eval_model(data).local_value() - output_flat = output.view(-1, ntokens) - # Need to move targets to the device where the output of the - # pipeline resides. - total_loss += len(data) * criterion(output_flat, targets.cuda(1)).item() - return total_loss / (len(data_source) - 1) - -###################################################################### -# Loop over epochs. Save the model if the validation loss is the best -# we've seen so far. Adjust the learning rate after each epoch. - -best_val_loss = float("inf") -epochs = 3 # The number of epochs -best_model = None - -for epoch in range(1, epochs + 1): - epoch_start_time = time.time() - train() - val_loss = evaluate(model, val_data) - print('-' * 89) - print('| end of epoch {:3d} | time: {:5.2f}s | valid loss {:5.2f} | ' - 'valid ppl {:8.2f}'.format(epoch, (time.time() - epoch_start_time), - val_loss, math.exp(val_loss))) - print('-' * 89) - - if val_loss < best_val_loss: - best_val_loss = val_loss - best_model = model - - scheduler.step() - - -###################################################################### -# Evaluate the model with the test dataset -# ---------------------------------------- -# - - -###################################################################### -# Apply the best model to check the result with the test dataset. - -test_loss = evaluate(best_model, test_data) -print('=' * 89) -print('| End of training | test loss {:5.2f} | test ppl {:8.2f}'.format( - test_loss, math.exp(test_loss))) -print('=' * 89)