autoregressive_decoder
autoregressive_decoder
¶
GPT-style autoregressive decoder.
GPTDecoder
¶
GPTDecoder(number_of_layers, 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=False, positional_encoding_type=None, maximum_sequence_length=2048, bias=True, normalization_epsilon=1e-06, initializer_range=0.02)
Bases: TransformerMixin, Module
GPT-style autoregressive decoder, with KV caching, extended to support cross-attention.
Stacks multiple TransformerDecoderLayer modules and manages KV cache across layers. Applies causal masking for autoregressive generation.
Initialize GPT decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number_of_layers
|
int
|
Number of decoder layers |
required |
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 |
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 use cross-attention (False for decoder-only models) |
False
|
positional_encoding_type
|
str | None
|
Type of positional encoding (use PositionalEncodingType enum values, or None) |
None
|
maximum_sequence_length
|
int
|
Maximum sequence length for positional encoding |
2048
|
bias
|
bool
|
Whether to use bias in linear layers |
True
|
normalization_epsilon
|
float
|
Epsilon for normalization layers |
1e-06
|
initializer_range
|
float
|
Standard deviation for weight initialization |
0.02
|
Source code in src/versatil/models/layers/transformer/autoregressive_decoder.py
create_empty_generation_cache
¶
Create an initial empty GenerationCache for autoregressive generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Batch size. |
required |
device
|
device | str
|
Device for cache tensors. |
required |
dtype
|
dtype
|
Data type for cache tensors. |
float32
|
Returns:
| Type | Description |
|---|---|
GenerationCache
|
GenerationCache with empty layers ready for the first generation step. |
Source code in src/versatil/models/layers/transformer/autoregressive_decoder.py
precompute_conditioning_kv
¶
Precompute conditioning K/V for all layers for forward pass reuse.
Source code in src/versatil/models/layers/transformer/autoregressive_decoder.py
forward
¶
forward(hidden_states, encoded_features=None, self_attention_mask=None, cross_attention_mask=None, key_padding_mask=None, generation_cache=None, conditioning_cache=None)
Forward pass through decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Input token embeddings (B, query_length, D). |
required |
encoded_features
|
Tensor | None
|
Encoder features (B, num_features, D). Required when use_cross_attention=True and no conditioning_cache. |
None
|
self_attention_mask
|
Tensor | None
|
Custom causal mask (B, 1, query_length, query_length), True = masked. If None, generates standard triangular causal mask. |
None
|
cross_attention_mask
|
Tensor | None
|
Mask for cross-attention (B, 1, query_length, key_length), True = masked. |
None
|
key_padding_mask
|
Tensor | None
|
Padding mask for observation tokens (B, query_length), True = masked. |
None
|
generation_cache
|
GenerationCache | None
|
Cached K/V from previous generation steps. When provided, an updated cache is returned. |
None
|
conditioning_cache
|
ConditioningCache | None
|
Precomputed K/V for static conditioning. When provided, encoded_features is not needed for cross-attention. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, GenerationCache | None]
|
Tuple of (output (B, query_length, D), updated GenerationCache or None). |
Source code in src/versatil/models/layers/transformer/autoregressive_decoder.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |