Multi-Head Attention Forward Pass (Simplified)
Implement a simplified Multi-Head Self-Attention (MHSA) forward pass.
Given input embeddings and weight matrix :
- Compute (split into three D-dimensional matrices)
- Compute attention: (unscaled, no softmax)
- Apply residual:
Simplifications:
- Single head (no multi-head concatenation)
- No softmax or scaling factor
- No output projection
- No layer normalization or MLP
Example
X = [[1, 0], [0, 1]] W_QKV = [[1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1]]
[[2.0, 0.0], [0.0, 2.0]]
- Inputs:
Xis tokens with features, the identity .W_QKVis : three blocks (query, key, value), each , so . - Projection: is ; its column blocks of width are , , . With identity blocks, .
- Scores: is ; entry is the dot product of token 's query with token 's key. Here and , so : each token attends only to itself, with raw weight 1 (no scaling, no softmax).
- Mix + residual: , so , giving
[[2.0, 0.0], [0.0, 2.0]], an result like . - Notice: with all-ones , every score is 2, is all 4s and is all 5s: unnormalised scores grow fast.
Constraints
- (Sequence Length): 2-4
- (Embedding Dimension): 2-3
- Input and are provided as numpy arrays