下列文本提取自本地原版 PDF。图表和公式的准确布局请对照 PDF。
3.4.4 Scaled Dot-Product Attention
We will now implement scaled dot-product attention as described in A. Vaswani et al. [8] (section 3.2.1).
As a preliminary step, the definition of the Attention operation will make use of softmax, an operation
that takes an unnormalized vector of scores and turns it into a normalized distribution:
exp(𝑣𝑖 )
softmax(𝑣)𝑖 = 𝑛 . (10)
∑𝑗=1 exp(𝑣𝑗 )
Note that exp(𝑣𝑖 ) can become inf for large values (then, inf
inf = NaN). We can avoid this by noticing that
the softmax operation is invariant to adding any constant 𝑐 to all inputs. We can leverage this property
for numerical stability—typically, we will subtract the largest entry of 𝑣 from all elements of 𝑣, making
the new largest entry 0. You will now implement softmax, using this trick for numerical stability.
Problem (softmax): Implement softmax (1 point)
Deliverable: Write a function to apply the softmax operation on a tensor. Your function should
take two parameters: a tensor and a dimension 𝑖, and apply softmax to the 𝑖-th dimension of the
input tensor. The output tensor should have the same shape as the input tensor, but its 𝑖-th
dimension will now have a normalized probability distribution. Use the trick of subtracting the
maximum value in the 𝑖-th dimension from all elements of the 𝑖-th dimension to avoid numerical
stability issues.
To test your implementation, complete [adapters.run_softmax] and make sure it passes uv run
pytest -k test_softmax_matches_pytorch.
We can now define the Attention operation mathematically as follows:
𝑄𝐾 𝑇
Attention(𝑄, 𝐾, 𝑉 ) = softmax( )𝑉 (11)
√𝑑𝑘
where 𝑄 ∈ ℝ𝑛×𝑑𝑘 , 𝐾 ∈ ℝ𝑚×𝑑𝑘 , and 𝑉 ∈ ℝ𝑚×𝑑𝑣 . Here, 𝑄, 𝐾 and 𝑉 are all inputs to this operation—note
that these are not the learnable parameters.
Masking: It is sometimes convenient to mask the output of an attention operation. A mask should have
the shape 𝑀 ∈ {True, False}𝑛×𝑚 , and each row 𝑖 of this boolean matrix indicates which keys the query 𝑖
should attend to. Canonically (and slightly confusingly), a value of True at position (𝑖, 𝑗) indicates that
the query 𝑖 does attend to the key 𝑗, and a value of False indicates that the query does not attend to the
key. In other words, “information flows” at (𝑖, 𝑗) pairs with value True. For example, consider a 1 × 3
mask matrix with entries [[True, True, False]]. The single query vector attends only to the first two keys.
Computationally, it will be much more efficient to use masking than to compute attention on
subsequences, and we can do this by taking the pre-softmax values (𝑄𝐾 ⊤ /√𝑑𝑘 ) and adding a −∞ to
any entry of the mask matrix that is False.
Problem (scaled_dot_product_attention): Implement scaled dot-product attention (5
points)
Deliverable: Implement the scaled dot-product attention function. Your implementation should
handle keys and queries of shape (batch_size, ..., seq_len, d_k) and values of shape
(batch_size, ..., seq_len, d_v), where ... represents any number of other batch-like
dimensions (if provided). The implementation should return an output with the shape
(batch_size, ..., seq_len, d_v). See Section 3.2 for a discussion on batch-like dimensions.
Your implementation should also support an optional user-provided boolean mask of shape
(seq_len, seq_len). The attention probabilities of positions with a mask value of True should
collectively sum to 1, and the attention probabilities of positions with a mask value of False
should be zero.
To test your implementation against our provided tests, you will need to implement the test
adapter at [adapters.run_scaled_dot_product_attention] . uv run pytest -k
test_scaled_dot_product_attention tests your implementation on third-order input tensors, while
uv run pytest -k test_4d_scaled_dot_product_attention tests your implementation on fourth-
order input tensors.
本章讨论
在 GitHub 查看记录 ↗