flow_matching
flow_matching
¶
Flow Matching algorithm for action generation via learned velocity fields.
VelocityWrapper
¶
Reshapes between the flat ODE state and per-key action dicts for the decoder.
The ODE solver operates on a single flat tensor (B, H * sum(D_k)).
The decoder network expects and returns {key: (B, H, D_k)} dicts.
This callable handles the conversion in both directions.
When reverse_convention is enabled, the timestep passed to the network
is flipped (1 - t) and the returned velocity is negated, adapting
VersatIL's forward-time ODE integration to checkpoints trained with the
opposite convention (t=0 = clean, t=1 = noise).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
ActionDecoder
|
Action decoder that maps features + noisy actions to velocities. |
required |
features
|
dict[str, Tensor]
|
Encoded observation features from the encoding pipeline. |
required |
action_keys
|
list[str]
|
Sorted action key names matching the flat concatenation order. |
required |
flat_dimensions
|
dict[str, int]
|
Per-key flat sizes |
required |
tensor_shapes
|
dict[str, tuple[int, ...]]
|
Per-key full shapes |
required |
reverse_convention
|
bool
|
Negate velocity and flip timestep for reversed conventions. |
required |
Source code in src/versatil/models/decoding/algorithm/flow_matching.py
__call__
¶
Compute velocity from the current ODE state and time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Tensor
|
Flat action state |
required |
time
|
Tensor
|
Scalar or |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Flat velocity |
Source code in src/versatil/models/decoding/algorithm/flow_matching.py
FlowMatching
¶
FlowMatching(sigma=0.0, num_inference_steps=10, ode_solver=value, timestep_sampler=BETA.value, logit_mean=0.0, logit_std=1.0, beta_alpha=1.5, beta_beta=1.0, max_timestep=0.999, reverse_flow_convention=False)
Bases: DecodingAlgorithm
Flow Matching algorithm for action prediction.
Trains a model to predict velocity fields that transport samples from a noise distribution to the target action distribution. Uses Conditional Flow Matching with straight conditional paths.
During training, samples a time t in [0,1] and trains the model to predict the velocity field u_t = dx/dt that moves from noise (t=0) to actions (t=1).
During inference, integrates the learned velocity field using an ODE solver (Euler, Heun, or RK4) to generate actions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sigma
|
float
|
Noise level for conditional flow matching (0 = deterministic OT). |
0.0
|
num_inference_steps
|
int
|
Number of integration steps during inference. |
10
|
ode_solver
|
str
|
ODE solver to use ("euler", "heun", or "rk4"). |
value
|
timestep_sampler
|
str
|
Timestep sampling strategy. |
BETA.value
|
logit_mean
|
float
|
Mean for logit-normal (shifts mode; 0 centers at t=0.5). |
0.0
|
logit_std
|
float
|
Std for logit-normal (smaller = more concentrated). |
1.0
|
beta_alpha
|
float
|
First shape parameter for Beta distribution (pi0 uses 1.5). |
1.5
|
beta_beta
|
float
|
Second shape parameter for Beta distribution (pi0 uses 1.0). |
1.0
|
max_timestep
|
float
|
Upper bound s for Beta sampling (pi0 uses 0.999). |
0.999
|
reverse_flow_convention
|
bool
|
Reverse the time/velocity convention during inference. When True, the inference loop remaps t to (1-t) and negates the predicted velocity. |
False
|
Initialize Flow Matching algorithm.
Source code in src/versatil/models/decoding/algorithm/flow_matching.py
predicts_in_action_space
property
¶
Network predicts velocity fields, not actions.
injected_feature_keys
¶
forward
¶
Forward pass during training.
Samples a time t and trains the network to predict the velocity field that transports noise to the target actions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
network
|
ActionDecoder
|
The action decoder network module (should support time conditioning) |
required |
features
|
dict[str, Tensor]
|
Dictionary of encoded features from the encoding pipeline. |
required |
actions
|
dict[str, Tensor] | None
|
Dictionary of ground truth actions. Required for flow matching training. Expected keys depend on action space (e.g., 'position_action', 'gripper_action') |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Decoder output dictionary containing: - Velocity predictions for each action component - 'target_velocity': The true velocity field - 'time': The sampled time values - All action keys with '_pred' suffix for predictions |
Raises:
| Type | Description |
|---|---|
ValueError
|
If actions are not provided (required for flow matching training) |
Source code in src/versatil/models/decoding/algorithm/flow_matching.py
get_targets
¶
Return the target velocity field for flow matching loss.
Source code in src/versatil/models/decoding/algorithm/flow_matching.py
predict
¶
Inference pass using ODE integration.
Generates actions by integrating the learned velocity field from noise (t=0) to actions (t=1) using an ODE solver.
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 action predictions. |