Skip to content

Explore fixing test issues for older pytorch versions #3434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
16 changes: 11 additions & 5 deletions .github/workflows/pytorch-version-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ on:
# Run at 00:00 UTC Every Day
- cron: "0 0 * * *"
workflow_dispatch:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
Expand All @@ -15,14 +21,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 +73,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 +89,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
46 changes: 23 additions & 23 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
name: Run unit tests
on:
push:
branches:
- master
- "*.*.*"
paths:
- "examples/**.py"
- "ignite/**"
- "pyproject.toml"
- "tests/ignite/**"
- "tests/run_code_style.sh"
- "tests/run_cpu_tests.sh"
- "requirements-dev.txt"
- ".github/workflows/unit-tests.yml"
pull_request:
paths:
- "examples/**.py"
- "ignite/**"
- "pyproject.toml"
- "tests/ignite/**"
- "tests/run_code_style.sh"
- "tests/run_cpu_tests.sh"
- "requirements-dev.txt"
- ".github/workflows/unit-tests.yml"
# push:
# branches:
# - master
# - "*.*.*"
# paths:
# - "examples/**.py"
# - "ignite/**"
# - "pyproject.toml"
# - "tests/ignite/**"
# - "tests/run_code_style.sh"
# - "tests/run_cpu_tests.sh"
# - "requirements-dev.txt"
# - ".github/workflows/unit-tests.yml"
# pull_request:
# paths:
# - "examples/**.py"
# - "ignite/**"
# - "pyproject.toml"
# - "tests/ignite/**"
# - "tests/run_code_style.sh"
# - "tests/run_cpu_tests.sh"
# - "requirements-dev.txt"
# - ".github/workflows/unit-tests.yml"
workflow_dispatch:
merge_group:

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
6 changes: 6 additions & 0 deletions tests/ignite/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ def _destroy_dist_context():

dist.barrier()

print("before destroy")
dist.destroy_process_group()
print("after destroy")

from ignite.distributed.utils import _SerialModel, _set_model

Expand Down Expand Up @@ -243,6 +245,7 @@ def distributed_context_single_node_nccl(local_rank, world_size):

@pytest.fixture()
def distributed_context_single_node_gloo(local_rank, world_size):
print("INFO:", local_rank, world_size)
from datetime import timedelta

if sys.platform.startswith("win"):
Expand All @@ -251,6 +254,7 @@ def distributed_context_single_node_gloo(local_rank, world_size):
backslash = "\\"
init_method = f'file:///{temp_file.name.replace(backslash, "/")}'
else:
print("Setting up free port for Gloo backend")
free_port = _setup_free_port(local_rank)
init_method = f"tcp://localhost:{free_port}"
temp_file = None
Expand All @@ -262,7 +266,9 @@ def distributed_context_single_node_gloo(local_rank, world_size):
"init_method": init_method,
"timeout": timedelta(seconds=30),
}
print("Before yield")
yield _create_dist_context(dist_info, local_rank)
print("After yield")
_destroy_dist_context()
if temp_file:
temp_file.close()
Expand Down
6 changes: 6 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,8 @@
)


_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 +232,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 +256,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 +276,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")

# [email protected], [email protected], [email protected], 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