miles 是一个高性能、面向企业级的大规模模型后训练强化学习(RL)框架,构建为 slime 的生产级分支,主要解决 MoE 训练稳定性、低精度训练以及训练-推理对齐等关键挑战。
一、用途与适用场景 miles 适用于以下需求:训练 1TB 以上规模的 MoE 模型(如 DeepSeek V3、Qwen3-MoE);进行 FP8 或 INT4 量化感知训练;需要逐位一致的训练-推理对齐;使用投机式 RL 以获得最大吞吐量;以及需要生产级稳定性与企业支持。若需要研究级原始版本可考虑 slime,需要灵活后端切换可考虑 verl,需要 PyTorch 原生抽象可考虑 torchforge。
二、主要能力
- 低精度训练:统一 FP8,推理与训练端到端使用 FP8;INT4 QAT,可在单机显存(H200)上运行 1TB 模型;Rollout Routing Replay(R3),为 MoE 提供逐位专家对齐。
- 性能优化:投机式 RL,借助在线 SFT 草稿模型实现 25% 以上的 rollout 加速;零拷贝权重同步,使用 CUDA IPC 零拷贝映射;部分 rollout,回收未完成的轨迹。
- 训练-推理对齐:TIS/MIS,即截断/掩码重要性采样,用于离策略校正;内核级优化,集成 FlashAttention-3、DeepGEMM。
三、安装方式
推荐使用 Docker:
docker pull radixark/miles:latest
docker run --rm --gpus all --ipc=host --shm-size=16g
-it radixark/miles:latest /bin/bash
从源码安装: git clone https://github.com/radixark/miles.git cd miles pip install -r requirements.txt pip install -e .
四、使用方式
miles 继承 slime 的配置系统。基础训练示例:
python train.py
--advantage-estimator grpo
--model-name qwen3-30b-a3b
--hf-checkpoint /path/to/qwen3-30b-a3b-hf
--rollout-batch-size 512
--n-samples-per-prompt 8
工作流一:大规模 MoE 训练。前置条件包括支持 FP8 的 H100/H200 GPU、MoE 模型(DeepSeek V3、Qwen3-MoE)、带 miles 的 Docker 环境。环境设置:
export NVTE_FP8_BLOCK_SCALING_FP32_SCALES=1
export CUDA_DEVICE_MAX_CONNECTIONS=1
训练配置示例:
python train.py
--actor-num-gpus-per-node 8
--rollout-num-gpus 8
--hf-checkpoint /path/to/deepseek-v3
--advantage-estimator grpo
--tensor-model-parallel-size 8
--expert-model-parallel-size 4
--prompt-data /path/to/data.jsonl
--num-rollout 3000
验证要点:模型加载无错误、路由决策一致、损失值无 NaN/Inf。
工作流二:投机式 RL 训练。其原理为:小草稿模型生成候选 token,目标模型并行验证,草稿模型通过在线 SFT 更新以跟踪策略。启用 EAGLE 投机解码(通过 SGLang):
python train.py
--actor-num-gpus-per-node 8
--hf-checkpoint /path/to/target-model
--sglang-speculative-algorithm EAGLE
--sglang-speculative-num-steps 3
--sglang-speculative-eagle-topk 1
--sglang-speculative-num-draft-tokens 4
--sglang-speculative-draft-model-path /path/to/draft-model
--advantage-estimator grpo
--prompt-data /path/to/data.jsonl
可选启用在线 MTP 训练:
--mtp-num-layers 1
--enable-mtp-training
--mtp-loss-scaling-factor 0.2
注意:在线 MTP 训练需要带 MTP 权重的 torch dist checkpoint,从 HuggingFace 转换 checkpoint 时需添加 --mtp-num-layers 1。预期加速:标准 rollout 为基线,投机式 RL 快 25-40%,配合部分 rollout 可再提升 10-15% 吞吐量。
五、配置参考 miles 继承 slime 的全部参数。集群资源(来自 slime):--actor-num-nodes、--actor-num-gpus-per-node、--rollout-num-gpus、--rollout-num-gpus-per-engine、--colocate。Megatron 并行(来自 slime):--tensor-model-parallel-size、--pipeline-model-parallel-size、--expert-model-parallel-size。投机解码(miles 特有):--sglang-speculative-algorithm EAGLE、--sglang-speculative-num-steps、--sglang-speculative-eagle-topk、--sglang-speculative-num-draft-tokens、--sglang-enable-draft-weights-cpu-backup、--sglang-speculative-draft-model-path。在线 MTP 训练(miles 特有):--mtp-num-layers、--enable-mtp-training、--mtp-loss-scaling-factor。
六、关键特性(概念) 统一 FP8 流水线:端到端 FP8 采样与训练,消除量化引起的差异,避免 MoE 模型 RL 崩溃。Rollout Routing Replay(R3):在 SGLang 推理期间记录专家路由决策,并在 Megatron 训练期间重放,实现逐位专家对齐;路由决策存储在 sample.rollout_routed_experts 中,训练时重放而非重新计算,确保训练与推理的专家选择一致。INT4 量化感知训练:支持 1TB 以上模型单机部署(如 H200)。内存节省示例:70B 模型 BF16 为 140GB、INT4 为 45GB(约 3.1 倍);235B 为 470GB 对 150GB;671B 为 1.3TB 对 420GB。训练-推理对齐:通过 Flash Attention 3、DeepGEMM、Thinking Machines Lab 的批不变内核以及 torch.compile 集成,实现训练与推理之间“恰好 0 KL 散度”。
七、样本数据结构 miles 使用与 slime 相同的 Sample dataclass,并带有用于 MoE 路由重放的 rollout_routed_experts 字段: @dataclass class Sample: prompt: str | list[dict] tokens: list[int] response: str reward: float | dict loss_mask: list[int] status: Status metadata: dict rollout_log_probs: list[float] rollout_routed_experts: list[list[int]] # MoE routing for R3
八、常见问题与解决方案 FP8 训练崩溃:症状为损失爆炸、出现 NaN。解决:使用块缩放 export NVTE_FP8_BLOCK_SCALING_FP32_SCALES=1;降低学习率 --lr 5e-7;确保训练/推理间 MoE 路由一致。 投机草稿漂移:症状为接受率随时间下降。解决:启用在线 MTP 训练保持草稿模型对齐;减少投机步数 --sglang-speculative-num-steps 2;使用 CPU 备份 --sglang-enable-draft-weights-cpu-backup。 训练-推理不匹配:症状为策略发散、奖励崩溃。解决:使用 TIS 进行离策略校正 --use-tis --tis-threshold 0.9;验证 SGLang 与 Megatron 的 log probs 是否匹配;对 MoE 模型启用 R3。
九、支持的模型 DeepSeek:R1、V3、V3.2,完整 MoE 支持;Qwen:2、2.5、3(含 MoE),完整 MoE 支持;Llama:3、3.1、3.3、4,仅稠密;Gemma:2、3、3N,仅稠密;GLM:4.5、4.6、4.7,仅稠密;MiniMax:M2、M2.1,完整 MoE 支持。
十、资源 GitHub:https://github.com/radixark/miles 介绍博客:https://lmsys.org/blog/2025-11-19-miles/ Slime(上游):https://github.com/THUDM/slime SGLang:https://github.com/sgl-project/sglang
相关技能:tensorrt-llm(使用 NVIDIA TensorRT 优化 LLM 推理以获得最大吞吐量与最低延迟);slime-rl-training(使用 slime 这一 Megatron+SGLang 框架进行 LLM 后训练 RL 的指导);serving-llms-vllm(使用 vLLM 的 PagedAttention 与连续批处理高吞吐地服务 LLM)。