执行摘要
禁用参数 CPU 备份以节省主机内存
减少训练过程中参数和梯度CPU备份带来的大量主机内存开销,特别是在offload训练和大型模型场景下,此PR通过禁用不必要的备份来优化内存使用。这是持续内存优化系列的一部分。
该PR值得性能优化工程师和训练系统开发者关注,尤其是如何通过禁用不必要的CPU备份来节省内存的设计决策,以及对Megatron底层补丁的实现技巧。
该PR没有review讨论。
减少训练过程中参数和梯度CPU备份带来的大量主机内存开销,特别是在offload训练和大型模型场景下,此PR通过禁用不必要的备份来优化内存使用。这是持续内存优化系列的一部分。
该PR值得性能优化工程师和训练系统开发者关注,尤其是如何通过禁用不必要的CPU备份来节省内存的设计决策,以及对Megatron底层补丁的实现技巧。
该PR没有review讨论。
在Megatron层增加参数支持:在 docker/patch/latest/megatron.patch 中,向 DistributedDataParallel 和 _ParamAndGradBuffer 新增 disable_param_buffers_cpu_backup 和 disable_grad_buffers_cpu_backup 参数,并在 nccl_ub 为假时,利用 torch_memory_saver.region 创建无CPU备份的内存分配上下文。
在配置层调整参数:在 slime/utils/arguments.py 中移除 --disable-weights-backuper 参数及其相关逻辑;在 slime_validate_args 中,当 offload_train 时自动设置 disable_param_buffers_cpu_backup=True;在 _apply_megatron_role_overrides 中,对critic角色默认禁用此选项(除非显式覆盖)。
在actor初始化中去掉了 translate_gpu_to_cpu 参数和根据 enable_weights_backuper 决定 single_tag 的逻辑,简化 TensorBackuper 创建。同时,在 wake_up 方法中添加 self._switch_model("actor") 调用,确保在恢复训练时正确切换模型状态。
在 slime/ray/placement_group.py 中,对critic模型的参数进行额外处理:当未通过 megatron_config_path 指定配置时,强制设置 disable_param_buffers_cpu_backup=False,避免继承actor的设置导致错误。
| 文件 | 模块 | 状态 | 重要度 |
|---|---|---|---|
docker/patch/latest/megatron.patch |
分布式层 | modified | 6.43 |
slime/ray/placement_group.py |
调度层 | modified | 5.52 |
slime/utils/arguments.py |
配置管理 | modified | 5.88 |
slime/backends/megatron_utils/actor.py |
训练后端 | modified | 5.9 |
docker/patch/latest/megatron.patch
core-logic
核心变更,在 Megatron 分布式训练底层添加禁用参数和梯度 CPU 备份的支持,利用 torch_memory_saver 实现无备份内存分配。
# Inside _ParamAndGradBuffer.__init__, after setting up basic attributes:
def _make_no_backup_context(tag, disable, flag_name="disable_grad_buffers_cpu_backup"):
"""Return a context manager that disables CPU backup for memory allocation."""
if disable:
try:
from torch_memory_saver import torch_memory_saver
except ImportError as e:
raise ImportError(
f"{flag_name}=True requires torch_memory_saver. "
"Install with: pip install torch-memory-saver"
) from e
return partial(
torch_memory_saver.region,
tag=tag,
enable_cpu_backup=False,
)
return nullcontext
# Create context for gradient buffer
# (parameter buffer context follows similar pattern)
grad_mem_alloc_context = _make_no_backup_context(
"grad_buffer", disable_grad_buffers_cpu_backup
)
slime/backends/megatron_utils/actor.py
core-logic
简化 TensorBackuper 创建,移除废弃参数,将模型切换逻辑内联化。
# In actor's init method:
self.weights_backuper = TensorBackuper.create(
source_getter=lambda: named_params_and_buffers(
self.args,
self.model,
convert_to_global_name=args.megatron_to_hf_mode == "raw",
),
single_tag=None, # Previously depended on enable_weights_backuper; now always None
)
self._active_model_tag: str | None = "actor"
self.weights_backuper.backup("actor")
# In wake_up method:
@timer
def wake_up(self) -> None:
assert self.args.offload_train
print_memory("before wake_up model")
torch_memory_saver.resume()
clear_memory()
reload_process_groups()
if self.role == "actor":
self._switch_model("actor") # New: restore actor model after sleep
print_memory("after wake_up model")
当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。
--disable-weights-backuper 参数,旧有脚本若使用该参数将会报错,需更新配置。disable_param_buffers_cpu_backup 与 nccl_ub=True 不兼容,代码中已通过 assert 保护,但用户需注意避免同时设置。disable_param_buffers_cpu_backup 从 actor 继承 True,可能导致训练异常,代码专门在 placement_group.py 和 arguments.py 中处理了此情况。--disable-weights-backuper,但功能由新标志自动接管,预期用户无感知(除了参数移除带来的错误提示)。主机内存使用将显著降低,有利于部署更大模型。当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。
参与讨论