Skip to content

Commit

Permalink
Add cache to metric prediction_type to speedup (#801)
Browse files Browse the repository at this point in the history
loading of tasks.

Signed-off-by: Yoav Katz <katz@il.ibm.com>
  • Loading branch information
yoavkatz authored May 6, 2024
1 parent 6ce2567 commit 72803c2
Showing 1 changed file with 10 additions and 4 deletions.
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

0 comments on commit 72803c2

Please sign in to comment.