physicalai-train-adding-a-policy
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAdding a Studio Policy
添加Studio策略
Policies live in . Each family is a Lightning-facing wrapping a , split across three files. Base classes are in ( in , in ); shared / types come from Runtime () — see Runtime docs and .
library/src/physicalai/policies/<name>/Policytorch.nn.ModuleModelpolicies/base/Policypolicy.pyModelmodel.pyConfigFromConfigphysicalai.configuse-from-configconfiguration策略存放在目录下。每个策略家族都是面向Lightning的,封装了类型的,拆分到三个文件中。基类位于(中的、中的);通用的/类型来自Runtime()——可查看Runtime文档和。
library/src/physicalai/policies/<name>/Policytorch.nn.ModuleModelpolicies/base/policy.pyPolicymodel.pyModelConfigFromConfigphysicalai.configuse-from-configconfigurationWorkflow
工作流程
-
Read a nearby family first. Study(current reference implementation):
policies/pi05/(config.py),Pi05Config(Config)(model.py),Pi05Model(Model)(policy.py),Pi05(ExportablePolicyMixin, Policy), and any extra modules the architecture needs (e.g.preprocessor.py). For a deliberately minimal family,pi_gemma.pyis a smaller three-file layout without the VLM stack.policies/act/- Done when: you can name which existing file each new file mirrors.
-
Create the three-file split in:
policies/<name>/- —
config.py, all hyperparameters as typed fields.<Name>Config(Config) - —
model.py, pure<Name>Model(Model)logic.torch.nn.Module - —
policy.py(add<Name>(Policy)only when export is implemented).ExportablePolicyMixin - Done when: imports cleanly.
from physicalai.policies.<name> import <Name>, <Name>Config, <Name>Model
-
Implement the policy interface used by both training and inference through the base:
Policy- — training path; return values compatible with
forward(...).training_step - — inference path; return a tensor with the configured action horizon.
predict_action_chunk(...) - — use base-class action-queue behavior unless a specialized flow is justified.
select_action(...) - Done when: shapes match the checks below for a synthetic batch.
-
Register the family so both API and CLI users can find it:
- Add exports to (
policies/__init__.pyand imports, e.g.__all__,<Name>,<Name>Config).<Name>Model - Add the lowercase name to the /
get_physicalai_policy_class(...)dispatch inget_policy(...).policies/__init__.py - Done when: works,
from physicalai.policies import <Name>, get_policyreturns an instance, andget_policy("<name>")resolves.--model physicalai.policies.<Name>
- Add exports to
-
Prove direct API construction before adding CLI config:python
from physicalai.policies import get_policy policy = get_policy("<name>")- Done when: direct construction, config round-trip, and synthetic /
forward(...)shape checks pass.predict_action_chunk(...)
- Done when: direct construction, config round-trip, and synthetic
-
Add a training config inwhen the policy is user-facing from the CLI. Wire
library/configs/physicalai/<name>.yaml, amodel.class_path(usuallydata.class_path), andphysicalai.data.lerobot.LeRobotDataModule. Mirrortrainer.*.configs/physicalai/pi05.yaml- Done when: completes one step.
physicalai fit --config configs/physicalai/<name>.yaml --trainer.fast_dev_run=true
- Done when:
-
Wire export only when ready. Addand a valid sample input, then follow the
ExportablePolicyMixinskill. If export is intentionally unsupported, say so explicitly in the policy docstring.physicalai-train-exporting-and-validating -
Add tests undernext to existing policy tests: at least one construction/config path and one shape-validation test.
library/tests/unit/policies/- Done when: passes.
uv run pytest tests/unit/policies -k <name>
- Done when:
-
Update docs if the policy is user-visible:and any config/API examples.
library/docs/explanation/policy/
-
先参考现有策略家族。研究(当前参考实现):包含
policies/pi05/(config.py)、Pi05Config(Config)(model.py)、Pi05Model(Model)(policy.py)、Pi05(ExportablePolicyMixin, Policy),以及该架构所需的任何额外模块(如preprocessor.py)。如果需要极简的策略家族,pi_gemma.py是不含VLM栈的小型三文件结构。policies/act/- 完成标志:能够明确每个新文件对应的现有文件。
-
在目录下创建三文件结构:
policies/<name>/- ——
config.py,所有超参数均为类型化字段。<Name>Config(Config) - ——
model.py,纯<Name>Model(Model)逻辑。torch.nn.Module - ——
policy.py(仅当实现导出功能时添加<Name>(Policy))。ExportablePolicyMixin - 完成标志:可正常导入。
from physicalai.policies.<name> import <Name>, <Name>Config, <Name>Model
-
实现基类定义的、同时适用于训练和推理的策略接口:
Policy- —— 训练路径;返回值需与
forward(...)兼容。training_step - —— 推理路径;返回符合配置动作时间跨度的张量。
predict_action_chunk(...) - —— 除非需要特殊流程,否则使用基类的动作队列逻辑。
select_action(...) - 完成标志:形状与下文合成批次的检查要求匹配。
-
注册策略家族,以便API和CLI用户均可访问:
- 在中添加导出项(
policies/__init__.py和导入语句,例如__all__、<Name>、<Name>Config)。<Name>Model - 在的
policies/__init__.py/get_physicalai_policy_class(...)分发逻辑中添加小写的策略名称。get_policy(...) - 完成标志:可正常工作,
from physicalai.policies import <Name>, get_policy返回实例,且get_policy("<name>")可正确解析。--model physicalai.policies.<Name>
- 在
-
在添加CLI配置前,先验证直接API构建:python
from physicalai.policies import get_policy policy = get_policy("<name>")- 完成标志:直接构建、配置往返、合成/
forward(...)的形状检查均通过。predict_action_chunk(...)
- 完成标志:直接构建、配置往返、合成
-
当策略支持CLI用户访问时,在中添加训练配置。配置
library/configs/physicalai/<name>.yaml、model.class_path(通常为data.class_path)和physicalai.data.lerobot.LeRobotDataModule。参考trainer.*的结构。configs/physicalai/pi05.yaml- 完成标志:可完成一个步骤。
physicalai fit --config configs/physicalai/<name>.yaml --trainer.fast_dev_run=true
- 完成标志:
-
仅当准备就绪时配置导出功能。添加和有效的示例输入,然后遵循
ExportablePolicyMixin技能流程。如果有意不支持导出,请在策略的文档字符串中明确说明。physicalai-train-exporting-and-validating -
在目录下添加测试用例,与现有策略测试放在一起:至少包含一个构建/配置路径测试和一个形状验证测试。
library/tests/unit/policies/- 完成标志:执行通过。
uv run pytest tests/unit/policies -k <name>
- 完成标志:
-
如果策略对用户可见,更新文档:更新中的内容以及任何配置/API示例。
library/docs/explanation/policy/
Required checks
必要检查
Account for every item below (not just "looks fine"):
- Action shape semantics — batch, horizon/chunk length, and action dimension are correct and unchanged from the family's convention.
- Observation features — feature names align with dataset/config conventions (:
data/observation.py,Feature).FeatureType - API construction path — imports, , direct constructor use, and synthetic shape checks pass without CLI involvement.
get_policy(...) - Config path — construction works through the jsonargparse CLI path used by (
physicalai fit/class_path) when the policy is CLI-visible.init_args - Heavy dependencies — gate large families behind an optional extra in and import lazily, matching
library/pyproject.toml/pi05/pi0/groot.smolvla - No silent contract changes — do not alter action dims, feature names, or preprocessing without coordinating export/Runtime.
需逐一确认以下所有项(不能仅靠“看起来正常”判断):
- 动作形状语义——batch、horizon/chunk length和动作维度需符合策略家族的约定,且保持一致。
- 观测特征——特征名称需与数据集/配置约定对齐(中的
data/observation.py、Feature)。FeatureType - API构建路径——导入、、直接构造函数调用以及合成形状检查无需CLI参与即可通过。
get_policy(...) - 配置路径——当策略支持CLI访问时,通过使用的jsonargparse CLI路径(
physicalai fit/class_path)可正常构建策略。init_args - 重依赖处理——在中将大型策略家族设为可选依赖,并延迟导入,与
library/pyproject.toml/pi05/pi0/groot的处理方式一致。smolvla - 无静默契约变更——未经与导出/Runtime团队协调,不得修改动作维度、特征名称或预处理逻辑。
Verify
验证步骤
From :
library/bash
uv run pytest tests/unit/policies -k <name>
physicalai fit --config configs/physicalai/<name>.yaml --trainer.fast_dev_run=true
prek run --all-files library/在目录下执行:
library/bash
uv run pytest tests/unit/policies -k <name>
physicalai fit --config configs/physicalai/<name>.yaml --trainer.fast_dev_run=true
prek run --all-files library/References
参考资料
- — the
references/base-classes.md/Policycontract and file-split expectations.Model
- ——
references/base-classes.md/Policy契约及文件拆分要求。",Model