-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a198621
commit e64be33
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import os | ||
|
||
from tqdm.auto import tqdm | ||
from mttl.evaluators.base import GenerativeEvaluator, switch_to_eval_mode | ||
|
||
|
||
class GsmEvaluator(GenerativeEvaluator): | ||
def __init__( | ||
self, | ||
datamodule, | ||
use_vllm=False, | ||
generation_kwargs=None, | ||
prepend_source=True, | ||
split="test", | ||
): | ||
super().__init__( | ||
datamodule=datamodule, | ||
use_vllm=use_vllm, | ||
generation_kwargs=generation_kwargs, | ||
) | ||
|
||
self.split = split | ||
self.prepend_source = prepend_source | ||
os.environ["HF_ALLOW_CODE_EVAL"] = "1" | ||
|
||
@switch_to_eval_mode | ||
def evaluate( | ||
self, | ||
model, | ||
split=None, | ||
subsample=-1, | ||
num_batches=None, | ||
verbose=True, | ||
shuffle=False, | ||
output_path=None, | ||
): | ||
dataloader = self.get_dataloader(split, subsample, shuffle=shuffle) | ||
|
||
pbar = tqdm( | ||
enumerate(dataloader), | ||
total=len(dataloader), | ||
) | ||
|
||
all_predictions = [] | ||
all_targets = [] | ||
|
||
for num_batch, batch in pbar: | ||
predictions = self.generate_for_batch(model, batch) | ||
|
||
all_predictions.extend(predictions) | ||
all_targets.extend(batch["target"]) | ||
breakpoint() | ||
metrics = self.compute_metrics(all_predictions, all_targets) | ||
return metrics | ||
|
||
def compute_metrics(self, predictions, targets): | ||
# compute the accuracy | ||
correct = 0 | ||
for pred, target in zip(predictions, targets): | ||
if pred == target: | ||
correct += 1 | ||
accuracy = correct / len(predictions) | ||
return accuracy |