policy
policy
¶
Policy module that handles the sequence of input encoding, output decoding, and loss computation.
Policy
¶
Policy(encoding_pipeline, algorithm, decoder, observation_space, action_space, prediction_horizon, observation_horizon, loss, device, metadata_passthrough=None)
Bases: Module
General policy class that orchestrates observation encoding, action decoding, and loss computation.
Initialize policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
encoding_pipeline
|
EncodingPipeline
|
Observation encoding pipeline. |
required |
algorithm
|
DecodingAlgorithm
|
Decoding algorithm (diffusion, flow matching, etc.). |
required |
decoder
|
ActionDecoder
|
Action decoder architecture. |
required |
observation_space
|
ObservationSpace
|
Observation space configuration. |
required |
action_space
|
ActionSpace
|
Action space configuration. |
required |
prediction_horizon
|
int
|
Number of future actions to predict. |
required |
observation_horizon
|
int
|
Number of past observations to condition on. |
required |
loss
|
BaseLoss
|
Loss module for training. |
required |
device
|
str
|
Device to run on. |
required |
metadata_passthrough
|
dict[str, dict[str, str]] | None
|
Mapping from source dictionaries to metadata keys for logging/visualization. |
None
|
Source code in src/versatil/models/policy.py
set_normalizer
¶
Set normalizer for observations and actions.
Source code in src/versatil/models/policy.py
set_tokenizer
¶
Set tokenizer and pass it to the decoder.
set_denoising_thresholds
¶
Set the denoising thresholds from training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
thresholds
|
dict[str, float]
|
Dictionary mapping observation keys to their denoising thresholds. May be empty for precomputed actions. |
required |
Note
These thresholds are computed from the dataset's action processor and stored via DictOfTensorMixin to persist through checkpointing.
Source code in src/versatil/models/policy.py
get_denoising_thresholds
¶
Return the stored denoising thresholds as plain floats.
set_gripper_class_weights
¶
Set positive class weight for GripperLoss components in the loss module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos_weight
|
Tensor | None
|
Tensor with positive class weight for BCE loss, or None to disable. |
required |
Source code in src/versatil/models/policy.py
forward
¶
Forward pass through observation encoding → action decoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
dict[str, dict[str, Tensor]]
|
A batch dictionary containing normalized observations and actions dictionaries. Each is a dict of tensors. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Decoder output dictionary containing action predictions and any architecture-specific outputs. |
Source code in src/versatil/models/policy.py
compute_loss
¶
Compute loss using the configured loss module.
The algorithm determines what the regression targets are via
get_targets. For BC this is the ground-truth actions; for
flow matching it is the target velocity field; for diffusion it
depends on the prediction type (noise, sample, or velocity).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
dict[str, dict[str, Tensor]]
|
Batch dictionary containing observations and actions |
required |
Returns:
| Type | Description |
|---|---|
LossOutput
|
LossOutput with total loss and component losses |
Source code in src/versatil/models/policy.py
predict_action
¶
Predict actions from observations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obs_dict
|
dict[str, Tensor]
|
Dictionary of observation tensors |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Predicted actions (on same device as policy) |
Source code in src/versatil/models/policy.py
build_algorithm_features
¶
Encode observations and select the features the decoder declared.
Merges raw observations with encoding-pipeline outputs, then filters to
the decoder's decoder_input.keys allowlist plus their padding masks.
Both Policy and ExportablePolicy must build features through this
function so that exported models see exactly the training-time inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
observation
|
dict[str, Tensor]
|
Raw observation dictionary. |
required |
encoding_pipeline
|
EncodingPipeline
|
Pipeline producing encoded features. |
required |
decoder
|
ActionDecoder
|
Decoder whose input specification selects the features. |
required |
algorithm_injected_keys
|
set[str] | None
|
Decoder-declared keys the algorithm adds later (latents, timesteps); they are not required here. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the decoder requests keys that are neither raw observations, encoding-pipeline outputs, nor algorithm-injected. |