precomputed_joint_attention
precomputed_joint_attention
¶
Joint attention where secondary Q/K/V are provided externally.
PrecomputedPrimaryJointAttention
¶
PrecomputedPrimaryJointAttention(primary_embedding_dimension, number_of_heads, secondary_embedding_dimension, number_of_key_value_heads=None, head_dimension=None, dropout=0.0, use_query_key_norm=True, normalization_epsilon=1e-06, bias=True)
Bases: JointAttentionBase
Joint attention where the secondary stream's Q/K/V is precomputed externally.
Only the primary stream has learned projections and output projection. The secondary attention output is returned raw for external post-processing.
Note
This computation method is used by VLA decoders (Pi0, SmolVLA) whose secondary observation stream is extracted by a VLM backbone.
Initialize PrecomputedPrimaryJointAttention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
primary_embedding_dimension
|
int
|
Hidden dimension for the primary stream. |
required |
number_of_heads
|
int
|
Number of query attention heads for both streams. |
required |
secondary_embedding_dimension
|
int
|
Hidden dimension for the secondary stream. Used to derive head_dimension if not provided. |
required |
number_of_key_value_heads
|
int | None
|
Number of key/value heads for GQA.
Defaults to |
None
|
head_dimension
|
int | None
|
Per-head dimension. Defaults to
|
None
|
dropout
|
float
|
Dropout rate for attention weights. |
0.0
|
use_query_key_norm
|
bool
|
Whether to apply QK-normalization to the primary stream. |
True
|
normalization_epsilon
|
float
|
Epsilon for normalization layers. |
1e-06
|
bias
|
bool
|
Whether to use bias in projections. |
True
|
Source code in src/versatil/models/layers/transformer/attention/precomputed_joint_attention.py
forward
¶
forward(hidden_states_primary, conditioning_cache, attention_mask_primary=None, attention_mask_secondary=None, joint_attention_mask=None, positional_encoding_primary=None, precomputed_primary_rope=None)
Compute joint attention with precomputed secondary Q/K/V.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states_primary
|
Tensor
|
Primary stream tokens (B, T, D_p). |
required |
conditioning_cache
|
ConditioningLayerCache
|
Precomputed secondary Q/K/V. queries, keys, values each shaped (B, H/KV_H, S, D_head). |
required |
attention_mask_primary
|
Tensor | None
|
Padding mask (B, T), True = masked. |
None
|
attention_mask_secondary
|
Tensor | None
|
Padding mask (B, S), True = masked. |
None
|
joint_attention_mask
|
Tensor | None
|
Pre-built joint mask (B, 1, T+S, T+S). |
None
|
positional_encoding_primary
|
RotaryPositionalEncoding | None
|
Optional RoPE layer for primary stream. |
None
|
precomputed_primary_rope
|
tuple[Tensor, Tensor] | None
|
Pre-computed (cos, sin) for the primary stream. Applied via half-rotation instead of RoPE module. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple of (projected_primary_output (B, T, D_p), |
Tensor
|
raw_secondary_output (B, S, H*D_head)). |
Source code in src/versatil/models/layers/transformer/attention/precomputed_joint_attention.py
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |