joint_attention
joint_attention
¶
Joint attention with full Q/K/V projections for both streams.
JointAttention
¶
JointAttention(primary_embedding_dimension, number_of_heads, secondary_embedding_dimension=None, 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
Dual-stream joint attention where both streams have Q/K/V projections.
Keys and values from both streams are concatenated, allowing each stream to attend to both itself and the other stream.
Initialize JointAttention.
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 | None
|
Hidden dimension for the secondary stream.
Defaults to |
None
|
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. |
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/joint_attention.py
forward
¶
forward(hidden_states_primary, hidden_states_secondary, attention_mask_primary=None, attention_mask_secondary=None, joint_attention_mask=None, positional_encoding_primary=None, positional_encoding_secondary=None)
Compute joint attention for both streams.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hidden_states_primary
|
Tensor
|
Primary stream tokens (B, S, D_p). |
required |
hidden_states_secondary
|
Tensor
|
Secondary stream tokens (B, T, D_s). |
required |
attention_mask_primary
|
Tensor | None
|
Padding mask (B, S), True = masked. |
None
|
attention_mask_secondary
|
Tensor | None
|
Padding mask (B, T), True = masked. |
None
|
joint_attention_mask
|
Tensor | None
|
Pre-built joint mask (B, 1, S+T, S+T). |
None
|
positional_encoding_primary
|
RotaryPositionalEncoding | None
|
Optional RoPE for primary stream. |
None
|
positional_encoding_secondary
|
RotaryPositionalEncoding | None
|
Optional RoPE for secondary stream. |
None
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
Tuple of (primary_output (B, S, D_p), secondary_output (B, T, D_s)). |
Source code in src/versatil/models/layers/transformer/attention/joint_attention.py
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 185 186 187 188 189 190 191 192 193 194 195 196 197 | |