fix: make batch size caching in benchmark scorer actually work

This commit is contained in:
Philipp Emanuel Weidmann 2026-11-06 14:51:15 +05:30
parent 3dbc48156d
commit 7ac0e0e6a1
No known key found for this signature in database
GPG Key ID: CE6A7756AD520E0F
2 changed files with 23 additions and 17 deletions

View File

@ -292,6 +292,9 @@ task = "piqa"
# Task metric to use as the benchmark score.
metric = "acc_norm,none"
# Number of input sequences to process in parallel (0 = auto).
batch_size = 0 # auto
[scorer.ResponseLength]
# Whether to strip leading and trailing whitespace from responses before measuring their length.

View File

@ -3,7 +3,7 @@
import lm_eval
from lm_eval.models.huggingface import HFLM
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, NonNegativeInt
from heretic.scorer import Context, Score, Scorer
@ -24,6 +24,11 @@ class Settings(BaseModel):
description="Task metric to use as the benchmark score.",
)
batch_size: NonNegativeInt = Field(
default=0, # auto
description="Number of input sequences to process in parallel (0 = auto).",
)
class BenchmarkScore(Scorer):
"""
@ -40,29 +45,27 @@ class BenchmarkScore(Scorer):
def score_name(self) -> str:
return self.settings.score_name
def init(self, ctx: Context) -> None:
def get_score(self, ctx: Context) -> Score:
model = ctx.get_model()
self.hflm = HFLM(
hflm = HFLM(
pretrained=model.model,
tokenizer=model.tokenizer, # ty: ignore[invalid-argument-type]
batch_size="auto",
batch_size="auto"
if self.settings.batch_size == 0
else self.settings.batch_size,
)
def get_score(self, ctx: Context) -> Score:
# The purpose of this hack, where we initialize the HFLM object once,
# then update its internal model every time we calculate the score,
# is to get the benefits of batch size caching while allowing for
# model reloads, e.g. when using --evaluate-model.
model = ctx.get_model()
self.hflm.pretrained = model.model
self.hflm._model = model.model
results = lm_eval.simple_evaluate(
model=self.hflm,
model=hflm,
tasks=[self.settings.task],
)
if self.settings.batch_size == 0 and hflm.batch_sizes:
# Cache automatically determined batch size to avoid re-determining
# the batch size on every future run.
self.settings.batch_size = min(hflm.batch_sizes.values())
benchmark_score = float(
results["results"][self.settings.task][self.settings.metric]
)