Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4bde2eb
Fix sync_all_reduce
sadra-barikbin Dec 21, 2022
f174e7d
Merge branch 'master' into Fix-sync_all_reduce-decorator-to-consider-…
sadra-barikbin Jan 4, 2023
83dd16e
Merge branch 'master' into Fix-sync_all_reduce-decorator-to-consider-…
sadra-barikbin Jan 5, 2023
f9970d3
Make _is_reduced no-op, add a test and a little improvement
sadra-barikbin Jan 6, 2023
f5b434e
Make _is_reduced no-op, add a test and a little improvement
sadra-barikbin Jan 6, 2023
1bf75bb
Merge branch 'master' into Fix-sync_all_reduce-decorator-to-consider-…
sadra-barikbin Jan 11, 2023
2f0d9a6
Merge branch 'master' into Fix-sync_all_reduce-decorator-to-consider-…
sadra-barikbin Jan 11, 2023
ea2474e
Fix Mypy
sadra-barikbin Jan 11, 2023
74515fe
Merge branch 'master' into Fix-sync_all_reduce-decorator-to-consider-…
sadra-barikbin Jan 18, 2023
319f0f6
Merge branch 'master' into Fix-sync_all_reduce-decorator-to-consider-…
vfdev-5 Feb 1, 2023
392a191
Merge branch 'master' into Fix-sync_all_reduce-decorator-to-consider-…
sadra-barikbin Feb 15, 2023
cdc7e5a
Merge branch 'master' into Fix-sync_all_reduce-decorator-to-consider-…
sadra-barikbin Feb 16, 2023
ca391f9
Remove some asserts from test_loss & test_accuracy
sadra-barikbin Feb 16, 2023
fb8b431
Fix bug
sadra-barikbin Feb 16, 2023
4687414
Merge branch 'master' into Fix-sync_all_reduce-decorator-to-consider-…
sadra-barikbin Feb 16, 2023
6ba844f
Remove _is_reduced
sadra-barikbin Feb 16, 2023
086fa72
Merge remote-tracking branch 'upstream/Fix-sync_all_reduce-decorator-…
sadra-barikbin Feb 16, 2023
2c0d020
Revert deleted assertions
sadra-barikbin Feb 16, 2023
67d0833
Fix a bug in precision
sadra-barikbin Feb 16, 2023
c048119
Fix a mypy error
sadra-barikbin Feb 16, 2023
3017b8e
Merge branch 'master' into Fix-sync_all_reduce-decorator-to-consider-…
sadra-barikbin Feb 16, 2023
3c0f305
Update ignite/metrics/metric.py
sadra-barikbin Feb 17, 2023
20690af
Merge branch 'master' into Fix-sync_all_reduce-decorator-to-consider-…
sadra-barikbin Feb 17, 2023
c9f474e
Revert a change in test_accuracy
sadra-barikbin Feb 17, 2023
ac95fa5
Merge branch 'master' into Fix-sync_all_reduce-decorator-to-consider-…
sadra-barikbin Feb 17, 2023
af380f5
Revert a change in test_accuracy exactly
sadra-barikbin Feb 17, 2023
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
3 changes: 1 addition & 2 deletions ignite/contrib/metrics/precision_recall_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,10 @@ def compute(self) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
_target_tensor = torch.cat(self._targets, dim=0)

ws = idist.get_world_size()
if ws > 1 and not self._is_reduced:
if ws > 1:
# All gather across all processes
_prediction_tensor = cast(torch.Tensor, idist.all_gather(_prediction_tensor))
_target_tensor = cast(torch.Tensor, idist.all_gather(_target_tensor))
self._is_reduced = True

if idist.get_rank() == 0:
# Run compute_fn on zero rank only
Expand Down
3 changes: 1 addition & 2 deletions ignite/metrics/epoch_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,10 @@ def compute(self) -> Any:

ws = idist.get_world_size()

if ws > 1 and not self._is_reduced:
if ws > 1:
# All gather across all processes
_prediction_tensor = cast(torch.Tensor, idist.all_gather(_prediction_tensor))
_target_tensor = cast(torch.Tensor, idist.all_gather(_target_tensor))
self._is_reduced = True

result = 0.0
if idist.get_rank() == 0:
Expand Down
4 changes: 2 additions & 2 deletions ignite/metrics/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ def __init__(
raise ValueError("Cannot create metric on an XLA device. Use device='cpu' instead.")

self._device = torch.device(device)
self._is_reduced = False
self.reset()

@abstractmethod
Expand Down Expand Up @@ -604,7 +603,8 @@ def reinit__is_reduced(func: Callable) -> Callable:
@wraps(func)
def wrapper(self: Metric, *args: Any, **kwargs: Any) -> None:
func(self, *args, **kwargs)
self._is_reduced = False
if "_result" in self.__dict__:
self._result = None # type: ignore[attr-defined]

setattr(wrapper, "_decorated", True)
return wrapper
Expand Down
14 changes: 5 additions & 9 deletions ignite/metrics/precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import ignite.distributed as idist
from ignite.exceptions import NotComputableError
from ignite.metrics.accuracy import _BaseClassification
from ignite.metrics.metric import reinit__is_reduced
from ignite.metrics.metric import reinit__is_reduced, sync_all_reduce
from ignite.utils import to_onehot

__all__ = ["Precision"]
Expand Down Expand Up @@ -121,6 +121,7 @@ def reset(self) -> None:

super(_BasePrecisionRecall, self).reset()

@sync_all_reduce("_numerator", "_denominator")
def compute(self) -> Union[torch.Tensor, float]:

# Return value of the metric for `average` options `'weighted'` and `'macro'` is computed as follows.
Expand All @@ -138,18 +139,13 @@ def compute(self) -> Union[torch.Tensor, float]:
raise NotComputableError(
f"{self.__class__.__name__} must have at least one example before it can be computed."
)
if not self._is_reduced:
self._numerator = idist.all_reduce(self._numerator) # type: ignore[assignment]
self._denominator = idist.all_reduce(self._denominator) # type: ignore[assignment]
if self._average == "weighted":
self._weight = idist.all_reduce(self._weight) # type: ignore[assignment]
self._is_reduced: bool = True

fraction = self._numerator / (self._denominator + (self.eps if self._average != "samples" else 0))

if self._average == "weighted":
sum_of_weights = cast(torch.Tensor, self._weight).sum() + self.eps
return ((fraction @ self._weight) / sum_of_weights).item() # type: ignore
_weight = idist.all_reduce(self._weight)
sum_of_weights = cast(torch.Tensor, _weight).sum() + self.eps
return ((fraction @ _weight) / sum_of_weights).item() # type: ignore
elif self._average == "micro" or self._average == "samples":
return cast(torch.Tensor, fraction).item()
elif self._average == "macro":
Expand Down
7 changes: 7 additions & 0 deletions tests/ignite/metrics/test_accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ def _test(metric_device):
np_y = to_numpy_multilabel(y.cpu()) # (N, C, H, W, ...) -> (N * H * W ..., C)
assert acc._type == "multilabel"
res = acc.compute()
assert n == acc._num_examples
assert isinstance(res, float)
assert accuracy_score(np_y, np_y_pred) == pytest.approx(res)

Expand All @@ -311,8 +312,13 @@ def _test(metric_device):

assert acc._type == "multilabel"
res = acc.compute()
assert n == acc._num_examples
assert isinstance(res, float)
assert accuracy_score(np_y, np_y_pred) == pytest.approx(res)
# check that result is not changed
res = acc.compute()
assert n == acc._num_examples
assert isinstance(res, float)

# Batched Updates
acc.reset()
Expand Down Expand Up @@ -343,6 +349,7 @@ def _test(metric_device):

assert acc._type == "multilabel"
res = acc.compute()
assert n == acc._num_examples
assert isinstance(res, float)
assert accuracy_score(np_y, np_y_pred) == pytest.approx(res)

Expand Down
3 changes: 3 additions & 0 deletions tests/ignite/metrics/test_loss.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def _test(metric_device, y_test_1, y_test_2):
n = loss._num_examples
assert n == len(y)
res = loss.compute()
assert n == loss._num_examples

y_pred = idist.all_gather(y_pred)
y = idist.all_gather(y)
Expand All @@ -157,7 +158,9 @@ def _test(metric_device, y_test_1, y_test_2):
loss.reset()
y_pred, y, _ = y_test_2
loss.update((y_pred, y))
n = loss._num_examples
res = loss.compute()
assert n == loss._num_examples

y_pred = idist.all_gather(y_pred)
y = idist.all_gather(y)
Expand Down