下列文本提取自本地原版 PDF。图表和公式的准确布局请对照 PDF。
3.4.5 Causal Multi-Head Self-Attention
We will implement multi-head self-attention as described in section 3.2.2 of A. Vaswani et al. [8]. Recall
that, mathematically, the operation of applying multi-head attention is defined as follows:
MultiHead(𝑄, 𝐾, 𝑉 ) = Concat(head1 , …, headℎ ) (12)
for head𝑖 = Attention(𝑄𝑖 , 𝐾𝑖 , 𝑉𝑖 ) (13)
with 𝑄𝑖 , 𝐾𝑖 , 𝑉𝑖 being slice number 𝑖 ∈ {1, …, ℎ} of size 𝑑𝑘 or 𝑑𝑣 of the embedding dimension for 𝑄, 𝐾, and
𝑉 respectively. With Attention being the scaled dot-product attention operation defined in Section 3.4.4.
From this we can form the multi-head self-attention operation:
MultiHeadSelfAttention(𝑥) = 𝑊𝑂 MultiHead(𝑊𝑄 𝑥, 𝑊𝐾 𝑥, 𝑊𝑉 𝑥) (14)
Here, the learnable parameters are 𝑊𝑄 ∈ ℝℎ𝑑𝑘 ×𝑑model , 𝑊𝐾 ∈ ℝℎ𝑑𝑘 ×𝑑model , 𝑊𝑉 ∈ ℝℎ𝑑𝑣 ×𝑑model , and 𝑊𝑂 ∈
ℝ𝑑model ×ℎ𝑑𝑣 . Since the 𝑄s, 𝐾s, and 𝑉 s are sliced in the multi-head attention operation, we can think of
𝑊𝑄 , 𝑊𝐾 and 𝑊𝑉 as being separated for each head along the output dimension. When you have this
working, you should be computing the key, value, and query projections in a total of three matrix
multiplies.5
Causal masking
Your implementation should prevent the model from attending to future tokens in the sequence. In other
words, if the model is given a token sequence 𝑡1 , …, 𝑡𝑛 , and we want to calculate the next-word predictions
for the prefix 𝑡1 , …, 𝑡𝑖 (where 𝑖 < 𝑛), the model should not be able to access (attend to) the token
representations at positions 𝑡𝑖+1 , …, 𝑡𝑛 since it will not have access to these tokens when generating text
during inference (and these future tokens leak information about the identity of the true next word,
trivializing the language modeling pre-training objective). For an input token sequence 𝑡1 , …, 𝑡𝑛 we can
naively prevent access to future tokens by running multi-head self-attention 𝑛 times (for the 𝑛 unique
prefixes in the sequence). Instead, we’ll use causal attention masking, which allows token 𝑖 to attend to all
positions 𝑗 ≤ 𝑖 in the sequence. You can use torch.triu or a broadcasted index comparison to construct
this mask, and you should take advantage of the fact that your scaled dot-product attention
implementation from Section 3.4.4 already supports attention masking.
Applying RoPE
RoPE should be applied to the query and key vectors, but not the value vectors. Also, the head
dimension should be handled as a batch dimension, because in multi-head attention, attention is being
applied independently for each head. This means that precisely the same RoPE rotation should be
applied to the query and key vectors for each head.
Problem (multihead_self_attention): Implement causal multi-head self-attention (5 points)
5
As a stretch goal, try combining the key, query, and value projections into a single weight matrix so you only need a
single matrix multiply.
Deliverable: Implement causal multi-head self-attention as a torch.nn.Module. Your
implementation should accept (at least) the following parameters:
d_model: int Dimensionality of the Transformer block inputs.
num_heads: int Number of heads to use in multi-head self-attention.
𝑑
Following A. Vaswani et al. [8], set 𝑑𝑘 = 𝑑𝑣 = model
ℎ . To test your implementation against our
provided tests, implement the test adapter at [adapters.run_multihead_self_attention] . Then,
run uv run pytest -k test_multihead_self_attention to test your implementation.
本章讨论
在 GitHub 查看记录 ↗