Skip to content

Commit 769a575

Browse files
committed
Fix failure reported in huggingface#1005 (from Pull Request huggingface#1006)
1 parent 1deed74 commit 769a575

6 files changed

Lines changed: 40 additions & 19 deletions

File tree

src/lighteval/logging/evaluation_tracker.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,15 @@ class EnhancedJSONEncoder(json.JSONEncoder):
6363
Notably manages the json encoding of dataclasses.
6464
"""
6565

66-
def default(self, o):
66+
def default(self, o): # noqa : C901
6767
if is_dataclass(o):
6868
try:
6969
return asdict(o) # type: ignore
7070
except Exception:
71-
return str(o)
71+
try:
72+
return o.__dict__
73+
except Exception:
74+
return str(o)
7275
if callable(o):
7376
if hasattr(o, "__name__"):
7477
return o.__name__

src/lighteval/metrics/metrics_sample.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ def compute(self, responses: list[ModelResponse], docs: list[Doc], **kwargs) ->
10591059
questions = [formatted_doc.query for formatted_doc in docs]
10601060
options = [formatted_doc.choices for formatted_doc in docs]
10611061
golds = [formatted_doc.get_golds()[0] for formatted_doc in docs]
1062-
predictions = [response.text[0] for response in responses]
1062+
predictions = [response.final_text[0] for response in responses]
10631063

10641064
scores, messages, judgements = self.judge.evaluate_answer_batch(questions, predictions, options, golds)
10651065

@@ -1077,18 +1077,21 @@ def compute(self, responses: list[ModelResponse], docs: list[Doc], **kwargs) ->
10771077

10781078

10791079
class JudgeLLMMTBench(JudgeLLM):
1080-
def compute(self, model_response: list[ModelResponse], docs: list[Doc], **kwargs):
1080+
def compute(self, model_response: list[ModelResponse], doc: list[Doc], **kwargs):
10811081
"""Compute the score of a generative task using a llm as a judge.
10821082
The generative task can be multiturn with 2 turns max, in that case, we
10831083
return scores for turn 1 and 2. Also returns user_prompt and judgement
10841084
which are ignored later by the aggregator.
10851085
"""
10861086
import json
10871087

1088+
model_responses = as_list(model_response)
1089+
docs = as_list(doc)
1090+
10881091
# If we are evaluating a multiturn task, we need to have specific field in the formatted doc
10891092
questions = [doc.specific["multi_turn_queries"] for doc in docs]
10901093
golds = [doc.specific.get("reference", None) for doc in docs]
1091-
predictions = [response.text[0] for response in model_response]
1094+
predictions = [response.final_text[0] for response in model_responses]
10921095

10931096
query_context_1 = {"query": questions[0], "context": ""}
10941097
query_context_2 = {"query": questions[1], "context": predictions[0]}
@@ -1109,7 +1112,7 @@ def compute(self, model_response: list[ModelResponse], docs: list[Doc], **kwargs
11091112

11101113

11111114
class JudgeLLMMixEval(JudgeLLM):
1112-
def compute(self, model_responses: list[ModelResponse], docs: list[Doc], **kwargs):
1115+
def compute(self, responses: list[ModelResponse], docs: list[Doc], **kwargs):
11131116
"""Compute the score of a generative task using a llm as a judge.
11141117
The generative task can be multiturn with 2 turns max, in that case, we
11151118
return scores for turn 1 and 2. Also returns user_prompt and judgement
@@ -1118,7 +1121,7 @@ def compute(self, model_responses: list[ModelResponse], docs: list[Doc], **kwarg
11181121
questions = [doc.specific["question"] for doc in docs]
11191122
options = [doc.choices for doc in docs]
11201123
golds = [doc.get_golds()[0] for doc in docs]
1121-
predictions = [response.text[0] for response in model_responses]
1124+
predictions = [response.final_text[0] for response in responses]
11221125

11231126
scores, messages, judgements = self.judge.evaluate_answer_batch(questions, predictions, options, golds)
11241127

@@ -1127,8 +1130,8 @@ def compute(self, model_responses: list[ModelResponse], docs: list[Doc], **kwarg
11271130
metrics.append(
11281131
{
11291132
f"judge_score_{self.short_judge_name}": scores[i],
1130-
f"user_prompt_{self.short_judge_name}": messages[i],
1131-
f"judgement_{self.short_judge_name}": judgements[i],
1133+
# f"user_prompt_{self.short_judge_name}": messages[i],
1134+
# f"judgement_{self.short_judge_name}": judgements[i],
11321135
}
11331136
)
11341137

src/lighteval/metrics/utils/llm_as_judge.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def __lazy_load_client(self): # noqa: C901
172172

173173
self.sampling_params = SamplingParams(temperature=0.8, top_p=0.95, max_tokens=self.max_tokens)
174174
self.tokenizer = get_tokenizer(self.model, tokenizer_mode="auto")
175-
self.pipe = LLM(model=self.model, max_model_len=2048, gpu_memory_utilization=0.5, dtype="float16")
175+
self.pipe = LLM(model=self.model, gpu_memory_utilization=0.8, dtype="float16")
176176
return self.__call_vllm
177177

178178
case "transformers":
@@ -300,7 +300,7 @@ def __call_vllm(self, prompt):
300300
outputs = [output.outputs[0].text for output in output]
301301
return outputs
302302

303-
def __call_litellm(self, prompts):
303+
def __call_litellm(self, prompts): # noqa: C901
304304
import litellm
305305

306306
if self.backend_options.caching:
@@ -324,10 +324,11 @@ def __call_api(prompt):
324324
kwargs = {
325325
"model": self.model,
326326
"messages": prompt,
327-
"max_tokens": max_new_tokens,
328327
"n": 1,
329328
"caching": True,
330329
}
330+
if max_new_tokens is not None:
331+
kwargs["max_tokens"] = (max_new_tokens,)
331332

332333
response = litellm.completion(**kwargs)
333334
text = response.choices[0].message.content
@@ -412,7 +413,7 @@ def __call_api(self, prompt):
412413
model=self.model,
413414
messages=as_list(prompt),
414415
response_format=self.response_format,
415-
max_tokens=4096,
416+
max_tokens=self.max_tokens,
416417
temperature=0.0,
417418
n=1,
418419
)
@@ -425,7 +426,7 @@ def __call_api(self, prompt):
425426
model=self.model,
426427
messages=as_list(prompt),
427428
response_format=self.response_format,
428-
max_tokens=512,
429+
max_tokens=self.max_tokens,
429430
n=1,
430431
)
431432
text = response.choices[0].message.content
@@ -438,3 +439,6 @@ def __call_api(self, prompt):
438439
time.sleep(self.API_RETRY_SLEEP)
439440

440441
raise Exception("Failed to get response from the API")
442+
443+
def __str__(self) -> str:
444+
return f"Model: {self.model}, Judge Backend: {self.backend}, URL: {self.url}"

src/lighteval/tasks/extended/mix_eval/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ def process_judge_response_freeform_gpt(x):
115115
corpus_level_fn={
116116
"judge_score_flow": np.mean,
117117
},
118+
batched_compute=True,
118119
)
119120

120121
llm_judge_mixeval_multichoice_gpt_judge = SampleLevelMetricGrouping(
@@ -131,6 +132,7 @@ def process_judge_response_freeform_gpt(x):
131132
corpus_level_fn={
132133
"judge_score_gpt-3.5": np.mean,
133134
},
135+
batched_compute=True,
134136
)
135137

136138

@@ -152,6 +154,7 @@ def mean_dv_5(x):
152154
corpus_level_fn={
153155
"judge_score_flow": mean_dv_5,
154156
},
157+
batched_compute=True,
155158
)
156159

157160
llm_judge_mixeval_freeform_gpt_judge = SampleLevelMetricGrouping(
@@ -168,6 +171,7 @@ def mean_dv_5(x):
168171
corpus_level_fn={
169172
"judge_score_gpt-3.5": np.mean,
170173
},
174+
batched_compute=True,
171175
)
172176

173177

src/lighteval/tasks/lighteval_task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def _get_docs_from_split(self, splits: list[str], few_shots=False) -> list[Doc]:
301301
doc = self.formatter(item, self.name)
302302

303303
# Skip if formatter returns None (e.g., to filter out certain samples)
304-
if doc is None:
304+
if doc is None or doc == []:
305305
continue
306306

307307
doc.id = str(ix)

src/lighteval/utils/cache_management.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ def __init__(self, model_config: ModelConfig):
9292
self.registry = None
9393

9494
self.existing_indices = self._load_cached_indices()
95+
# Caching the task_hashes to avoid grabbing the registry all the time
96+
self._task_hashes = {}
9597

9698
def _init_registry(self, registry: Registry):
9799
self.registry = registry
@@ -163,10 +165,15 @@ def _get_task_hash(self, full_task_name: str) -> str:
163165
"The task registry was not provided to the cache config. We can't test if the current task has the same hash as the saved tasks."
164166
)
165167
return "NO_HASH"
166-
task_suite, task_name, _ = full_task_name.split("|")
167-
task_configs: list[LightevalTaskConfig] = sorted(self.registry.task_to_configs[f"{task_suite}|{task_name}"])
168-
config_str = "|".join([task_config.__str__(lite=True) for task_config in task_configs])
169-
return hashlib.sha256(config_str.encode()).hexdigest()[:16]
168+
if full_task_name not in self._task_hashes:
169+
task_suite, task_name, _ = full_task_name.split("|")
170+
task_configs: list[LightevalTaskConfig] = sorted(
171+
self.registry.task_to_configs[f"{task_suite}|{task_name}"]
172+
)
173+
config_str = "|".join([task_config.__str__(lite=True) for task_config in task_configs])
174+
task_hash = hashlib.sha256(config_str.encode()).hexdigest()[:16]
175+
self._task_hashes[full_task_name] = task_hash
176+
return self._task_hashes[full_task_name]
170177

171178
def get_cache_path(self, task_id: TaskID) -> Path:
172179
"""Get the file path for a specific task's cache file.

0 commit comments

Comments
 (0)