Skip to content
Open
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions tests/metrics/test_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

import subprocess
import sys


def test_metrics_import_without_torchscript_deprecation():
result = subprocess.run(
[
sys.executable,
"-c",
"import torch, warnings\n"
'warnings.filterwarnings("error", message=r"`torch\\.jit\\.script` is deprecated")\n'
"import torcheval.metrics",
],
capture_output=True,
text=True,
)
assert result.returncode == 0, result.stderr
5 changes: 4 additions & 1 deletion torcheval/metrics/classification/accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import TypeVar

import torch
from torcheval.metrics.functional._jit import _script_on_use
from torcheval.metrics.functional.classification.accuracy import (
_accuracy_compute,
_accuracy_param_check,
Expand Down Expand Up @@ -139,7 +140,9 @@ def compute(self: TAccuracy) -> torch.Tensor:

NaN is returned if no calls to ``update()`` are made before ``compute()`` is called.
"""
return _accuracy_compute(self.num_correct, self.num_total, self.average)
return _script_on_use(_accuracy_compute)(
self.num_correct, self.num_total, self.average
)

@torch.inference_mode()
def merge_state(self: TAccuracy, metrics: Iterable[TAccuracy]) -> TAccuracy:
Expand Down
3 changes: 2 additions & 1 deletion torcheval/metrics/classification/auroc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import TypeVar

import torch
from torcheval.metrics.functional._jit import _script_on_use
from torcheval.metrics.functional.classification.auroc import (
_binary_auroc_compute,
_binary_auroc_update_input_check,
Expand Down Expand Up @@ -259,7 +260,7 @@ def compute(
"No samples to compute auroc with. Did you call update?"
)

return _multiclass_auroc_compute(
return _script_on_use(_multiclass_auroc_compute)(
torch.cat(self.inputs),
torch.cat(self.targets),
self.num_classes,
Expand Down
13 changes: 10 additions & 3 deletions torcheval/metrics/classification/binned_auprc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import TypeVar

import torch
from torcheval.metrics.functional._jit import _script_on_use
from torcheval.metrics.functional.classification.binned_auprc import (
_binary_binned_auprc_param_check,
_binary_binned_auprc_update_input_check,
Expand Down Expand Up @@ -134,7 +135,9 @@ def update(
target = target[None, :]

for i in range(self.num_tasks):
num_tp, num_fp, num_fn = _update(input[i], target[i], self.threshold)
num_tp, num_fp, num_fn = _script_on_use(_update)(
input[i], target[i], self.threshold
)
self.num_tp[i] += num_tp.to(self.device)
self.num_fp[i] += num_fp.to(self.device)
self.num_fn[i] += num_fn.to(self.device)
Expand All @@ -156,7 +159,9 @@ def compute(

result = []
for i in range(self.num_tasks):
prec, recall, thresh = _binary_binned_precision_recall_curve_compute(
prec, recall, thresh = _script_on_use(
_binary_binned_precision_recall_curve_compute
)(
self.num_tp[i],
self.num_fp[i],
self.num_fn[i],
Expand Down Expand Up @@ -457,7 +462,9 @@ def compute(
Returns:
- Binned_AUPRC (Tensor): The return value of Binned_AUPRC for each task (num_tasks,).
"""
prec, recall, thresh = _multilabel_binned_precision_recall_curve_compute(
prec, recall, thresh = _script_on_use(
_multilabel_binned_precision_recall_curve_compute
)(
self.num_tp,
self.num_fp,
self.num_fn,
Expand Down
5 changes: 3 additions & 2 deletions torcheval/metrics/classification/binned_auroc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import TypeVar

import torch
from torcheval.metrics.functional._jit import _script_on_use
from torcheval.metrics.functional.classification.binned_auroc import (
_binary_binned_auroc_compute,
_binary_binned_auroc_param_check,
Expand Down Expand Up @@ -128,7 +129,7 @@ def compute(
- Binned_AUROC (Tensor): The return value of Binned_AUROC for each task (num_tasks,).
- threshold (Tensor): Tensor of threshold. Its shape is (n_thresholds, ).
"""
return _binary_binned_auroc_compute(
return _script_on_use(_binary_binned_auroc_compute)(
torch.cat(self.inputs, -1), torch.cat(self.targets, -1), self.threshold
)

Expand Down Expand Up @@ -232,7 +233,7 @@ def update(
def compute(
self: TMulticlassBinnedAUROC,
) -> tuple[torch.Tensor, torch.Tensor]:
return _multiclass_binned_auroc_compute(
return _script_on_use(_multiclass_binned_auroc_compute)(
torch.cat(self.inputs),
torch.cat(self.targets),
self.num_classes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import TypeVar

import torch
from torcheval.metrics.functional._jit import _script_on_use
from torcheval.metrics.functional.classification.binned_precision_recall_curve import (
_binary_binned_precision_recall_curve_compute,
_binary_binned_precision_recall_curve_update,
Expand Down Expand Up @@ -122,7 +123,7 @@ def compute(
- recall (Tensor): Tensor of recall result. Its shape is (n_thresholds + 1, )
- thresholds (Tensor): Tensor of threshold. Its shape is (n_thresholds, )
"""
return _binary_binned_precision_recall_curve_compute(
return _script_on_use(_binary_binned_precision_recall_curve_compute)(
self.num_tp, self.num_fp, self.num_fn, self.threshold
)

Expand Down Expand Up @@ -398,7 +399,7 @@ def compute(
recall: Tensor of recall result. Its shape is (n_thresholds + 1, )
thresholds: Tensor of threshold. Its shape is (n_thresholds, )
"""
return _multilabel_binned_precision_recall_curve_compute(
return _script_on_use(_multilabel_binned_precision_recall_curve_compute)(
self.num_tp,
self.num_fp,
self.num_fn,
Expand Down
5 changes: 3 additions & 2 deletions torcheval/metrics/classification/precision_recall_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import TypeVar

import torch
from torcheval.metrics.functional._jit import _script_on_use
from torcheval.metrics.functional.classification.precision_recall_curve import (
_binary_precision_recall_curve_compute,
_binary_precision_recall_curve_update,
Expand Down Expand Up @@ -211,7 +212,7 @@ def compute(
- thresholds: List of threshold. Each index indicates the result of a class.

"""
return _multiclass_precision_recall_curve_compute(
return _script_on_use(_multiclass_precision_recall_curve_compute)(
torch.cat(self.inputs), torch.cat(self.targets), self.num_classes
)

Expand Down Expand Up @@ -318,7 +319,7 @@ def compute(
- recall: List of recall result. Each index indicates the result of a label.
- thresholds: List of threshold. Each index indicates the result of a label.
"""
return _multilabel_precision_recall_curve_compute(
return _script_on_use(_multilabel_precision_recall_curve_compute)(
torch.cat(self.inputs), torch.cat(self.targets), self.num_labels
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import TypeVar

import torch
from torcheval.metrics.functional._jit import _script_on_use
from torcheval.metrics.functional.classification.recall_at_fixed_precision import (
_binary_recall_at_fixed_precision_compute,
_binary_recall_at_fixed_precision_update_input_check,
Expand Down Expand Up @@ -204,7 +205,7 @@ def update(
def compute(
self: TMultilabelRecallAtFixedPrecision,
) -> tuple[list[torch.Tensor], list[torch.Tensor]]:
return _multilabel_recall_at_fixed_precision_compute(
return _script_on_use(_multilabel_recall_at_fixed_precision_compute)(
torch.cat(self.inputs),
torch.cat(self.targets),
self.num_labels,
Expand Down
23 changes: 23 additions & 0 deletions torcheval/metrics/functional/_jit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

from __future__ import annotations

from collections.abc import Callable
from functools import lru_cache
from typing import TypeVar

import torch
from typing_extensions import ParamSpec

_Params = ParamSpec("_Params")
_Result = TypeVar("_Result")


@lru_cache(maxsize=None)
def _script_on_use(function: Callable[_Params, _Result]) -> Callable[_Params, _Result]:
"""Cache a metric helper's compilation at its eager call site."""
return torch.jit.script(function)
15 changes: 7 additions & 8 deletions torcheval/metrics/functional/classification/accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


import torch
from torcheval.metrics.functional._jit import _script_on_use


@torch.inference_mode()
Expand Down Expand Up @@ -42,7 +43,7 @@ def binary_accuracy(
"""

num_correct, num_total = _binary_accuracy_update(input, target, threshold)
return _accuracy_compute(num_correct, num_total, "micro")
return _script_on_use(_accuracy_compute)(num_correct, num_total, "micro")


@torch.inference_mode()
Expand Down Expand Up @@ -101,7 +102,7 @@ def multiclass_accuracy(
num_correct, num_total = _multiclass_accuracy_update(
input, target, average, num_classes, k
)
return _accuracy_compute(num_correct, num_total, average)
return _script_on_use(_accuracy_compute)(num_correct, num_total, average)


@torch.inference_mode()
Expand Down Expand Up @@ -172,7 +173,7 @@ def multilabel_accuracy(
num_correct, num_total = _multilabel_accuracy_update(
input, target, threshold, criteria
)
return _accuracy_compute(num_correct, num_total, "micro")
return _script_on_use(_accuracy_compute)(num_correct, num_total, "micro")


@torch.inference_mode()
Expand Down Expand Up @@ -243,7 +244,7 @@ def topk_multilabel_accuracy(
num_correct, num_total = _topk_multilabel_accuracy_update(
input, target, criteria, k
)
return _accuracy_compute(num_correct, num_total, "micro")
return _script_on_use(_accuracy_compute)(num_correct, num_total, "micro")


def _multiclass_accuracy_update(
Expand Down Expand Up @@ -276,7 +277,6 @@ def _multiclass_accuracy_update(
return num_correct, num_total


@torch.jit.script
def _accuracy_compute(
num_correct: torch.Tensor,
num_total: torch.Tensor,
Expand Down Expand Up @@ -390,7 +390,7 @@ def _multilabel_accuracy_update(
) -> tuple[torch.Tensor, torch.Tensor]:
_multilabel_accuracy_update_input_check(input, target)
input_label = torch.where(input < threshold, 0, 1)
return _multilabel_update(input_label, target, criteria)
return _script_on_use(_multilabel_update)(input_label, target, criteria)


def _topk_multilabel_accuracy_update(
Expand All @@ -403,10 +403,9 @@ def _topk_multilabel_accuracy_update(
input_label = torch.zeros(input.size(), device=input.device).scatter_(
-1, input.topk(k=k, dim=-1).indices, 1.0
)
return _multilabel_update(input_label, target, criteria)
return _script_on_use(_multilabel_update)(input_label, target, criteria)


@torch.jit.script
def _multilabel_update(
input: torch.Tensor,
target: torch.Tensor,
Expand Down
7 changes: 5 additions & 2 deletions torcheval/metrics/functional/classification/auprc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


import torch
from torcheval.metrics.functional._jit import _script_on_use
from torcheval.metrics.functional.classification.precision_recall_curve import (
_compute_for_each_class,
multiclass_precision_recall_curve,
Expand Down Expand Up @@ -240,12 +241,14 @@ def _binary_auprc_compute(
) -> torch.Tensor:
# for one task preserve the ndim of the input and target tensor
if num_tasks == 1 and input.ndim == 1:
p, r, t = _compute_for_each_class(input, target, 1)
p, r, t = _script_on_use(_compute_for_each_class)(input, target, 1)
return _riemann_integral(r, p)
else:
auprcs = []
for i in range(num_tasks):
p, r, t = _compute_for_each_class(input[i, :], target[i, :], 1)
p, r, t = _script_on_use(_compute_for_each_class)(
input[i, :], target[i, :], 1
)
auprcs.append(_riemann_integral(r, p))
return torch.tensor(auprcs, device=input.device)

Expand Down
9 changes: 5 additions & 4 deletions torcheval/metrics/functional/classification/auroc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import torch
from torch.nn import functional as F
from torcheval.metrics.functional._jit import _script_on_use

# Optionally import fbgemm_gpu to enable use of hand fused kernel
try:
Expand Down Expand Up @@ -108,10 +109,11 @@ def multiclass_auroc(
"""
_multiclass_auroc_param_check(num_classes, average)
_multiclass_auroc_update_input_check(input, target, num_classes)
return _multiclass_auroc_compute(input, target, num_classes, average)
return _script_on_use(_multiclass_auroc_compute)(
input, target, num_classes, average
)


@torch.jit.script
def _binary_auroc_compute_jit(
input: torch.Tensor,
target: torch.Tensor,
Expand Down Expand Up @@ -175,7 +177,7 @@ def _binary_auroc_compute(
else:
return auroc
else:
return _binary_auroc_compute_jit(input, target, weight)
return _script_on_use(_binary_auroc_compute_jit)(input, target, weight)


def _binary_auroc_update_input_check(
Expand Down Expand Up @@ -206,7 +208,6 @@ def _binary_auroc_update_input_check(
)


@torch.jit.script
def _multiclass_auroc_compute(
input: torch.Tensor,
target: torch.Tensor,
Expand Down
Loading