Skip to content

Commit

Permalink
Release GPU memory after Optuna trial (#35440)
Browse files Browse the repository at this point in the history
* Release GPU memory after trial

* Update to use release_memory from accelerate.utils.memory after suggestion
  • Loading branch information
JanetVictorious authored Jan 7, 2025
1 parent 665a494 commit 628cd83
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/transformers/integrations/integration_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ def hp_params(trial):

def run_hp_search_optuna(trainer, n_trials: int, direction: str, **kwargs) -> BestRun:
import optuna
from accelerate.utils.memory import release_memory

if trainer.args.process_index == 0:

Expand All @@ -251,6 +252,11 @@ def _objective(trial: optuna.Trial, checkpoint_dir=None):
if getattr(trainer, "objective", None) is None:
metrics = trainer.evaluate()
trainer.objective = trainer.compute_objective(metrics)

# Free GPU memory
trainer.model_wrapped, trainer.model = release_memory(trainer.model_wrapped, trainer.model)
trainer.accelerator.clear()

return trainer.objective

timeout = kwargs.pop("timeout", None)
Expand Down

0 comments on commit 628cd83

Please sign in to comment.