Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GRPO profiling #2522

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions recipes/dev/grpo_full_finetune_distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ def _setup_profiler(
self.profiler_wait_steps = profiler_cfg["wait_steps"]
self.profiler_warmup_steps = profiler_cfg["warmup_steps"]
self.profiler_active_steps = profiler_cfg["active_steps"]
self.profiler_num_cycles = profiler_cfg["num_cycles"]

return profiler

Expand Down Expand Up @@ -949,6 +950,7 @@ def train(self) -> None:
and curr_epoch == 0
and self.profiler_profile_memory
and idx == self.profiler_wait_steps + self.profiler_warmup_steps
and self._device.type == "cuda"
):
torch.cuda.memory._record_memory_history()

Expand Down Expand Up @@ -983,6 +985,19 @@ def train(self) -> None:
if self._lr_scheduler is not None:
self._lr_scheduler.step()

# Stop tracking CUDA memory now that active steps are complete
if (
self._is_rank_zero
and curr_epoch == 0
and self.profiler_profile_memory
and idx
== self.profiler_wait_steps
+ self.profiler_warmup_steps
+ self.profiler_active_steps
and self._device.type == "cuda"
):
torch.cuda.memory._record_memory_history(enabled=None)

self._steps_run += 1
if self._steps_run % self._log_every_n_steps == 0:
extra_metrics = {}
Expand All @@ -997,6 +1012,8 @@ def train(self) -> None:
)

self.cleanup_after_step(trajectory, grpo_stats)
self._profiler.step()

pbar.update(1)

if self._steps_run == self._total_steps:
Expand All @@ -1007,6 +1024,7 @@ def train(self) -> None:
if self._epochs_run % self._save_every_n_epochs == 0:
self.save_checkpoint(curr_epoch)
if training_completed:
self._profiler.stop()
Copy link
Contributor

@felipemello1 felipemello1 Mar 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont think we need this. There is already one at the end.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto - once this is removed, I can approve and merge.

Copy link
Contributor

@musabgultekin musabgultekin Mar 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One line after this, there is return, which causes self._profiler.stop() not to be executed. That is why leak was happening.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, do we need the "return"?

If "training_completed", maybe we should exit the loop and let it reach the next self._profiler.stop()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, can someone who's more familiar with how profiling works here, do a sanity check on this? I initially added this fix in my fork some time ago, which definitely stopped the leaks at the time. But when I'm running it now, there's still some leak. I'm not sure what else changed in the meantime - I'll need to check it again, but maybe it's obvious to someone else.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check it out

return

self._profiler.stop()
Expand Down