diff --git a/slime/backends/megatron_utils/model.py b/slime/backends/megatron_utils/model.py index 320a8188c5..8886f82732 100644 --- a/slime/backends/megatron_utils/model.py +++ b/slime/backends/megatron_utils/model.py @@ -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], @@ -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