# LeRobot make_policy构造失败时cfg.pretrained_path未恢复

> 原文链接：https://robotics.hellobike.com/pitfalls/2026-11-12-lerobot-5176
> 频道：pitfalls | 更新日期：2026-11-12 | 标签：auto, github-issue, lerobot
> 核心摘要：make_policy在加载resume checkpoint异常时未还原cfg.pretrained_path，需将恢复逻辑移入finally块以确保配置状态一致。

# LeRobot make_policy构造失败时cfg.pretrained_path未恢复

::: v-pre
> 本文由 AI 根据公开内容整理，可能有误，请以原文为准：[make_policy leaves the resume checkpoint in cfg.pretrained_path when construction fails](https://github.com/huggingface/lerobot/issues/5176)（GitHub huggingface/lerobot #5176）

**状态**：有绕过办法

## 问题

调用`make_policy`传入`pretrained_path`参数时，函数内部会临时修改`cfg.pretrained_path`为resume路径。若特征准备、`from_pretrained`或PEFT分支抛出异常，原路径恢复逻辑被跳过，导致调用方持有的config对象残留错误的checkpoint路径，后续重试可能加载错误模型或丢失父模型溯源信息。

## 环境

LeRobot main分支 (commit 3f797de5)，Windows系统，Python 3.12.14，PyTorch 2.11.0+cpu，离线异常注入测试环境。

## 解决办法

1. 定位`src/lerobot/policies/factory.py`中策略构造代码块，将其包裹在`try`语句中。
2. 将原本仅在成功路径执行的`cfg.pretrained_path`还原逻辑移动至`finally`块中，确保无论构造成功或异常均能恢复原始父模型路径。
3. 保留成功路径下对`base_policy.config.pretrained_path`的现有恢复逻辑不变。
4. 添加参数化回归测试`test_make_policy_restores_parent_path_after_failed_resume`，覆盖特征转换、权重加载及延迟初始化三个阶段的异常场景，验证异常后config路径正确还原且原始异常对象未被吞没。

讨论原作者：@JonnyFaeth 等，版权归原作者所有。
:::

## 原始参考来源 (Sources)

- [GitHub huggingface/lerobot #5176：make_policy leaves the resume checkpoint in cfg.pretrained_path when construction fails](https://github.com/huggingface/lerobot/issues/5176)