slime 框架技术总览:面向 RL Scaling 的 Megatron + SGLang 后训练架构解析
2026/9/16 15:03:50 网站建设 项目流程

slime 框架技术总览:面向 RL Scaling 的 Megatron + SGLang 后训练架构解析

【免费下载链接】slimeslime is an LLM post-training framework for RL Scaling.项目地址: https://gitcode.com/GitHub_Trending/slime12/slime

slime 是一个面向RL Scaling(强化学习规模化)的 LLM 后训练(post-training)框架,其文档首页(docs/en/index.rst)将框架定位为两大核心能力的统一体:高性能训练(连接 Megatron 与 SGLang,支持多种模式下的高效训练)与灵活数据生成(通过自定义数据生成接口与基于服务器的引擎,实现任意训练数据生成流程)。阅读本文后,你将完整理解 slime 的设计理念、单条 training / rollout / Data Buffer 数据通路、参数透传机制、生产验证情况,以及如何按使用场景索引其全部文档与源码。

两大核心能力与统一设计目标

slime 提供的两大核心能力是:

  1. 高性能训练(High-Performance Training):通过将 Megatron 训练引擎与 SGLang 推理引擎连接,支持多种模式下的高效训练;
  2. 灵活数据生成(Flexible Data Generation):通过自定义数据生成接口与基于服务器的引擎,实现任意训练数据生成流程。

框架的设计目标是让这两大能力彼此强化,而不是把系统变成一堆割裂的 trainer、rollout service 和 agent framework 的沉重堆叠。在 slime 中,Megatron training(Megatron 训练)、SGLang rollout(SGLang 数据采样)、custom data generation(自定义数据生成)、reward computation(奖励计算)、verifier feedback(验证器反馈)和 environment interaction(环境交互)全部流经同一条 training / rollout / Data Buffer 路径

这个设计让 slime 保持了"足够轻量、清晰、易扩展",同时又在 SOTA 级模型发布的完整训练闭环中得到验证。README 中给出的"代码阅读路径"可以直观印证这一条单一路径的设计:

train.py: train ├─ slime/ray/placement_group.py Ray 资源与 worker 初始化 ├─ slime/ray/rollout.py RolloutManager.generate: rollout 编排 │ └─ slime/rollout/sglang_rollout.py 样本生成与奖励计算 └─ slime/ray/actor_group.py RayTrainGroup.async_train: 训练分发 └─ slime/backends/megatron_utils/actor.py ├─ model.py Megatron 模型执行 └─ loss.py RL 损失与优势计算

在入口文件 train.py 中可以看到这个闭环的实际调度:create_rollout_manager创建包含 SGLang 引擎的 rollout 管理器,create_training_models创建 actor/critic 训练模型,随后主循环反复执行rollout_manager.generate.remote(rollout_id)采样数据、actor_model.async_train(...)训练、actor_model.update_weights()将权重同步回推理引擎,形成"数据采样 → 权重更新"的迭代。

为什么这个设计重要:六条核心设计原则

文档首页用六个要点阐述了该设计理念的工程价值:

1. 经过 frontier model 训练验证(Battle-tested)

slime 是 GLM-5.2、GLM-5.1、GLM-5、GLM-4.7、GLM-4.6、GLM-4.5 等系列模型发布背后的 RL 训练框架。这验证的是完整的后训练闭环而不仅仅是孤立的示例。README 进一步补充了 "Correctness-first infrastructure" 原则:RL 的 bug 往往是静默的(silent),因此 slime 保持数据流显式化,提供独立的 rollout-only 与 train-only 调试路径,并把可复现性、容错、trace、profiling 和 CI 作为一等工程关注点。

2. 从设计开始就是 native(Native by design)

slime直接透传 Megatron 参数,并通过--sglang-前缀暴露当前安装版本 SGLang 支持的全部参数。这意味着上游训练与 serving 的新优化可以直接使用,无需在 slime 内再加一层 wrapper 抽象。例如 SGLang 的--mem-fraction-static在 slime 中写作--sglang-mem-fraction-static

这一机制在 slime/utils/arguments.py 的parse_args中有清晰的实现:两阶段解析——阶段一用独立的 parser 调用sglang_parse_args()解析全部--sglang-*参数;阶段二用megatron_parse_args()解析 Megatron + slime 参数(通过ignore_unknown_args静默忽略 sglang 前缀参数),最后将两部分的 namespace 合并。因此"已安装 SGLang 支持的每个参数,加上前缀即可使用"是字面意义的透传。

3. 专注 SGLang rollout(SGLang-focused rollout)

slime有意选择单一的 rollout 后端。多后端框架往往被迫把多个推理引擎抽象成 lowest-common-denominator(最低公共分母)的能力子集,从而隐藏各后端最强特性。slime 只做 Megatron + SGLang 一条路径,因此 RL 负载可以直接使用 SGLang-specific 的 serving、routing、caching、disaggregation 和 weight-sync 行为。README 强调这是 "Lightweight and opinionated":只深入打磨大规模 RL 所用的 Megatron + SGLang 路径。

4. Agentic workflow 就是数据生成

tool use(工具调用)、sandbox interaction(沙箱交互)、verifier reward(验证器奖励)、environment feedback(环境反馈)、multi-agent loop(多智能体循环)和 long-horizon agentic workflow(长视野智能体工作流)都接入同一条 training / rollout / Data Buffer 路径,而不是 fork 训练内核。仓库 examples 目录下的多智能体(examples/multi_agent)、搜索型 RAG(examples/search-r1)、全异步(examples/fully_async)、SWE 编码智能体(examples/coding_agent_rl)等示例均通过--custom-generate-function-path插入标准 rollout 循环,而非独立框架。

5. BF16 训练 + FP8 rollout

大规模 MoE recipe 使用 MegatronBF16 training state搭配 SGLangFP8 rollout/inference;long-context rollout 还可以通过--sglang-kv-cache-dtype fp8_e4m3提升有效 KV cache 容量。此能力已在 docs/en/get_started/quick_start.md 的 "bf16 Training fp8 Inference" 一节给出完整操作步骤:只需把--hf-checkpoint指向 FP8 权重(如Qwen/Qwen3-4B-FP8),Megatron 侧 checkpoint 仍使用 bf16 HF 权重转换而来。详细的精度与量化讨论见 docs/en/advanced/low-precision.md。

6. 作为 RL 基础设施来测试(Tested as RL infrastructure)

CPU correctness tests默认运行(每个 PR、每次 push 到 main 以及手动workflow_dispatch都会触发),GPU e2e tests在自托管 GPU runner 上覆盖真实 Megatron + SGLang training/rollout 路径,包括 dense/MoE recipe、async rollout、SGLang config、checkpoint、precision 和 debug replay。分层设计是有意为之:多数不变量应在不等待 GPU 队列的情况下快速验证,而完整训练/rollout 行为仍由 GPU e2e 任务兜底。详见 docs/en/developer_guide/ci.md。

生产验证与模型支持矩阵

文档首页明确列出,除 GLM 系列之外,slime 还支持:

  • Qwen 系列:Qwen3.6、Qwen3.5、Qwen3Next、Qwen3MoE、Qwen3、Qwen2.5;
  • DeepSeek V3 系列:DeepSeek V3、V3.1、DeepSeek R1;
  • Llama 3

这些模型在仓库中有对应的配置与启动脚本佐证:scripts/models 目录下提供各模型的 Megatron 参数配置(如 qwen3-30B-A3B.sh、qwen3-next-80B-A3B.sh、deepseek-v3.sh、glm5.2-744B-A40B.sh 等),docs/en/examples 提供端到端训练案例。文档首页据此将示例按Dense(qwen3-4B、glm4-9B)与MoE(glm4.7-30B-A3B、qwen3-30B-A3B、glm5.2-744B-A40B、glm4.7-355B-A32B、deepseek-r1)分类组织。

按使用场景开始的文档导航

文档首页提供了一套"按使用场景开始"的入口,是读者定位所需内容的最快路径:

场景推荐入口
第一次使用 slimedocs/en/get_started/quick_start.md
配置 training 和 rollout 参数docs/en/get_started/usage.md
添加 custom generation、reward 或 rollout functiondocs/en/get_started/customization.md
构建 agentic RL workflowdocs/en/get_started/agent.md
配置生产级 SGLang rollout topologydocs/en/advanced/sglang-config.md
接入 external rollout enginesdocs/en/advanced/external-rollout-engines.md
以字节级 delta 同步权重docs/en/advanced/delta-weight-sync.md
使用 PD disaggregationdocs/en/advanced/pd-disaggregation.md
使用 BF16 训练 + FP8 rollout 或 FP8 KV cachedocs/en/advanced/low-precision.md
了解 CI 和可靠性覆盖docs/en/developer_guide/ci.md
调试、trace、profiling 长时间任务docs/en/developer_guide/debug.md、docs/en/developer_guide/trace.md、docs/en/developer_guide/profiling.md

文档体系中的其他导航分组

除上述场景入口外,文档首页的 toctree 还按主题组织了完整的文档地图,供深度阅读:

  • Get Started:quick_start、usage、customization、agent、qa(docs/en/get_started);
  • Dense 示例:qwen3-4B、glm4-9B;
  • MoE 示例:glm4.7-30B-A3B、qwen3-30B-A3B、glm5.2-744B-A40B、glm4.7-355B-A32B、deepseek-r1;
  • Advanced Features:on-policy-distillation、speculative-decoding、low-precision、reproducibility、fault-tolerance、observability、pd-disaggregation、external-rollout-engines、delta-weight-sync、sglang-config、megatron-config、arch-support-beyond-megatron(docs/en/advanced);
  • Other Usage:qwen3-4b-base-openhermes 以及 examples/search-r1/README.md、examples/fully_async/README.md、examples/retool/README.md、examples/multi_agent/README.md、examples/coding_agent_rl/README.md 等仓库示例;
  • Developer Guide:ci、debug、trace、profiling;
  • Hardware Platforms:AMD 使用教程;
  • Blogs:v0.1.0 发布说明、slime 设计理念博客。

三大参数体系的源码印证

文档首页强调的 "native by design" 在参数层面体现为三类参数(与 README.md 的 Arguments Walkthrough 章节一致):

  1. Megatron 参数:slime 直接读取。通过from megatron.training.arguments import parse_args复用当前环境PYTHONPATH中 Megatron 的全部参数,如--tensor-model-parallel-size 2--sequence-parallel--context-parallel-size--expert-model-parallel-size等并行配置;
  2. SGLang 参数:通过--sglang-前缀透传,如--mem-fraction-static--sglang-mem-fraction-static--ep-size--sglang-ep-size--enable-dp-attention--sglang-enable-dp-attention。sgl-router 的参数则加router前缀,如--router-balance-abs-threshold
  3. slime 专属参数:完整定义在 slime/utils/arguments.py,覆盖集群资源分配(--actor-num-nodes--actor-num-gpus-per-node--rollout-num-gpus--rollout-num-gpus-per-engine--colocate)、rollout 采样(--rollout-batch-size--n-samples-per-prompt--dynamic-sampling-filter-path--partial-rollout)、算法(--advantage-estimator,支持 grpo/gspo/cispo/reinforce_plus_plus/ppo)、奖励模型(--rm-type--custom-rm-path)、权重同步(--update-weight-mode--update-weight-transport)以及可观测性(wandb/tensorboard)等。

其中关键的训练/采样预算约束在设计上保持显式:每轮"产出"与"消耗"必须相等,即rollout-batch-size × n-samples-per-prompt = global-batch-size × num-steps-per-rollout。参数解析中对--num-steps-per-rollout的设置会自动推导或校验--global-batch-size(见 slime/utils/arguments.py 中--global-batch-size--num-steps-per-rollout的定义与注释)。

架构模块与数据通路

README 的 Architecture Overview 与文档首页的"统一路径"表述相互印证,slime 由三个模块构成:

  • training(Megatron):负责主训练进程,从 Data Buffer 读取数据,训练后把参数同步到 rollout 模块。支持 TP/PP/EP/CP 全并行(docs/en/get_started/usage.md 的 "How to Use Megatron" 一节有各并行策略的配置说明);
  • rollout(SGLang + router):生成新数据(含 reward/verifier 输出)并存入 Data Buffer。自定义 generate 函数可在此基础上封装多轮循环、工具调用、环境/沙箱交互和基于验证器的奖励。slime 使用 sgl-router 管理多个 SGLang server:所有 server 通过/add_worker注册到 router,生成时只需向 router 发送 HTTP 请求,router 负责负载均衡与转发,且支持 OpenAI 兼容 API;
  • data buffer:桥接模块,管理 prompt 初始化、自定义数据与 rollout 生成方法(包括通过同一接口产出样本的 agentic workflow)。

可靠性:作为 RL 基础设施的 CI 体系

文档首页明确指出 slime 是"作为 RL 基础设施来测试"的,docs/en/developer_guide/ci.md 给出了两层 CI 结构:

  • CPU correctness tests(常开):在 GitHub 托管 runner 上运行,覆盖 Megatron 参数与 HF 配置校验、DP/CP 调度与 CP loss 不变性、指标上报与分布式聚合、math/GPQA/F1/DeepScaler/DAPO 类奖励模型、Sample行为与 agent 轨迹合并、HF checkpoint saver、定制化钩子契约等;
  • GPU e2e tests(label 门控):在自托管 GPU runner 上运行,通过tests/ci/gpu_lock_exec.py获取 GPU,覆盖 dense/MoE recipe、async rollout、OPD、PPO 风格路径、PD/Mooncake、debug rollout-then-train 重放、精度一致性与 checkpoint 保存/加载。

仓库 tests 目录下的测试文件与 CI 描述一一对应,例如test_megatron_argument_validation.pytest_loss_cp_invariance.pytest_rm_math.pytest_plugin_generate_contracts.pytest_qwen3_4B_ppo.pytest_qwen2.5_0.5B_fully_async_short.py等,可作为理解框架正确性边界的直接源码证据。

快速上手路径

文档首页将 docs/en/get_started/quick_start.md 作为第一次使用 slime 的入口,其完整流程包括:

  1. 环境准备:推荐使用预装全部依赖的 Docker 镜像(docker pull slimerl/slime:latest),支持 B200 与 H100/H200 系列;不方便使用 Docker 时可参考根目录 build_conda.sh;
  2. 模型与数据下载:通过hf download获取模型权重(如 GLM-Z1-9B)、训练集(如 dapo-math-17k)与评测集(如 aime-2024);
  3. 权重转换:使用tools/convert_hf_to_torch_dist.py将 HF 格式转为 Megatrontorch_dist格式,训练后可用tools/convert_torch_dist_to_hf.py转回;
  4. 启动训练bash scripts/run-glm4-9B.sh,其中 MODEL_ARGS/CKPT_ARGS/ROLLOUT_ARGS/EVAL_ARGS/PERF_ARGS/GRPO_ARGS/OPTIMIZER_ARGS/SGLANG_ARGS 各参数组的含义在 quick start 文档中有逐项讲解。

总结

从 docs/en/index.rst 可以看出,slime 的架构哲学可以概括为一句话:用一条统一的 training / rollout / Data Buffer 数据通路,同时承载高性能 RL 训练与任意复杂度的数据生成。它通过 Megatron 与 SGLang 的原生参数透传保持与上游引擎同步演进的能力,通过单一 SGLang 后端避免多引擎抽象带来的能力损失,通过把 agentic 工作流降维为可插拔的数据生成函数避免框架分化,并以分层 CI 体系保证作为 RL 基础设施的正确性与稳定性。无论你是第一次接触 slime,还是需要配置生产级 rollout 拓扑、接入外部引擎、优化低精度训练,都可以从本文的导航索引出发,按需进入对应文档与源码深入阅读。

【免费下载链接】slimeslime is an LLM post-training framework for RL Scaling.项目地址: https://gitcode.com/GitHub_Trending/slime12/slime

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询