Multi-Head Attention
Implement multi-head attention by splitting Q, K, V into multiple heads.
Given Q, K, V of shape (n, d) and number of heads h:
- Split each into h heads: reshape (n, d) → (n, h, d/h) → (h, n, d/h)
- Apply scaled dot-product attention per head
- Concatenate heads: (h, n, d/h) → (n, d)
Assume d is divisible by h. No linear projections needed — just split and concat.
Input:
- Line 1: n d h
- Next n lines: Q matrix
- Next n lines: K matrix
- Next n lines: V matrix
Output: Multi-head attention output (n, d), values rounded to 4 decimal places.
Example:
2 4 2 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 2 3 4 5 6 7 8
[1.7311 2.7311 3.2689 4.2689] [4.2689 5.2689 6.7311 7.7311]
- First, we split the input matrices Q, K, V into 2 heads: Q = [10​01​], [01​10​], K = [10​01​], [01​10​], V = [15​26​], [37​48​]
- Then, we apply scaled dot-product attention per head: for the first head, Attention(Q,K,V)=2​Q⋅KT​⋅V=2​[10​01​]⋅[10​01​]​⋅[15​26​] and similarly for the second head
- Next, we calculate the attention output for each head and concatenate them: Output=Concat(Attention1​,Attention2​)=[1.73114.2689​2.73115.2689​], [3.26896.7311​4.26897.7311​]
- The final output is the concatenated output matrix, rounded to 4 decimal places: [1.73114.2689​2.73115.2689​3.26896.7311​4.26897.7311​]
Constraints:
- d is divisible by h
- 1 <= h <= d, 1 <= n <= 10
- No projection matrices — just split/concat
- Round to 4 decimal places
Background Knowledge
The concept of attention mechanisms is crucial in understanding the problem of multi-head attention. Attention mechanisms allow a model to focus on specific parts of the input data that are relevant for a particular task. This is particularly useful in sequence-to-sequence models, such as machine translation, where the model needs to attend to different parts of the input sequence to generate the output sequence. The scaled dot-product attention is a specific type of attention mechanism that calculates the attention weights by taking the dot product of the query and key vectors and applying a scaling factor.
In the context of multi-head attention, the idea is to split the input vectors (Q, K, V) into multiple heads, allowing the model to jointly attend to information from different representation subspaces at different positions. This is achieved by splitting the input vectors into multiple heads, applying scaled dot-product attention to each head, and then concatenating the outputs. The number of heads (h) is a hyperparameter that needs to be set, and the dimensionality of the input vectors (d) should be divisible by the number of heads.
The mathematical formulation of scaled dot-product attention involves calculating the attention weights using the formula: A=softmax(d​Q⋅KT​), where Q, K, and V are the query, key, and value vectors, respectively, and d is the dimensionality of the vectors. The output of the attention mechanism is then calculated as: O=A⋅V. In the context of multi-head attention, this process is applied to each head separately.
Algorithm/Approach
The general approach to solving this problem involves the following steps:
- Split the input vectors (Q, K, V) into multiple heads
- Apply scaled dot-product attention to each head
- Concatenate the outputs from each head This approach requires a good understanding of tensor manipulation and attention mechanisms.
Step-by-Step Strategy
To implement the solution, follow these steps:
- Read the input values: n, d, h, Q, K, V
- Split each of Q, K, V into h heads by reshaping the tensors
- Apply scaled dot-product attention to each head
- Concatenate the outputs from each head
- Round the output values to 4 decimal places
Common Pitfalls
Some common pitfalls to watch out for include:
- Incorrect tensor reshaping and concatenation
- Forgetting to apply the scaling factor in the scaled dot-product attention
- Not handling the case where d is not divisible by h (although this is not a concern in this specific problem)
Time & Space Complexity
The time complexity of the solution is O(n^2 * d) due to the matrix multiplication involved in the scaled dot-product attention. The space complexity is O(n * d) for storing the input vectors and the output. Note that these complexities assume that the number of heads (h) is a constant factor.