evals new rebase

This commit is contained in:
Xi Yan 2024-10-10 11:35:26 -07:00
parent 89d24a07f0
commit 31c046dcdf
28 changed files with 1141 additions and 87 deletions

View file

@ -0,0 +1,13 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.
# TODO: make these import config based
from llama_stack.providers.impls.meta_reference.evals.tasks.mmlu_task import MMLUTask
from .task_registry import TaskRegistry
TaskRegistry.register(
"mmlu",
MMLUTask,
)

View file

@ -0,0 +1,49 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.
from abc import ABC, abstractmethod
class BaseTask(ABC):
"""
A task represents a single evaluation benchmark, including it's dataset, preprocessing, postprocessing and scoring methods.
Base class for all evaluation tasks. Each task needs to implement the following methods:
- F1: preprocess_sample(self)
- F2: postprocess_sample(self)
- F3: score_sample(self)
"""
def __init__(self, dataset, *args, **kwargs):
super().__init__(*args, **kwargs)
self._name = self.__class__.__name__
self.dataset = dataset
@abstractmethod
def preprocess_sample(self, sample):
raise NotImplementedError()
@abstractmethod
def postprocess_sample(self, sample):
raise NotImplementedError()
@abstractmethod
def score_sample(self, sample, ground_truth):
raise NotImplementedError()
@abstractmethod
def aggregate_results(self, eval_results):
raise NotImplementedError()
def preprocess(self):
return [self.preprocess_sample(sample) for sample in self.dataset]
def postprocess(self, generation):
return [self.postprocess_sample(sample) for sample in generation]
def score(self, postprocessed):
return [
self.score_sample(sample, ground_truth)
for sample, ground_truth in zip(postprocessed, self.dataset)
]

View file

@ -0,0 +1,32 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.
from typing import AbstractSet, Dict
from .task import BaseTask
class TaskRegistry:
_REGISTRY: Dict[str, BaseTask] = {}
@staticmethod
def names() -> AbstractSet[str]:
return TaskRegistry._REGISTRY.keys()
@staticmethod
def register(name: str, task: BaseTask) -> None:
if name in TaskRegistry._REGISTRY:
raise ValueError(f"Task {name} already exists.")
TaskRegistry._REGISTRY[name] = task
@staticmethod
def get_task(name: str) -> BaseTask:
if name not in TaskRegistry._REGISTRY:
raise ValueError(f"Task {name} not found.")
return TaskRegistry._REGISTRY[name]
@staticmethod
def reset() -> None:
TaskRegistry._REGISTRY = {}