Skip to content

Commit a5f1b90

Browse files
committed
Use uv to increase speed of pip installs
1 parent 72fede0 commit a5f1b90

File tree

6 files changed

+101
-54
lines changed

6 files changed

+101
-54
lines changed

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

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ on:
66
# Run at 00:00 UTC Every Day
77
- cron: "0 0 * * *"
88
workflow_dispatch:
9+
push:
10+
branches:
11+
- master
12+
pull_request:
13+
branches:
14+
- master
915

1016
jobs:
1117
build:
@@ -15,14 +21,13 @@ jobs:
1521
max-parallel: 5
1622
fail-fast: false
1723
matrix:
18-
python-version: [3.9, "3.10", "3.11"]
24+
python-version: ["3.9", "3.10", "3.11"]
1925
pytorch-version: [2.5.1, 2.4.1, 2.3.1, 2.2.2, 1.13.1, 1.12.1, 1.10.0]
2026
exclude:
2127
- pytorch-version: 1.10.0
2228
python-version: "3.10"
2329
- pytorch-version: 1.10.0
2430
python-version: "3.11"
25-
2631
- pytorch-version: 1.11.0
2732
python-version: "3.10"
2833
- pytorch-version: 1.11.0
@@ -33,7 +38,31 @@ jobs:
3338
# failing with unrelated errors
3439
- pytorch-version: 1.13.1
3540
python-version: "3.11"
36-
41+
# Testing fdailures, excluding passes
42+
- pytorch-version: 2.5.1
43+
python-version: "3.9"
44+
- pytorch-version: 2.4.1
45+
python-version: "3.9"
46+
- pytorch-version: 2.3.1
47+
python-version: "3.9"
48+
- pytorch-version: 2.2.2
49+
python-version: "3.9"
50+
- pytorch-version: 2.5.1
51+
python-version: "3.10"
52+
- pytorch-version: 2.4.1
53+
python-version: "3.10"
54+
- pytorch-version: 2.3.1
55+
python-version: "3.10"
56+
- pytorch-version: 2.2.2
57+
python-version: "3.10"
58+
- pytorch-version: 2.5.1
59+
python-version: "3.11"
60+
- pytorch-version: 2.4.1
61+
python-version: "3.11"
62+
- pytorch-version: 2.3.1
63+
python-version: "3.11"
64+
- pytorch-version: 2.2.2
65+
python-version: "3.11"
3766
steps:
3867
- uses: actions/checkout@v4
3968

@@ -68,22 +97,23 @@ jobs:
6897
- name: Install dependencies
6998
shell: bash -l {0}
7099
run: |
71-
conda install pytorch=${{ matrix.pytorch-version }} torchvision cpuonly python=${{ matrix.python-version }} -c pytorch
72-
100+
conda install pytorch=${{ matrix.pytorch-version }} torchvision cpuonly python=${{ matrix.python-version }} -c pytorch -y
101+
102+
pip install uv
73103
# We should install numpy<2.0 for pytorch<2.3
74104
numpy_one_pth_version=$(python -c "import torch; print(float('.'.join(torch.__version__.split('.')[:2])) < 2.3)")
75105
if [ "${numpy_one_pth_version}" == "True" ]; then
76-
pip install -U "numpy<2.0"
106+
uv pip install "numpy<2.0"
77107
fi
78108
79-
pip install -r requirements-dev.txt
80-
pip install .
109+
uv pip install -r requirements-dev.txt
110+
uv pip install .
81111
82112
# pytorch>=1.9.0,<1.11.0 is using "from setuptools import distutils; distutils.version.LooseVersion" anti-pattern
83113
# which raises the error: AttributeError: module 'distutils' has no attribute 'version' for setuptools>59
84114
bad_pth_version=$(python -c "import torch; print('.'.join(torch.__version__.split('.')[:2]) in ['1.9', '1.10'])")
85115
if [ "${bad_pth_version}" == "True" ]; then
86-
pip install --upgrade "setuptools<59"
116+
uv pip install "setuptools<59"
87117
python -c "from setuptools import distutils; distutils.version.LooseVersion"
88118
fi
89119

.github/workflows/unit-tests.yml

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
name: Run unit tests
22
on:
3-
push:
4-
branches:
5-
- master
6-
- "*.*.*"
7-
paths:
8-
- "examples/**.py"
9-
- "ignite/**"
10-
- "pyproject.toml"
11-
- "tests/ignite/**"
12-
- "tests/run_code_style.sh"
13-
- "tests/run_cpu_tests.sh"
14-
- "requirements-dev.txt"
15-
- ".github/workflows/unit-tests.yml"
16-
pull_request:
17-
paths:
18-
- "examples/**.py"
19-
- "ignite/**"
20-
- "pyproject.toml"
21-
- "tests/ignite/**"
22-
- "tests/run_code_style.sh"
23-
- "tests/run_cpu_tests.sh"
24-
- "requirements-dev.txt"
25-
- ".github/workflows/unit-tests.yml"
3+
# push:
4+
# branches:
5+
# - master
6+
# - "*.*.*"
7+
# paths:
8+
# - "examples/**.py"
9+
# - "ignite/**"
10+
# - "pyproject.toml"
11+
# - "tests/ignite/**"
12+
# - "tests/run_code_style.sh"
13+
# - "tests/run_cpu_tests.sh"
14+
# - "requirements-dev.txt"
15+
# - ".github/workflows/unit-tests.yml"
16+
# pull_request:
17+
# paths:
18+
# - "examples/**.py"
19+
# - "ignite/**"
20+
# - "pyproject.toml"
21+
# - "tests/ignite/**"
22+
# - "tests/run_code_style.sh"
23+
# - "tests/run_cpu_tests.sh"
24+
# - "requirements-dev.txt"
25+
# - ".github/workflows/unit-tests.yml"
2626
workflow_dispatch:
2727
merge_group:
2828

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/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)