text
stringlengths 0
15.3k
|
---|
num_docs = len(doc_id_docs) |
for (doc_id, doc) in tqdm(doc_id_docs, total=num_docs): |
fewshot_ctx = self.fewshot_context(doc, 0 if self.config.num_fewshot is None else self.config.num_fewshot, system_instruction, apply_chat_template, fewshot_as_multiturn, chat_template) |
inst = self.construct_requests(doc=doc, ctx=fewshot_ctx, metadata=(self.config['task'], doc_id, self.config.repeats), apply_chat_template=apply_chat_template) |
if not isinstance(inst, list): |
inst = [inst] |
instances.append(inst) |
sliced_instances = instances[:og_limit] |
flattened_instances = [instance for instance_group in sliced_instances for instance in instance_group] |
self._instances = flattened_instances |
if len(self._instances) == 0: |
raise ValueError('task.build_requests() did not find any docs!') |
if cache_requests and (not cached_instances or rewrite_requests_cache): |
save_to_cache(file_name=cache_key, obj=instances) |
@abc.abstractmethod |
def construct_requests(self, doc, ctx, **kwargs): |
pass |
@abc.abstractmethod |
def process_results(self, doc, results): |
pass |
@abc.abstractmethod |
def aggregation(self): |
pass |
@abc.abstractmethod |
def higher_is_better(self): |
pass |
def get_config(self, key: str) -> Any: |
return getattr(self._config, key, None) |
@classmethod |
def count_bytes(cls, doc): |
return len(doc.encode('utf-8')) |
@classmethod |
def count_words(cls, doc): |
return len(re.split('\\s+', doc)) |
@utils.positional_deprecated |
def fewshot_context(self, doc, num_fewshot, rnd=None, description=None): |
if rnd is None: |
if self.fewshot_rnd is not None: |
rnd = self.fewshot_rnd |
else: |
raise ValueError('A `random.Random` generator argument must be provided to `rnd`') |
description = description if description else '' |
if num_fewshot == 0: |
labeled_examples = '' |
else: |
if self.has_training_docs(): |
fewshotex = self.fewshot_examples(k=num_fewshot, rnd=rnd) |
else: |
if self._fewshot_docs is None: |
self._fewshot_docs = list(self.validation_docs() if self.has_validation_docs() else self.test_docs()) |
fewshotex = rnd.sample(self._fewshot_docs, num_fewshot + 1) |
fewshotex = [x for x in fewshotex if x != doc][:num_fewshot] |
labeled_examples = '\n\n'.join([self.doc_to_text(doc) + self.doc_to_target(doc) for doc in fewshotex]) + '\n\n' |
example = self.doc_to_text(doc) |
return description + labeled_examples + example |
def apply_filters(self) -> Optional[List[Instance]]: |
if hasattr(self, '_filters'): |
for f in self._filters: |
f.apply(self._instances) |
else: |
eval_logger.warning('No filter defined, passing through instances') |
return self._instances |
def dump_config(self) -> dict: |
return self.config.to_dict() |
def set_config(self, key: str, value: Any, update: bool=False) -> None: |
if key is None: |
raise ValueError('Key must be provided.') |
if update: |
current_value = getattr(self._config, key, {}) |
if not isinstance(current_value, dict): |
raise TypeError(f"Expected a dict for key '{key}', got {type(current_value).__name__} instead.") |
current_value.update(value) |
else: |
setattr(self._config, key, value) |
def override_metric(self, metric_name: str) -> None: |
(self._metric_fn_list, self._aggregation_list, self._metric_fn_kwargs, self._higher_is_better) = ({}, {}, {}, {}) |
self._metric_fn_list[metric_name] = get_metric(metric_name) |
self._aggregation_list[metric_name] = get_metric_aggregation(metric_name) |
self._higher_is_better[metric_name] = is_higher_better(metric_name) |
self._metric_fn_kwargs[metric_name] = {} |
if not isinstance(self, ConfigurableTask): |
self.process_results = lambda x, y: {metric_name: get_metric(metric_name)} |
self.aggregation = lambda : {metric_name: get_metric_aggregation(metric_name)} |
setattr(self._config, 'metric_list', [{'metric': metric_name}]) |
setattr(self._config, 'process_results', None) |
def set_fewshot_seed(self, seed: Optional[int]=None) -> None: |
self.fewshot_rnd = random.Random(seed) |