base
base
¶
Base class for action-decoding algorithms (BC, Diffusion, FlowMatching, etc.).
DecodingAlgorithm
¶
Bases: Module, ABC
Base class for decoding algorithms.
Algorithms define the learning paradigm (behavioral cloning, diffusion, flow matching, etc.). Pure algorithms should not contain latent variable logic - use VariationalAlgorithm wrapper to add variational inference capabilities to any algorithm.
Examples:
Pure algorithm (deterministic)¶
algorithm = BehavioralCloning()
Algorithm with variational inference¶
algorithm = VariationalAlgorithm( base_algorithm=BehavioralCloning(), posterior_encoder=VAETransformerEncoder(...), prior=GaussianPrior(...) # or DiTPrior(...) for learned prior )
Initialize decoding algorithm.
Source code in src/versatil/models/decoding/algorithm/base.py
predicts_in_action_space
property
¶
Whether the network output lives in the action space.
When True (e.g. Behavioral Cloning), the network directly predicts actions and classification losses like BCE are valid. When False (e.g. Flow Matching, Diffusion with epsilon/velocity), the network predicts in a derived space (velocity field, noise) and classification losses on those outputs are meaningless.
forward
abstractmethod
¶
Forward pass during training.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
ActionDecoder
|
The action decoder network module |
required |
features
|
dict[str, Tensor]
|
Dictionary of encoded features from the encoding pipeline. |
required |
actions
|
dict[str, Tensor] | None
|
Optional dictionary of ground truth actions, if architecture requires it, e.g. ACT. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Decoder output dictionary containing predictions and any algorithm-specific outputs. |
Source code in src/versatil/models/decoding/algorithm/base.py
injected_feature_keys
¶
Feature keys this algorithm adds to the decoder features itself.
Decoders may declare these in their input specification even though they never come from observations or the encoding pipeline.
Source code in src/versatil/models/decoding/algorithm/base.py
get_auxiliary_output_keys
¶
Get keys for auxiliary outputs this algorithm adds to the decoder output.
Override in subclasses that inject additional outputs (e.g. latent variables).
Returns:
| Type | Description |
|---|---|
set[str]
|
Set of auxiliary output key strings. |
Source code in src/versatil/models/decoding/algorithm/base.py
get_targets
¶
Return the correct regression targets for the loss.
The loss module is algorithm-agnostic: it computes error between predictions and targets. This method lets each algorithm specify what those targets are.
Default returns the ground-truth actions (correct for Behavioral Cloning). Generative algorithms override to return their algorithm-specific targets (velocity field, noise, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm_output
|
dict[str, Tensor | dict[str, Tensor]]
|
Full output dict from |
required |
ground_truth_actions
|
dict[str, Tensor]
|
Ground-truth actions from the data batch. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Per-key target tensors the loss should compare predictions against. |
Source code in src/versatil/models/decoding/algorithm/base.py
predict
abstractmethod
¶
Inference/prediction pass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
ActionDecoder
|
The action decoder network module |
required |
features
|
dict[str, Tensor]
|
Dict of encoded features from encoding pipeline |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Decoder output dictionary containing predictions and any algorithm-specific outputs. |
Source code in src/versatil/models/decoding/algorithm/base.py
resolve_feature_reference
¶
Return batch size, device, and dtype for tensors created from features.
Feature dicts mix encoder outputs with integer token ids and boolean padding masks, and their ordering follows decoder configuration, so the reference must come from a floating-point feature rather than whichever value happens to be first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
dict[str, Tensor]
|
Encoded features keyed by name. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the feature dict is empty. |