π₀.₅ 训练流程
Physical Intelligence OpenPI 官方路径:LeRobot 轨迹 → transform → flow matching 速度场 MSE → 只对可训练参数反传。
本文对应仓库 scripts/train.py 与 src/openpi/models/pi0.py。
1. 总览
OpenPI 不是从零预训练那 1 万小时,而是从 π₀.₅ 基座微调。 主路径 JAX + Flax NNX;PyTorch 对照实现同一套 flow matching,不含 FAST / LoRA / FSDP / EMA。
flowchart LR disk["LeRobot 帧
image / state / action / task"] --> load["delta_timestamps
拉 actions[H]"] load --> xf["4 段 transform"] xf --> batch["Observation + Actions"] batch --> loss["compute_loss
flow matching MSE"] loss --> opt["AdamW ± EMA"] opt --> ckpt["checkpoint"]
一次前向的语义:当前帧图像 + 语言 + 本体 作为条件,预测从噪声走到未来 H 步动作的速度场。
π₀.₅ 默认 action_horizon=10(LIBERO 10 fps 即约 1 秒),动作 pad 到 32 维。
2. 磁盘上的数据字段
主路径是 HuggingFace LeRobot。以官方微调范例 LIBERO 转换脚本为准,同一 step 写入:
| 字段 | 形状 | 含义 |
|---|---|---|
image |
(256, 256, 3) | 第三人称相机,当前拍 |
wrist_image |
(256, 256, 3) | 腕部相机,当前拍 |
state |
(8,) | 本体(关节 + 夹爪等),当前拍 |
actions |
(7,) | 本拍要执行的动作(LIBERO 已是 delta) |
task / task_index |
str / int | 语言指令;prompt_from_task=True 变成 prompt |
dataset.add_frame({
"image": step["observation"]["image"],
"wrist_image": step["observation"]["wrist_image"],
"state": step["observation"]["state"],
"actions": step["action"],
"task": step["language_instruction"].decode(),
})
加载时只给 actions 配 delta_timestamps = [0, 1, …, H-1] / fps,
因此进 transform 前 actions 已是 (H, d),图像和 state 仍是单帧。
dataset = lerobot_dataset.LeRobotDataset(
data_config.repo_id,
delta_timestamps={
key: [t / dataset_meta.fps for t in range(action_horizon)]
for key in data_config.action_sequence_keys
},
)
3. Transform:四段流水线
顺序固定,transform_dataset 里写死:
flowchart TD raw["LeRobot dict"] --> r1["① repack
只改 key 名"] r1 --> r2["② data_transforms
LiberoInputs 等"] r2 --> r3["③ Normalize
z-score 或 quantile"] r3 --> r4["④ model_transforms
resize / tokenize / pad 32"] r4 --> obs["Observation + Actions[B,H,32]"]
TransformedDataset(dataset, [
*data_config.repack_transforms.inputs,
*data_config.data_transforms.inputs,
Normalize(norm_stats, use_quantiles=...),
*data_config.model_transforms.inputs,
])
以 pi05_libero 为例,② 之后模型约定的中间字段是:
| 字段 | 内容 |
|---|---|
image/base_0_rgb |
第三人称图 |
image/left_wrist_0_rgb |
腕部图 |
image/right_wrist_0_rgb |
没有则全 0;π₀/π₀.₅ 的 mask 为 False |
state |
(8,) → 之后 pad 到 32 |
actions |
(H, 7) → pad 到 (H, 32) |
prompt |
任务字符串 |
π₀ 用 z-score;π₀.₅ / FAST 用 quantile(1%/99% 映射到 [-1, 1])。 先 Normalize 再 pad,padding 维是归一化之后的零。
π₀.₅ 的 tokenize 把离散化 state 写进语言,没有连续 state_proj:
# π₀.₅
discretized_state = np.digitize(state, bins=np.linspace(-1, 1, 257)[:-1]) - 1
full_prompt = f"Task: {cleaned_text}, State: {state_str};\nAction: "
# π₀ 则是:encode(指令) + encode("\n"),state 走连续 token
DataLoader 最后拆成 (Observation, Actions)。
Observation 含图、mask、state、token;actions 是监督,不进 Observation。
4. 图像 / state / action 的时间对齐
OpenPI 不按采集 timestamp 做 nearest。训练只认同一条样本的整数帧号。
时钟差要在写数据集或 get_observation() 里处理完。
flowchart LR
t0["帧 t"] --> img["I_t 图像"]
t0 --> st["s_t state"]
t0 --> a0["a_t"]
t0 --> a1["a_t+1"]
t0 --> aH["a_t+H-1"]
actions[0]与当前图、state 同一拍(offset = 0),不是下一拍。- 后面是未来 H−1 拍,间隔 1 个控制周期。
- 语言整条 episode 共用。
- 越出 episode 时 DROID RLDS 会 clamp 并重复最后一拍动作。
若开了 DeltaActions,整段 chunk 都相对当前帧 s_t:
time ~ Beta 是噪声插值系数,不是第几拍。动作顺序只靠索引 0…H−1。
5. State 与 Action 各自干什么
State 是条件,Action 是监督。 MSE 只打在动作速度场上,没有 state 回归项。
| 模型 | state | actions |
|---|---|---|
| π₀ | state_proj 成 1 个连续 suffix token;图像/语言看不到它 |
构造 x_t / u_t,不直接喂干净动作 |
| π₀.₅ | 离散进 prompt 文本 | 同上,flow matching |
| π₀-FAST | 离散进 prefix | 编成 FAST token,CE 只打 postfix |
flowchart TB
subgraph cond [条件 Observation]
I[三路图像 SigLIP]
L[语言 tokens]
S[state:π₀ 连续 token / π₀.₅ 进 prompt]
end
subgraph tgt [监督]
A["actions x0"]
end
noise["ε ~ N(0,I)"] --> xt["xt = t·ε + (1-t)·x0"]
A --> xt
A --> ut["ut = ε − x0"]
I --> net[PaliGemma prefix + Action expert suffix]
L --> net
S --> net
xt --> net
net --> vt["vt = action_out_proj"]
vt --> mse["MSE(vt, ut)"]
ut --> mse
6. Loss:直线路径上的速度场
π₀ / π₀.₅ 不是 DDPM 预测噪声,也没有 CFG。约定 t=1 纯噪声,t=0 数据(与 π₀ 论文时间方向相反)。
ut = ε − x0
L = mean( (vθ − ut)2 ) 先对动作维,再对 batch 与 horizon
noise = jax.random.normal(noise_rng, actions.shape) time = jax.random.beta(time_rng, 1.5, 1, batch_shape) * 0.999 + 0.001 x_t = time_expanded * noise + (1 - time_expanded) * actions u_t = noise - actions prefix_tokens, ... = self.embed_prefix(observation) suffix_tokens, ... = self.embed_suffix(observation, x_t, time) (..., suffix_out), _ = self.PaliGemma.llm([prefix_tokens, suffix_tokens], ...) v_t = self.action_out_proj(suffix_out[:, -self.action_horizon :]) return jnp.mean(jnp.square(v_t - u_t), axis=-1) # [B, H]
注意力:prefix(图+语言)双向互看;suffix 看 prefix,prefix 不看动作。π₀.₅ 用 adaRMS 把 flow 时间打进 action expert。
chunked_loss = model.compute_loss(rng, observation, actions, train=True) return jnp.mean(chunked_loss)
推理时没有 GT action:从纯噪声 Euler 积分约 10 步到 t=0,得到动作块。 客户端开环执行若干步再重规划。
7. 训练一步与梯度
flowchart LR batch["Observation, Actions"] --> step["train_step JIT"] step --> vg["nnx.value_and_grad
仅 trainable Param"] vg --> adam["AdamW update"] adam --> ema["可选 EMA 0.99"] ema --> log["loss / grad_norm"]
diff_state = nnx.DiffState(0, config.trainable_filter)
loss, grads = nnx.value_and_grad(loss_fn, argnums=diff_state)(
model, train_rng, observation, actions
)
updates, new_opt_state = state.tx.update(grads, state.opt_state, params)
new_params = optax.apply_updates(params, updates)
trainable_filter = Param ∧ ¬ freeze_filter。全量微调时梯度回到:
action_out_proj→ Gemma action expert- PaliGemma expert(suffix 对 prefix 的注意力)
- SigLIP(
embed_prefix里train=False只关 dropout,仍在计算图上) - π₀ 的
state_proj/ action-time MLP;π₀.₅ 的 time MLP
u_t = noise − actions 对参数无梯度。LoRA 配置冻住非 LoRA 权重,不进 opt_state。
默认约 30k step、batch 32、cosine + AdamW。
8. 入口命令
uv run scripts/compute_norm_stats.py --config-name pi05_libero XLA_PYTHON_CLIENT_MEM_FRACTION=0.9 \ uv run scripts/train.py pi05_libero --exp-name=my_experiment --overwrite
实验名都在 src/openpi/training/config.py 的 TrainConfig。
JAX 全量微调大约 >70GB;LoRA 大约 >22.5GB。JAX 路径不支持多机。