Skip to content

refactor(cookbook): configure examples directly in Python - #428

Merged
kevssim merged 3 commits into
modelscope:dev-swift-v5from
werwrewe:refactor/cookbook-no-cli
Dec 2, 2026
Merged

kevssim merged 3 commits into
modelscope:dev-swift-v5from
werwrewe:refactor/cookbook-no-cli

Conversation

@werwrewe

@werwrewe werwrewe commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

Twinkle 移除 twinkle.cli 后,部分 Cookbook 仍导入旧的参数解析器,启动时会报 ModuleNotFoundError。本 PR 将这些示例迁移到脚本内配置,让读者打开 Python 文件就能看到模型、数据集、并行度和训练参数。

  • 将训练参数移到各示例的 Python 文件顶部,覆盖 Transformers、Megatron、多模态、SFT、DPO、GRPO、PPO 和蒸馏等示例。
  • Shell 脚本继续负责设备选择、环境准备和 python/torchrun 启动。运行前在对应的 Python 文件中修改训练配置。
  • 同步更新 AgentEnv、OpenEnv 和 RSI 示例的说明及启动方式。

以 FSDP2 为例,先在 cookbook/transformers/fsdp2.py 中编辑 MODEL_ID、DATASET_ID、FSDP_SIZE 和 DP_SIZE,然后运行:

sh cookbook/transformers/fsdp2.sh

调整卡数时,需同步调整启动脚本中的设备列表和 --nproc_per_node。

Experiment results

PR 提交 141fbd3 的 31 个 Python 文件和 26 个 Shell 脚本均通过语法检查,修改后的 Python 示例已移除对 twinkle.cli 的导入。

在 Ascend 910B4 上使用本地模型和数据,对同一迁移改动做了以下短程训练验证:

Cookbook 配置 结果
cookbook/transformers/fsdp2.py Qwen3-4B,2 卡 FSDP2 通过,4 步
cookbook/transformers/sp_fsdp_dense.py Qwen3-4B,2 卡 SP2 通过,4 步
cookbook/transformers/ep_fsdp2_lora_qwen3_5_moe.py Qwen3-30B-A3B-8L,2 卡 EP2 通过,4 步
cookbook/mm/fsdp2.py Qwen3.5-0.8B,2 卡 通过,8 步
cookbook/rl/dpo/dpo_full.py Qwen3-0.6B,2 卡 通过,8 步
cookbook/rl/dpo/dpo_lora.py Qwen3-0.6B,1 卡 通过,8 步

Replace the retired twinkle.cli parser with recipe-local settings and keep the shell launchers focused on process setup.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical issues cause default recipes to fail during import or model configuration and allow the documented RSI launch to execute generated commands on the training host instead of the requested sandbox.

3 open issues
What changed in this PR

Moves cookbook configuration into Python scripts, removing dependencies on the deleted twinkle.cli module while keeping shell scripts responsible for launching examples.

Changes:

  • Replaces CLI arguments with editable Python settings across training and sampling recipes.
  • Simplifies launchers and makes many script paths independent of the working directory.
  • Updates AgentEnv, OpenEnv, and RSI configuration instructions.
File Description
cookbook/​transformers/​spectral_hybrid_lora.sh Simplifies training launch.
cookbook/​transformers/​spectral_hybrid_lora.py Inlines allocation and training settings.
cookbook/​transformers/​sp_fsdp_dense.sh Removes training flags.
cookbook/​transformers/​sp_fsdp_dense.py Inlines sequence-parallel settings.
cookbook/​transformers/​fsdp2.sh Simplifies torchrun launch.
cookbook/​transformers/​fsdp2.py Inlines training and checkpoint settings.
cookbook/​transformers/​ep_fsdp2_lora_qwen3_5_moe.sh Removes training flags.
cookbook/​transformers/​ep_fsdp2_lora_qwen3_5_moe.py Inlines expert-parallel LoRA settings.
cookbook/​transformers/​ep_fsdp2_lora_deepseek_v4.py Inlines settings; derives parallel sizes from WORLD_SIZE.
cookbook/​transformers/​ep_fsdp2_lora_deepseek_v4_multinode.sh Retains cluster setup and launch.
cookbook/​sample/​sample.py Inlines model and adapter settings.
cookbook/​sample/​emb_sample.py Removes CLI dependency; imports os.
cookbook/​rsi/​run.sh Exports sandbox and agent settings.
cookbook/​rsi/​rsi_grpo.py Replaces CLI configuration with constants and environment settings.
cookbook/​rsi/​agentic/​rsi_agent.yaml Updates configuration names in comments only.
cookbook/​rsi/​agentic/​README.md Documents revised agent launch.
cookbook/​rl/​ppo/​ppo.sh Simplifies PPO launch.
cookbook/​rl/​ppo/​ppo.py Inlines PPO hyperparameters.
cookbook/​rl/​multi_turn/​multi_turn_grpo.py Inlines multi-turn training settings.
cookbook/​rl/​grpo/​short_math_grpo.sh Simplifies short-math launch.
cookbook/​rl/​grpo/​short_math_grpo.py Inlines short-math settings.
cookbook/​rl/​grpo/​short_math_grpo_routing_replay.sh Removes routing-replay flags.
cookbook/​rl/​grpo/​short_math_grpo_routing_replay.py Inlines replay and parallelism settings.
cookbook/​rl/​grpo/​short_math_grpo_multi_lora.sh Simplifies multi-adapter launch.
cookbook/​rl/​grpo/​short_math_grpo_multi_lora.py Inlines training and adapter-sync settings.
cookbook/​rl/​grpo/​short_math_grpo_moe.sh Simplifies MoE launch.
cookbook/​rl/​grpo/​short_math_grpo_moe.py Inlines MoE training settings.
cookbook/​rl/​grpo/​mbpp_grpo.py Inlines MBPP training settings.
cookbook/​rl/​grpo/​kodcode_grpo.py Inlines KodCode training settings.
cookbook/​rl/​grpo/​grpo.sh Simplifies GRPO launch.
cookbook/​rl/​grpo/​grpo.py Inlines GRPO settings.
cookbook/​rl/​grpo/​grpo_sampling_replay.sh Removes sampling-replay flags.
cookbook/​rl/​grpo/​grpo_sampling_replay.py Inlines settings and updates validation message.
cookbook/​rl/​grpo/​grpo_mm.sh Simplifies multimodal GRPO launch.
cookbook/​rl/​grpo/​grpo_mm.py Inlines multimodal training settings.
cookbook/​rl/​grpo/​group_admission.sh Updates launch and smoke-test guidance.
cookbook/​rl/​grpo/​group_admission.py Inlines group-admission settings.
cookbook/​rl/​gkd/​gkd_on_policy.sh Simplifies on-policy distillation launch.
cookbook/​rl/​gkd/​gkd_on_policy.py Inlines student, teacher, and loss settings.
cookbook/​rl/​gkd/​gkd_off_policy.sh Simplifies off-policy distillation launch.
cookbook/​rl/​gkd/​gkd_off_policy.py Inlines distillation settings.
cookbook/​rl/​envs/​train.py Centralizes backend-independent training settings.
cookbook/​rl/​envs/​run_openenv.sh Keeps connection setup; removes training flags.
cookbook/​rl/​envs/​run_agentenv.sh Keeps connection setup; removes training flags.
cookbook/​rl/​envs/​README.md Documents Python configuration and smoke tests.
cookbook/​rl/​dpo/​dpo_multi_lora.sh Simplifies multi-adapter DPO launch.
cookbook/​rl/​dpo/​dpo_multi_lora.py Inlines DPO, device, and sample settings.
cookbook/​rl/​dpo/​dpo_lora.sh Simplifies LoRA DPO launch.
cookbook/​rl/​dpo/​dpo_lora.py Inlines settings; scales mesh with device count.
cookbook/​rl/​dpo/​dpo_full.sh Simplifies full-parameter DPO launch.
cookbook/​rl/​dpo/​dpo_full.py Inlines settings and parameterizes device meshes.
cookbook/​mm/​fsdp2.sh Simplifies multimodal launch.
cookbook/​mm/​fsdp2.py Inlines OCR training settings.
cookbook/​mm/​fsdp2_gemma4_mm.sh Simplifies Gemma launch.
cookbook/​mm/​fsdp2_gemma4_mm.py Inlines Gemma training settings.
cookbook/​mm/​fsdp2_gemma4_12b_mm.py Inlines model, dataset, and mesh settings.
cookbook/​megatron/​tp.sh Simplifies Megatron launch.
cookbook/​megatron/​tp.py Inlines parallelism and checkpoint settings.
cookbook/​megatron/​tp_moe.sh Removes MoE training flags.
cookbook/​megatron/​tp_moe.py Inlines MoE parallelism and training settings.

Review effort: Balanced

Comment thread cookbook/mm/fsdp2_gemma4_12b_mm.py Outdated

# Cookbook configuration.
FSDP_SIZE = None
DP_SIZE = None
Comment thread cookbook/rsi/rsi_grpo.py
Comment on lines +202 to +203
SANDBOX_TEMPLATE = os.environ.get('SANDBOX_TEMPLATE', '')
SANDBOX_API_URL = os.environ.get('SANDBOX_API_URL', '')
werwrewe and others added 2 commits December 2, 2026 10:19
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Default Gemma to a one-rank data mesh. Reject removed RSI flags before setup and document sandbox configuration through the environment.
@kevssim
kevssim merged commit 69f0eb4 into modelscope:dev-swift-v5 Dec 2, 2026
4 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants