base
base
¶
Base class for vision-language models used by VLA decoders.
GenerativeVLM
¶
Bases: LanguageEncoderMixin, GenerativeLanguageModel, ABC
Base for VLM components that fuse vision and language in one language-model pass.
Initialize common VLM input wiring and runtime options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_keys
|
str | list[str]
|
RGB camera keys consumed by the VLM. Tokenized text and padding-mask keys are added by the base class. |
required |
pretrained
|
bool
|
Whether the concrete subclass loads pretrained weights. |
required |
frozen
|
bool
|
Whether all model weights are frozen. |
required |
model_dtype
|
str | None
|
Optional precision string for model parameter dtype. |
None
|
max_text_length
|
int | None
|
Optional text sequence length used when running the
language model. |
None
|
Source code in src/versatil/models/decoding/generative_language_models/vision_language/base.py
rotary_embedding
property
¶
Rotary embedding module used by the language-model transformer layers.
embed_input_ids
¶
Embed language-vocabulary token IDs with the VLM language tower.
forward_language_model
¶
forward_language_model(input_ids=None, inputs_embeds=None, attention_mask=None, past_key_values=None, use_cache=False, cache_position=None, position_ids=None, output_hidden_states=True)
Run the VLM language tower over caller-provided token inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_ids
|
Tensor | None
|
Optional token IDs with shape |
None
|
inputs_embeds
|
Tensor | None
|
Optional token embeddings with shape |
None
|
attention_mask
|
Tensor | None
|
Optional language-model attention mask. |
None
|
past_key_values
|
Cache | tuple[tuple[Tensor, ...], ...] | None
|
Optional cached key/value tensors. |
None
|
use_cache
|
bool
|
Whether to return/update cached key/value tensors. |
False
|
cache_position
|
Tensor | None
|
Optional HuggingFace KV-cache slots for the tokens
in this call. During cached decoding, if the prefix has length
|
None
|
position_ids
|
Tensor | None
|
Optional positions for the language model positional
encoding, with shape |
None
|
output_hidden_states
|
bool
|
Whether to return hidden states. |
True
|
Returns:
| Type | Description |
|---|---|
CausalLanguageModelOutput
|
Causal language-model output with logits shape |
Source code in src/versatil/models/decoding/generative_language_models/vision_language/base.py
encode
¶
Embed images and text, concatenate, and run the language model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
dict[str, Tensor]
|
Camera images, tokenized text, and optional padding mask. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Dict with fused sequential features and padding mask. |
Source code in src/versatil/models/decoding/generative_language_models/vision_language/base.py
forward
¶
Run the VLM on raw inputs with or without an observation horizon.
Source code in src/versatil/models/decoding/generative_language_models/vision_language/base.py
build_prefix
¶
Build VLM prefix input embeddings for VLA decoder execution.
The prefix is the pre-transformer multimodal input sequence. The consuming decoder runs the language-model layers over it exactly once itself (interleaved expert layers or causal-LM prefill), so the prefix must not already be language-model output states.
Source code in src/versatil/models/decoding/generative_language_models/vision_language/base.py
validate_input_metadata
¶
Validate metadata for VLM camera and tokenized-text inputs.
Source code in src/versatil/models/decoding/generative_language_models/vision_language/base.py
get_vocab_size
¶
get_backbone_layers
¶
Return the language-model transformer layers for interleaved decoding.
get_rotary_embedding
¶
Return the language-model rotary positional encoding module.
get_backbone_hidden_dim
¶
get_text_config
¶
extract_key_value
staticmethod
¶
Project hidden states to key and value through a VLM layer's projections.
Does not apply RoPE — use extract_query_key_value when positional
information is needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vlm_layer
|
Module
|
Pretrained VLM transformer layer. |
required |
hidden_states
|
Tensor
|
(B, S, D). |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
(keys, values) each (B, S, key_value_dimension). |
Source code in src/versatil/models/decoding/generative_language_models/vision_language/base.py
compute_rope
staticmethod
¶
Compute (cos, sin) RoPE components for given positions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rotary_embedding
|
Module
|
The language-model rotary embedding module. |
required |
hidden_states
|
Tensor
|
Tensor whose dtype/device to match. |
required |
position_ids
|
Tensor
|
Position indices (B, S). |
required |
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
(cos, sin) each (B, 1, S, head_dim) for head broadcast. |
Source code in src/versatil/models/decoding/generative_language_models/vision_language/base.py
build_additive_attention_mask
staticmethod
¶
Convert a bool mask to the additive mask expected by HF decoder layers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attention_mask
|
Tensor | None
|
Boolean attention mask where |
required |
dtype
|
dtype
|
Floating dtype for the returned additive mask. |
required |
Returns:
| Type | Description |
|---|---|
Tensor | None
|
Additive attention mask with |
Tensor | None
|
|
Tensor | None
|
only when no mask is provided. An all-visible mask is returned as |
Tensor | None
|
explicit zeros: HF decoder layers fall back to causal attention |
Tensor | None
|
when given |
Tensor | None
|
bidirectional structure. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/versatil/models/decoding/generative_language_models/vision_language/base.py
extract_key_value_with_rope
staticmethod
¶
Project hidden states to K/V with RoPE applied to keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vlm_layer
|
Module
|
Pretrained VLM transformer layer. |
required |
hidden_states
|
Tensor
|
(B, S, D). |
required |
rotary_embedding
|
Module
|
The language-model rotary embedding module. |
required |
position_ids
|
Tensor
|
(B, S) position indices. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
(keys, values) — keys have RoPE applied and are flattened to |
Tensor
|
(B, S, key_value_dimension), values are flat (B, S, key_value_dimension). |
Source code in src/versatil/models/decoding/generative_language_models/vision_language/base.py
extract_query_key_value
staticmethod
¶
Project hidden states to Q/K/V and apply rotary positional encoding.
Works for any HF decoder layer that exposes input_layernorm,
self_attn.{q,k,v}_proj, head_dim, and
config.num_{attention,key_value}_heads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vlm_layer
|
Module
|
Pretrained VLM transformer layer. |
required |
hidden_states
|
Tensor
|
(B, S, D). |
required |
rotary_embedding
|
Module
|
The language-model rotary embedding module. |
required |
position_ids
|
Tensor
|
(B, S) position indices. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tensor
|
(query, key, value) with RoPE on Q and K. |
|
Shapes |
Tensor
|
Q (B, H, S, D_head), K (B, KV_H, S, D_head), V same as K. |
Source code in src/versatil/models/decoding/generative_language_models/vision_language/base.py
apply_residual_feedforward
staticmethod
¶
Complete a transformer layer after externally computed attention.
Called by interleaved decoders that run joint attention outside the VLM layer and need it to finish: O-projection → normalization → residual add → feedforward → residual add.
Handles both Llama-style layers (single post-attention norm) and Gemma2-style layers (sandwich norms around sublayer outputs).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vlm_layer
|
Module
|
Pretrained VLM transformer layer. |
required |
vlm_residual
|
Tensor
|
Hidden states before attention (B, S, D). |
required |
vlm_attention_output
|
Tensor
|
Raw attention output before O-projection (B, S, H*D_head). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Updated hidden states (B, S, D). |