Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache to metric prediction_type to speedup #801

Merged
merged 1 commit into from
May 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/unitxt/task.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import lru_cache
from typing import Any, Dict, List, Optional, Union

from .artifact import fetch_artifact
Expand Down Expand Up @@ -75,11 +76,16 @@ def verify(self):
augmentable_input in self.inputs
), f"augmentable_input {augmentable_input} is not part of {self.inputs}"

@staticmethod
@lru_cache(maxsize=None)
def get_metric_prediction_type(metric_id: str):
metric = fetch_artifact(metric_id)[0]
return metric.get_prediction_type()

def check_metrics_type(self) -> None:
prediction_type = parse_type_string(self.prediction_type)
for metric_name in self.metrics:
metric = fetch_artifact(metric_name)[0]
metric_prediction_type = metric.get_prediction_type()
for metric_id in self.metrics:
metric_prediction_type = FormTask.get_metric_prediction_type(metric_id)

if (
prediction_type == metric_prediction_type
Expand All @@ -93,7 +99,7 @@ def check_metrics_type(self) -> None:
continue

raise ValueError(
f"The task's prediction type ({prediction_type}) and '{metric_name}' "
f"The task's prediction type ({prediction_type}) and '{metric_id}' "
f"metric's prediction type ({metric_prediction_type}) are different."
)

Expand Down
Loading