Skip to content

Work: 提取训练脚本公共逻辑 — 消除 4 脚本重复代码 #150

Closed

Description

@TATP-233

Work type

refactor

Area

other

Problem

4 个训练脚本存在大量复制粘贴代码,违反 DRY 原则:

功能 train_rsl_rl.py train_mlx_ppo.py train_appo.py train_offpolicy.py 重复度
ensure_registries() ✅ ✅ ✅ ✅ 100%
Play 模式渲染循环 ✅ ❌ ✅ ✅ ~80%
Checkpoint 解析逻辑 ✅ ✅ ✅ ✅ ~90%
Logger 设置 ✅ ✅ ✅ ✅ ~80%
Motrix 兼容层 ✅ ❌ ✅ ✅ ~70%

代码重复导致:

  • 修改通用逻辑需要在 4 个文件同步修改
  • 新增训练脚本需要复制大量样板代码
  • 修复 bug 容易遗漏某个脚本

Deliverable

  1. 新建 src/unilab/training/common.py 模块,提取以下公共函数:

    • ensure_registries() -> None — 确保环境/算法注册
    • parse_checkpoint_path(cfg: DictConfig) -> Optional[str] — 解析 checkpoint 路径
    • setup_logger(log_dir: Path, algo_name: str) -> Logger — 统一日志设置
    • create_env(cfg: DictConfig, device: str) -> NpEnv — 统一环境创建
    • render_play_mode(env: NpEnv, policy, num_steps: int) -> None — Play 模式渲染
  2. 创建 src/unilab/training/backend_adapter.py 模块:

    • BackendAdapter 类 — 集中处理 Motrix 兼容逻辑
    • 替代分散在各脚本中的 motrix_legacy 处理
  3. 所有训练脚本改为导入公共函数,移除重复代码

Definition of done

  • training/common.py 公共模块创建
  • training/backend_adapter.py 后端适配器创建
  • 4 个训练脚本的重复代码减少 60%+
  • 删除各脚本中的 ensure_registries() 重复实现
  • 删除各脚本中的 Motrix 兼容层重复代码
  • 无功能回归 (所有训练脚本可正常启动)
  • CI green

Dependencies and blockers

建议 Issue #137 (DR 能力查询 API) 完成后进行,BackendAdapter 需要依赖后端能力检测

Validation plan

  • uv run python scripts/train_rsl_rl.py task=go2_joystick algo.max_iterations=1 正常
  • uv run python scripts/train_appo.py task=go2_joystick algo.max_iterations=1 正常
  • uv run python scripts/train_offpolicy.py task=go2_joystick algo.max_iterations=1 正常
  • make test 通过

Activity

  1. TATP-233 commented on Apr 9, 2026

    @TATP-233
    CollaboratorAuthor

    Closing in favor of the split canonical issues #166, #167, and #168 so bootstrap drift, play contract drift, and log/load_run drift can be tracked independently.

  2. TATP-233 commented on Apr 9, 2026

    @TATP-233
    CollaboratorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions