mr2.nn.attention.SpatialTransformerBlock

class mr2.nn.attention.SpatialTransformerBlock[source]

Bases: CondMixin, Module

Spatial transformer block.

__init__(dim_groups: Sequence[tuple[int, ...]], channels: int, n_heads: int, depth: int = 1, p_dropout: float = 0.0, cond_dim: int = 0, rope_embed_fraction: float = 0.0, attention_neighborhood: int | None = None, features_last: bool = False, norm: Literal['group', 'rms'] = 'group')[source]

Initialize the spatial transformer block.

Parameters:
  • dim_groups (Sequence[tuple[int, ...]]) – Groups of spatial dimensions for separate attention mechanisms.

  • channels (int) – Number of channels in the input and output.

  • n_heads (int) – Number of attention heads for each group.

  • depth (int, default: 1) – Number of transformer blocks for each group.

  • p_dropout (float, default: 0.0) – Dropout probability.

  • cond_dim (int, default: 0) – Dimension of the conditioning tensor.

  • rope_embed_fraction (float, default: 0.0) – Fraction of channels to embed with RoPE.

  • attention_neighborhood (int | None, default: None) – If not None, use NeighborhoodSelfAttention with the given neighborhood size instead of MultiHeadAttention.

  • features_last (bool, default: False) – Whether the features are last in the input tensor, as common in transformer models.

  • norm (Literal['group', 'rms'], default: 'group') – Whether to use GroupNorm or RMSNorm.

__call__(x: Tensor, *, cond: Tensor | None = None) → Tensor[source]

Apply the spatial transformer block.

Parameters:
  • x (Tensor) – Input tensor.

  • cond (Tensor | None, default: None) – Conditioning tensor. If None, no conditioning is applied.

Returns:

Output tensor.

forward(x: Tensor, *, cond: Tensor | None = None) → Tensor[source]

Apply the spatial transformer block.