transformer_input_builder
transformer_input_builder
¶
torch.nn.Module to construct transformer input token sequences from a group of diverse input features.
Across the module, the following abbreviations are used: - B: Batch size - T: Temporal length (if applicable) - C: Channels/ Original Feature dimension - H: Height (for spatial features) - W: Width (for spatial features) - Emb: Embedding dimension - PE: Positional Encoding
TransformerInputBuilder
¶
TransformerInputBuilder(embedding_dimension, spatial_positional_encoding_layer=None, flat_positional_encoding_layer=None, temporal_positional_encoding_layer=None, use_camera_embeddings=True, exclude_keys=None)
Bases: Module
Transforms input features into a sequence of token embeddings to feed into a transformer.
Note:
This module:
i). projects multiple features into a common embedding dimension, following the
canonical rank contract:
- 5D spatial feature maps (B, T, C, H, W) are projected into the common channel
embedding dimension Emb using 1x1 convolutions, with output (B, T, Emb, H, W).
- 4D token sequences (B, T, S, D), 3D temporal vectors (B, T, D), and 2D
algorithm context (B, D) are projected using a linear layer.
ii). Each feature is flattened into a token sequence: spatial maps become
(B, T*H*W, Emb), token sequences (B, T*S, Emb), temporal vectors (B, T, Emb),
and algorithm context a single token (B, 1, Emb).
iii). The feature tokens are concatenated together along the sequence dimension to produce a unified
token sequence (B, Total_Seq, Emb).
Optionally, accepts a spatial positional encoding layer (1D or 2D, sinusoidal or learned), a temporal one (1D)
and a flat feature positional encoding (1D)cto compute and return the final matching positional encodings
with shape (B, Total_Seq, Emb).
For spatial features
- if `spatial_positional_encoding_layer` (2D) is provided and no temporal layer → identical 2D PE
is repeated for every frame
- If both `spatial` (2D) and `temporal` (1D) PE layers are provided → repeated 2D spatial PE
+ 1D temporal PE broadcast over H×W tokens and added
- If spatial_positional_encoding_layer is not provided → all features (visual + flat) receive
one global 1D positional encoding from `flat_positional_encoding_layer`.
Features may have different spatial sizes (H, W) or temporal lengths (T),
as long as batch dims are consistent within each feature.
Positional encoding contract:
Callers must always pre-add the returned ``pos_encodings`` to ``input_tokens``
before passing them to the transformer (``hidden_states = tokens + pos_encodings``).
This ensures cross-attention keys carry absolute position information regardless
of the transformer's internal PE setting. When ``positional_encoding_type`` on
the transformer is not None (e.g. RoPE), the internal PE applies to self-attention
Q/K only and complements — not replaces — the pre-added additive PE.
Example
pos_enc = SinusoidalPositionalEncoding2D(embedding_dimension=256) input_builder = TransformerInputBuilder(embedding_dimension=256, spatial_positional_encoding_layer=pos_enc) features = { ... "rgb": torch.randn(8, 1, 3, 16, 16), # (B, T, C, H, W) ... "depth": torch.randn(8, 5, 1, 32, 32), # (B, T, C, H, W) ... } tokens, pos, padding_mask = input_builder(features) tokens.shape # (8, (1616 + 53232), 256) pos.shape # (8, (1616 + 53232), 256)
Initialize TransformerInputBuilder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedding_dimension
|
int
|
Common embedding dimension for all features. |
required |
spatial_positional_encoding_layer
|
PositionalEncoding2D | None
|
Optional 2D positional encoding layer for spatial features. |
None
|
flat_positional_encoding_layer
|
PositionalEncoding1D | None
|
Optional 1D positional encoding layer for flat/sequential features. |
None
|
temporal_positional_encoding_layer
|
PositionalEncoding1D | None
|
Optional 1D positional encoding layer for temporal dimension. |
None
|
use_camera_embeddings
|
bool
|
Whether to use camera embeddings for multi-camera 2D PE, so that each camera view can be distinguished in the transformer input. |
True
|
exclude_keys
|
list[str] | None
|
Optional list of feature keys to exclude from the input sequence. |
None
|
Raises: ValueError: If provided positional encoding layers do not match expected types or dimensions.
Source code in src/versatil/models/decoding/transformer_input_builder.py
forward
¶
Project and concatenate features into token embeddings with optional positional encodings.
Note
Processes all features in the dict (except padding masks and exclude_keys).
Callers must filter the features dict to only include the keys the decoder
should attend to. Passing the full encoding pipeline output without filtering
will project and attend to every encoder's features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
dict[str, Tensor]
|
Dict of features with several possible shapes: - spatial features (B, C, H, W) - temporal-spatial features(B, T, C, H, W) - flat features(B, D) - sequential features (B, T, D) - temporal-sequential features (B, T, Seq, D) - padding mask of shape (B, Seq) or (B, T) or (B, T, Seq) with boolean values |
required |
Note: If the CLS token is included, it is always appended at the end of the sequence.
Returns:
| Type | Description |
|---|---|
Tensor
|
Tuple of: |
Tensor | None
|
|
Tensor | None
|
|
tuple[Tensor, Tensor | None, Tensor | None]
|
|
Source code in src/versatil/models/decoding/transformer_input_builder.py
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 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |