podcasts / The attention family / episode / question
Question

Why do you transpose dimensions 1 and 2 when computing batched multi-head attention?

Answered from Introduction to Multi-Head Attention (MHA) | The attention family series - Lecture 8 · The attention family · 2026-10-03

Sreedath Panat explains that batched queries arrive with shape batch, tokens, heads, head dimension, for example 2, 6, 2, 3. Naively transposing the last two axes of the keys and multiplying would give 2 by 2 score matrices per token, not the 6 by 6 token-by-token matrix attention needs. Swapping axes 1 and 2 first gives 2, 2, 6, 3, so queries times transposed keys gives 2, 2, 6, 6: one 6 by 6 score matrix per head per batch item. After the values are applied the same transpose is reversed, and the heads are merged back to 2, 6, 6.

Source: our own transcription of the episode. The full conversation is on The attention family (2026-10-03). Read the whole episode summary →

▸More from this episode

Q1Why do transformers use multiple attention heads, according to Vizuara?Q2How are the outputs of multiple attention heads combined into one context vector per token?Q3How many dimensions does each attention head get in GPT-2 and GPT-3?Q4Why is implementing multi-head attention with a for loop slow?Q5What is the weight split approach to multi-head attention that Vizuara teaches?
Transcribe your links. Paste a YouTube, Google Drive or podcast link and WhipScribe gives you the transcript. Transcribe a link →