Imports
run firstThe lab file's imports. Run this cell first (or press Run all).
Hands-on lab 10 of 10 · Flow matching
A VLA such as pi0 outputs a CHUNK of H future actions at once, and it generates the chunk with flow matching: start from Gaussian noise and follow a learned velocity field for ~10 Euler steps. You implement the interpolation path, the velocity target, the loss, the training loop, the Euler sampler, and the two chunk utilities (cutting demonstrations into chunks, and executing chunks with re-planning). Then you train a tiny velocity MLP on demonstrations with TWO valid behaviours (pass an obstacle on the left or on the right) and check that samples land on one of the two modes, not on their average (which would drive into the obstacle).
a: an action chunk [H, d_a] from the data (H actions of d_a numbers); eps ~ N(0, I) of the same shape; t in [0, 1]: t = 0 is pure noise, t = 1 is data.
x_t = (1 - t) * eps + t * a
v = d x_t / dt = a - eps
Loss: t ~ Uniform(0, 1) per sample (pi0 uses a Beta distribution that
favours noisy t; uniform is fine here):
L = || v_theta(x_t, t) - (a - eps) ||^2
The network sees only x_t and t, so it learns the AVERAGE of a - eps over all (eps, a) pairs that pass through x_t: a velocity field whose flow carries the noise distribution onto the data distribution.
n_steps in the code), dt = 1 / K:x = eps; for i = 0 .. K-1: t_i = i / K; x = x + v_theta(x, t_i) * dt
After the last step x is a sample at t = 1.
Chunks: a demonstration of T_demo steps gives T_demo - H + 1 training chunks (sliding windows). At run time the policy predicts a chunk, the robot executes the first n_exec actions, then the policy is called again.
(openpi's code writes the same ODE backwards: x_t = t eps + (1 - t) a, t from 1 to 0. Same model, same samples, mirrored time.)
euler_integrate, Pi0.compute_loss / generate_actionEditor keys: Tab / Shift+Tab indent and dedent by 4 spaces, Enter keeps the indentation, Shift+Enter runs the cell and moves to the next one.
Cells on this page:
The lab file's imports. Run this cell first (or press Run all).
interpolate(noise, actions, t)x_t = (1 - t) * noise + t * actions.
noise, actions: [B, H, d_a]; t: [B] floats in [0, 1] (one time per sample).
velocity_target(noise, actions)v = d x_t / dt = actions - noise.
flow_matching_loss(model, actions, noise, t)Scalar MSE between model(x_t, t) and the velocity target.
time_embedding(t, dim=32, max_period=1000.0)Sinusoidal embedding of continuous t in [0, 1]: [B] → [B, dim].
class VelocityMLP(nn.Module)v_theta(x_t, t) for a whole chunk: flatten [H, d_a], concatenate a time embedding, MLP.
mode_chunks(horizon=8)The two clean behaviours, [2, H, 2]: velocity (vx, vy) per step. vx = 1 (drive forward); vy = +/- a smooth bump (swerve left or right around an obstacle).
make_demo_chunks(n, generator=None, horizon=8, std=0.05)n demonstration chunks [n, H, 2]: half swerve left, half right, plus small noise.
train_flow_matching(model, chunks, steps=2000, batch_size=256, lr=0.001, seed=0)Train with flow_matching_loss on random minibatches of chunks [N, H, d_a].
Each step: batch_size random chunks, noise ~ N(0, I) of the same shape, t ~ Uniform(0, 1) [batch_size], Adam update. Use a torch.Generator seeded with seed for the random draws. Returns the per-step losses (floats).
euler_sample(model, noise, n_steps=10)Integrate dx/dt = model(x, t) from t = 0 (noise [B, H, d_a]) to t = 1.
Step i (i = 0 .. n_steps - 1) evaluates the model at t_i = i / n_steps, passed as a float tensor [B], and moves x by velocity * (1 / n_steps).
chunk_trajectory(traj, horizon, stride=1)Sliding windows of a demonstration: traj [T_demo, d_a] → [num_chunks, horizon, d_a].
Window c starts at step c * stride; num_chunks = (T_demo - horizon) // stride + 1.
rollout_chunks(policy, n_actions, n_exec)Receding-horizon execution.
policy(step) returns a chunk [H, d_a] planned at time step (the number of actions executed so far). Execute its first n_exec actions (fewer at the very end), then call the policy again, until n_actions actions are executed. Returns (actions [n_actions, d_a], call_steps: list of the step values passed to the policy).
Experiment here: print shapes, call your functions on small inputs. This cell runs in the same Python process as the cells above, is not part of Run all and is not saved to the lab file (your browser keeps it).