bidirectional_decoder
bidirectional_decoder
¶
Bidirectional transformer decoder for non-autoregressive generation.
BidirectionalDecoder
¶
BidirectionalDecoder(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, positional_encoding_type=None, maximum_sequence_length=2048, bias=True, normalization_epsilon=1e-06, initializer_range=0.02, use_cross_attention=True)
Bases: TransformerMixin, Module
Bidirectional transformer decoder for non-autoregressive generation.
All tokens are processed in parallel (no causal mask). Supports optional cross-attention to encoded features and conditioning cache for reusing precomputed K/V projections across repeated forward calls.
Initialize bidirectional 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
|
positional_encoding_type
|
str | None
|
Type of positional encoding (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
|
use_cross_attention
|
bool
|
Whether to include cross-attention blocks. |
True
|
Source code in src/versatil/models/layers/transformer/bidirectional_decoder.py
precompute_conditioning_kv
¶
Precompute conditioning K/V for all layers for forward pass reuse.
Source code in src/versatil/models/layers/transformer/bidirectional_decoder.py
forward
¶
forward(hidden_states, encoded_features=None, conditioning_cache=None, query_padding_mask=None, memory_padding_mask=None)
Forward pass through bidirectional decoder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Query embeddings (B, query_length, D). |
required |
encoded_features
|
Tensor | None
|
Encoder features to cross-attend to (B, memory_length, D). |
None
|
conditioning_cache
|
ConditioningCache | None
|
Precomputed K/V for static conditioning. When provided, encoded_features is not needed for cross-attention. |
None
|
query_padding_mask
|
Tensor | None
|
Optional padding mask for queries (B, query_length). |
None
|
memory_padding_mask
|
Tensor | None
|
Optional padding mask for memory (B, memory_length). |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Output hidden states (B, query_length, D). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If use_cross_attention=True but neither encoded_features nor conditioning_cache is provided. |