SDPA#
- class sdm.nn.SDPA(num_query_heads: int, num_key_value_heads: int | None = None, query_scaling: QueryScaling | None = None, scale: float | None = None)#
Bases:
ModuleScaled Dot-Product Attention (SDPA).
This module wraps
torch.nn.functional.scaled_dot_product_attention()and extends it by arbitrary batch dimensions, optional inference-time batch chunking, query-scaling, and padding support for key/value pairs.- Parameters:
num_query_heads (int) – The number of query attention 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_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().Noneuses the default value of1 / sqrt(channels).
- forward(query: Tensor, key: Tensor, value: Tensor, seqused_key_value: Tensor | None = None, attn_mask: Tensor | None = None) Tensor#
The forward pass.
- Parameters:
query (Tensor) – The query tensor with shape
[..., Q, Hq, C].Qis the query sequence length,Hqis the number of query attention heads (num_query_heads), andCis the channels per head.key (Tensor) – The key tensor with shape
[..., KV, Hkv, C].KVis the key/value sequence length andHkvis the number of key/value heads (num_key_value_heads).value (Tensor) – The value tensor with shape
[..., KV, Hkv, C].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.
- Returns:
Tensor with shape
[..., Q, Hq, C].- Return type: