Skip to content

Commit 08cf6f6

Browse files
committed
Fix testing issues for older pytorch versions
1 parent 72fede0 commit 08cf6f6

File tree

8 files changed

+53
-30
lines changed

8 files changed

+53
-30
lines changed

.github/workflows/pytorch-version-tests.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ jobs:
1515
max-parallel: 5
1616
fail-fast: false
1717
matrix:
18-
python-version: [3.9, "3.10", "3.11"]
18+
python-version: ["3.9", "3.10", "3.11"]
1919
pytorch-version: [2.5.1, 2.4.1, 2.3.1, 2.2.2, 1.13.1, 1.12.1, 1.10.0]
2020
exclude:
2121
- pytorch-version: 1.10.0
2222
python-version: "3.10"
2323
- pytorch-version: 1.10.0
2424
python-version: "3.11"
25-
2625
- pytorch-version: 1.11.0
2726
python-version: "3.10"
2827
- pytorch-version: 1.11.0
@@ -68,12 +67,13 @@ jobs:
6867
- name: Install dependencies
6968
shell: bash -l {0}
7069
run: |
71-
conda install pytorch=${{ matrix.pytorch-version }} torchvision cpuonly python=${{ matrix.python-version }} -c pytorch
7270
71+
conda install pytorch=${{ matrix.pytorch-version }} torchvision cpuonly python=${{ matrix.python-version }} -c pytorch -y
72+
7373
# We should install numpy<2.0 for pytorch<2.3
7474
numpy_one_pth_version=$(python -c "import torch; print(float('.'.join(torch.__version__.split('.')[:2])) < 2.3)")
7575
if [ "${numpy_one_pth_version}" == "True" ]; then
76-
pip install -U "numpy<2.0"
76+
pip install "numpy<2.0"
7777
fi
7878

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

ignite/metrics/gan/fid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def fid_score(
3737
diff = mu1 - mu2
3838

3939
# Product might be almost singular
40-
covmean, _ = scipy.linalg.sqrtm(sigma1.mm(sigma2), disp=False)
40+
covmean, _ = scipy.linalg.sqrtm(sigma1.mm(sigma2).numpy(), disp=False)
4141
# Numerical error might give slight imaginary component
4242
if np.iscomplexobj(covmean):
4343
if not np.allclose(np.diagonal(covmean).imag, 0, atol=1e-3):
@@ -48,7 +48,7 @@ def fid_score(
4848
tr_covmean = np.trace(covmean)
4949

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

5353
return float(diff.dot(diff).item() + torch.trace(sigma1) + torch.trace(sigma2) - 2 * tr_covmean)
5454

ignite/metrics/mean_average_precision.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Callable, cast, List, Optional, Sequence, Tuple, Union
33

44
import torch
5+
from packaging.version import Version
56
from typing_extensions import Literal
67

78
import ignite.distributed as idist
@@ -11,6 +12,9 @@
1112
from ignite.utils import to_onehot
1213

1314

15+
_torch_version_lt_113 = Version(torch.__version__) < Version("1.13.0")
16+
17+
1418
class _BaseAveragePrecision:
1519
def __init__(
1620
self,
@@ -97,9 +101,12 @@ def _compute_average_precision(self, recall: torch.Tensor, precision: torch.Tens
97101
if self.rec_thresholds is not None:
98102
rec_thresholds = self.rec_thresholds.repeat((*recall.shape[:-1], 1))
99103
rec_thresh_indices = torch.searchsorted(recall, rec_thresholds)
100-
precision = precision.take_along_dim(
101-
rec_thresh_indices.where(rec_thresh_indices != recall.size(-1), 0), dim=-1
102-
).where(rec_thresh_indices != recall.size(-1), 0)
104+
rec_mask = rec_thresh_indices != recall.size(-1)
105+
precision = torch.where(
106+
rec_mask,
107+
precision.take_along_dim(torch.where(rec_mask, rec_thresh_indices, 0), dim=-1),
108+
0.0,
109+
)
103110
recall = rec_thresholds
104111
recall_differential = recall.diff(
105112
dim=-1, prepend=torch.zeros((*recall.shape[:-1], 1), device=recall.device, dtype=recall.dtype)
@@ -335,9 +342,10 @@ def _compute_recall_and_precision(
335342
Returns:
336343
`(recall, precision)`
337344
"""
338-
indices = torch.argsort(y_pred, stable=True, descending=True)
345+
kwargs = {} if _torch_version_lt_113 else {"stable": True}
346+
indices = torch.argsort(y_pred, descending=True, **kwargs)
339347
tp_summation = y_true[indices].cumsum(dim=0)
340-
if tp_summation.device != torch.device("mps"):
348+
if tp_summation.device.type != "mps":
341349
tp_summation = tp_summation.double()
342350

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

356364
predicted_positive = tp_summation + fp_summation
357-
precision = tp_summation / torch.where(predicted_positive == 0, 1, predicted_positive)
365+
precision = tp_summation / torch.where(predicted_positive == 0, 1.0, predicted_positive)
358366
return recall, precision
359367

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

377385
if self._type == "multiclass":

ignite/metrics/vision/object_detection_average_precision_recall.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Callable, cast, Dict, List, Optional, Sequence, Tuple, Union
22

33
import torch
4+
from packaging.version import Version
45
from typing_extensions import Literal
56

67
from ignite.metrics import MetricGroup
@@ -9,6 +10,9 @@
910
from ignite.metrics.metric import Metric, reinit__is_reduced, sync_all_reduce
1011

1112

13+
_torch_version_lt_113 = Version(torch.__version__) < Version("1.13.0")
14+
15+
1216
def coco_tensor_list_to_dict_list(
1317
output: Tuple[
1418
Union[List[torch.Tensor], List[Dict[str, torch.Tensor]]],
@@ -213,7 +217,8 @@ def _compute_recall_and_precision(
213217
Returns:
214218
`(recall, precision)`
215219
"""
216-
indices = torch.argsort(scores, dim=-1, stable=True, descending=True)
220+
kwargs = {} if _torch_version_lt_113 else {"stable": True}
221+
indices = torch.argsort(scores, descending=True, **kwargs)
217222
tp = TP[..., indices]
218223
tp_summation = tp.cumsum(dim=-1)
219224
if tp_summation.device.type != "mps":
@@ -226,7 +231,7 @@ def _compute_recall_and_precision(
226231

227232
recall = tp_summation / y_true_count
228233
predicted_positive = tp_summation + fp_summation
229-
precision = tp_summation / torch.where(predicted_positive == 0, 1, predicted_positive)
234+
precision = tp_summation / torch.where(predicted_positive == 0, 1.0, predicted_positive)
230235

231236
return recall, precision
232237

@@ -258,9 +263,12 @@ def _compute_average_precision(self, recall: torch.Tensor, precision: torch.Tens
258263
if recall.size(-1) != 0
259264
else torch.LongTensor([], device=self._device)
260265
)
261-
precision_integrand = precision_integrand.take_along_dim(
262-
rec_thresh_indices.where(rec_thresh_indices != recall.size(-1), 0), dim=-1
263-
).where(rec_thresh_indices != recall.size(-1), 0)
266+
recall_mask = rec_thresh_indices != recall.size(-1)
267+
precision_integrand = torch.where(
268+
recall_mask,
269+
precision_integrand.take_along_dim(torch.where(recall_mask, rec_thresh_indices, 0), dim=-1),
270+
0.0,
271+
)
264272
return torch.sum(precision_integrand, dim=-1) / len(cast(torch.Tensor, self.rec_thresholds))
265273

266274
@reinit__is_reduced
@@ -298,6 +306,7 @@ def update(self, output: Tuple[List[Dict[str, torch.Tensor]], List[Dict[str, tor
298306
This key is optional.
299307
========= ================= =================================================
300308
"""
309+
kwargs = {} if _torch_version_lt_113 else {"stable": True}
301310
self._check_matching_input(output)
302311
for pred, target in zip(*output):
303312
labels = target["labels"]
@@ -312,7 +321,7 @@ def update(self, output: Tuple[List[Dict[str, torch.Tensor]], List[Dict[str, tor
312321

313322
# Matching logic of object detection mAP, according to COCO reference implementation.
314323
if len(pred["labels"]):
315-
best_detections_index = torch.argsort(pred["scores"], stable=True, descending=True)
324+
best_detections_index = torch.argsort(pred["scores"], descending=True, **kwargs)
316325
max_best_detections_index = torch.cat(
317326
[
318327
best_detections_index[pred["labels"][best_detections_index] == c][

tests/ignite/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def distributed_context_single_node_gloo(local_rank, world_size):
250250
# can't use backslashes in f-strings
251251
backslash = "\\"
252252
init_method = f'file:///{temp_file.name.replace(backslash, "/")}'
253-
else:
253+
else:
254254
free_port = _setup_free_port(local_rank)
255255
init_method = f"tcp://localhost:{free_port}"
256256
temp_file = None

tests/ignite/distributed/utils/test_native.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
)
2626

2727

28+
_torch_version_lt_1132 = Version(torch.__version__) < Version("1.13.2")
29+
2830
def _test_native_distrib_single_node_launch_tool(backend, device, local_rank, world_size, init_method=None, **kwargs):
2931
import os
3032

@@ -230,7 +232,9 @@ def test_idist_all_reduce_nccl(distributed_context_single_node_nccl):
230232

231233

232234
@pytest.mark.distributed
235+
@pytest.mark.order(-1)
233236
@pytest.mark.skipif(not has_native_dist_support, reason="Skip if no native dist support")
237+
@pytest.mark.skipif(_torch_version_lt_1132, reason="Skip if older pytorch version")
234238
def test_idist_all_reduce_gloo(distributed_context_single_node_gloo):
235239
device = idist.device()
236240
_test_distrib_all_reduce(device)
@@ -252,6 +256,7 @@ def test_idist_all_gather_nccl(distributed_context_single_node_nccl):
252256
@pytest.mark.distributed
253257
@pytest.mark.skipif(not has_native_dist_support, reason="Skip if no native dist support")
254258
@pytest.mark.skipif(Version(torch.__version__) < Version("1.7.0"), reason="dist.all_gather_object is not implemented")
259+
@pytest.mark.order(-3)
255260
def test_idist_all_gather_gloo(distributed_context_single_node_gloo):
256261
device = idist.device()
257262
_test_distrib_all_gather(device)
@@ -271,6 +276,7 @@ def test_idist_all_gather_tensors_with_shapes_nccl(distributed_context_single_no
271276

272277
@pytest.mark.distributed
273278
@pytest.mark.skipif(not has_native_dist_support, reason="Skip if no native dist support")
279+
@pytest.mark.order(-2)
274280
def test_idist_all_gather_tensors_with_shapes_gloo(distributed_context_single_node_gloo):
275281
device = idist.device()
276282
_test_idist_all_gather_tensors_with_shapes(device)

tests/ignite/metrics/test_mean_average_precision.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,30 @@ def test__prepare_output():
4545
metric = MeanAveragePrecision()
4646

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

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

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

5959

6060
def test_update():
6161
metric = MeanAveragePrecision()
6262
assert len(metric._y_pred) == len(metric._y_true) == 0
63-
metric.update((torch.rand((5, 4)), torch.randint(0, 2, (5, 4)).bool()))
63+
metric.update((torch.rand((5, 4)), torch.randint(0, 2, (5, 4))))
6464
assert len(metric._y_pred) == len(metric._y_true) == 1
6565

6666

6767
def test__compute_recall_and_precision():
6868
m = MeanAveragePrecision()
6969

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

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

148148
# Multilabel
149149
m = MeanAveragePrecision(is_multilabel=True, class_mean=class_mean)
150-
y_true = torch.randint(0, 2, (130, 5, 2, 2)).bool()
150+
y_true = torch.randint(0, 2, (130, 5, 2, 2))
151151
m.update((scores[:50], y_true[:50]))
152152
m.update((scores[50:], y_true[50:]))
153153
ignite_map = m.compute().numpy()

tests/ignite/metrics/vision/test_object_detection_map.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ def test__compute_recall_and_precision(available_device):
872872
def test_compute(sample):
873873
device = idist.device()
874874

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

878878
# [email protected], [email protected], [email protected], AP-S, AP-M, AP-L, AR-1, AR-10, AR-100, AR-S, AR-M, AR-L
@@ -932,7 +932,7 @@ def test_integration(sample):
932932
bs = 3
933933

934934
device = idist.device()
935-
if device == torch.device("mps"):
935+
if device.type == "mps":
936936
pytest.skip("Due to MPS backend out of memory")
937937

938938
def update(engine, i):
@@ -1003,7 +1003,7 @@ def test_distrib_update_compute(distributed, sample):
10031003

10041004
device = idist.device()
10051005

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

10091009
metric_device = "cpu" if device.type == "xla" else device

0 commit comments

Comments
 (0)