braindecode.modules.MultiHeadAttention#
- class braindecode.modules.MultiHeadAttention(emb_size, num_heads, dropout=0.0, scale=None, head_dim=None, qk_norm=False)[source]#
Multi-head self-attention block.
Uses
F.scaled_dot_product_attentionfor optimized attention kernels (flash-attention on CUDA, memory-efficient on other devices).- Parameters:
emb_size (int) – The embedding dimension.
num_heads (int) – Number of attention heads. Must evenly divide
emb_sizewhenhead_dimisNone.dropout (float, optional) – Dropout probability applied to attention weights. Default: 0.0.
scale (
float|None) – Multiplier applied to attention scores before softmax. WhenNone, PyTorch’s defaulthead_dim ** -0.5is used.head_dim (
int|None) – Width of each head. WhenNone,emb_size // num_heads; otherwise the query/key/value projections arenum_heads * head_dimwide.qk_norm (
bool) – Apply aLayerNormoverhead_dimto the queries and keys of each head (as in LaBraM and Du-IN). DefaultFalse.
Examples
>>> import torch >>> from braindecode.modules import MultiHeadAttention >>> module = MultiHeadAttention(emb_size=32, num_heads=4, dropout=0.1) >>> inputs = torch.randn(2, 10, 32) >>> outputs = module(inputs) >>> outputs.shape torch.Size([2, 10, 32])
Methods
- forward(x, mask=None)[source]#
Forward pass.
- Parameters:
x (
Tensor) – Input tensor of shape(batch, seq, emb_size).mask (
Optional[Tensor]) – Attention mask following the convention oftorch.nn.functional.scaled_dot_product_attention(): for boolean masksTruemeans the position takes part in attention andFalsethat it is ignored (the opposite of thekey_padding_maskoftorch.nn.MultiheadAttention); for float masks the values are added to the attention scores before softmax.
- Return type: