Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

docs(geometry): nms2d and nms3d give wrong masks on MPS before torch 2.16 for nan and -inf maps (#6494)

Dmytro Mishkinb71df0d82384
diff --git a/kornia/geometry/subpix/nms.py b/kornia/geometry/subpix/nms.py
index 6f5d45921..465547f1c 100644
--- a/kornia/geometry/subpix/nms.py
+++ b/kornia/geometry/subpix/nms.py
@@ -105,6 +105,10 @@ class NonMaximaSuppression2d(nn.Module):
Convention:
- See the convention block of :func:`~kornia.geometry.subpix.nms2d`.
+
+ .. warning::
+ On MPS, torch earlier than 2.16 gives wrong masks for some windows when the input holds nan or -inf; see the
+ warning of :func:`~kornia.geometry.subpix.nms2d`.
"""
def __init__(self, kernel_size: tuple[int, int]) -> None:
@@ -295,6 +299,10 @@ class NonMaximaSuppression3d(nn.Module):
Convention:
- See the convention block of :func:`~kornia.geometry.subpix.nms2d`.
+
+ .. warning::
+ On MPS, torch earlier than 2.16 gives wrong masks for some windows when the input holds nan; see the warning
+ of :func:`~kornia.geometry.subpix.nms3d`.
"""
def __init__(self, kernel_size: tuple[int, int, int]) -> None:
@@ -399,6 +407,15 @@ def nms2d(input: torch.Tensor, kernel_size: tuple[int, int], mask_only: bool = F
each axis are never maxima.
- The default output keeps the input value at each maximum and is ``0`` elsewhere, which is larger than a
negative maximum, so use ``mask_only=True`` for a signed response.
+
+ .. warning::
+ On MPS, torch earlier than 2.16 gives wrong masks for a ``kernel_size`` other than ``(1, 1)``, ``(3, 3)``,
+ ``(5, 5)`` and ``(7, 7)`` when the map holds nan or -inf: its stride-1 ``max_pool2d`` drops nan and returns
+ ``0`` for a window of -inf
+ (`pytorch/pytorch#200910 <https://github.com/pytorch/pytorch/issues/200910>`__,
+ `pytorch/pytorch#200789 <https://github.com/pytorch/pytorch/issues/200789>`__). A position next to a nan can
+ then be kept, and a negative maximum among -inf neighbours suppressed. Those four windows compare the
+ neighbours directly and are not affected, and neither are other devices or torch 2.16 or later.
"""
return NonMaximaSuppression2d(kernel_size)(input, mask_only)
@@ -408,6 +425,13 @@ def nms3d(input: torch.Tensor, kernel_size: tuple[int, int, int], mask_only: boo
Convention:
- See the convention block of :func:`nms2d`, applied over depth, height and width.
+
+ .. warning::
+ On MPS, torch earlier than 2.16 gives wrong masks for a ``kernel_size`` other than ``(1, 1, 1)`` and
+ ``(3, 3, 3)`` when the volume holds nan: its ``max_pool3d`` drops a nan that is not the first value of its
+ window (`pytorch/pytorch#200910 <https://github.com/pytorch/pytorch/issues/200910>`__), so a position next to
+ a nan can be kept. Those two windows compare the neighbours directly and are not affected, and neither are
+ other devices or torch 2.16 or later.
"""
return NonMaximaSuppression3d(kernel_size)(input, mask_only)