braindecode.modules.PreNormBlock#

class braindecode.modules.PreNormBlock(dim, attn, mlp, norm_layer=<class 'torch.nn.modules.normalization.LayerNorm'>, drop_path=0.0, layer_scale=None)[source]#

Pre-norm transformer block with timm’s parameter names.

x = x + drop_path(gamma_1 * attn(norm1(x))) then x = x + drop_path(gamma_2 * mlp(norm2(x))), as in ViT/MAE/BEiT checkpoints (norm1, attn, norm2, mlp; gamma_1, gamma_2 with layer scale). EEGPT, EEGDINO, LUNA and NeuroRVQ use it, so their released weights load with the same keys.

Parameters:
  • dim (int) – Token width, normalized by norm1 and norm2.

  • attn (Module) – Token-mixing sub-layer, (batch, seq, dim) -> (batch, seq, dim).

  • mlp (Module) – Feed-forward sub-layer, same shape.

  • norm_layer (Callable[[int], Module]) – Called as norm_layer(dim).

  • drop_path (float) – Stochastic depth rate on both sub-layer outputs (DropPath); 0 disables it.

  • layer_scale (Optional[float]) – Initial value of the per-feature gamma_1/gamma_2 scales (CaiT/BEiT layer scale); None adds no such parameters.

Examples

>>> import torch
>>> from torch import nn
>>> from braindecode.modules import PreNormBlock, TimmAttention, TimmMlp
>>> block = PreNormBlock(16, TimmAttention(16, 4), TimmMlp(16, 32))
>>> block(torch.randn(2, 5, 16)).shape
torch.Size([2, 5, 16])

Methods

forward(x)[source]#

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

Return type:

Tensor