Skip to content

Commit

Permalink
Return metrics to origin
Browse files Browse the repository at this point in the history
Signed-off-by: elronbandel <[email protected]>
  • Loading branch information
elronbandel committed Feb 16, 2025
1 parent 3668f23 commit 240d630
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 39 deletions.
58 changes: 21 additions & 37 deletions src/unitxt/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import numpy as np
import pandas as pd
import requests
from datasets import DownloadConfig
from scipy.stats import bootstrap
from scipy.stats._warnings_errors import DegenerateDataWarning

Expand Down Expand Up @@ -75,20 +74,6 @@

warnings.filterwarnings("ignore", category=DegenerateDataWarning)

def hf_evaluate_load(*args, **kwargs):
return evaluate.load(
*args,
**kwargs,
experiment_id=str(uuid.uuid4()),
cache_dir=settings.local_cache,
download_config=DownloadConfig(
cache_dir=settings.local_cache,
max_retries=settings.loaders_max_retries,
),
verification_mode="no_checks",
trust_remote_code=settings.allow_unverified_code,
download_mode= "force_redownload" if settings.disable_hf_datasets_cache else "reuse_dataset_if_exists"
)

class MetricsList(ListCollection):
def verify(self):
Expand Down Expand Up @@ -2325,7 +2310,9 @@ def verify(self):
def prepare(self):
super().prepare()

self.metric = hf_evaluate_load(self.hf_metric_name)
self.metric = evaluate.load(
self.hf_metric_name, experiment_id=str(uuid.uuid4())
)

def compute(
self,
Expand Down Expand Up @@ -2400,7 +2387,9 @@ class HuggingfaceBulkMetric(BulkInstanceMetric):
def prepare(self):
super().prepare()

self.metric = hf_evaluate_load(self.hf_metric_name)
self.metric = evaluate.load(
self.hf_metric_name, experiment_id=str(uuid.uuid4())
)

def compute(
self,
Expand Down Expand Up @@ -2445,7 +2434,9 @@ class HuggingfaceInstanceMetric(InstanceMetric):
def prepare(self):
super().prepare()

self.metric = hf_evaluate_load(self.hf_metric_name)
self.metric = evaluate.load(
self.hf_metric_name, experiment_id=str(uuid.uuid4())
)

def compute(self, references: List[Any], prediction: Any, task_data: Dict) -> dict:
# invokes module.compute, which invokes, e.g., meteor's _compute
Expand Down Expand Up @@ -2549,7 +2540,7 @@ class F1(GlobalMetric):
def prepare(self):
super().prepare()

self._metric = hf_evaluate_load(self.metric)
self._metric = evaluate.load(self.metric, experiment_id=str(uuid.uuid4()))

def get_str_id(self, str):
if str not in self.str_to_id:
Expand Down Expand Up @@ -2826,8 +2817,8 @@ class F1MultiLabel(GlobalMetric, PackageRequirementsMixin):
def prepare(self):
super().prepare()

self._metric = hf_evaluate_load(
self.metric, "multilabel"
self._metric = evaluate.load(
self.metric, "multilabel", experiment_id=str(uuid.uuid4())
)

def add_str_to_id(self, str):
Expand Down Expand Up @@ -3584,10 +3575,7 @@ def map_stream(

if self.model is None:
self.model = pipeline(
"text-classification",
model=self.model_name,
device=self.get_device(),
model_kwargs= {"cache_dir": settings.local_cache},
"text-classification", model=self.model_name, device=self.get_device()
)

inputs = []
Expand Down Expand Up @@ -3620,9 +3608,7 @@ def prepare(self):

device = "cuda:0" if torch.cuda.is_available() else "cpu"
self.pipe = pipeline(
"text-classification", model=self.model_name, device=device,
model_kwargs= {"cache_dir": settings.local_cache},

"text-classification", model=self.model_name, device=device
)

def compute(
Expand Down Expand Up @@ -3655,10 +3641,9 @@ def prepare(self):
from transformers import AutoModelForSequenceClassification, AutoTokenizer

self.regard_model = AutoModelForSequenceClassification.from_pretrained(
self.model_name, cache_dir=settings.local_cache,

self.model_name
)
self.regard_tokenizer = AutoTokenizer.from_pretrained(self.model_name, cache_dir=settings.local_cache)
self.regard_tokenizer = AutoTokenizer.from_pretrained(self.model_name)

def _evaluate(self, predictions, inputs):
import torch
Expand Down Expand Up @@ -3846,7 +3831,6 @@ def prepare(self):
"text-classification",
model=self.reward_name,
device=self.get_device(),
model_kwargs= {"cache_dir": settings.local_cache},
)


Expand Down Expand Up @@ -4029,7 +4013,7 @@ def compute(
if self.lm is None:
from transformers import AutoConfig

config = AutoConfig.from_pretrained(self.model_name, trust_remote_code=True, cache_dir=settings.local_cache)
config = AutoConfig.from_pretrained(self.model_name, trust_remote_code=True)
self.lm = (
self.EncoderDecoderLM(
model_name=self.model_name, single_token_mode=self.single_token_mode
Expand Down Expand Up @@ -4108,9 +4092,9 @@ def __init__(self, model_name, single_token_mode):
self.model_name = model_name
self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
self.model = (
self.model_class().from_pretrained(self.model_name, cache_dir=settings.local_cache).to(self.device)
self.model_class().from_pretrained(self.model_name).to(self.device)
)
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name, cache_dir=settings.local_cache)
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
if self.tokenizer.pad_token_id is None:
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
self.single_token_mode = single_token_mode
Expand Down Expand Up @@ -4294,7 +4278,7 @@ def prepare(self):
from transformers import AutoModelForSequenceClassification

self.model = AutoModelForSequenceClassification.from_pretrained(
self.model_name, trust_remote_code=True, cache_dir=settings.local_cache
self.model_name, trust_remote_code=True
).to(device)

def compute(
Expand Down Expand Up @@ -5975,7 +5959,7 @@ def compute(self, references: List[Any], prediction: Any, task_data: Dict) -> di
self.verify_granite_guardian_config(task_data)
self.set_main_score()
if not hasattr(self, "_tokenizer") or self._tokenizer is None:
self._tokenizer = AutoTokenizer.from_pretrained(self.hf_model_name, cache_dir=settings.local_cache)
self._tokenizer = AutoTokenizer.from_pretrained(self.hf_model_name)
if self.inference_engine is None:
self.inference_engine = WMLInferenceEngineGeneration(
model_name=self.wml_model_name,
Expand Down
4 changes: 2 additions & 2 deletions utils/.secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
"filename": "src/unitxt/metrics.py",
"hashed_secret": "fa172616e9af3d2a24b5597f264eab963fe76889",
"is_verified": false,
"line_number": 71
"line_number": 70
}
],
"tests/library/test_loaders.py": [
Expand All @@ -178,5 +178,5 @@
}
]
},
"generated_at": "2025-02-16T13:45:53Z"
"generated_at": "2025-02-16T14:55:15Z"
}

0 comments on commit 240d630

Please sign in to comment.