Implement the multi-head attention mechanism, a critical component of transformer models. You need to implement three functions:
compute_qkv(X, W_q, W_k, W_v): Compute Query, Key, and Value matrices by multiplying input X with weight matrices. Returns a tuple (Q, K, V) where each has the same shape as X.
self_attention(Q, K, V): Compute scaled dot-product attention for a single head. Returns the attention output with the same shape as V.
multi_head_attention(Q, K, V, n_heads): Split Q, K, V into multiple heads along the feature dimension, compute self-attention for each head independently, and concatenate results. Returns output with the same shape as Q.
Inputs:
Workflow: First call compute_qkv to get Q, K, V matrices, then pass them to multi_head_attention.
Important: Use numerically stable softmax (subtract max before exponentiating) to prevent overflow.