Attention#
- class sdm.nn.Attention(channels: int, num_query_heads: int, num_key_value_heads: int | None = None, query_transform: Module | None = None, key_transform: Module | None = None, query_scaling: QueryScaling | None = None, scale: float | None = None, bias: bool = True, device: device | str | None = None, dtype: dtype | None = None)#
Bases:
ModuleMulti-head attention layer with grouped-query attention support.
This module owns the query, key, value, and output projections. It performs self-attention when
key_valueis omitted and cross-attention whenkey_valueis given.- Parameters:
channels (int) – The number of input and output channels.
num_query_heads (int) – The number of query attention heads.
channelsmust be divisible bynum_query_heads.num_key_value_heads (int | None) – The number of key/value attention heads. Setting this below
num_query_headsenables grouped-query attention (GQA); setting it to1enables multi-query attention (MQA). Must dividenum_query_heads. Defaults tonum_query_heads(standard multi-head attention).query_transform (Module | None) – Transformation applied to projected query heads before scaled dot-product attention.
key_transform (Module | None) – Transformation applied to projected key heads before scaled dot-product attention.
query_scaling (QueryScaling | None) – Query scaling module to scale projected query heads before scaled dot-product attention, e.g.,
QASSMax.scale (float | None) – Scaling factor passed to
torch.nn.functional.scaled_dot_product_attention().Noneuses1 / sqrt(channels_per_head).bias (bool) – If set to
False, the module will not learn an additive bias.dtype (dtype | None) – The dtype.
- forward(query: Tensor, key_value: Tensor | KVCacheEntry | None = None, seqused_key_value: Tensor | None = None, attn_mask: Tensor | None = None, *, return_key_value: Literal[False] = False) Tensor#
- forward(query: Tensor, key_value: Tensor | KVCacheEntry | None = None, seqused_key_value: Tensor | None = None, attn_mask: Tensor | None = None, *, return_key_value: Literal[True]) tuple[Tensor, KVCacheEntry]
- forward(query: Tensor, key_value: Tensor | KVCacheEntry | None = None, seqused_key_value: Tensor | None = None, attn_mask: Tensor | None = None, *, return_key_value: bool) Tensor | tuple[Tensor, KVCacheEntry]
The forward pass.
- Parameters:
query (Tensor) – The query tensor with shape
[..., Q, C].Qis the query sequence length,Cis the number of channels.key_value (Tensor | KVCacheEntry | None) – The key/value tensor with shape
[..., KV, C]or precomputed key/value projections as aKVCacheEntry.KVis the key/value sequence length. If omitted,queryis used for self-attention.seqused_key_value (Tensor | None) – Valid key/value lengths with shape
[...]and torch.int32 dtype.attn_mask (Tensor | None) – Boolean attention mask with shape
[..., Q, KV]. Entries set toTrueparticipate in attention.return_key_value (bool) – Whether to return the computed key and value projections alongside the attention output.
- Returns:
Tensor with shape
[..., Q, C]whenreturn_key_valueisFalse. Otherwise, a tuple of the output tensor and aKVCacheEntry.- Return type: