precomputed_dual_stream_layer
precomputed_dual_stream_layer
¶
Dual-stream layer where the secondary stream has precomputed Q/K/V: joint attention + primary FFN.
PrecomputedDualStreamLayer
¶
PrecomputedDualStreamLayer(primary_embedding_dimension, secondary_embedding_dimension, number_of_heads, number_of_key_value_heads, head_dimension, primary_feedforward_dimension, normalization_type=value, conditioning_dimension=None, use_gating=False, dropout=0.1, activation=value, bias=False, use_query_key_norm=False)
Bases: Module
Joint attention with precomputed secondary Q/K/V, plus primary feedforward.
The secondary stream provides pre-projected Q/K/V from an external source. Only the primary stream has learnable normalization and feedforward.
Initialize PrecomputedDualStreamLayer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primary_embedding_dimension
|
int
|
Primary stream embedding dimension. |
required |
secondary_embedding_dimension
|
int
|
Secondary stream embedding dimension. |
required |
number_of_heads
|
int
|
Number of attention heads. |
required |
number_of_key_value_heads
|
int
|
Number of K/V heads. |
required |
head_dimension
|
int
|
Dimension per attention head. |
required |
primary_feedforward_dimension
|
int
|
FFN hidden dimension for primary stream. |
required |
normalization_type
|
str
|
Normalization type for primary stream. |
value
|
conditioning_dimension
|
int | None
|
Conditioning dimension for adaptive normalization. |
None
|
use_gating
|
bool
|
Whether to use gating in adaptive normalization. |
False
|
dropout
|
float
|
Dropout rate for residual connections. |
0.1
|
activation
|
str
|
Activation function for FFN. |
value
|
bias
|
bool
|
Whether to use bias in linear layers. |
False
|
use_query_key_norm
|
bool
|
Whether to apply QK-normalization. |
False
|
Source code in src/versatil/models/layers/transformer/layer/precomputed_dual_stream_layer.py
forward
¶
forward(hidden_states, conditioning_cache, conditioning=None, attention_mask=None, precomputed_rope=None)
Forward pass with precomputed secondary Q/K/V from conditioning cache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states
|
Tensor
|
Primary stream tokens (B, T, D). |
required |
conditioning_cache
|
ConditioningLayerCache
|
Precomputed secondary Q/K/V. queries, keys, values each shaped (B, H/KV_H, S, D_head). |
required |
conditioning
|
Tensor | None
|
Conditioning vector for adaptive normalization (B, C). |
None
|
attention_mask
|
Tensor | None
|
Pre-built joint mask (B, 1, S+T, S+T). |
None
|
precomputed_rope
|
tuple[Tensor, Tensor] | None
|
Pre-computed (cos, sin) rotary positional encodings for primary stream positions. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Processed primary stream output (B, T, D). |
Source code in src/versatil/models/layers/transformer/layer/precomputed_dual_stream_layer.py
forward_with_secondary
¶
forward_with_secondary(hidden_states_primary, conditioning_cache, conditioning=None, joint_attention_mask=None, precomputed_primary_rope=None)
Forward pass returning both primary hidden states and secondary attention output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states_primary
|
Tensor
|
Primary stream tokens (B, T, D). |
required |
conditioning_cache
|
ConditioningLayerCache
|
Precomputed secondary Q/K/V. |
required |
conditioning
|
Tensor | None
|
Conditioning vector for adaptive normalization (B, C). |
None
|
joint_attention_mask
|
Tensor | None
|
Pre-built joint mask (B, 1, S+T, S+T). |
None
|
precomputed_primary_rope
|
tuple[Tensor, Tensor] | None
|
Pre-computed (cos, sin) rotary positional encodings for primary stream positions. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple of ( |
Tensor
|
|