decoder_layer
decoder_layer
¶
General transformer decoder layer with KV cache support.
TransformerDecoderLayer
¶
TransformerDecoderLayer(embedding_dimension, number_of_heads, number_of_key_value_heads=None, feedforward_dimension=None, dropout=0.1, attention_dropout=0.0, activation=value, normalization_type=value, attention_type=value, use_cross_attention=True, bias=True, normalization_epsilon=1e-06, conditioning_dimension=None, use_gating=False, cross_attention_normalization_type=None, cross_attention_conditioning_dimension=None)
Bases: Module
Self-attention + optional cross-attention + feedforward blocks.
Note
Supports generation caching for autoregressive decoding and conditioning caching for static context reuse. Optionally supports conditioning via adaptive normalization and cross-attention.
Initialize Transformer decoder layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_dimension
|
int
|
Model embedding dimension. |
required |
number_of_heads
|
int
|
Number of attention heads. |
required |
number_of_key_value_heads
|
int | None
|
Number of K/V heads (for GQA). |
None
|
feedforward_dimension
|
int | None
|
FFN hidden dimension (defaults to 4 * embedding_dimension). |
None
|
dropout
|
float
|
Dropout probability for residual connections. |
0.1
|
attention_dropout
|
float
|
Dropout probability for attention weights. |
0.0
|
activation
|
str
|
Activation function (use ActivationFunction enum values). |
value
|
normalization_type
|
str
|
Type of normalization (use NormalizationType enum values). |
value
|
attention_type
|
str
|
Type of attention (use AttentionType enum values). |
value
|
use_cross_attention
|
bool
|
Whether to include cross-attention block. |
True
|
bias
|
bool
|
Whether to use bias in linear layers. |
True
|
normalization_epsilon
|
float
|
Epsilon for normalization layers. |
1e-06
|
conditioning_dimension
|
int | None
|
Conditioning dimension for adaptive normalization. When set, wraps normalization in AdaNorm. |
None
|
use_gating
|
bool
|
Whether to use gating in adaptive normalization (AdaLN-Zero). |
False
|
cross_attention_normalization_type
|
str | None
|
Normalization type for the cross-attention
block. Defaults to |
None
|
cross_attention_conditioning_dimension
|
int | None
|
Conditioning dimension for cross-attention normalization. None means no conditioning. |
None
|
Source code in src/versatil/models/layers/transformer/layer/decoder_layer.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
precompute_conditioning_kv
¶
Precompute conditioning K/V for this layer's cross-attention block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoded_features
|
Tensor
|
Encoder features (B, memory_length, D). |
required |
Returns:
| Type | Description |
|---|---|
ConditioningLayerCache | None
|
ConditioningLayerCache if this layer has cross-attention, None otherwise. |
Source code in src/versatil/models/layers/transformer/layer/decoder_layer.py
forward
¶
forward(hidden_states, encoded_features=None, self_attention_mask=None, cross_attention_mask=None, generation_cache=None, conditioning_cache=None, positional_encoding=None, conditioning=None)
Forward pass through decoder layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Input embeddings (B, T, D). |
required |
encoded_features
|
Tensor | None
|
Encoder output for cross-attention (B, S, D). Required when use_cross_attention=True and no conditioning_cache. |
None
|
self_attention_mask
|
Tensor | None
|
Causal mask (B, 1, T, T), True = masked. |
None
|
cross_attention_mask
|
Tensor | None
|
Cross-attention mask (B, 1, T, S), True = masked. |
None
|
generation_cache
|
GenerationLayerCache | None
|
Cached K/V from the main sequence. When provided, an updated cache is returned. |
None
|
conditioning_cache
|
ConditioningLayerCache | None
|
Precomputed K/V for static conditioning. |
None
|
positional_encoding
|
RotaryPositionalEncoding | None
|
Optional rotary positional encoding module. |
None
|
conditioning
|
Tensor | None
|
Conditioning vector for adaptive normalization (B, C). |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, GenerationLayerCache | None]
|
Tuple of (output hidden states (B, T, D), updated GenerationLayerCache or None). |
Raises:
| Type | Description |
|---|---|
ValueError
|
if cross-attention is enabled without encoded_features or conditioning_cache. |