What it does
Multi-head attention projects the input into h smaller sets of queries, keys and values with learned dense layers, runs scaled dot-product attention on each set independently, concatenates the h results and mixes them with a final dense projection. Each head can specialize: one may track the previous word, another the subject of a sentence, another a matching image patch. In ONNX exports from PyTorch it usually appears as three MatMul + Add projections, Reshape and Transpose to split heads, the attention MatMul–Softmax–MatMul core, a Transpose and Reshape to merge heads, and the output MatMul + Add.
Formula
headi = Attention(X WiQ, X WiK, X WiV)MHA(X) = Concat(head1, …, headh) WOWiQ, WiK, WiV ∈ RD × D/h, WO ∈ RD × DShapes and parameters
- Shapes
- Query input [N, T, D], key and value input [N, S, D]; per-head tensors [N, h, T, D/h]; output [N, T, D].
- Parameters
- Four D × D projections plus four bias vectors: 4D² + 4D. With D = 256: 262,144 + 1,024 = 263,168, independent of the number of heads and of sequence length.
- Used for
- Every layer of BERT, ALBERT, DistilBERT, MiniLM, TAPAS, Marian translation, ViT, CLIP, Depth Anything and the Kokoro speech model.
In WeightSpace
Read-only in WeightSpace today. In the catalog’s transformers the editable parameters sit in the output head or the final LayerNorm, so the attention patterns stay as shipped.
Where it appears
- Translate a Chinese sentenceHelsinki-NLP OPUS-MT36111M params
- Segment an object by clickingSlimSAM209.7M params
- Find a caption that fits a photoTinyCLIP ViT-8M/161323M params
- Estimate depth from a photoDepth Anything V2 Small1225M params
- Fill a gap in ChineseBERT Base Chinese12102M params
- Name the emotion in a sentenceALBERT Base v2 Emotion1212M params
- Read a phrase aloudKokoro 82M1281M params
- Transcribe a short English recordingWav2Vec2 Base 960h1294M params
- Change how a voice speakstiny-voca74.5M params
- Classify text with your labelsXtremeDistil zero-shot613M params
- Find an answer in a passageDistilBERT SQuAD665M params
- Find the most relevant answerall-MiniLM-L6-v2623M params
- Ask a question about a tableGoogle TAPAS Tiny24.5M params
- Rerank search resultsMS MARCO TinyBERT-L224.4M params