Skip to content

Commit

Permalink
Merge pull request #93 from huridocs/gpu-oom
Browse files Browse the repository at this point in the history
Fix CUDA out of memory
  • Loading branch information
ali6parmak authored Oct 7, 2024
2 parents 83bda69 + ddfa964 commit 4d8f28e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import os
import shutil
from os.path import join, exists

import pandas as pd
import torch
from datasets import load_dataset

from data.ExtractionData import ExtractionData
from data.Option import Option
from setfit import SetFitModel, TrainingArguments, Trainer

from extractors.ExtractorBase import ExtractorBase
from extractors.bert_method_scripts.AvoidAllEvaluation import AvoidAllEvaluation
from extractors.bert_method_scripts.EarlyStoppingAfterInitialTraining import EarlyStoppingAfterInitialTraining
from extractors.bert_method_scripts.get_batch_size import get_batch_size, get_max_steps
from extractors.pdf_to_multi_option_extractor.MultiLabelMethod import MultiLabelMethod
from send_logs import send_logs

import gc


Expand Down Expand Up @@ -104,17 +100,17 @@ def train(self, extraction_data: ExtractionData):

del model
del trainer
torch.cuda.empty_cache()
gc.collect()
torch.cuda.empty_cache()

def predict(self, multi_option_data: ExtractionData) -> list[list[Option]]:
model = SetFitModel.from_pretrained(self.get_model_path(), trust_remote_code=True)
predict_texts = [sample.pdf_data.get_text() for sample in multi_option_data.samples]
predictions = model.predict(predict_texts)

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

return self.predictions_to_options_list(predictions.tolist())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ def train(self, extraction_data: ExtractionData):

del model
del trainer
torch.cuda.empty_cache()
gc.collect()
torch.cuda.empty_cache()

def predict(self, multi_option_data: ExtractionData) -> list[list[Option]]:
model = SetFitModel.from_pretrained(self.get_model_path(), trust_remote_code=True)
predict_texts = [sample.pdf_data.get_text() for sample in multi_option_data.samples]
predictions = model.predict(predict_texts)

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

return [[option for option in self.options if option.label == prediction] for prediction in predictions]

0 comments on commit 4d8f28e

Please sign in to comment.