Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion slime/backends/megatron_utils/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,16 @@ def should_disable_forward_pre_hook(args: Namespace) -> bool:
return args.use_distributed_optimizer and args.overlap_param_gather


def finalize_model_grads_with_empty_cache(*args, **kwargs):
# trigger empty cache when there are less than 10% free memory before the final reduce scatter.
# TODO: this is an ad-hoc method and we should figure out why the oom happens in the first place.
device = torch.cuda.current_device()
free, total = torch.cuda.mem_get_info(device)
if free / total < 0.1:
clear_memory()
return finalize_model_grads(*args, **kwargs)


def train(
rollout_id: int,
model: Sequence[DDP],
Expand Down Expand Up @@ -550,7 +560,7 @@ def train(
config.param_sync_func = [model_chunk.start_param_sync for model_chunk in model]
if len(model) == 1:
config.param_sync_func = config.param_sync_func[0]
config.finalize_model_grads_func = finalize_model_grads
config.finalize_model_grads_func = finalize_model_grads_with_empty_cache

pre_hook_enabled = False

Expand Down