Skip to content

Commit

Permalink
Merge branch 'add-custom-model' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelNiklaus committed Dec 19, 2024
2 parents a1b0d6b + f1ba65c commit 78f7a4e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions examples/custom_models/local_mt_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,32 @@ def get_langs(task_name: str) -> tuple[str, str]:

return dataset.get_original_order(results)

def cleanup(self):
import gc

logger.info("Cleaning up GPU memory for local MT client.")

# Show GPU memory before cleanup
if torch.cuda.is_available():
logger.info(f"GPU memory before cleanup: {torch.cuda.memory_allocated() / 1024**2:.2f} MB")

# Delete model and move to CPU
if hasattr(self, "_model"):
self._model.cpu()
del self._model
self._model = None

if hasattr(self, "_tokenizer"):
del self._tokenizer
self._tokenizer = None

torch.cuda.empty_cache()
gc.collect()

# Show GPU memory after cleanup
if torch.cuda.is_available():
logger.info(f"GPU memory after cleanup: {torch.cuda.memory_allocated() / 1024**2:.2f} MB")

@property
def tokenizer(self):
return self._tokenizer
Expand Down

0 comments on commit 78f7a4e

Please sign in to comment.