You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a batch_first argument to MultiheadAttention #127
torch.nn.MultiheadAttention defaults to batch_first=False, so sequence-first is the layout a torch user arrives holding. Here the last axis is the embedding and the second-to-last is treated as the sequence, so a seq-first batch is legal, produces an output of the same shape, and mixes every example in the batch into every other one. With is_causal=True you additionally get a causal mask over the batch axis. Nothing raises at any point.
Checking that the embedding axis matches n_embd would catch it, since the seq-first layout only type-checks when n_embd happens to equal the batch size.
changed the title [-]MultiheadAttention on a (seq, batch, embd) input attends across the batch[/-][+]Add a batch_first argument to MultiheadAttention[/+]on Sep 1, 2026
The layer documents (batch, time, n_embd), and both layouts are legal shapes, so nothing can tell a seq-first input from a batch-first one. That makes this a missing feature rather than a bug. What's worth doing is a batch_first argument, the way torch spells it.
Activity
[-]MultiheadAttention on a (seq, batch, embd) input attends across the batch[/-][+]Add a batch_first argument to MultiheadAttention[/+]jessegrabowski commentedon Sep 1, 2026
The layer documents
(batch, time, n_embd), and both layouts are legal shapes, so nothing can tell a seq-first input from a batch-first one. That makes this a missing feature rather than a bug. What's worth doing is abatch_firstargument, the way torch spells it.