PIXELBANKv9.1.0
Menu

Multi-Head Attention Forward Pass (Simplified)

Implement a simplified Multi-Head Self-Attention (MHSA) forward pass.

Given input embeddings X∈RNΓ—DX \in \mathbb{R}^{N \times D} and weight matrix WQKV∈RDΓ—3DW_{QKV} \in \mathbb{R}^{D \times 3D}:

  1. Compute [Q,K,V]=Xβ‹…WQKV[Q, K, V] = X \cdot W_{QKV} (split into three D-dimensional matrices)
  2. Compute attention: A=Qβ‹…KTA = Q \cdot K^T (unscaled, no softmax)
  3. Apply residual: Y=X+Aβ‹…VY = X + A \cdot V

Simplifications:

  • Single head (no multi-head concatenation)
  • No softmax or scaling factor D\sqrt{D}
  • No output projection WOW_O
  • No layer normalization or MLP

Example:

Input:
X = [[1,0],[0,1]], W_QKV = 2x6 identity-like matrix
Output:
2x2 matrix Y after residual connection
Reasoning:

Q, K, V are derived from X @ W_QKV split into thirds. A = QK^T computes attention scores. Y = X + AV applies residual.

Constraints:

  • NN (Sequence Length): 2-4
  • DD (Embedding Dimension): 2-3
  • Input XX and WQKVW_{QKV} are provided as numpy arrays
πŸ”’

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Multi-Head Attention Forward Pass (Simplified) - Hard | PixelBank