处理器是机器人、dataset 和 policy 之间的数据转换层。流水线是一系列 ProcessorStep 的链;每个步骤声明它如何转换数据和特征契约。
概念请参阅机器人处理器简介, 编写步骤请参阅实现你自己的处理器, 当流水线行为异常时请参阅调试处理器流水线。
数据处理流水线中单个步骤的抽象基类。
每个步骤必须实现 __call__ 方法,以对数据转换执行其变换,
并实现 transform_features 方法,以描述它如何
改变数据特征的形状或类型。
子类可以选择通过实现 state_dict 和 load_state_dict 来支持有 state。
返回该步骤的配置以进行序列化。
从 state 字典加载该步骤的 state。
重置处理器步骤的内部 state(如果有)。
保存非张量资产,并将构造函数参数映射到相对路径。
返回该步骤的 state(例如,学习到的参数、运行均值)。
( features: dict[PipelineFeatureType, dict[str, PolicyFeature]] )
定义此步骤如何修改流水线特征的描述。
此方法用于在数据流经流水线时跟踪数据形状、数据类型或模态的变化, 而无需处理实际数据。
( steps: Sequence[ProcessorStep] = <factory>name: str = 'DataProcessorPipeline'to_transition: Callable[[TInput], EnvTransition] = <factory>to_output: Callable[[EnvTransition], TOutput] = <factory>before_step_hooks: list[Callable[[int, EnvTransition], None]] = <factory>after_step_hooks: list[Callable[[int, EnvTransition], None]] = <factory> )
用于处理数据的顺序流水线,与 Hugging Face Hub 集成。
此类将多个 ProcessorStep 实例串联起来,形成一个完整的
数据处理工作流。它是通用的,允许自定义输入和输出类型,
这些类型由 to_transition 和 to_output 转换器处理。
( config: dict[str, Any]state_dict: dict[str, dict[str, torch.Tensor]] | None = Noneoverrides: dict[str, Any] | None = Noneto_transition: Callable[[TInput], EnvTransition] | None = Noneto_output: Callable[[EnvTransition], TOutput] | None = None )
从内存中的配置和可选 state 张量构建流水线。
( pretrained_model_name_or_path: str | Pathconfig_filename: strforce_download: bool = Falseresume_download: bool | None = Noneproxies: dict[str, str] | None = Nonetoken: str | bool | None = Nonecache_dir: str | Path | None = Nonelocal_files_only: bool = Falserevision: str | None = Noneoverrides: dict[str, Any] | None = Noneto_transition: Callable[[TInput], EnvTransition] | None = Noneto_output: Callable[[EnvTransition], TOutput] | None = None**kwargs )
参数
EnvTransition 的自定义函数。EnvTransition 转换为输出格式的自定义函数。抛出异常
FileNotFoundError or ValueError or ImportError or KeyError or ProcessorMigrationError
FileNotFoundError — 如果找不到配置文件。ValueError — 如果配置有歧义或实例化失败。ImportError — 如果无法导入某个步骤的类。KeyError — 如果某个覆盖键与流水线中的任何步骤都不匹配。ProcessorMigrationError — 如果模型需要迁移为处理器格式。从本地目录、单个文件或 Hugging Face Hub 仓库加载流水线。
此方法实现了一个简化的加载流水线,并带有智能迁移检测:
简化的加载 policy:
配置加载(_load_config):
配置验证(_validate_loaded_config):
步骤构建(_build_steps_with_overrides):
覆盖验证(_validate_overrides_used):
迁移检测:
加载示例:
# Directory loading
pipeline = DataProcessorPipeline.from_pretrained("/models/my_model", config_filename="processor.json")
# Single file loading
pipeline = DataProcessorPipeline.from_pretrained(
"/models/my_model/processor.json", config_filename="processor.json"
)
# Hub loading
pipeline = DataProcessorPipeline.from_pretrained("user/repo", config_filename="processor.json")
# Multiple configs (preprocessor/postprocessor)
preprocessor = DataProcessorPipeline.from_pretrained(
"model", config_filename="policy_preprocessor.json"
)
postprocessor = DataProcessorPipeline.from_pretrained(
"model", config_filename="policy_postprocessor.json"
)覆盖系统:
返回可 JSON 序列化的流水线配置。
( state_dict: dict[str, dict[str, torch.Tensor]] )
将流水线 state 张量加载到现有步骤中。
仅通过流水线处理转换中的 action 部分。
( complementary_data: dict[str, Any] )
仅通过流水线处理转换中的补充数据部分。
仅通过流水线处理转换中的 done 标志。
仅通过流水线处理转换中的 info 字典。
仅通过流水线处理转换中的 observation 部分。
仅通过流水线处理转换中的奖励部分。
仅通过流水线处理转换中的 truncated 标志。
( fn: Callable[[int, EnvTransition], None] )
注册一个在每个步骤之后调用的函数。
( fn: Callable[[int, EnvTransition], None] )
注册一个在每个步骤之前调用的函数。
重置流水线中所有有 state 步骤的 state。
( save_directory: str | Path | None = Nonerepo_id: str | None = Nonepush_to_hub: bool = Falsecard_kwargs: dict[str, Any] | None = Noneconfig_filename: str | None = None**push_to_hub_kwargs )
参数
push_to_hub=true 时使用。name 属性生成名称。将流水线的配置和 state 保存到目录。
此方法创建一个 JSON 配置文件,用于定义流水线的结构
(名称和步骤)。对于每个有 state 步骤,它还会保存一个 .safetensors 文件,
其中包含其 state 字典。
返回按 state 键分组的流水线 state 张量。
逐步处理数据,在每个阶段产出转换。
这是一个生成器方法,可用于调试和检查数据通过流水线时的中间 state。
( initial_features: dict[PipelineFeatureType, dict[str, PolicyFeature]] )
按顺序应用所有步骤的特征变换。
此方法将特征描述字典传播通过每个步骤的 transform_features 方法,使流水线能够在不处理任何真实数据的情况下静态确定
输出特征规范。
( fn: Callable[[int, EnvTransition], None] )
注销一个 ‘after_step’ 钩子。
( fn: Callable[[int, EnvTransition], None] )
注销一个 ‘before_step’ 钩子。