text
stringlengths
0
15.3k
except KeyError:
eval_logger.warning(f'{name} metric is not assigned a default aggregation!')
def is_higher_better(metric_name) -> bool:
try:
return HIGHER_IS_BETTER_REGISTRY[metric_name]
except KeyError:
eval_logger.warning(f"higher_is_better not specified for metric '{metric_name}'!")
def register_filter(name):
def decorate(cls):
if name in FILTER_REGISTRY:
eval_logger.info(f'Registering filter `{name}` that is already in Registry {FILTER_REGISTRY}')
FILTER_REGISTRY[name] = cls
return cls
return decorate
def get_filter(filter_name: str) -> type:
try:
return FILTER_REGISTRY[filter_name]
except KeyError:
eval_logger.warning(f'filter `{filter_name}` is not registered!')
# File: lm-evaluation-harness-main/lm_eval/api/samplers.py
import datasets
class ContextSampler:
def __init__(self, docs, task, fewshot_indices=None, rnd=None) -> None:
self.rnd = rnd
if not self.rnd:
raise ValueError('A `random.Random` generator argument must be provided to `rnd` of FewShotSampler!')
self.task = task
self.config = task._config
self.target_delimiter = self.config.target_delimiter
self.fewshot_delimiter = self.config.fewshot_delimiter
self.doc_to_text = self.task.doc_to_text
self.doc_to_target = self.task.doc_to_target
self.doc_to_choice = self.task.doc_to_choice
self.docs = docs
if fewshot_indices:
if not isinstance(self.docs, datasets.Dataset):
raise ValueError("Got `fewshot_indices` but fewshot_docs are not a HF dataset. Don't use both `fewshot_indices` and a user-defined few-shot sample list simultaneously")
self.docs = self.docs.select(fewshot_indices)
def get_context(self, doc, num_fewshot):
n_samples = num_fewshot + 1 if self.config.fewshot_split == self.config.test_split else num_fewshot
fewshotex = self.sample(n_samples)
selected_docs = [x for x in fewshotex if x != doc][:num_fewshot]
labeled_examples = ''
for doc in selected_docs:
doc_content = self.doc_to_text(doc)
doc_target = self.doc_to_target(doc)
labeled_examples += doc_content if self.config.doc_to_choice is None or isinstance(doc_content, str) else self.doc_to_choice(doc)[doc_content]
labeled_examples += self.target_delimiter
labeled_examples += str(doc_target[0]) if isinstance(doc_target, list) else str(doc_target) if self.config.doc_to_choice is None or isinstance(doc_target, str) else str(self.doc_to_choice(doc)[doc_target])
labeled_examples += self.fewshot_delimiter
return labeled_examples
def get_chat_context(self, doc, num_fewshot, fewshot_as_multiturn: bool=False):
chat_history = []
n_samples = num_fewshot + 1 if self.config.fewshot_split == self.config.test_split else num_fewshot
fewshotex = self.sample(n_samples)
selected_docs = [x for x in fewshotex if x != doc][:num_fewshot]
if fewshot_as_multiturn:
for doc in selected_docs:
doc_content = self.doc_to_text(doc)
doc_target = self.doc_to_target(doc)
chat_history.append({'role': 'user', 'content': doc_content if self.config.doc_to_choice is None or isinstance(doc_content, str) else self.doc_to_choice(doc)[doc_content]})
chat_history.append({'role': 'assistant', 'content': str(doc_target[0]) if isinstance(doc_target, list) else doc_target if self.config.doc_to_choice is None or isinstance(doc_target, str) else str(self.doc_to_choice(doc)[doc_target])})
else:
chat_history.append({'role': 'user', 'content': self.get_context(doc, num_fewshot)})
return chat_history
def sample(self, n):
return self.rnd.sample(self.docs, n)
class FirstNSampler(ContextSampler):
def sample(self, n) -> None:
assert n <= len(self.docs), f'Error: number of fewshot samples requested exceeds the {len(self.docs)} that are available.'
return self.docs[:n]
class BalancedSampler(ContextSampler):
def sample(self, n) -> None:
pass
class ManualSampler(ContextSampler):
def sample(self, n) -> None:
""""""
pass
SAMPLER_REGISTRY = {'default': ContextSampler, 'first_n': FirstNSampler}
def get_sampler(name):
try:
return SAMPLER_REGISTRY[name]
except KeyError: