cross_attention
cross_attention
¶
Cross-attention block: norm -> cross-attention -> gated residual.
CrossAttentionBlock
¶
Bases: TransformerBlock
Norm -> cross-attention to encoder hidden states -> gated residual.
Source code in src/versatil/models/layers/transformer/block/cross_attention.py
precompute_kv
¶
Precompute K/V projections for static conditioning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded_features
|
Tensor
|
Encoder features (B, memory_length, D). |
required |
Returns:
| Type | Description |
|---|---|
ConditioningLayerCache
|
ConditioningLayerCache with projected keys and values. |
Source code in src/versatil/models/layers/transformer/block/cross_attention.py
forward
¶
forward(hidden_states, encoder_hidden_states=None, conditioning=None, attention_mask=None, conditioning_cache=None)
Norm -> cross-attention -> gated residual.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Query input (B, T, D). |
required |
encoder_hidden_states
|
Tensor | None
|
Encoder output for K/V projection (B, S, D). Can be None when using conditioning_cache. |
None
|
conditioning
|
Tensor | None
|
Conditioning vector for AdaNorm (B, C). Ignored by UnconditionedNorm. |
None
|
attention_mask
|
Tensor | None
|
Bool mask (B, 1, T, S), True = masked. |
None
|
conditioning_cache
|
ConditioningLayerCache | None
|
Precomputed K/V for static conditioning. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Output hidden states (B, T, D). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both encoder_hidden_states and conditioning_cache are None. |