下列文本提取自本地原版 PDF。图表和公式的准确布局请对照 PDF。
3 Transformer Language Model Architecture
A language model takes as input a batched sequence of integer token IDs (i.e., torch.Tensor of shape
(batch_size, sequence_length)), and returns a (batched) normalized probability distribution over the
vocabulary (i.e., a PyTorch Tensor of shape (batch_size, sequence_length, vocab_size)), where the
predicted distribution is over the next word for each input token. When training the language model, we
use these next-word predictions to calculate the cross-entropy loss between the actual next word and the
predicted next word. When generating text from the language model during inference, we take the
predicted next-word distribution from the final time step (i.e., the last item in the sequence) to generate
the next token in the sequence (e.g., by taking the token with the highest probability, sampling from the
distribution, etc.), add the generated token to the input sequence, and repeat.
In this part of the assignment, you will build this Transformer language model from scratch. We will
begin with a high-level description of the model before progressively detailing the individual components.
3.1 Transformer LM
Given a sequence of token IDs, the Transformer language model uses an input embedding to convert
token IDs to dense vectors, passes the embedded tokens through num_layers Transformer blocks, and then
applies a learned linear projection (the “output embedding” or “LM head”) to produce the predicted
next-token logits. See Figure 1 for a schematic representation.
Output Output tensor with shape
Probabilities (batch_size, seq_len, d_model)
Softmax
Add
Linear
(Output Embedding)
Position-Wise
Feed-Forward
Norm
Norm
Transformer Block
num_layers
... Transformer Add
Blocks
Transformer Block Causal Multi-Head
Self-Attention
w/ RoPE
Token Norm
Embedding
Input tensor with shape
Inputs (batch_size, seq_len, d_model)
Figure 1: An overview of our Transformer language Figure 2: A pre-norm Transformer block.
model.
Token Embeddings
In the very first step, the Transformer embeds the (batched) sequence of token IDs into a sequence of
vectors containing information on the token identity (red blocks in Figure 1).
More specifically, given a sequence of token IDs, the Transformer language model uses a token embedding
layer to produce a sequence of vectors. Each embedding layer takes in a tensor of integers of shape
(batch_size, sequence_length) and produces a sequence of vectors of shape (batch_size,
sequence_length, d_model).
Pre-norm Transformer Block
After embedding, the activations are processed by several identically structured neural net layers. A
standard decoder-only Transformer language model consists of num_layers identical layers (commonly
called Transformer “blocks”). Each Transformer block takes in an input of shape (batch_size,
sequence_length, d_model) and returns an output of shape (batch_size, sequence_length, d_model).
Each block aggregates information across the sequence (via self-attention) and non-linearly transforms it
(via the feed-forward layers).
After num_layers Transformer blocks, we will take the final activations and turn them into a distribution
over the vocabulary.
We will implement the “pre-norm” Transformer block (detailed in Section 3.4), which additionally
requires the use of layer normalization (detailed below) after the final Transformer block to ensure its
outputs are properly scaled.
After this normalization, we will use a standard learned linear transformation to convert the output of
the Transformer blocks into predicted next-token logits (see, e.g., A. Radford et al. [7] equation 2).
本章讨论
在 GitHub 查看记录 ↗