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
10 changes: 5 additions & 5 deletions .github/workflows/pytorch-version-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ jobs:
max-parallel: 5
fail-fast: false
matrix:
python-version: [3.9, "3.10", "3.11"]
python-version: ["3.9", "3.10", "3.11"]
pytorch-version: [2.5.1, 2.4.1, 2.3.1, 2.2.2, 1.13.1, 1.12.1, 1.10.0]
exclude:
- pytorch-version: 1.10.0
python-version: "3.10"
- pytorch-version: 1.10.0
python-version: "3.11"

- pytorch-version: 1.11.0
python-version: "3.10"
- pytorch-version: 1.11.0
Expand Down Expand Up @@ -68,12 +67,13 @@ jobs:
- name: Install dependencies
shell: bash -l {0}
run: |
conda install pytorch=${{ matrix.pytorch-version }} torchvision cpuonly python=${{ matrix.python-version }} -c pytorch

conda install pytorch=${{ matrix.pytorch-version }} torchvision cpuonly python=${{ matrix.python-version }} -c pytorch -y

# We should install numpy<2.0 for pytorch<2.3
numpy_one_pth_version=$(python -c "import torch; print(float('.'.join(torch.__version__.split('.')[:2])) < 2.3)")
if [ "${numpy_one_pth_version}" == "True" ]; then
pip install -U "numpy<2.0"
pip install "numpy<2.0"
fi

pip install -r requirements-dev.txt
Expand All @@ -83,7 +83,7 @@ jobs:
# which raises the error: AttributeError: module 'distutils' has no attribute 'version' for setuptools>59
bad_pth_version=$(python -c "import torch; print('.'.join(torch.__version__.split('.')[:2]) in ['1.9', '1.10'])")
if [ "${bad_pth_version}" == "True" ]; then
pip install --upgrade "setuptools<59"
pip install "setuptools<59"
python -c "from setuptools import distutils; distutils.version.LooseVersion"
fi

Expand Down
4 changes: 2 additions & 2 deletions ignite/metrics/gan/fid.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def fid_score(
diff = mu1 - mu2

# Product might be almost singular
covmean, _ = scipy.linalg.sqrtm(sigma1.mm(sigma2), disp=False)
covmean, _ = scipy.linalg.sqrtm(sigma1.mm(sigma2).numpy(), disp=False)
Copy link
Preview

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converting the tensor to numpy adds an unnecessary GPU-to-CPU transfer. Consider checking if the input tensors are on GPU and handle the conversion more efficiently, or use PyTorch native operations if available.

Copilot uses AI. Check for mistakes.

# Numerical error might give slight imaginary component
if np.iscomplexobj(covmean):
if not np.allclose(np.diagonal(covmean).imag, 0, atol=1e-3):
Expand All @@ -48,7 +48,7 @@ def fid_score(
tr_covmean = np.trace(covmean)

if not np.isfinite(covmean).all():
tr_covmean = np.sum(np.sqrt(((np.diag(sigma1) * eps) * (np.diag(sigma2) * eps)) / (eps * eps)))
tr_covmean = np.sum(np.sqrt(((np.diag(sigma1.numpy()) * eps) * (np.diag(sigma2.numpy()) * eps)) / (eps * eps)))

return float(diff.dot(diff).item() + torch.trace(sigma1) + torch.trace(sigma2) - 2 * tr_covmean)
Comment on lines +51 to 53
Copy link
Preview

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple .numpy() calls on the same tensors (sigma1 and sigma2) create redundant GPU-to-CPU transfers. Consider converting these tensors to numpy once and reusing the numpy arrays.

Suggested change
tr_covmean = np.sum(np.sqrt(((np.diag(sigma1.numpy()) * eps) * (np.diag(sigma2.numpy()) * eps)) / (eps * eps)))
return float(diff.dot(diff).item() + torch.trace(sigma1) + torch.trace(sigma2) - 2 * tr_covmean)
tr_covmean = np.sum(np.sqrt(((np.diag(sigma1) * eps) * (np.diag(sigma2) * eps)) / (eps * eps)))
return float(diff.dot(diff).item() + torch.trace(torch.tensor(sigma1)) + torch.trace(torch.tensor(sigma2)) - 2 * tr_covmean)

Copilot uses AI. Check for mistakes.


Expand Down
22 changes: 15 additions & 7 deletions ignite/metrics/mean_average_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Callable, cast, List, Optional, Sequence, Tuple, Union

import torch
from packaging.version import Version
from typing_extensions import Literal

import ignite.distributed as idist
Expand All @@ -11,6 +12,9 @@
from ignite.utils import to_onehot


_torch_version_lt_113 = Version(torch.__version__) < Version("1.13.0")


class _BaseAveragePrecision:
def __init__(
self,
Expand Down Expand Up @@ -97,9 +101,12 @@ def _compute_average_precision(self, recall: torch.Tensor, precision: torch.Tens
if self.rec_thresholds is not None:
rec_thresholds = self.rec_thresholds.repeat((*recall.shape[:-1], 1))
rec_thresh_indices = torch.searchsorted(recall, rec_thresholds)
precision = precision.take_along_dim(
rec_thresh_indices.where(rec_thresh_indices != recall.size(-1), 0), dim=-1
).where(rec_thresh_indices != recall.size(-1), 0)
rec_mask = rec_thresh_indices != recall.size(-1)
precision = torch.where(
rec_mask,
precision.take_along_dim(torch.where(rec_mask, rec_thresh_indices, 0), dim=-1),
0.0,
)
recall = rec_thresholds
recall_differential = recall.diff(
dim=-1, prepend=torch.zeros((*recall.shape[:-1], 1), device=recall.device, dtype=recall.dtype)
Expand Down Expand Up @@ -335,9 +342,10 @@ def _compute_recall_and_precision(
Returns:
`(recall, precision)`
"""
indices = torch.argsort(y_pred, stable=True, descending=True)
kwargs = {} if _torch_version_lt_113 else {"stable": True}
indices = torch.argsort(y_pred, descending=True, **kwargs)
tp_summation = y_true[indices].cumsum(dim=0)
if tp_summation.device != torch.device("mps"):
if tp_summation.device.type != "mps":
tp_summation = tp_summation.double()

# Adopted from Scikit-learn's implementation
Expand All @@ -354,7 +362,7 @@ def _compute_recall_and_precision(
recall = tp_summation / y_true_positive_count

predicted_positive = tp_summation + fp_summation
precision = tp_summation / torch.where(predicted_positive == 0, 1, predicted_positive)
precision = tp_summation / torch.where(predicted_positive == 0, 1.0, predicted_positive)
return recall, precision

def compute(self) -> Union[torch.Tensor, float]:
Expand All @@ -371,7 +379,7 @@ def compute(self) -> Union[torch.Tensor, float]:
torch.long if self._type == "multiclass" else torch.uint8,
self._device,
)
fp_precision = torch.double if self._device != torch.device("mps") else torch.float32
fp_precision = torch.double if self._device.type != "mps" else torch.float32
y_pred = _cat_and_agg_tensors(self._y_pred, (num_classes,), fp_precision, self._device)

if self._type == "multiclass":
Expand Down
21 changes: 15 additions & 6 deletions ignite/metrics/vision/object_detection_average_precision_recall.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Callable, cast, Dict, List, Optional, Sequence, Tuple, Union

import torch
from packaging.version import Version
from typing_extensions import Literal

from ignite.metrics import MetricGroup
Expand All @@ -9,6 +10,9 @@
from ignite.metrics.metric import Metric, reinit__is_reduced, sync_all_reduce


_torch_version_lt_113 = Version(torch.__version__) < Version("1.13.0")


def coco_tensor_list_to_dict_list(
output: Tuple[
Union[List[torch.Tensor], List[Dict[str, torch.Tensor]]],
Expand Down Expand Up @@ -213,7 +217,8 @@ def _compute_recall_and_precision(
Returns:
`(recall, precision)`
"""
indices = torch.argsort(scores, dim=-1, stable=True, descending=True)
kwargs = {} if _torch_version_lt_113 else {"stable": True}
indices = torch.argsort(scores, descending=True, **kwargs)
tp = TP[..., indices]
tp_summation = tp.cumsum(dim=-1)
if tp_summation.device.type != "mps":
Expand All @@ -226,7 +231,7 @@ def _compute_recall_and_precision(

recall = tp_summation / y_true_count
predicted_positive = tp_summation + fp_summation
precision = tp_summation / torch.where(predicted_positive == 0, 1, predicted_positive)
precision = tp_summation / torch.where(predicted_positive == 0, 1.0, predicted_positive)

return recall, precision

Expand Down Expand Up @@ -258,9 +263,12 @@ def _compute_average_precision(self, recall: torch.Tensor, precision: torch.Tens
if recall.size(-1) != 0
else torch.LongTensor([], device=self._device)
)
precision_integrand = precision_integrand.take_along_dim(
rec_thresh_indices.where(rec_thresh_indices != recall.size(-1), 0), dim=-1
).where(rec_thresh_indices != recall.size(-1), 0)
recall_mask = rec_thresh_indices != recall.size(-1)
precision_integrand = torch.where(
recall_mask,
precision_integrand.take_along_dim(torch.where(recall_mask, rec_thresh_indices, 0), dim=-1),
0.0,
)
return torch.sum(precision_integrand, dim=-1) / len(cast(torch.Tensor, self.rec_thresholds))

@reinit__is_reduced
Expand Down Expand Up @@ -298,6 +306,7 @@ def update(self, output: Tuple[List[Dict[str, torch.Tensor]], List[Dict[str, tor
This key is optional.
========= ================= =================================================
"""
kwargs = {} if _torch_version_lt_113 else {"stable": True}
self._check_matching_input(output)
for pred, target in zip(*output):
labels = target["labels"]
Expand All @@ -312,7 +321,7 @@ def update(self, output: Tuple[List[Dict[str, torch.Tensor]], List[Dict[str, tor

# Matching logic of object detection mAP, according to COCO reference implementation.
if len(pred["labels"]):
best_detections_index = torch.argsort(pred["scores"], stable=True, descending=True)
best_detections_index = torch.argsort(pred["scores"], descending=True, **kwargs)
max_best_detections_index = torch.cat(
[
best_detections_index[pred["labels"][best_detections_index] == c][
Expand Down
7 changes: 7 additions & 0 deletions tests/ignite/distributed/utils/test_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
)


_torch_version_lt_1132 = Version(torch.__version__) < Version("1.13.2")


def _test_native_distrib_single_node_launch_tool(backend, device, local_rank, world_size, init_method=None, **kwargs):
import os

Expand Down Expand Up @@ -230,7 +233,9 @@ def test_idist_all_reduce_nccl(distributed_context_single_node_nccl):


@pytest.mark.distributed
@pytest.mark.order(-1)
@pytest.mark.skipif(not has_native_dist_support, reason="Skip if no native dist support")
@pytest.mark.skipif(_torch_version_lt_1132, reason="Skip if older pytorch version")
def test_idist_all_reduce_gloo(distributed_context_single_node_gloo):
device = idist.device()
_test_distrib_all_reduce(device)
Expand All @@ -252,6 +257,7 @@ def test_idist_all_gather_nccl(distributed_context_single_node_nccl):
@pytest.mark.distributed
@pytest.mark.skipif(not has_native_dist_support, reason="Skip if no native dist support")
@pytest.mark.skipif(Version(torch.__version__) < Version("1.7.0"), reason="dist.all_gather_object is not implemented")
@pytest.mark.order(-3)
def test_idist_all_gather_gloo(distributed_context_single_node_gloo):
device = idist.device()
_test_distrib_all_gather(device)
Expand All @@ -271,6 +277,7 @@ def test_idist_all_gather_tensors_with_shapes_nccl(distributed_context_single_no

@pytest.mark.distributed
@pytest.mark.skipif(not has_native_dist_support, reason="Skip if no native dist support")
@pytest.mark.order(-2)
def test_idist_all_gather_tensors_with_shapes_gloo(distributed_context_single_node_gloo):
device = idist.device()
_test_idist_all_gather_tensors_with_shapes(device)
Expand Down
12 changes: 6 additions & 6 deletions tests/ignite/metrics/test_mean_average_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,30 @@ def test__prepare_output():
metric = MeanAveragePrecision()

metric._type = "binary"
scores, y = metric._prepare_output((torch.rand((5, 4, 3, 2)), torch.randint(0, 2, (5, 4, 3, 2)).bool()))
scores, y = metric._prepare_output((torch.rand((5, 4, 3, 2)), torch.randint(0, 2, (5, 4, 3, 2))))
assert scores.shape == y.shape == (1, 120)

metric._type = "multiclass"
scores, y = metric._prepare_output((torch.rand((5, 4, 3, 2)), torch.randint(0, 4, (5, 3, 2))))
assert scores.shape == (4, 30) and y.shape == (30,)

metric._type = "multilabel"
scores, y = metric._prepare_output((torch.rand((5, 4, 3, 2)), torch.randint(0, 2, (5, 4, 3, 2)).bool()))
scores, y = metric._prepare_output((torch.rand((5, 4, 3, 2)), torch.randint(0, 2, (5, 4, 3, 2))))
assert scores.shape == y.shape == (4, 30)


def test_update():
metric = MeanAveragePrecision()
assert len(metric._y_pred) == len(metric._y_true) == 0
metric.update((torch.rand((5, 4)), torch.randint(0, 2, (5, 4)).bool()))
metric.update((torch.rand((5, 4)), torch.randint(0, 2, (5, 4))))
assert len(metric._y_pred) == len(metric._y_true) == 1


def test__compute_recall_and_precision():
m = MeanAveragePrecision()

scores = torch.rand((50,))
y_true = torch.randint(0, 2, (50,)).bool()
y_true = torch.randint(0, 2, (50,))
precision, recall, _ = precision_recall_curve(y_true.numpy(), scores.numpy())
P = y_true.sum(dim=-1)
ignite_recall, ignite_precision = m._compute_recall_and_precision(y_true, scores, P)
Expand All @@ -77,7 +77,7 @@ def test__compute_recall_and_precision():

# When there's no actual positive. Numpy expectedly raises warning.
scores = torch.rand((50,))
y_true = torch.zeros((50,)).bool()
y_true = torch.zeros((50,))
precision, recall, _ = precision_recall_curve(y_true.numpy(), scores.numpy())
P = torch.tensor(0)
ignite_recall, ignite_precision = m._compute_recall_and_precision(y_true, scores, P)
Expand Down Expand Up @@ -147,7 +147,7 @@ def test_compute_nonbinary_data(class_mean):

# Multilabel
m = MeanAveragePrecision(is_multilabel=True, class_mean=class_mean)
y_true = torch.randint(0, 2, (130, 5, 2, 2)).bool()
y_true = torch.randint(0, 2, (130, 5, 2, 2))
m.update((scores[:50], y_true[:50]))
m.update((scores[50:], y_true[50:]))
ignite_map = m.compute().numpy()
Expand Down
6 changes: 3 additions & 3 deletions tests/ignite/metrics/vision/test_object_detection_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ def test__compute_recall_and_precision(available_device):
def test_compute(sample):
device = idist.device()

if device == torch.device("mps"):
if device.type == "mps":
pytest.skip("Due to MPS backend out of memory")

# AP@.5...95, AP@.5, AP@.75, AP-S, AP-M, AP-L, AR-1, AR-10, AR-100, AR-S, AR-M, AR-L
Expand Down Expand Up @@ -932,7 +932,7 @@ def test_integration(sample):
bs = 3

device = idist.device()
if device == torch.device("mps"):
if device.type == "mps":
pytest.skip("Due to MPS backend out of memory")

def update(engine, i):
Expand Down Expand Up @@ -1003,7 +1003,7 @@ def test_distrib_update_compute(distributed, sample):

device = idist.device()

if device == torch.device("mps"):
if device.type == "mps":
pytest.skip("Due to MPS backend out of memory")

metric_device = "cpu" if device.type == "xla" else device
Expand Down
Loading