query_key_norm
query_key_norm
¶
Query-Key Normalization for attention mechanisms.
Applies RMSNorm to query and key tensors in the attention head dimension for improved training stability at scale. Used in SD3/MMDiT architectures.
References
Esser et al. "Scaling Rectified Flow Transformers for High-Resolution Image Synthesis" https://arxiv.org/abs/2403.03206
QueryKeyNorm
¶
Bases: Module
Query-Key normalization using RMSNorm.
Normalizes query and key tensors independently using RMSNorm in the head dimension before computing attention. This improves training stability especially when scaling to larger models.
Initialize QueryKeyNorm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
head_dimension
|
int
|
Dimension of each attention head. |
required |
epsilon
|
float
|
Small constant for numerical stability. |
1e-06
|
Source code in src/versatil/models/layers/transformer/attention/query_key_norm.py
forward
¶
Apply RMSNorm to query and key tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
Tensor
|
Query tensor (B, number_of_heads, sequence_length, head_dimension). |
required |
key
|
Tensor
|
Key tensor (B, number_of_heads, sequence_length, head_dimension). |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
Tuple of normalized (query, key) tensors with same shapes. |