Skip to content

Commit

Permalink
Add method to cleanup resources
Browse files Browse the repository at this point in the history
  • Loading branch information
shankarg87 committed Jul 31, 2024
1 parent a7a1bf5 commit eca377e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/aihero/research/finetuning/infer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module to run batch inference jobs."""
import gc
import os
from pathlib import Path
from tempfile import TemporaryDirectory
Expand Down Expand Up @@ -233,6 +234,16 @@ def run(self) -> None:
print("Save and Uploading model..")
finish()

def cleanup(self) -> None:
"""Clean up memory useage."""
del self.model
del self.tokenizer
del self.dataset_dict
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
gc.collect()


class BatchInferenceWithEval:
"""Batch inference class for generating predictions and running custom tests and metrics."""
Expand Down
11 changes: 11 additions & 0 deletions src/aihero/research/finetuning/train.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Launch the training job inside a container."""
import gc
import os
import time
import traceback
Expand Down Expand Up @@ -428,3 +429,13 @@ def run(self) -> None:
print("Saving model..")
self.save_model()
finish()

def cleanup(self) -> None:
"""Clean up memory useage."""
del self.model
del self.tokenizer
del self.dataset_dict
if torch.cuda.is_available():
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
gc.collect()

0 comments on commit eca377e

Please sign in to comment.